|
@@ -3,21 +3,23 @@
|
|
*/
|
|
*/
|
|
/* global document: true, Node: true, window: true */
|
|
/* global document: true, Node: true, window: true */
|
|
|
|
|
|
-exports.version = '0.1.a';
|
|
|
|
|
|
+exports.version = '1.0.a';
|
|
|
|
|
|
exports.module = function(phantomas) {
|
|
exports.module = function(phantomas) {
|
|
'use strict';
|
|
'use strict';
|
|
|
|
|
|
// total length of HTML of hidden elements (i.e. display: none)
|
|
// 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
|
|
phantomas.setMetric('hiddenContentSize'); // @desc the size of content of hidden elements on the page (with CSS display: none) @offenders
|
|
|
|
+ phantomas.setMetric('hiddenImages'); // @desc number of hidden images that can be lazy-loaded @offenders
|
|
|
|
|
|
// HTML size
|
|
// HTML size
|
|
phantomas.on('report', function() {
|
|
phantomas.on('report', function() {
|
|
phantomas.evaluate(function() {
|
|
phantomas.evaluate(function() {
|
|
(function(phantomas) {
|
|
(function(phantomas) {
|
|
- phantomas.spyEnabled(false, 'checking the hiddenContentSize');
|
|
|
|
|
|
+ var runner = new phantomas.nodeRunner(),
|
|
|
|
+ lazyLoadableImages = {};
|
|
|
|
|
|
- var runner = new phantomas.nodeRunner();
|
|
|
|
|
|
+ phantomas.spyEnabled(false, 'analyzing hidden content');
|
|
|
|
|
|
runner.walk(document.body, function(node, depth) {
|
|
runner.walk(document.body, function(node, depth) {
|
|
switch (node.nodeType) {
|
|
switch (node.nodeType) {
|
|
@@ -36,6 +38,29 @@ exports.module = function(phantomas) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // count hidden images that can be lazy loaded (issue #524)
|
|
|
|
+ var images = [];
|
|
|
|
+ if (node.tagName === 'IMG') {
|
|
|
|
+ images = [node];
|
|
|
|
+ } else if (typeof node.querySelectorAll === 'function') {
|
|
|
|
+ images = node.querySelectorAll('img') || [];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (var i = 0, len = images.length; i < len; i++) {
|
|
|
|
+ var src = images[i].src,
|
|
|
|
+ path;
|
|
|
|
+
|
|
|
|
+ if (src === '' || src.indexOf('data:image') === 0) continue;
|
|
|
|
+
|
|
|
|
+ if (!lazyLoadableImages[src]) {
|
|
|
|
+ path = phantomas.getDOMPath(images[i]);
|
|
|
|
+
|
|
|
|
+ lazyLoadableImages[src] = {
|
|
|
|
+ path: path
|
|
|
|
+ };
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
// don't run for child nodes as they're hidden as well
|
|
// don't run for child nodes as they're hidden as well
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
@@ -43,6 +68,15 @@ exports.module = function(phantomas) {
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
|
|
|
|
+ Object.keys(lazyLoadableImages).forEach(function(img) {
|
|
|
|
+ var entry = lazyLoadableImages[img];
|
|
|
|
+
|
|
|
|
+ phantomas.incrMetric('hiddenImages');
|
|
|
|
+ phantomas.addOffender('hiddenImages', img);
|
|
|
|
+
|
|
|
|
+ phantomas.log('hiddenImages: <%s> image (%s) is hidden and can be lazy-loaded', img, entry.path);
|
|
|
|
+ });
|
|
|
|
+
|
|
phantomas.spyEnabled(true);
|
|
phantomas.spyEnabled(true);
|
|
}(window.__phantomas));
|
|
}(window.__phantomas));
|
|
});
|
|
});
|