domHiddenYLT.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * Analyzes DOM hidden content
  3. */
  4. /* global document: true, Node: true, window: true */
  5. exports.version = '0.1.a';
  6. exports.module = function(phantomas) {
  7. 'use strict';
  8. // total length of HTML of hidden elements (i.e. display: none)
  9. phantomas.setMetric('hiddenContentSize'); // @desc the size of content of hidden elements on the page (with CSS display: none) @offenders
  10. // HTML size
  11. phantomas.on('report', function() {
  12. phantomas.evaluate(function() {
  13. (function(phantomas) {
  14. phantomas.spyEnabled(false, 'checking the hiddenContentSize');
  15. var runner = new phantomas.nodeRunner();
  16. runner.walk(document.body, function(node, depth) {
  17. switch (node.nodeType) {
  18. case Node.ELEMENT_NODE:
  19. // @see https://developer.mozilla.org/en/DOM%3awindow.getComputedStyle
  20. var styles = window.getComputedStyle(node);
  21. if (styles && styles.getPropertyValue('display') === 'none') {
  22. if (typeof node.innerHTML === 'string') {
  23. var size = node.innerHTML.length;
  24. phantomas.incrMetric('hiddenContentSize', size);
  25. // log hidden containers bigger than 1 kB
  26. if (size > 1024) {
  27. phantomas.addOffender('hiddenContentSize', phantomas.getDOMPath(node) + ' (' + size + ' characters)');
  28. }
  29. }
  30. // don't run for child nodes as they're hidden as well
  31. return false;
  32. }
  33. break;
  34. }
  35. });
  36. phantomas.spyEnabled(true);
  37. }(window.__phantomas));
  38. });
  39. });
  40. };