From e9505f31f76c8a67054704cb70c51a3dfa2b7b17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20M=C3=A9tais?= Date: Sun, 28 Dec 2014 16:57:45 +0100 Subject: [PATCH 1/7] Update phantomas and other dependencies --- bower.json | 6 +++--- package.json | 18 +++++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/bower.json b/bower.json index 9d7da0f..8609ca8 100644 --- a/bower.json +++ b/bower.json @@ -1,8 +1,8 @@ { "name": "yellowlabtools", "dependencies": { - "angular": "~1.3.5", - "angular-route": "~1.3.6", - "angular-resource": "~1.3.6" + "angular": "~1.3.8", + "angular-route": "~1.3.8", + "angular-resource": "~1.3.7" } } diff --git a/package.json b/package.json index c7cfe8c..f77e320 100644 --- a/package.json +++ b/package.json @@ -12,12 +12,12 @@ "dependencies": { "async": "~0.9.0", "body-parser": "~1.10.0", - "compression": "~1.2.1", - "debug": "^2.1.0", - "express": "~4.10.4", - "phantomas": "1.7.0", - "rimraf": "^2.2.8", - "q": "^1.1.2" + "compression": "~1.2.2", + "debug": "~2.1.0", + "express": "~4.10.6", + "phantomas": "1.8.0", + "rimraf": "~2.2.8", + "q": "~1.1.2" }, "devDependencies": { "chai": "^1.10.0", @@ -31,9 +31,9 @@ "grunt-fontsmith": "^0.9.1", "grunt-mocha-test": "^0.12.4", "matchdep": "^0.3.0", - "mocha": "^2.0.1", - "phantomjs": "^1.9.12", - "request": "^2.49.0", + "mocha": "^2.1.0", + "phantomjs": "^1.9.13", + "request": "^2.51.0", "sinon": "^1.12.1", "sinon-chai": "^2.6.0" }, From dccb9bb49cea5ada648b0bcac0ac2ed6976e2150 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20M=C3=A9tais?= Date: Sun, 28 Dec 2014 18:05:10 +0100 Subject: [PATCH 2/7] Move phantomas_custom to the phantomas tool directory --- lib/runner.js | 2 +- .../tools/phantomas/custom_modules}/core/scopeYLT/scopeYLT.js | 0 .../custom_modules}/modules/domComplexYLT/domComplexYLT.js | 2 +- .../phantomas/custom_modules}/modules/domQYLT/domQYLT.js | 2 +- .../custom_modules}/modules/eventListYLT/eventListYLT.js | 0 .../tools/phantomas/custom_modules}/modules/jQYLT/jQYLT.js | 2 +- .../phantomas/custom_modules}/modules/jsErrYLT/jsErrYLT.js | 0 .../custom_modules}/modules/jsFileLoadYLT/jsFileLoadYLT.js | 0 .../phantomas/custom_modules}/modules/jsTreeYLT/jsTreeYLT.js | 0 .../phantomas/custom_modules}/modules/keepAlive/keepAlive.js | 0 .../custom_modules}/modules/windowPerfYLT/windowPerfYLT.js | 0 lib/tools/{ => phantomas}/phantomasWrapper.js | 4 ++-- test/core/phantomasWrapperTest.js | 2 +- 13 files changed, 7 insertions(+), 7 deletions(-) rename {phantomas_custom => lib/tools/phantomas/custom_modules}/core/scopeYLT/scopeYLT.js (100%) rename {phantomas_custom => lib/tools/phantomas/custom_modules}/modules/domComplexYLT/domComplexYLT.js (98%) rename {phantomas_custom => lib/tools/phantomas/custom_modules}/modules/domQYLT/domQYLT.js (99%) rename {phantomas_custom => lib/tools/phantomas/custom_modules}/modules/eventListYLT/eventListYLT.js (100%) rename {phantomas_custom => lib/tools/phantomas/custom_modules}/modules/jQYLT/jQYLT.js (99%) rename {phantomas_custom => lib/tools/phantomas/custom_modules}/modules/jsErrYLT/jsErrYLT.js (100%) rename {phantomas_custom => lib/tools/phantomas/custom_modules}/modules/jsFileLoadYLT/jsFileLoadYLT.js (100%) rename {phantomas_custom => lib/tools/phantomas/custom_modules}/modules/jsTreeYLT/jsTreeYLT.js (100%) rename {phantomas_custom => lib/tools/phantomas/custom_modules}/modules/keepAlive/keepAlive.js (100%) rename {phantomas_custom => lib/tools/phantomas/custom_modules}/modules/windowPerfYLT/windowPerfYLT.js (100%) rename lib/tools/{ => phantomas}/phantomasWrapper.js (95%) diff --git a/lib/runner.js b/lib/runner.js index 0cd4dcb..ab6ea6d 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -1,7 +1,7 @@ var Q = require('q'); var debug = require('debug')('ylt:runner'); -var phantomasWrapper = require('./tools/phantomasWrapper'); +var phantomasWrapper = require('./tools/phantomas/phantomasWrapper'); var jsExecutionTransformer = require('./tools/jsExecutionTransformer'); var rulesChecker = require('./rulesChecker'); var scoreCalculator = require('./scoreCalculator'); diff --git a/phantomas_custom/core/scopeYLT/scopeYLT.js b/lib/tools/phantomas/custom_modules/core/scopeYLT/scopeYLT.js similarity index 100% rename from phantomas_custom/core/scopeYLT/scopeYLT.js rename to lib/tools/phantomas/custom_modules/core/scopeYLT/scopeYLT.js diff --git a/phantomas_custom/modules/domComplexYLT/domComplexYLT.js b/lib/tools/phantomas/custom_modules/modules/domComplexYLT/domComplexYLT.js similarity index 98% rename from phantomas_custom/modules/domComplexYLT/domComplexYLT.js rename to lib/tools/phantomas/custom_modules/modules/domComplexYLT/domComplexYLT.js index a64f3c6..f03bf84 100644 --- a/phantomas_custom/modules/domComplexYLT/domComplexYLT.js +++ b/lib/tools/phantomas/custom_modules/modules/domComplexYLT/domComplexYLT.js @@ -28,7 +28,7 @@ exports.module = function(phantomas) { // duplicated ID (issue #392) phantomas.setMetric('DOMidDuplicated'); // @desc number of duplicated IDs found in DOM - var Collection = require('../../../node_modules/phantomas/lib/collection'), + var Collection = require('../../../../../../node_modules/phantomas/lib/collection'), DOMids = new Collection(); phantomas.on('domId', function(id) { diff --git a/phantomas_custom/modules/domQYLT/domQYLT.js b/lib/tools/phantomas/custom_modules/modules/domQYLT/domQYLT.js similarity index 99% rename from phantomas_custom/modules/domQYLT/domQYLT.js rename to lib/tools/phantomas/custom_modules/modules/domQYLT/domQYLT.js index 8f0e4c9..78260d2 100644 --- a/phantomas_custom/modules/domQYLT/domQYLT.js +++ b/lib/tools/phantomas/custom_modules/modules/domQYLT/domQYLT.js @@ -246,7 +246,7 @@ exports.module = function(phantomas) { // count DOM queries by either ID, tag name, class name and selector query // @see https://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-document-doctype - var Collection = require('../../../node_modules/phantomas/lib/collection'), + var Collection = require('../../../../../../node_modules/phantomas/lib/collection'), DOMqueries = new Collection(); phantomas.on('domQuery', function(type, query, fnName, context) { diff --git a/phantomas_custom/modules/eventListYLT/eventListYLT.js b/lib/tools/phantomas/custom_modules/modules/eventListYLT/eventListYLT.js similarity index 100% rename from phantomas_custom/modules/eventListYLT/eventListYLT.js rename to lib/tools/phantomas/custom_modules/modules/eventListYLT/eventListYLT.js diff --git a/phantomas_custom/modules/jQYLT/jQYLT.js b/lib/tools/phantomas/custom_modules/modules/jQYLT/jQYLT.js similarity index 99% rename from phantomas_custom/modules/jQYLT/jQYLT.js rename to lib/tools/phantomas/custom_modules/modules/jQYLT/jQYLT.js index 149fce0..b1014c3 100644 --- a/phantomas_custom/modules/jQYLT/jQYLT.js +++ b/lib/tools/phantomas/custom_modules/modules/jQYLT/jQYLT.js @@ -298,7 +298,7 @@ exports.module = function(phantomas) { // count Sizzle calls to detect duplicated queries - var Collection = require('../../../node_modules/phantomas/lib/collection'), + var Collection = require('../../../../../../node_modules/phantomas/lib/collection'), sizzleCalls = new Collection(), jQueryLoading = new Collection(); diff --git a/phantomas_custom/modules/jsErrYLT/jsErrYLT.js b/lib/tools/phantomas/custom_modules/modules/jsErrYLT/jsErrYLT.js similarity index 100% rename from phantomas_custom/modules/jsErrYLT/jsErrYLT.js rename to lib/tools/phantomas/custom_modules/modules/jsErrYLT/jsErrYLT.js diff --git a/phantomas_custom/modules/jsFileLoadYLT/jsFileLoadYLT.js b/lib/tools/phantomas/custom_modules/modules/jsFileLoadYLT/jsFileLoadYLT.js similarity index 100% rename from phantomas_custom/modules/jsFileLoadYLT/jsFileLoadYLT.js rename to lib/tools/phantomas/custom_modules/modules/jsFileLoadYLT/jsFileLoadYLT.js diff --git a/phantomas_custom/modules/jsTreeYLT/jsTreeYLT.js b/lib/tools/phantomas/custom_modules/modules/jsTreeYLT/jsTreeYLT.js similarity index 100% rename from phantomas_custom/modules/jsTreeYLT/jsTreeYLT.js rename to lib/tools/phantomas/custom_modules/modules/jsTreeYLT/jsTreeYLT.js diff --git a/phantomas_custom/modules/keepAlive/keepAlive.js b/lib/tools/phantomas/custom_modules/modules/keepAlive/keepAlive.js similarity index 100% rename from phantomas_custom/modules/keepAlive/keepAlive.js rename to lib/tools/phantomas/custom_modules/modules/keepAlive/keepAlive.js diff --git a/phantomas_custom/modules/windowPerfYLT/windowPerfYLT.js b/lib/tools/phantomas/custom_modules/modules/windowPerfYLT/windowPerfYLT.js similarity index 100% rename from phantomas_custom/modules/windowPerfYLT/windowPerfYLT.js rename to lib/tools/phantomas/custom_modules/modules/windowPerfYLT/windowPerfYLT.js diff --git a/lib/tools/phantomasWrapper.js b/lib/tools/phantomas/phantomasWrapper.js similarity index 95% rename from lib/tools/phantomasWrapper.js rename to lib/tools/phantomas/phantomasWrapper.js index 8dbd614..803fb00 100644 --- a/lib/tools/phantomasWrapper.js +++ b/lib/tools/phantomas/phantomasWrapper.js @@ -43,8 +43,8 @@ var PhantomasWrapper = function() { 'windowPerformance' // overriden ].join(','), 'include-dirs': [ - 'phantomas_custom/core', - 'phantomas_custom/modules' + 'lib/tools/phantomas/custom_modules/core', + 'lib/tools/phantomas/custom_modules/modules' ].join(',') }; diff --git a/test/core/phantomasWrapperTest.js b/test/core/phantomasWrapperTest.js index 904915e..62ddd08 100644 --- a/test/core/phantomasWrapperTest.js +++ b/test/core/phantomasWrapperTest.js @@ -1,5 +1,5 @@ var should = require('chai').should(); -var phantomasWrapper = require('../../lib/tools/phantomasWrapper'); +var phantomasWrapper = require('../../lib/tools/phantomas/phantomasWrapper'); describe('phantomasWrapper', function() { From fb323c2086d400dcba2b2cf05cb4e5b6828a49b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20M=C3=A9tais?= Date: Mon, 29 Dec 2014 08:20:46 +0100 Subject: [PATCH 3/7] Import DOMqueriesWithoutResults fonctionnality --- .../custom_modules/core/scopeYLT/scopeYLT.js | 4 +- .../custom_modules/modules/domQYLT/domQYLT.js | 69 +++++++++++++++---- 2 files changed, 58 insertions(+), 15 deletions(-) diff --git a/lib/tools/phantomas/custom_modules/core/scopeYLT/scopeYLT.js b/lib/tools/phantomas/custom_modules/core/scopeYLT/scopeYLT.js index 9ed515a..c7478bb 100644 --- a/lib/tools/phantomas/custom_modules/core/scopeYLT/scopeYLT.js +++ b/lib/tools/phantomas/custom_modules/core/scopeYLT/scopeYLT.js @@ -7,7 +7,7 @@ */ /* global document: true, window: true */ -exports.version = '0.1'; +exports.version = '0.2'; exports.module = function(phantomas) { 'use strict'; @@ -62,7 +62,7 @@ exports.module = function(phantomas) { // After if (enabled && callbackAfter) { - callbackAfter.call(this, result); + callbackAfter.call(this, result, arguments); } } diff --git a/lib/tools/phantomas/custom_modules/modules/domQYLT/domQYLT.js b/lib/tools/phantomas/custom_modules/modules/domQYLT/domQYLT.js index 78260d2..81985f4 100644 --- a/lib/tools/phantomas/custom_modules/modules/domQYLT/domQYLT.js +++ b/lib/tools/phantomas/custom_modules/modules/domQYLT/domQYLT.js @@ -3,12 +3,13 @@ */ /* global Element: true, Document: true, Node: true, window: true */ -exports.version = '0.9.a'; +exports.version = '0.10.a'; exports.module = function(phantomas) { 'use strict'; phantomas.setMetric('DOMqueries'); // @desc number of all DOM queries @offenders + phantomas.setMetric('DOMqueriesWithoutResults'); // @desc number of DOM queries that retutned nothing @offenders phantomas.setMetric('DOMqueriesById'); // @desc number of document.getElementById calls phantomas.setMetric('DOMqueriesByClassName'); // @desc number of document.getElementsByClassName calls phantomas.setMetric('DOMqueriesByTagName'); // @desc number of document.getElementsByTagName calls @@ -21,14 +22,13 @@ exports.module = function(phantomas) { phantomas.once('init', function() { phantomas.evaluate(function() { (function(phantomas) { - function querySpy(type, query, fnName, context) { - phantomas.emit('domQuery', type, query, fnName, context); // @desc DOM query has been made + function querySpy(type, query, fnName, context, hasNoResults) { + phantomas.emit('domQuery', type, query, fnName, context, hasNoResults); // @desc DOM query has been made } phantomas.spy(Document.prototype, 'getElementById', function(id) { phantomas.incrMetric('DOMqueriesById'); phantomas.addOffender('DOMqueriesById', '#%s (in %s)', id, '#document'); - querySpy('id', '#' + id, 'getElementById', '#document'); phantomas.enterContext({ type: 'getElementById', @@ -41,9 +41,13 @@ exports.module = function(phantomas) { backtrace: phantomas.getBacktrace() }); - }, function(result) { + }, function(result, args) { + var id = args[0]; + + querySpy('id', '#' + id, 'getElementById', '#document', (result === null)); + var moreData = { - resultsNumber : result ? 1 : 0 + resultsNumber : (result === null) ? 0 : 1 }; phantomas.leaveContext(moreData); }); @@ -56,7 +60,6 @@ exports.module = function(phantomas) { phantomas.incrMetric('DOMqueriesByClassName'); phantomas.addOffender('DOMqueriesByClassName', '.%s (in %s)', className, context); - querySpy('class', '.' + className, 'getElementsByClassName', context); phantomas.enterContext({ type: 'getElementsByClassName', @@ -70,7 +73,14 @@ exports.module = function(phantomas) { }); } - function selectorClassNameAfter(result) { + function selectorClassNameAfter(result, args) { + /*jshint validthis: true */ + + var className = args[0]; + var context = phantomas.getDOMPath(this); + + querySpy('class', '.' + className, 'getElementsByClassName', context, (result.length === 0)); + var moreData = { resultsNumber : (result && result.length > 0) ? result.length : 0 }; @@ -88,7 +98,6 @@ exports.module = function(phantomas) { phantomas.incrMetric('DOMqueriesByTagName'); phantomas.addOffender('DOMqueriesByTagName', '%s (in %s)', tagName, context); - querySpy('tag name', tagName.toLowerCase(), 'getElementsByTagName', context); phantomas.enterContext({ type: 'getElementsByTagName', @@ -102,7 +111,14 @@ exports.module = function(phantomas) { }); } - function selectorTagNameSpyAfter(result) { + function selectorTagNameSpyAfter(result, args) { + /*jshint validthis: true */ + + var tagName = args[0]; + var context = phantomas.getDOMPath(this); + + querySpy('tag name', tagName.toLowerCase(), 'getElementsByTagName', context, (result.length === 0)); + var moreData = { resultsNumber : (result && result.length > 0) ? result.length : 0 }; @@ -112,11 +128,11 @@ exports.module = function(phantomas) { phantomas.spy(Document.prototype, 'getElementsByTagName', selectorTagNameSpyBefore, selectorTagNameSpyAfter); phantomas.spy(Element.prototype, 'getElementsByTagName', selectorTagNameSpyBefore, selectorTagNameSpyAfter); + // selector queries function selectorQuerySpy(selector, context) { phantomas.incrMetric('DOMqueriesByQuerySelectorAll'); phantomas.addOffender('DOMqueriesByQuerySelectorAll', '%s (in %s)', selector, context); - querySpy('selector', selector, 'querySelectorAll', context); } function selectorQuerySpyBefore(selector) { @@ -137,7 +153,14 @@ exports.module = function(phantomas) { }); } - function selectorQuerySpyAfter(result) { + function selectorQuerySpyAfter(result, args) { + /*jshint validthis: true */ + + var selector = args[0]; + var context = phantomas.getDOMPath(this); + + querySpy('selector', selector, 'querySelectorAll', context, (!result || result.length === 0)); + var moreData = { resultsNumber : result ? 1 : 0 }; @@ -162,7 +185,14 @@ exports.module = function(phantomas) { }); } - function selectorAllQuerySpryAfter(result) { + function selectorAllQuerySpryAfter(result, args) { + /*jshint validthis: true */ + + var selector = args[0]; + var context = phantomas.getDOMPath(this); + + querySpy('selector', selector, 'querySelectorAll', context, (!result || result.length === 0)); + var moreData = { resultsNumber : (result && result.length > 0) ? result.length : 0 }; @@ -244,6 +274,19 @@ exports.module = function(phantomas) { }); }); + // report DOM queries that return no results (issue #420) + phantomas.on('domQuery', function(type, query, fnName, context, hasNoResults) { + // ignore DOM queries within DOM fragments (used internally by jQuery) + if (context.indexOf('body') !== 0 && context.indexOf('#document') !== 0) { + return; + } + + if (hasNoResults === true) { + phantomas.incrMetric('DOMqueriesWithoutResults'); + phantomas.addOffender('DOMqueriesWithoutResults', '%s (in %s) using %s', query, context, fnName); + } + }); + // count DOM queries by either ID, tag name, class name and selector query // @see https://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-document-doctype var Collection = require('../../../../../../node_modules/phantomas/lib/collection'), From 6fe61a041eb94bad8dd073aa772012a28e2d33ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20M=C3=A9tais?= Date: Mon, 29 Dec 2014 10:26:08 +0100 Subject: [PATCH 4/7] Add DOMqueriesWithoutResults as a rule --- lib/metadata/policies.js | 8 ++++++++ lib/metadata/scoreProfileGeneric.json | 1 + 2 files changed, 9 insertions(+) diff --git a/lib/metadata/policies.js b/lib/metadata/policies.js index b9cb910..c01566d 100644 --- a/lib/metadata/policies.js +++ b/lib/metadata/policies.js @@ -58,6 +58,14 @@ var policies = { "isAbnormalThreshold": 500, "takeOffendersFrom": "DOMqueriesDuplicated" }, + "DOMqueriesWithoutResults": { + "tool": "phantomas", + "label": "DOM queries without result", + "message": "

Number of queries that return no result.

It suggests the query is not used on the page, probably because it is some dead code.

Or maybe the code is trying to find an HTML block that is not always here. Look at the JS Timeline to see if the scripts correctly figures out the HTML block is not here and immediatly stops interacting further with the DOM.

", + "isOkThreshold": 0, + "isBadThreshold": 100, + "isAbnormalThreshold": 200 + }, "eventsBound": { "tool": "phantomas", "label": "Events bound", diff --git a/lib/metadata/scoreProfileGeneric.json b/lib/metadata/scoreProfileGeneric.json index 8feb3ff..d104e51 100644 --- a/lib/metadata/scoreProfileGeneric.json +++ b/lib/metadata/scoreProfileGeneric.json @@ -14,6 +14,7 @@ "policies": { "DOMinserts": 2, "DOMqueries": 1, + "DOMqueriesWithoutResults": 2, "DOMqueriesAvoidable": 2, "eventsBound": 1 } From 0b23fad9093ab33f31f3a03ea605a8dba5c7849f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20M=C3=A9tais?= Date: Mon, 29 Dec 2014 10:35:18 +0100 Subject: [PATCH 5/7] Remove custom closedConnections module implemented in Phantomas --- .../modules/keepAlive/keepAlive.js | 37 ------------------- 1 file changed, 37 deletions(-) delete mode 100644 lib/tools/phantomas/custom_modules/modules/keepAlive/keepAlive.js diff --git a/lib/tools/phantomas/custom_modules/modules/keepAlive/keepAlive.js b/lib/tools/phantomas/custom_modules/modules/keepAlive/keepAlive.js deleted file mode 100644 index 4550e77..0000000 --- a/lib/tools/phantomas/custom_modules/modules/keepAlive/keepAlive.js +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Analyzes if HTTP responses keep the connections alive. - */ - -exports.version = '0.1'; - -exports.module = function(phantomas) { - 'use strict'; - - phantomas.setMetric('closedConnections'); // @desc requests not keeping the connection alive and slowing down the next request @offenders - - var closedConnectionHosts = {}; - - phantomas.on('recv', function(entry, res) { - var connectionHeader = (entry.headers.Connection || '').toLowerCase(); - // Taking the protocol in account, in case the same domain is called with two different protocols. - var host = entry.protocol + '://' + entry.domain; - - if (connectionHeader.indexOf('close') >= 0) { - // Don't blame it immediatly, wait to see if the connection is needed a second time. - closedConnectionHosts[host] = entry.url; - } - }); - - phantomas.on('send', function(entry, res) { - var host = entry.protocol + '://' + entry.domain; - var previousClosedConnection = closedConnectionHosts[host]; - - if (previousClosedConnection) { - // There was a closed connection. We can blame it safely now! - phantomas.incrMetric('closedConnections'); - phantomas.addOffender('closedConnections', previousClosedConnection); - - closedConnectionHosts[host] = null; - } - }); -}; From 519f2ba61739f7767cb0a0cf2b29f0485ffcdd5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20M=C3=A9tais?= Date: Mon, 29 Dec 2014 12:17:52 +0100 Subject: [PATCH 6/7] Remove custom domComplexity module, maxDepth offenders added to phantomas --- .../modules/domComplexYLT/domComplexYLT.js | 135 ------------------ lib/tools/phantomas/phantomasWrapper.js | 1 - 2 files changed, 136 deletions(-) delete mode 100644 lib/tools/phantomas/custom_modules/modules/domComplexYLT/domComplexYLT.js diff --git a/lib/tools/phantomas/custom_modules/modules/domComplexYLT/domComplexYLT.js b/lib/tools/phantomas/custom_modules/modules/domComplexYLT/domComplexYLT.js deleted file mode 100644 index f03bf84..0000000 --- a/lib/tools/phantomas/custom_modules/modules/domComplexYLT/domComplexYLT.js +++ /dev/null @@ -1,135 +0,0 @@ -/** - * Analyzes DOM complexity - */ -/* global document: true, Node: true, window: true */ - -exports.version = '1.0.a'; - -exports.module = function(phantomas) { - 'use strict'; - - // total length of HTML comments (including brackets) - phantomas.setMetric('commentsSize'); // @desc the size of HTML comments on the page @offenders - - // total length of text nodes with whitespaces only (i.e. pretty formatting of HTML) - phantomas.setMetric('whiteSpacesSize'); // @desc the size of text nodes with whitespaces only - - // count all tags - phantomas.setMetric('DOMelementsCount'); // @desc total number of HTML element nodes - phantomas.setMetric('DOMelementMaxDepth'); // @desc maximum level on nesting of HTML element node - - // nodes with inlines CSS (style attribute) - phantomas.setMetric('nodesWithInlineCSS'); // @desc number of nodes with inline CSS styling (with style attribute) @offenders - - // images - phantomas.setMetric('imagesScaledDown'); // @desc number of nodes that have images scaled down in HTML @offenders - phantomas.setMetric('imagesWithoutDimensions'); // @desc number of nodes without both width and height attribute @offenders - - // duplicated ID (issue #392) - phantomas.setMetric('DOMidDuplicated'); // @desc number of duplicated IDs found in DOM - - var Collection = require('../../../../../../node_modules/phantomas/lib/collection'), - DOMids = new Collection(); - - phantomas.on('domId', function(id) { - DOMids.push(id); - }); - - // HTML size - phantomas.on('report', function() { - phantomas.setMetricEvaluate('bodyHTMLSize', function() { // @desc the size of body tag content (document.body.innerHTML.length) - return document.body && document.body.innerHTML.length || 0; - }); - - phantomas.evaluate(function() { - (function(phantomas) { - var runner = new phantomas.nodeRunner(), - whitespacesRegExp = /^\s+$/, - DOMelementMaxDepth = 0, - DOMelementMaxDepthElts = [], - size = 0; - - runner.walk(document.body, function(node, depth) { - switch (node.nodeType) { - case Node.COMMENT_NODE: - size = node.textContent.length + 7; // ''.length - phantomas.incrMetric('commentsSize', size); - - // log HTML comments bigger than 64 characters - if (size > 64) { - phantomas.addOffender('commentsSize', phantomas.getDOMPath(node) + ' (' + size + ' characters)'); - } - break; - - case Node.ELEMENT_NODE: - phantomas.incrMetric('DOMelementsCount'); - - if (depth > DOMelementMaxDepth) { - DOMelementMaxDepth = depth; - DOMelementMaxDepthElts = [phantomas.getDOMPath(node)]; - } else if (depth === DOMelementMaxDepth) { - DOMelementMaxDepthElts.push(phantomas.getDOMPath(node)); - } - - // report duplicated ID (issue #392) - if (node.id) { - phantomas.emit('domId', node.id); - } - - // ignore inline