Browse Source

Merge domComplexity module from Phantomas 1.7.0

Gaël Métais 10 năm trước cách đây
mục cha
commit
c85d5042e0

+ 32 - 40
phantomas_custom/modules/domComplexYLT/domComplexYLT.js

@@ -3,7 +3,7 @@
  */
  */
 /* global document: true, Node: true, window: true */
 /* global document: true, Node: true, window: true */
 
 
-exports.version = '0.4.a';
+exports.version = '1.0.a';
 
 
 exports.module = function(phantomas) {
 exports.module = function(phantomas) {
     'use strict';
     'use strict';
@@ -18,11 +18,23 @@ exports.module = function(phantomas) {
     phantomas.setMetric('DOMelementsCount'); // @desc total number of HTML element nodes
     phantomas.setMetric('DOMelementsCount'); // @desc total number of HTML element nodes
     phantomas.setMetric('DOMelementMaxDepth'); // @desc maximum level on nesting of HTML element node
     phantomas.setMetric('DOMelementMaxDepth'); // @desc maximum level on nesting of HTML element node
 
 
-    phantomas.setMetric('DOMidDuplicated'); // @desc duplicated id found in DOM
-
     // nodes with inlines CSS (style attribute)
     // nodes with inlines CSS (style attribute)
     phantomas.setMetric('nodesWithInlineCSS'); // @desc number of nodes with inline CSS styling (with style attribute) @offenders
     phantomas.setMetric('nodesWithInlineCSS'); // @desc number of nodes with inline CSS styling (with style attribute) @offenders
 
 
+    // images
+    phantomas.setMetric('imagesScaledDown'); // @desc number of <img> nodes that have images scaled down in HTML @offenders
+    phantomas.setMetric('imagesWithoutDimensions'); // @desc number of <img> 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
     // HTML size
     phantomas.on('report', function() {
     phantomas.on('report', function() {
         phantomas.setMetricEvaluate('bodyHTMLSize', function() { // @desc the size of body tag content (document.body.innerHTML.length)
         phantomas.setMetricEvaluate('bodyHTMLSize', function() { // @desc the size of body tag content (document.body.innerHTML.length)
@@ -37,11 +49,6 @@ exports.module = function(phantomas) {
                     DOMelementMaxDepthElts = [],
                     DOMelementMaxDepthElts = [],
                     size = 0;
                     size = 0;
 
 
-                // include all nodes
-                runner.isSkipped = function(node) {
-                    return false;
-                };
-
                 runner.walk(document.body, function(node, depth) {
                 runner.walk(document.body, function(node, depth) {
                     switch (node.nodeType) {
                     switch (node.nodeType) {
                         case Node.COMMENT_NODE:
                         case Node.COMMENT_NODE:
@@ -64,8 +71,8 @@ exports.module = function(phantomas) {
                                 DOMelementMaxDepthElts.push(phantomas.getDOMPath(node));
                                 DOMelementMaxDepthElts.push(phantomas.getDOMPath(node));
                             }
                             }
 
 
+                            // report duplicated ID (issue #392)
                             if (node.id) {
                             if (node.id) {
-                                // Send id to a collection so that duplicated ids can be counted
                                 phantomas.emit('domId', node.id);
                                 phantomas.emit('domId', node.id);
                             }
                             }
 
 
@@ -74,6 +81,20 @@ exports.module = function(phantomas) {
                                 return false;
                                 return false;
                             }
                             }
 
 
+                            // images
+                            if (node.nodeName === 'IMG') {
+                                if (!node.hasAttribute('width') || !node.hasAttribute('height')) {
+                                    phantomas.incrMetric('imagesWithoutDimensions');
+                                    phantomas.addOffender('imagesWithoutDimensions', '%s <%s>', phantomas.getDOMPath(node), node.src);
+                                }
+                                if (node.naturalHeight && node.naturalWidth && node.height && node.width) {
+                                    if (node.naturalHeight > node.height || node.naturalWidth > node.width) {
+                                        phantomas.incrMetric('imagesScaledDown');
+                                        phantomas.addOffender('imagesScaledDown', '%s (%dx%d -> %dx%d)', node.src, node.naturalWidth, node.naturalHeight, node.width, node.height);
+                                    }
+                                }
+                            }
+
                             // count nodes with inline CSS
                             // count nodes with inline CSS
                             if (node.hasAttribute('style')) {
                             if (node.hasAttribute('style')) {
                                 phantomas.incrMetric('nodesWithInlineCSS');
                                 phantomas.incrMetric('nodesWithInlineCSS');
@@ -100,44 +121,15 @@ exports.module = function(phantomas) {
                 // count <iframe> tags
                 // count <iframe> tags
                 phantomas.setMetric('iframesCount', document.querySelectorAll('iframe').length); // @desc number of iframe nodes
                 phantomas.setMetric('iframesCount', document.querySelectorAll('iframe').length); // @desc number of iframe nodes
 
 
-                // <img> nodes without dimensions (one of width / height missing)
-                phantomas.setMetric('imagesWithoutDimensions', (function() { // @desc number of <img> nodes without both width and height attribute @offenders
-                    var imgNodes = document.body && document.body.querySelectorAll('img') || [],
-                        node,
-                        imagesWithoutDimensions = 0;
-
-                    for (var i=0, len=imgNodes.length; i<len; i++) {
-                        node = imgNodes[i];
-                        if (!node.hasAttribute('width') || !node.hasAttribute('height')) {
-                            phantomas.addOffender('imagesWithoutDimensions', phantomas.getDOMPath(node));
-                            imagesWithoutDimensions++;
-                        }
-                    }
-
-                    return imagesWithoutDimensions;
-                })());
-
                 phantomas.spyEnabled(true);
                 phantomas.spyEnabled(true);
             }(window.__phantomas));
             }(window.__phantomas));
         });
         });
-    });
-
-    // count ids in DOM to detect duplicated ids
-    // @see https://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-document-doctype
-    var Collection = require('../../../node_modules/phantomas/lib/collection'),
-        DOMids = new Collection();
-
-    phantomas.on('domId', function(id) {
-        DOMids.push(id);
-    });
 
 
-    phantomas.on('report', function() {
-        phantomas.log('Looking for duplicated DOM ids');
         DOMids.sort().forEach(function(id, cnt) {
         DOMids.sort().forEach(function(id, cnt) {
             if (cnt > 1) {
             if (cnt > 1) {
                 phantomas.incrMetric('DOMidDuplicated');
                 phantomas.incrMetric('DOMidDuplicated');
-                phantomas.addOffender('DOMidDuplicated', '%s: %d', id, cnt);
+                phantomas.addOffender('DOMidDuplicated', '%s: %d occurrences', id, cnt);
             }
             }
         });
         });
     });
     });
-};
+};