From 72afcc3e5a186594dd2c394799058ba5fabff339 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20M=C3=A9tais?= Date: Sat, 25 Apr 2015 18:08:28 +0200 Subject: [PATCH] Enabling spy when checking for hiddenContentSize --- .../modules/domHiddenYLT/domHiddenYLT.js | 50 +++++++++++++++++++ lib/tools/phantomas/phantomasWrapper.js | 1 + 2 files changed, 51 insertions(+) create mode 100644 lib/tools/phantomas/custom_modules/modules/domHiddenYLT/domHiddenYLT.js diff --git a/lib/tools/phantomas/custom_modules/modules/domHiddenYLT/domHiddenYLT.js b/lib/tools/phantomas/custom_modules/modules/domHiddenYLT/domHiddenYLT.js new file mode 100644 index 0000000..56aafd5 --- /dev/null +++ b/lib/tools/phantomas/custom_modules/modules/domHiddenYLT/domHiddenYLT.js @@ -0,0 +1,50 @@ +/** + * Analyzes DOM hidden content + */ +/* global document: true, Node: true, window: true */ + +exports.version = '0.1.a'; + +exports.module = function(phantomas) { + 'use strict'; + + // total length of HTML of hidden elements (i.e. display: none) + phantomas.setMetric('hiddenContentSize'); // @desc the size of content of hidden elements on the page (with CSS display: none) @offenders + + // HTML size + phantomas.on('report', function() { + phantomas.evaluate(function() { + (function(phantomas) { + phantomas.spyEnabled(false, 'checking the hiddenContentSize'); + + var runner = new phantomas.nodeRunner(); + + runner.walk(document.body, function(node, depth) { + switch (node.nodeType) { + case Node.ELEMENT_NODE: + // @see https://developer.mozilla.org/en/DOM%3awindow.getComputedStyle + var styles = window.getComputedStyle(node); + + if (styles && styles.getPropertyValue('display') === 'none') { + if (typeof node.innerHTML === 'string') { + var size = node.innerHTML.length; + phantomas.incrMetric('hiddenContentSize', size); + + // log hidden containers bigger than 1 kB + if (size > 1024) { + phantomas.addOffender('hiddenContentSize', phantomas.getDOMPath(node) + ' (' + size + ' characters)'); + } + } + + // don't run for child nodes as they're hidden as well + return false; + } + break; + } + }); + + phantomas.spyEnabled(true); + }(window.__phantomas)); + }); + }); +}; diff --git a/lib/tools/phantomas/phantomasWrapper.js b/lib/tools/phantomas/phantomasWrapper.js index a89655f..3e84138 100644 --- a/lib/tools/phantomas/phantomasWrapper.js +++ b/lib/tools/phantomas/phantomasWrapper.js @@ -35,6 +35,7 @@ var PhantomasWrapper = function() { 'analyze-css': true, 'skip-modules': [ 'blockDomains', // not needed + 'domHiddenContent', // overriden 'domMutations', // not compatible with webkit 'domQueries', // overriden 'eventListeners', // overridden