Blame view

lib/jsdom/living/nodes/GlobalEventHandlers-impl.js 2.41 KB
858f2bdf5   Boyan Georgiev   fixes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  "use strict";
  
  const { appendHandler, createEventAccessor } = require("../helpers/create-event-accessor");
  
  const events = new Set([
    "abort", "autocomplete",
    "autocompleteerror", "blur",
    "cancel", "canplay", "canplaythrough",
    "change", "click",
    "close", "contextmenu",
    "cuechange", "dblclick",
    "drag", "dragend",
    "dragenter", "dragexit",
    "dragleave", "dragover",
    "dragstart", "drop",
    "durationchange", "emptied",
    "ended", "error", "focus",
    "input", "invalid",
    "keydown", "keypress",
    "keyup", "load", "loadeddata",
    "loadedmetadata", "loadstart",
    "mousedown", "mouseenter",
    "mouseleave", "mousemove",
    "mouseout", "mouseover",
    "mouseup", "wheel",
    "pause", "play",
    "playing", "progress",
    "ratechange", "reset",
    "resize", "scroll",
    "securitypolicyviolation",
    "seeked", "seeking",
    "select", "sort", "stalled",
    "submit", "suspend",
    "timeupdate", "toggle",
    "volumechange", "waiting"
  ]);
  
  class GlobalEventHandlersImpl {
    _initGlobalEvents() {
      this._registeredHandlers = new Set();
      this._eventHandlers = Object.create(null);
    }
  
    _getEventHandlerTarget() {
      return this;
    }
  
    _getEventHandlerFor(event) {
      const target = this._getEventHandlerTarget(event);
      if (!target) {
        return null;
      }
  
      return target._eventHandlers[event];
    }
  
    _setEventHandlerFor(event, handler) {
      const target = this._getEventHandlerTarget(event);
      if (!target) {
        return;
      }
  
      if (!target._registeredHandlers.has(event) && handler !== null) {
        target._registeredHandlers.add(event);
        appendHandler(target, event);
      }
      target._eventHandlers[event] = handler;
    }
  
    _globalEventChanged(event) {
      const propName = "on" + event;
      if (!(propName in this)) {
        return;
      }
  
      // Only translate attribute changes into properties when runScripts: "dangerously" is set.
      // Documents without a browsing context (i.e. without a _defaultView) never run scripts.
      const runScripts = "_runScripts" in this ? this._runScripts : (this._ownerDocument._defaultView || {})._runScripts;
      if (runScripts !== "dangerously") {
        return;
      }
  
      const val = this.getAttributeNS(null, propName);
      const handler = val === null ? null : { body: val };
      this._setEventHandlerFor(event, handler);
    }
  }
  
  for (const event of events) {
    createEventAccessor(GlobalEventHandlersImpl.prototype, event);
  }
  
  module.exports = {
    implementation: GlobalEventHandlersImpl
  };