eventListYLT.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * Analyzes events bound to DOM elements
  3. */
  4. /* global Document: true, Element: true, window: true */
  5. exports.version = '0.2.a';
  6. exports.module = function(phantomas) {
  7. 'use strict';
  8. phantomas.setMetric('eventsBound'); // @desc number of EventTarget.addEventListener calls
  9. // spy calls to EventTarget.addEventListener
  10. // @see https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.addEventListener
  11. phantomas.once('init', function() {
  12. phantomas.evaluate(function() {
  13. (function(phantomas) {
  14. function eventSpyBefore(eventType) {
  15. /* jshint validthis: true */
  16. var path = phantomas.getDOMPath(this);
  17. //phantomas.log('DOM event: "' + eventType + '" bound to "' + path + '"');
  18. phantomas.incrMetric('eventsBound');
  19. phantomas.addOffender('eventsBound', '"%s" bound to "%s"', eventType, path);
  20. phantomas.enterContext({
  21. type: 'addEventListener',
  22. callDetails: {
  23. context: {
  24. domElement: path
  25. },
  26. arguments: [eventType]
  27. },
  28. backtrace: phantomas.getBacktrace()
  29. });
  30. }
  31. function eventSpyAfter(result) {
  32. phantomas.leaveContext();
  33. }
  34. phantomas.spy(Element.prototype, 'addEventListener', eventSpyBefore, eventSpyAfter);
  35. phantomas.spy(Document.prototype, 'addEventListener', eventSpyBefore, eventSpyAfter);
  36. phantomas.spy(window, 'addEventListener', eventSpyBefore, eventSpyAfter);
  37. })(window.__phantomas);
  38. });
  39. });
  40. };