eventYLT.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * Analyzes events bound to DOM elements
  3. */
  4. /* global Document: true, Element: true, window: true */
  5. exports.version = '0.4.a';
  6. exports.module = function(phantomas) {
  7. 'use strict';
  8. phantomas.setMetric('eventsBound'); // @desc number of EventTarget.addEventListener calls
  9. phantomas.setMetric('eventsDispatched'); // @desc number of EventTarget.dispatchEvent calls
  10. phantomas.setMetric('eventsScrollBound'); // @desc number of scroll event bounds
  11. phantomas.on('init', function() {
  12. phantomas.evaluate(function() {
  13. (function(phantomas) {
  14. // spy calls to EventTarget.addEventListener
  15. // @see https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.addEventListener
  16. function eventSpyBefore(eventType) {
  17. /* jshint validthis: true */
  18. var path = phantomas.getDOMPath(this);
  19. //phantomas.log('DOM event: "' + eventType + '" bound to "' + path + '"');
  20. phantomas.incrMetric('eventsBound');
  21. phantomas.addOffender('eventsBound', '"%s" bound to "%s"', eventType, path);
  22. phantomas.log('event ' + eventType + ' bound');
  23. phantomas.enterContext({
  24. type: 'addEventListener',
  25. callDetails: {
  26. context: {
  27. length: 1,
  28. elements: [path]
  29. },
  30. arguments: [eventType]
  31. },
  32. backtrace: phantomas.getBacktrace()
  33. });
  34. // count window.addEventListener('scroll', ...) - issue #508
  35. if (eventType === 'scroll' && (path === 'window' || path === '#document')) {
  36. phantomas.incrMetric('eventsScrollBound');
  37. phantomas.addOffender('eventsScrollBound', 'bound by %s on %s', phantomas.getBacktrace(), path);
  38. }
  39. }
  40. function eventSpyAfter(result) {
  41. phantomas.leaveContext();
  42. }
  43. phantomas.spy(Element.prototype, 'addEventListener', eventSpyBefore, eventSpyAfter);
  44. phantomas.spy(Document.prototype, 'addEventListener', eventSpyBefore, eventSpyAfter);
  45. phantomas.spy(window, 'addEventListener', eventSpyBefore, eventSpyAfter);
  46. // spy calls to EventTarget.dispatchEvent
  47. // @see https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.dispatchEvent
  48. phantomas.spy(Element.prototype, 'dispatchEvent', function(ev) {
  49. /* jshint validthis: true */
  50. var path = phantomas.getDOMPath(this);
  51. phantomas.log('Core JS event: triggered "%s" on "%s"', ev.type, path);
  52. phantomas.incrMetric('eventsDispatched');
  53. phantomas.addOffender('eventsDispatched', '"%s" on "%s"', ev.type, path);
  54. });
  55. })(window.__phantomas);
  56. });
  57. });
  58. phantomas.on('report', function() {
  59. phantomas.evaluate(function() {
  60. (function(phantomas) {
  61. // Check if a window.onscroll function is defined
  62. if (typeof(window.onscroll) === "function") {
  63. phantomas.incrMetric('eventsScrollBound');
  64. phantomas.addOffender('eventsScrollBound', 'bound by %s on %s', '', 'window.onscroll');
  65. }
  66. }(window.__phantomas));
  67. });
  68. });
  69. };