@remote-dom/polyfill 1.1.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/build/cjs/Attr.cjs +55 -0
  3. package/build/cjs/CharacterData.cjs +30 -0
  4. package/build/cjs/ChildNode.cjs +47 -0
  5. package/build/cjs/Comment.cjs +13 -0
  6. package/build/cjs/CustomElementRegistry.cjs +43 -0
  7. package/build/cjs/CustomEvent.cjs +30 -0
  8. package/build/cjs/Document.cjs +107 -0
  9. package/build/cjs/DocumentFragment.cjs +14 -0
  10. package/build/cjs/Element.cjs +97 -0
  11. package/build/cjs/Event.cjs +115 -0
  12. package/build/cjs/EventTarget.cjs +108 -0
  13. package/build/cjs/HTMLBodyElement.cjs +9 -0
  14. package/build/cjs/HTMLElement.cjs +9 -0
  15. package/build/cjs/HTMLHeadElement.cjs +9 -0
  16. package/build/cjs/HTMLHtmlElement.cjs +9 -0
  17. package/build/cjs/HTMLTemplateElement.cjs +28 -0
  18. package/build/cjs/MutationObserver.cjs +9 -0
  19. package/build/cjs/NamedNodeMap.cjs +123 -0
  20. package/build/cjs/Node.cjs +116 -0
  21. package/build/cjs/NodeList.cjs +11 -0
  22. package/build/cjs/ParentNode.cjs +131 -0
  23. package/build/cjs/SVGElement.cjs +21 -0
  24. package/build/cjs/Text.cjs +13 -0
  25. package/build/cjs/Window.cjs +78 -0
  26. package/build/cjs/constants.cjs +59 -0
  27. package/build/cjs/index.cjs +13 -0
  28. package/build/cjs/selectors.cjs +203 -0
  29. package/build/cjs/serialization.cjs +99 -0
  30. package/build/cjs/shared.cjs +65 -0
  31. package/build/esm/Element.mjs +2 -4
  32. package/build/esm/NamedNodeMap.mjs +1 -1
  33. package/build/esm/constants.mjs +1 -2
  34. package/build/esnext/Element.esnext +2 -4
  35. package/build/esnext/NamedNodeMap.esnext +1 -1
  36. package/build/esnext/constants.esnext +1 -2
  37. package/build/tsconfig.tsbuildinfo +1 -1
  38. package/build/typescript/Element.d.ts +1 -2
  39. package/build/typescript/Element.d.ts.map +1 -1
  40. package/build/typescript/NamedNodeMap.d.ts.map +1 -1
  41. package/build/typescript/constants.d.ts +0 -1
  42. package/build/typescript/constants.d.ts.map +1 -1
  43. package/package.json +3 -2
  44. package/rollup.config.js +1 -1
  45. package/source/Element.ts +3 -4
  46. package/source/NamedNodeMap.ts +2 -1
  47. package/source/constants.ts +0 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @remote-dom/polyfill
2
2
 
3
+ ## 1.2.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#419](https://github.com/Shopify/remote-dom/pull/419) [`3c6bd29`](https://github.com/Shopify/remote-dom/commit/3c6bd291121b9fa02cac4ba57274601e97b2a2d2) Thanks [@lemonmade](https://github.com/lemonmade)! - Fix synchronization of `slot` property in some edge cases
8
+
9
+ ## 1.2.0
10
+
11
+ ### Minor Changes
12
+
13
+ - [#411](https://github.com/Shopify/remote-dom/pull/411) [`3bec698`](https://github.com/Shopify/remote-dom/commit/3bec6983756c4b8a6834a037ac520438ef59d28f) Thanks [@lemonmade](https://github.com/lemonmade)! - Add CommonJS export conditions
14
+
3
15
  ## 1.1.0
4
16
 
5
17
  ### Minor Changes
@@ -0,0 +1,55 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const constants = require('./constants.cjs');
6
+ const Node = require('./Node.cjs');
7
+
8
+ class Attr extends Node.Node {
9
+ nodeType = constants.NodeType.ATTRIBUTE_NODE;
10
+ [constants.NS] = null;
11
+ [constants.NEXT] = null;
12
+ [constants.OWNER_ELEMENT] = null;
13
+ isConnected = false;
14
+ constructor(name, value, namespace) {
15
+ super();
16
+ this[constants.NAME] = name;
17
+ this[constants.VALUE] = value;
18
+ if (namespace) this[constants.NS] = namespace;
19
+ }
20
+ get nodeName() {
21
+ return this[constants.NAME];
22
+ }
23
+ set nodeName(_readonly) {}
24
+ get name() {
25
+ return this[constants.NAME];
26
+ }
27
+ set name(_readonly) {}
28
+ get value() {
29
+ return this[constants.VALUE];
30
+ }
31
+ set value(value) {
32
+ const str = String(value);
33
+ this[constants.VALUE] = str;
34
+ const ownerElement = this[constants.OWNER_ELEMENT];
35
+ if (!ownerElement) return;
36
+ this[constants.HOOKS].setAttribute?.(ownerElement, this[constants.NAME], str, this[constants.NS]);
37
+ }
38
+ get nodeValue() {
39
+ return this.value;
40
+ }
41
+ set nodeValue(value) {
42
+ this.value = value;
43
+ }
44
+ get ownerElement() {
45
+ return this[constants.OWNER_ELEMENT];
46
+ }
47
+ get namespaceURI() {
48
+ return this[constants.NS];
49
+ }
50
+ get specified() {
51
+ return true;
52
+ }
53
+ }
54
+
55
+ exports.Attr = Attr;
@@ -0,0 +1,30 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const constants = require('./constants.cjs');
6
+ const ChildNode = require('./ChildNode.cjs');
7
+
8
+ class CharacterData extends ChildNode.ChildNode {
9
+ [constants.DATA] = '';
10
+ constructor(data) {
11
+ super();
12
+ this[constants.DATA] = data == null ? '' : String(data);
13
+ }
14
+ setData(data) {
15
+ let str = '';
16
+ if (data != null) {
17
+ str = typeof data === 'string' ? data : String(data);
18
+ }
19
+ this[constants.DATA] = str;
20
+ this[constants.HOOKS].setText?.(this, str);
21
+ }
22
+ get data() {
23
+ return this[constants.DATA];
24
+ }
25
+ set data(data) {
26
+ this.setData(data);
27
+ }
28
+ }
29
+
30
+ exports.CharacterData = CharacterData;
@@ -0,0 +1,47 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const constants = require('./constants.cjs');
6
+ const Node = require('./Node.cjs');
7
+
8
+ class ChildNode extends Node.Node {
9
+ remove() {
10
+ const parent = this.parentNode;
11
+ if (!parent) return;
12
+ parent.removeChild(this);
13
+ }
14
+ replaceWith(...nodes) {
15
+ const parent = this.parentNode;
16
+ if (!parent) return;
17
+ const node = toNode(parent, nodes[0]);
18
+ const next = node[constants.NEXT];
19
+ parent.replaceChild(this, node);
20
+ for (let i = 1; i < nodes.length; i++) {
21
+ parent.insertBefore(toNode(parent, nodes[i]), next);
22
+ }
23
+ }
24
+ before(...nodes) {
25
+ const parent = this.parentNode;
26
+ if (!parent) return;
27
+ for (const node of nodes) {
28
+ parent.insertBefore(toNode(parent, node), this);
29
+ }
30
+ }
31
+ after(...nodes) {
32
+ const parent = this.parentNode;
33
+ if (!parent) return;
34
+ const next = this[constants.NEXT];
35
+ for (const node of nodes) {
36
+ parent.insertBefore(toNode(parent, node), next);
37
+ }
38
+ }
39
+ }
40
+ function toNode(parent, node) {
41
+ if (node instanceof Node.Node) return node;
42
+ const ownerDocument = parent.ownerDocument;
43
+ return ownerDocument.createTextNode(String(node));
44
+ }
45
+
46
+ exports.ChildNode = ChildNode;
47
+ exports.toNode = toNode;
@@ -0,0 +1,13 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const constants = require('./constants.cjs');
6
+ const CharacterData = require('./CharacterData.cjs');
7
+
8
+ class Comment extends CharacterData.CharacterData {
9
+ nodeType = 8;
10
+ [constants.NAME] = '#comment';
11
+ }
12
+
13
+ exports.Comment = Comment;
@@ -0,0 +1,43 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ class CustomElementRegistryImplementation {
6
+ registry = new Map();
7
+ listenersByName = new Map();
8
+ define(name, Constructor, _options) {
9
+ this.registry.set(name, Constructor);
10
+ const listeners = this.listenersByName.get(name);
11
+ if (listeners == null) return;
12
+ this.listenersByName.delete(name);
13
+ for (const listener of listeners) {
14
+ listener(Constructor);
15
+ }
16
+ }
17
+ get(name) {
18
+ return this.registry.get(name);
19
+ }
20
+ getName(Constructor) {
21
+ for (const [name, value] of this.registry) {
22
+ if (value === Constructor) return name;
23
+ }
24
+ return null;
25
+ }
26
+ whenDefined(name) {
27
+ const Constructor = this.registry.get(name);
28
+ if (Constructor != null) return Promise.resolve(Constructor);
29
+ let listeners = this.listenersByName.get(name);
30
+ if (listeners == null) {
31
+ listeners = [];
32
+ this.listenersByName.set(name, listeners);
33
+ }
34
+ return new Promise(resolve => {
35
+ listeners.push(resolve);
36
+ });
37
+ }
38
+ upgrade(_root) {
39
+ // TODO
40
+ }
41
+ }
42
+
43
+ exports.CustomElementRegistryImplementation = CustomElementRegistryImplementation;
@@ -0,0 +1,30 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const Event = require('./Event.cjs');
6
+
7
+ class CustomEvent extends Event.Event {
8
+ /**
9
+ * Returns any custom data event was created with. Typically used for synthetic events.
10
+ *
11
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CustomEvent/detail)
12
+ */
13
+
14
+ constructor(type, eventInitDict) {
15
+ super(type, eventInitDict);
16
+ this.detail = eventInitDict?.detail;
17
+ }
18
+
19
+ /**
20
+ * Returns any custom data event was created with. Typically used for synthetic events.
21
+ *
22
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CustomEvent/detail)
23
+ */
24
+ initCustomEvent(type, bubbles, cancelable, detail) {
25
+ super.initEvent(type, bubbles, cancelable);
26
+ this.detail = detail;
27
+ }
28
+ }
29
+
30
+ exports.CustomEvent = CustomEvent;
@@ -0,0 +1,107 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const constants = require('./constants.cjs');
6
+ const Event = require('./Event.cjs');
7
+ const ParentNode = require('./ParentNode.cjs');
8
+ const Element = require('./Element.cjs');
9
+ const SVGElement = require('./SVGElement.cjs');
10
+ const Text = require('./Text.cjs');
11
+ const Comment = require('./Comment.cjs');
12
+ const DocumentFragment = require('./DocumentFragment.cjs');
13
+ const HTMLTemplateElement = require('./HTMLTemplateElement.cjs');
14
+ const shared = require('./shared.cjs');
15
+ const HTMLBodyElement = require('./HTMLBodyElement.cjs');
16
+ const HTMLHeadElement = require('./HTMLHeadElement.cjs');
17
+ const HTMLHtmlElement = require('./HTMLHtmlElement.cjs');
18
+
19
+ class Document extends ParentNode.ParentNode {
20
+ nodeType = constants.NodeType.DOCUMENT_NODE;
21
+ [constants.NAME] = '#document';
22
+ constructor(defaultView) {
23
+ super();
24
+ this.defaultView = defaultView;
25
+ this[constants.OWNER_DOCUMENT] = this;
26
+ this.documentElement = setupElement(new HTMLHtmlElement.HTMLHtmlElement(), this, 'html');
27
+ this.body = setupElement(new HTMLBodyElement.HTMLBodyElement(), this, 'body');
28
+ this.head = setupElement(new HTMLHeadElement.HTMLHeadElement(), this, 'head');
29
+ this.documentElement.appendChild(this.head);
30
+ this.documentElement.appendChild(this.body);
31
+ }
32
+ createElement(localName) {
33
+ return createElement(this, localName);
34
+ }
35
+ createElementNS(namespaceURI, localName) {
36
+ return createElement(this, localName, namespaceURI);
37
+ }
38
+ createTextNode(data) {
39
+ return createNode(new Text.Text(data), this);
40
+ }
41
+ createComment(data) {
42
+ return createNode(new Comment.Comment(data), this);
43
+ }
44
+ createDocumentFragment() {
45
+ return createNode(new DocumentFragment.DocumentFragment(), this);
46
+ }
47
+ createEvent() {
48
+ return new Event.Event('');
49
+ }
50
+ importNode(node, deep) {
51
+ return shared.cloneNode(node, deep, this);
52
+ }
53
+ adoptNode(node) {
54
+ if (node[constants.OWNER_DOCUMENT] === this) return node;
55
+ node.parentNode?.removeChild(node);
56
+ adoptNode(node, this);
57
+ return node;
58
+ }
59
+ }
60
+ function createNode(node, ownerDocument) {
61
+ Object.defineProperty(node, constants.OWNER_DOCUMENT, {
62
+ value: ownerDocument,
63
+ writable: true,
64
+ enumerable: false
65
+ });
66
+ return node;
67
+ }
68
+ function createElement(ownerDocument, name, namespace) {
69
+ let element;
70
+ const lowerName = String(name).toLowerCase();
71
+ if (namespace === constants.NamespaceURI.SVG) {
72
+ element = new SVGElement.SVGElement();
73
+ } else if (lowerName === 'template') {
74
+ element = new HTMLTemplateElement.HTMLTemplateElement();
75
+ } else {
76
+ const CustomElement = ownerDocument.defaultView.customElements.get(name);
77
+ element = CustomElement ? new CustomElement() : new Element.Element();
78
+ }
79
+ return setupElement(element, ownerDocument, name, namespace);
80
+ }
81
+ function setupElement(element, ownerDocument, name, namespace) {
82
+ createNode(element, ownerDocument);
83
+ Object.defineProperty(element, constants.NAME, {
84
+ value: name
85
+ });
86
+ if (namespace) {
87
+ Object.defineProperty(element, constants.NS, {
88
+ value: namespace
89
+ });
90
+ }
91
+ ownerDocument[constants.HOOKS].createElement?.(element, namespace);
92
+ return element;
93
+ }
94
+ function adoptNode(node, document) {
95
+ node[constants.OWNER_DOCUMENT] = document;
96
+ if (shared.isParentNode(node)) {
97
+ for (const child of node.childNodes) {
98
+ adoptNode(child, document);
99
+ }
100
+ }
101
+ }
102
+
103
+ exports.Document = Document;
104
+ exports.adoptNode = adoptNode;
105
+ exports.createElement = createElement;
106
+ exports.createNode = createNode;
107
+ exports.setupElement = setupElement;
@@ -0,0 +1,14 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const constants = require('./constants.cjs');
6
+ const ParentNode = require('./ParentNode.cjs');
7
+
8
+ class DocumentFragment extends ParentNode.ParentNode {
9
+ nodeType = constants.NodeType.DOCUMENT_FRAGMENT_NODE;
10
+ [constants.NAME] = '#document-fragment';
11
+ [constants.OWNER_DOCUMENT] = window.document;
12
+ }
13
+
14
+ exports.DocumentFragment = DocumentFragment;
@@ -0,0 +1,97 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const constants = require('./constants.cjs');
6
+ const ParentNode = require('./ParentNode.cjs');
7
+ const NamedNodeMap = require('./NamedNodeMap.cjs');
8
+ const Attr = require('./Attr.cjs');
9
+ const serialization = require('./serialization.cjs');
10
+
11
+ class Element extends ParentNode.ParentNode {
12
+ nodeType = constants.NodeType.ELEMENT_NODE;
13
+ [constants.NS] = constants.NamespaceURI.XHTML;
14
+ get namespaceURI() {
15
+ return this[constants.NS];
16
+ }
17
+ get tagName() {
18
+ return this.nodeName;
19
+ }
20
+ get slot() {
21
+ return this.getAttribute('slot') ?? '';
22
+ }
23
+ set slot(slot) {
24
+ const finalSlot = String(slot);
25
+ if (this.getAttribute('slot') !== finalSlot) {
26
+ this.attributes.setNamedItem(new Attr.Attr('slot', finalSlot));
27
+ }
28
+ }
29
+ get attributes() {
30
+ let attributes = this[constants.ATTRIBUTES];
31
+ if (!attributes) {
32
+ attributes = new NamedNodeMap.NamedNodeMap(this);
33
+ this[constants.ATTRIBUTES] = attributes;
34
+ }
35
+ return attributes;
36
+ }
37
+ get firstElementChild() {
38
+ return this.children[0] ?? null;
39
+ }
40
+ get lastElementChild() {
41
+ return this.children[this.children.length - 1] ?? null;
42
+ }
43
+ get nextElementSibling() {
44
+ let sib = this.nextSibling;
45
+ while (sib && sib.nodeType !== 1) sib = sib.nextSibling;
46
+ return sib;
47
+ }
48
+ get previewElementSibling() {
49
+ let sib = this.previousSibling;
50
+ while (sib && sib.nodeType !== 1) sib = sib.previousSibling;
51
+ return sib;
52
+ }
53
+ setAttribute(name, value) {
54
+ this.attributes.setNamedItem(new Attr.Attr(name, String(value)));
55
+ }
56
+ setAttributeNS(namespace, name, value) {
57
+ this.attributes.setNamedItemNS(new Attr.Attr(name, String(value), namespace));
58
+ }
59
+ getAttribute(name) {
60
+ const attr = this.attributes.getNamedItem(name);
61
+ return attr && attr.value;
62
+ }
63
+ getAttributeNS(namespace, name) {
64
+ const attr = this.attributes.getNamedItemNS(namespace, name);
65
+ return attr && attr.value;
66
+ }
67
+ hasAttribute(name) {
68
+ const attr = this.attributes.getNamedItem(name);
69
+ return attr != null;
70
+ }
71
+ hasAttributeNS(namespace, name) {
72
+ const attr = this.attributes.getNamedItemNS(namespace, name);
73
+ return attr != null;
74
+ }
75
+ removeAttribute(name) {
76
+ this.attributes.removeNamedItem(name);
77
+ }
78
+ removeAttributeNS(namespace, name) {
79
+ this.attributes.removeNamedItemNS(namespace, name);
80
+ }
81
+ get outerHTML() {
82
+ return serialization.serializeNode(this);
83
+ }
84
+ get innerHTML() {
85
+ return serialization.serializeChildren(this);
86
+ }
87
+ set innerHTML(html) {
88
+ if (html == null || html === '') {
89
+ this.replaceChildren();
90
+ } else {
91
+ const fragment = serialization.parseHtml(String(html), this);
92
+ this.replaceChildren(fragment);
93
+ }
94
+ }
95
+ }
96
+
97
+ exports.Element = Element;
@@ -0,0 +1,115 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const constants = require('./constants.cjs');
6
+
7
+ let EventPhase = /*#__PURE__*/function (EventPhase) {
8
+ EventPhase[EventPhase["NONE"] = 0] = "NONE";
9
+ EventPhase[EventPhase["CAPTURING_PHASE"] = 1] = "CAPTURING_PHASE";
10
+ EventPhase[EventPhase["AT_TARGET"] = 2] = "AT_TARGET";
11
+ EventPhase[EventPhase["BUBBLING_PHASE"] = 3] = "BUBBLING_PHASE";
12
+ return EventPhase;
13
+ }({});
14
+ const CAPTURE_MARKER = '@';
15
+ const now = typeof performance === 'undefined' ? Date.now : performance.now.bind(performance);
16
+ class Event {
17
+ static NONE = EventPhase.NONE;
18
+ static CAPTURING_PHASE = EventPhase.CAPTURING_PHASE;
19
+ static AT_TARGET = EventPhase.AT_TARGET;
20
+ static BUBBLING_PHASE = EventPhase.BUBBLING_PHASE;
21
+
22
+ // NONE = EventPhase.NONE;
23
+ // CAPTURING_PHASE = EventPhase.CAPTURING_PHASE;
24
+ // AT_TARGET = EventPhase.AT_TARGET;
25
+ // BUBBLING_PHASE = EventPhase.BUBBLING_PHASE;
26
+
27
+ timeStamp = now();
28
+ target = null;
29
+ currentTarget = null;
30
+ srcElement = null;
31
+ bubbles = false;
32
+ cancelable = false;
33
+ composed = false;
34
+ defaultPrevented = false;
35
+ cancelBubble = false;
36
+ immediatePropagationStopped = false;
37
+ eventPhase = 0;
38
+ path = [];
39
+ // private inPassiveListener = false;
40
+
41
+ constructor(type, options) {
42
+ this.type = type;
43
+ Object.defineProperty(this, constants.IS_TRUSTED, {
44
+ writable: true,
45
+ value: false
46
+ });
47
+ if (options) {
48
+ if (options.bubbles) this.bubbles = options.bubbles;
49
+ if (options.cancelable) this.cancelable = options.cancelable;
50
+ if (options.composed) this.composed = options.composed;
51
+ }
52
+ }
53
+ get composedPath() {
54
+ return this.path;
55
+ }
56
+ get isTrusted() {
57
+ return this[constants.IS_TRUSTED];
58
+ }
59
+ stopPropagation() {
60
+ this.cancelBubble = true;
61
+ }
62
+ stopImmediatePropagation() {
63
+ this.immediatePropagationStopped = true;
64
+ }
65
+ preventDefault() {
66
+ this.defaultPrevented = true;
67
+ }
68
+ set returnValue(value) {
69
+ this.defaultPrevented = value;
70
+ }
71
+ get returnValue() {
72
+ return this.defaultPrevented;
73
+ }
74
+
75
+ /** @deprecated */
76
+ initEvent(type, bubbles, cancelable) {
77
+ this.type = type;
78
+ this.bubbles = Boolean(bubbles);
79
+ this.cancelable = Boolean(cancelable);
80
+ }
81
+ }
82
+ function fireEvent(event, target, phase) {
83
+ const listeners = target[constants.LISTENERS];
84
+ const list = listeners?.get(`${event.type}${phase === EventPhase.CAPTURING_PHASE ? CAPTURE_MARKER : ''}`);
85
+ if (!list) return false;
86
+ let defaultPrevented = false;
87
+ for (const listener of list) {
88
+ event.eventPhase = phase;
89
+ event.currentTarget = target;
90
+ try {
91
+ if (typeof listener === 'object') {
92
+ listener.handleEvent(event);
93
+ } else {
94
+ listener.call(target, event);
95
+ }
96
+ } catch (err) {
97
+ setTimeout(thrower, 0, err);
98
+ }
99
+ if (event.defaultPrevented === true) {
100
+ defaultPrevented = true;
101
+ }
102
+ if (event.immediatePropagationStopped) {
103
+ break;
104
+ }
105
+ }
106
+ return defaultPrevented;
107
+ }
108
+ function thrower(error) {
109
+ throw error;
110
+ }
111
+
112
+ exports.CAPTURE_MARKER = CAPTURE_MARKER;
113
+ exports.Event = Event;
114
+ exports.EventPhase = EventPhase;
115
+ exports.fireEvent = fireEvent;
@@ -0,0 +1,108 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const constants = require('./constants.cjs');
6
+ const Event = require('./Event.cjs');
7
+
8
+ const ONCE_LISTENERS = Symbol('onceListeners');
9
+ class EventTarget {
10
+ /**
11
+ * Property set by entities that extend this class that are part of the DOM tree.
12
+ * @internal
13
+ */
14
+
15
+ addEventListener(type, listener, options) {
16
+ if (listener == null) return;
17
+ const capture = options === true || options && options.capture === true;
18
+ const once = typeof options === 'object' && options.once === true;
19
+ const signal = typeof options === 'object' ? options.signal : undefined;
20
+ const key = `${type}${capture ? Event.CAPTURE_MARKER : ''}`;
21
+ let normalizedListener = listener;
22
+ if (once) {
23
+ normalizedListener = function normalizedListener(...args) {
24
+ this.removeEventListener(type, listener, options);
25
+ return typeof listener === 'object' ? listener.handleEvent(...args) : listener.call(this, ...args);
26
+ };
27
+ let onceListeners = this[ONCE_LISTENERS];
28
+ if (!onceListeners) {
29
+ onceListeners = new WeakMap();
30
+ this[ONCE_LISTENERS] = onceListeners;
31
+ }
32
+ onceListeners.set(listener, normalizedListener);
33
+ }
34
+ let listeners = this[constants.LISTENERS];
35
+ if (!listeners) {
36
+ listeners = new Map();
37
+ this[constants.LISTENERS] = listeners;
38
+ }
39
+ let list = listeners.get(key);
40
+ if (!list) {
41
+ list = new Set();
42
+ listeners.set(key, list);
43
+ }
44
+ if (list.has(normalizedListener)) return;
45
+ signal?.addEventListener('abort', () => {
46
+ removeEventListener.call(this, type, listener, options);
47
+ }, {
48
+ once: true
49
+ });
50
+ list.add(normalizedListener);
51
+ this[constants.OWNER_DOCUMENT]?.defaultView[constants.HOOKS].addEventListener?.(this, type, listener, options);
52
+ }
53
+ removeEventListener(type, listener, options) {
54
+ return removeEventListener.call(this, type, listener, options);
55
+ }
56
+
57
+ // function isChildNode(node: EventTarget): node is ChildNode {
58
+ // return PARENT in node;
59
+ // }
60
+
61
+ dispatchEvent(event) {
62
+ const path = [];
63
+ // instanceof here is just to keep TypeScript happy
64
+ let target = this;
65
+ while (target != null) {
66
+ path.push(target);
67
+ target = target.parentNode;
68
+ }
69
+ // while (target instanceof Node && (target = target.parentNode)) {
70
+ // path.push(target);
71
+ // }
72
+ event.target = this;
73
+ event.srcElement = this;
74
+ event.path = path;
75
+ let defaultPrevented = false;
76
+ for (let i = path.length; --i;) {
77
+ if (Event.fireEvent(event, path[i], Event.EventPhase.CAPTURING_PHASE)) {
78
+ defaultPrevented = true;
79
+ }
80
+ }
81
+ if (Event.fireEvent(event, this, Event.EventPhase.AT_TARGET)) {
82
+ defaultPrevented = true;
83
+ }
84
+ for (let i = 1; i < path.length; i++) {
85
+ if (Event.fireEvent(event, path[i], Event.EventPhase.BUBBLING_PHASE)) {
86
+ defaultPrevented = true;
87
+ }
88
+ }
89
+ return !defaultPrevented;
90
+ }
91
+ }
92
+ function removeEventListener(type, listener, options) {
93
+ if (listener == null) return;
94
+ const onceListeners = this[ONCE_LISTENERS];
95
+ const normalizedListener = onceListeners?.get(listener) ?? listener;
96
+ onceListeners?.delete(listener);
97
+ const capture = options === true || options && options.capture === true;
98
+ const key = `${type}${capture ? Event.CAPTURE_MARKER : ''}`;
99
+ const list = this[constants.LISTENERS]?.get(key);
100
+ if (list) {
101
+ const deleted = list.delete(normalizedListener);
102
+ if (deleted) {
103
+ this[constants.OWNER_DOCUMENT]?.defaultView[constants.HOOKS].removeEventListener?.(this, type, listener, options);
104
+ }
105
+ }
106
+ }
107
+
108
+ exports.EventTarget = EventTarget;
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const Element = require('./Element.cjs');
6
+
7
+ class HTMLBodyElement extends Element.Element {}
8
+
9
+ exports.HTMLBodyElement = HTMLBodyElement;