From 3170deef282d4670975ae17f762166f84574c561 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20M=C3=A9tais?= Date: Mon, 25 Aug 2014 18:33:35 +0200 Subject: [PATCH] Add addEventListeners to the JS execution tree --- app/lib/phantomasWrapper.js | 1 + .../modules/eventListYLT/eventListYLT.js | 44 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 phantomas_custom/modules/eventListYLT/eventListYLT.js diff --git a/app/lib/phantomasWrapper.js b/app/lib/phantomasWrapper.js index 21885df..2efe44f 100644 --- a/app/lib/phantomasWrapper.js +++ b/app/lib/phantomasWrapper.js @@ -37,6 +37,7 @@ var PhantomasWrapper = function() { 'domComplexity', 'domMutations', 'domQueries', + 'eventListeners', 'filmStrip', 'jQuery', 'jserrors', diff --git a/phantomas_custom/modules/eventListYLT/eventListYLT.js b/phantomas_custom/modules/eventListYLT/eventListYLT.js new file mode 100644 index 0000000..92dbaaf --- /dev/null +++ b/phantomas_custom/modules/eventListYLT/eventListYLT.js @@ -0,0 +1,44 @@ +/** + * Analyzes events bound to DOM elements + */ +/* global Document: true, Element: true, window: true */ +'use strict'; + +exports.version = '0.2.a'; + +exports.module = function(phantomas) { + phantomas.setMetric('eventsBound'); // @desc number of EventTarget.addEventListener calls + + // spy calls to EventTarget.addEventListener + // @see https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.addEventListener + phantomas.once('init', function() { + phantomas.evaluate(function() { + (function(phantomas) { + function eventSpyBefore(eventType) { + /* jshint validthis: true */ + var path = phantomas.getDOMPath(this); + //phantomas.log('DOM event: "' + eventType + '" bound to "' + path + '"'); + + phantomas.incrMetric('eventsBound'); + phantomas.addOffender('eventsBound', '"%s" bound to "%s"', eventType, path); + + phantomas.enterContext({ + type: 'addEventListener', + callDetails: { + context: { + domElement: path + }, + arguments: [eventType] + }, + caller: phantomas.getCaller(1), + backtrace: phantomas.getBacktrace() + }); + } + + phantomas.spy(Element.prototype, 'addEventListener', eventSpyBefore, phantomas.leaveContext); + phantomas.spy(Document.prototype, 'addEventListener', eventSpyBefore, phantomas.leaveContext); + phantomas.spy(window, 'addEventListener', eventSpyBefore, phantomas.leaveContext); + })(window.__phantomas); + }); + }); +};