@remote-dom/polyfill 0.0.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 (168) hide show
  1. package/LICENSE.md +7 -0
  2. package/README.md +45 -0
  3. package/build/cjs/Attr.cjs +54 -0
  4. package/build/cjs/CharacterData.cjs +29 -0
  5. package/build/cjs/ChildNode.cjs +45 -0
  6. package/build/cjs/Comment.cjs +11 -0
  7. package/build/cjs/CustomElementRegistry.cjs +35 -0
  8. package/build/cjs/CustomEvent.cjs +28 -0
  9. package/build/cjs/Document.cjs +94 -0
  10. package/build/cjs/DocumentFragment.cjs +12 -0
  11. package/build/cjs/Element.cjs +81 -0
  12. package/build/cjs/Event.cjs +113 -0
  13. package/build/cjs/EventTarget.cjs +101 -0
  14. package/build/cjs/HTMLElement.cjs +7 -0
  15. package/build/cjs/HTMLTemplateElement.cjs +26 -0
  16. package/build/cjs/MutationObserver.cjs +7 -0
  17. package/build/cjs/NamedNodeMap.cjs +115 -0
  18. package/build/cjs/Node.cjs +102 -0
  19. package/build/cjs/NodeList.cjs +9 -0
  20. package/build/cjs/ParentNode.cjs +130 -0
  21. package/build/cjs/SVGElement.cjs +19 -0
  22. package/build/cjs/Text.cjs +11 -0
  23. package/build/cjs/Window.cjs +69 -0
  24. package/build/cjs/constants.cjs +57 -0
  25. package/build/cjs/hooks.cjs +5 -0
  26. package/build/cjs/index.cjs +10 -0
  27. package/build/cjs/selectors.cjs +203 -0
  28. package/build/cjs/serialization.cjs +96 -0
  29. package/build/cjs/shared.cjs +63 -0
  30. package/build/esm/Attr.mjs +52 -0
  31. package/build/esm/CharacterData.mjs +27 -0
  32. package/build/esm/ChildNode.mjs +42 -0
  33. package/build/esm/Comment.mjs +9 -0
  34. package/build/esm/CustomElementRegistry.mjs +33 -0
  35. package/build/esm/CustomEvent.mjs +26 -0
  36. package/build/esm/Document.mjs +89 -0
  37. package/build/esm/DocumentFragment.mjs +10 -0
  38. package/build/esm/Element.mjs +95 -0
  39. package/build/esm/Event.mjs +108 -0
  40. package/build/esm/EventTarget.mjs +99 -0
  41. package/build/esm/HTMLElement.mjs +5 -0
  42. package/build/esm/HTMLTemplateElement.mjs +24 -0
  43. package/build/esm/MutationObserver.mjs +5 -0
  44. package/build/esm/NamedNodeMap.mjs +121 -0
  45. package/build/esm/Node.mjs +100 -0
  46. package/build/esm/NodeList.mjs +7 -0
  47. package/build/esm/ParentNode.mjs +128 -0
  48. package/build/esm/SVGElement.mjs +17 -0
  49. package/build/esm/Text.mjs +9 -0
  50. package/build/esm/Window.mjs +66 -0
  51. package/build/esm/constants.mjs +39 -0
  52. package/build/esm/hooks.mjs +3 -0
  53. package/build/esm/index.mjs +2 -0
  54. package/build/esm/selectors.mjs +197 -0
  55. package/build/esm/serialization.mjs +92 -0
  56. package/build/esm/shared.mjs +55 -0
  57. package/build/esnext/Attr.esnext +57 -0
  58. package/build/esnext/CharacterData.esnext +27 -0
  59. package/build/esnext/ChildNode.esnext +47 -0
  60. package/build/esnext/Comment.esnext +9 -0
  61. package/build/esnext/CustomElementRegistry.esnext +34 -0
  62. package/build/esnext/CustomEvent.esnext +25 -0
  63. package/build/esnext/Document.esnext +86 -0
  64. package/build/esnext/DocumentFragment.esnext +10 -0
  65. package/build/esnext/Element.esnext +99 -0
  66. package/build/esnext/Event.esnext +109 -0
  67. package/build/esnext/EventTarget.esnext +99 -0
  68. package/build/esnext/HTMLElement.esnext +6 -0
  69. package/build/esnext/HTMLTemplateElement.esnext +26 -0
  70. package/build/esnext/MutationObserver.esnext +5 -0
  71. package/build/esnext/NamedNodeMap.esnext +131 -0
  72. package/build/esnext/Node.esnext +116 -0
  73. package/build/esnext/NodeList.esnext +7 -0
  74. package/build/esnext/ParentNode.esnext +138 -0
  75. package/build/esnext/SVGElement.esnext +17 -0
  76. package/build/esnext/Text.esnext +9 -0
  77. package/build/esnext/Window.esnext +59 -0
  78. package/build/esnext/constants.esnext +37 -0
  79. package/build/esnext/hooks.esnext +3 -0
  80. package/build/esnext/index.esnext +2 -0
  81. package/build/esnext/selectors.esnext +169 -0
  82. package/build/esnext/serialization.esnext +76 -0
  83. package/build/esnext/shared.esnext +59 -0
  84. package/build/tsconfig.tsbuildinfo +1 -0
  85. package/build/typescript/Attr.d.ts +24 -0
  86. package/build/typescript/Attr.d.ts.map +1 -0
  87. package/build/typescript/CharacterData.d.ts +10 -0
  88. package/build/typescript/CharacterData.d.ts.map +1 -0
  89. package/build/typescript/ChildNode.d.ts +10 -0
  90. package/build/typescript/ChildNode.d.ts.map +1 -0
  91. package/build/typescript/Comment.d.ts +7 -0
  92. package/build/typescript/Comment.d.ts.map +1 -0
  93. package/build/typescript/CustomElementRegistry.d.ts +9 -0
  94. package/build/typescript/CustomElementRegistry.d.ts.map +1 -0
  95. package/build/typescript/CustomEvent.d.ts +17 -0
  96. package/build/typescript/CustomEvent.d.ts.map +1 -0
  97. package/build/typescript/Document.d.ts +27 -0
  98. package/build/typescript/Document.d.ts.map +1 -0
  99. package/build/typescript/DocumentFragment.d.ts +8 -0
  100. package/build/typescript/DocumentFragment.d.ts.map +1 -0
  101. package/build/typescript/Element.d.ts +32 -0
  102. package/build/typescript/Element.d.ts.map +1 -0
  103. package/build/typescript/Event.d.ts +47 -0
  104. package/build/typescript/Event.d.ts.map +1 -0
  105. package/build/typescript/EventTarget.d.ts +12 -0
  106. package/build/typescript/EventTarget.d.ts.map +1 -0
  107. package/build/typescript/HTMLElement.d.ts +4 -0
  108. package/build/typescript/HTMLElement.d.ts.map +1 -0
  109. package/build/typescript/HTMLTemplateElement.d.ts +11 -0
  110. package/build/typescript/HTMLTemplateElement.d.ts.map +1 -0
  111. package/build/typescript/MutationObserver.d.ts +3 -0
  112. package/build/typescript/MutationObserver.d.ts.map +1 -0
  113. package/build/typescript/NamedNodeMap.d.ts +18 -0
  114. package/build/typescript/NamedNodeMap.d.ts.map +1 -0
  115. package/build/typescript/Node.d.ts +37 -0
  116. package/build/typescript/Node.d.ts.map +1 -0
  117. package/build/typescript/NodeList.d.ts +4 -0
  118. package/build/typescript/NodeList.d.ts.map +1 -0
  119. package/build/typescript/ParentNode.d.ts +18 -0
  120. package/build/typescript/ParentNode.d.ts.map +1 -0
  121. package/build/typescript/SVGElement.d.ts +7 -0
  122. package/build/typescript/SVGElement.d.ts.map +1 -0
  123. package/build/typescript/Text.d.ts +7 -0
  124. package/build/typescript/Text.d.ts.map +1 -0
  125. package/build/typescript/Window.d.ts +45 -0
  126. package/build/typescript/Window.d.ts.map +1 -0
  127. package/build/typescript/constants.d.ts +35 -0
  128. package/build/typescript/constants.d.ts.map +1 -0
  129. package/build/typescript/hooks.d.ts +13 -0
  130. package/build/typescript/hooks.d.ts.map +1 -0
  131. package/build/typescript/index.d.ts +3 -0
  132. package/build/typescript/index.d.ts.map +1 -0
  133. package/build/typescript/selectors.d.ts +32 -0
  134. package/build/typescript/selectors.d.ts.map +1 -0
  135. package/build/typescript/serialization.d.ts +6 -0
  136. package/build/typescript/serialization.d.ts.map +1 -0
  137. package/build/typescript/shared.d.ts +16 -0
  138. package/build/typescript/shared.d.ts.map +1 -0
  139. package/package.json +35 -0
  140. package/rollup.config.js +3 -0
  141. package/source/Attr.ts +70 -0
  142. package/source/CharacterData.ts +29 -0
  143. package/source/ChildNode.ts +45 -0
  144. package/source/Comment.ts +7 -0
  145. package/source/CustomElementRegistry.ts +52 -0
  146. package/source/CustomEvent.ts +30 -0
  147. package/source/Document.ts +112 -0
  148. package/source/DocumentFragment.ts +8 -0
  149. package/source/Element.ts +119 -0
  150. package/source/Event.ts +142 -0
  151. package/source/EventTarget.ts +139 -0
  152. package/source/HTMLElement.ts +3 -0
  153. package/source/HTMLTemplateElement.ts +28 -0
  154. package/source/MutationObserver.ts +3 -0
  155. package/source/NamedNodeMap.ts +156 -0
  156. package/source/Node.ts +139 -0
  157. package/source/NodeList.ts +5 -0
  158. package/source/ParentNode.ts +158 -0
  159. package/source/SVGElement.ts +16 -0
  160. package/source/Text.ts +7 -0
  161. package/source/Window.ts +64 -0
  162. package/source/constants.ts +37 -0
  163. package/source/hooks.ts +28 -0
  164. package/source/index.ts +2 -0
  165. package/source/selectors.ts +230 -0
  166. package/source/serialization.ts +110 -0
  167. package/source/shared.ts +80 -0
  168. package/tsconfig.json +10 -0
package/LICENSE.md ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2020-present, Shopify Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # `@remote-dom/polyfill`
2
+
3
+ A polyfill for the browser APIs used by Remote DOM. This allows you to use Remote DOM in environments that don’t have a native DOM, like [Web Workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API).
4
+
5
+ This package provides a low-level polyfill, with hooks that allow other libraries to intercept changes to the DOM. Unless you know you need this package, you probably want to import from [`@remote-dom/core/polyfill`](../core/README.md#remote-domcorepolyfill) instead, which uses the hooks provided by this library to automatically synchronize remote elements between environments.
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ npm install @remote-dom/polyfill --save # npm
11
+ pnpm install @remote-dom/polyfill --save # pnpm
12
+ yarn add @remote-dom/polyfill # yarn
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ This package provides a `Window` class, which implements a limited subset of the [`Window` browser interface](https://developer.mozilla.org/en-US/docs/Web/API/Window). You’ll create an instance of the `Window` class and install it to the global environment using the `Window.setGlobal()` method.
18
+
19
+ ```ts
20
+ import {Window} from '@remote-dom/polyfill';
21
+
22
+ const window = new Window();
23
+ Window.setGlobal(window);
24
+
25
+ // Now you can use many important DOM APIs, like `document` and `Element`:
26
+ const div = document.createElement('div');
27
+ ```
28
+
29
+ This process will install polyfilled versions of the following globals:
30
+
31
+ - [`window`](https://developer.mozilla.org/en-US/docs/Web/API/Window/window), [`parent`](https://developer.mozilla.org/en-US/docs/Web/API/Window/parent), [`top`](https://developer.mozilla.org/en-US/docs/Web/API/Window/top), and [`self`](https://developer.mozilla.org/en-US/docs/Web/API/Window/self) which are all references to the `Window` instance (`self` is only overwritten when it is not already defined, to avoid overwriting the Web Worker `self` binding).
32
+ - [`document`](https://developer.mozilla.org/en-US/docs/Web/API/Window/document)
33
+ - [`customElements`](https://developer.mozilla.org/en-US/docs/Web/API/Window/customElements)
34
+ - [`location`](https://developer.mozilla.org/en-US/docs/Web/API/Window/location) and [`navigator`](https://developer.mozilla.org/en-US/docs/Web/API/Window/navigator), though these are just set to `globalThis.location` and `globalThis.navigator`.
35
+ - The [`Event`](https://developer.mozilla.org/en-US/docs/Web/API/Event), [`EventTarget`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget), [`CustomEvent`](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent), [`Node`](https://developer.mozilla.org/en-US/docs/Web/API/Node), [`ParentNode`](https://developer.mozilla.org/en-US/docs/Web/API/ParentNode), [`ChildNode`](https://developer.mozilla.org/en-US/docs/Web/API/ChildNode), [`Document`](https://developer.mozilla.org/en-US/docs/Web/API/Document), [`DocumentFragment`](https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment), [`CharacterData`](https://developer.mozilla.org/en-US/docs/Web/API/CharacterData), [`Comment`](https://developer.mozilla.org/en-US/docs/Web/API/Comment), [`Text`](https://developer.mozilla.org/en-US/docs/Web/API/Text), [`Element`](https://developer.mozilla.org/en-US/docs/Web/API/Element), [`HTMLElement`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement), [`SVGElement`](https://developer.mozilla.org/en-US/docs/Web/API/SVGElement), [`HTMLTemplateElement`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLTemplateElement), and [`MutationObserver`](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) constructors.
36
+
37
+ This polyfill lets you hook into many of the operations that happen in the DOM, like creating elements, updating attributes, and adding event listeners. You define these hooks by overwriting any of the properties on the `hooks` export of this library.
38
+
39
+ ```ts
40
+ import {hooks} from '@remote-dom/polyfill';
41
+
42
+ hooks.createElement = (element) => {
43
+ console.log('Creating element:', element);
44
+ };
45
+ ```
@@ -0,0 +1,54 @@
1
+ 'use strict';
2
+
3
+ var hooks = require('./hooks.cjs');
4
+ var constants = require('./constants.cjs');
5
+ var Node = require('./Node.cjs');
6
+
7
+ class Attr extends Node.Node {
8
+ nodeType = constants.NodeType.ATTRIBUTE_NODE;
9
+ [constants.NS] = null;
10
+ [constants.NEXT] = null;
11
+ [constants.OWNER_ELEMENT] = null;
12
+ isConnected = false;
13
+ constructor(name, value, namespace) {
14
+ super();
15
+ this[constants.NAME] = name;
16
+ this[constants.VALUE] = value;
17
+ if (namespace) this[constants.NS] = namespace;
18
+ }
19
+ get nodeName() {
20
+ return this[constants.NAME];
21
+ }
22
+ set nodeName(_readonly) {}
23
+ get name() {
24
+ return this[constants.NAME];
25
+ }
26
+ set name(_readonly) {}
27
+ get value() {
28
+ return this[constants.VALUE];
29
+ }
30
+ set value(value) {
31
+ const str = String(value);
32
+ this[constants.VALUE] = str;
33
+ const ownerElement = this[constants.OWNER_ELEMENT];
34
+ if (!ownerElement) return;
35
+ hooks.hooks.setAttribute?.(ownerElement, this[constants.NAME], str, this[constants.NS]);
36
+ }
37
+ get nodeValue() {
38
+ return this.value;
39
+ }
40
+ set nodeValue(value) {
41
+ this.value = value;
42
+ }
43
+ get ownerElement() {
44
+ return this[constants.OWNER_ELEMENT];
45
+ }
46
+ get namespaceURI() {
47
+ return this[constants.NS];
48
+ }
49
+ get specified() {
50
+ return true;
51
+ }
52
+ }
53
+
54
+ exports.Attr = Attr;
@@ -0,0 +1,29 @@
1
+ 'use strict';
2
+
3
+ var hooks = require('./hooks.cjs');
4
+ var constants = require('./constants.cjs');
5
+ var ChildNode = require('./ChildNode.cjs');
6
+
7
+ class CharacterData extends ChildNode.ChildNode {
8
+ [constants.DATA] = '';
9
+ constructor(data) {
10
+ super();
11
+ this.setData(data);
12
+ }
13
+ setData(data) {
14
+ let str = '';
15
+ if (data != null) {
16
+ str = typeof data === 'string' ? data : String(data);
17
+ }
18
+ this[constants.DATA] = str;
19
+ hooks.hooks.setText?.(this, str);
20
+ }
21
+ get data() {
22
+ return this[constants.DATA];
23
+ }
24
+ set data(data) {
25
+ this.setData(data);
26
+ }
27
+ }
28
+
29
+ exports.CharacterData = CharacterData;
@@ -0,0 +1,45 @@
1
+ 'use strict';
2
+
3
+ var constants = require('./constants.cjs');
4
+ var Node = require('./Node.cjs');
5
+
6
+ class ChildNode extends Node.Node {
7
+ remove() {
8
+ const parent = this.parentNode;
9
+ if (!parent) return;
10
+ parent.removeChild(this);
11
+ }
12
+ replaceWith(...nodes) {
13
+ const parent = this.parentNode;
14
+ if (!parent) return;
15
+ const node = toNode(parent, nodes[0]);
16
+ const next = node[constants.NEXT];
17
+ parent.replaceChild(this, node);
18
+ for (let i = 1; i < nodes.length; i++) {
19
+ parent.insertBefore(toNode(parent, nodes[i]), next);
20
+ }
21
+ }
22
+ before(...nodes) {
23
+ const parent = this.parentNode;
24
+ if (!parent) return;
25
+ for (const node of nodes) {
26
+ parent.insertBefore(toNode(parent, node), this);
27
+ }
28
+ }
29
+ after(...nodes) {
30
+ const parent = this.parentNode;
31
+ if (!parent) return;
32
+ const next = this[constants.NEXT];
33
+ for (const node of nodes) {
34
+ parent.insertBefore(toNode(parent, node), next);
35
+ }
36
+ }
37
+ }
38
+ function toNode(parent, node) {
39
+ if (node instanceof Node.Node) return node;
40
+ const ownerDocument = parent.ownerDocument;
41
+ return ownerDocument.createTextNode(String(node));
42
+ }
43
+
44
+ exports.ChildNode = ChildNode;
45
+ exports.toNode = toNode;
@@ -0,0 +1,11 @@
1
+ 'use strict';
2
+
3
+ var constants = require('./constants.cjs');
4
+ var CharacterData = require('./CharacterData.cjs');
5
+
6
+ class Comment extends CharacterData.CharacterData {
7
+ nodeType = 8;
8
+ [constants.NAME] = '#comment';
9
+ }
10
+
11
+ exports.Comment = Comment;
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+
3
+ class CustomElementRegistryImplementation {
4
+ registry = new Map();
5
+ listenersByName = new Map();
6
+ define(name, Constructor, _options) {
7
+ this.registry.set(name, Constructor);
8
+ const listeners = this.listenersByName.get(name);
9
+ if (listeners == null) return;
10
+ this.listenersByName.delete(name);
11
+ for (const listener of listeners) {
12
+ listener(Constructor);
13
+ }
14
+ }
15
+ get(name) {
16
+ return this.registry.get(name);
17
+ }
18
+ whenDefined(name) {
19
+ const Constructor = this.registry.get(name);
20
+ if (Constructor != null) return Promise.resolve(Constructor);
21
+ let listeners = this.listenersByName.get(name);
22
+ if (listeners == null) {
23
+ listeners = [];
24
+ this.listenersByName.set(name, listeners);
25
+ }
26
+ return new Promise(resolve => {
27
+ listeners.push(resolve);
28
+ });
29
+ }
30
+ upgrade(_root) {
31
+ // TODO
32
+ }
33
+ }
34
+
35
+ exports.CustomElementRegistryImplementation = CustomElementRegistryImplementation;
@@ -0,0 +1,28 @@
1
+ 'use strict';
2
+
3
+ var Event = require('./Event.cjs');
4
+
5
+ class CustomEvent extends Event.Event {
6
+ /**
7
+ * Returns any custom data event was created with. Typically used for synthetic events.
8
+ *
9
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CustomEvent/detail)
10
+ */
11
+
12
+ constructor(type, eventInitDict) {
13
+ super(type, eventInitDict);
14
+ this.detail = eventInitDict?.detail;
15
+ }
16
+
17
+ /**
18
+ * Returns any custom data event was created with. Typically used for synthetic events.
19
+ *
20
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CustomEvent/detail)
21
+ */
22
+ initCustomEvent(type, bubbles, cancelable, detail) {
23
+ super.initEvent(type, bubbles, cancelable);
24
+ this.detail = detail;
25
+ }
26
+ }
27
+
28
+ exports.CustomEvent = CustomEvent;
@@ -0,0 +1,94 @@
1
+ 'use strict';
2
+
3
+ var constants = require('./constants.cjs');
4
+ var Event = require('./Event.cjs');
5
+ var ParentNode = require('./ParentNode.cjs');
6
+ var Element = require('./Element.cjs');
7
+ var SVGElement = require('./SVGElement.cjs');
8
+ var Text = require('./Text.cjs');
9
+ var Comment = require('./Comment.cjs');
10
+ var DocumentFragment = require('./DocumentFragment.cjs');
11
+ var HTMLTemplateElement = require('./HTMLTemplateElement.cjs');
12
+ var shared = require('./shared.cjs');
13
+
14
+ class Document extends ParentNode.ParentNode {
15
+ nodeType = constants.NodeType.DOCUMENT_NODE;
16
+ [constants.NAME] = '#document';
17
+ constructor(defaultView) {
18
+ super();
19
+ this.defaultView = defaultView;
20
+ this[constants.OWNER_DOCUMENT] = this;
21
+ }
22
+ createElement(localName) {
23
+ const lowerName = String(localName).toLowerCase();
24
+ if (lowerName === 'template') {
25
+ return createElement(new HTMLTemplateElement.HTMLTemplateElement(), this, 'template');
26
+ }
27
+ const CustomElement = this.defaultView.customElements.get(localName);
28
+ if (CustomElement) {
29
+ return createElement(new CustomElement(), this, localName);
30
+ } else {
31
+ return createElement(new Element.Element(), this, localName);
32
+ }
33
+ }
34
+ createElementNS(localName, namespaceURI) {
35
+ if (namespaceURI === constants.NamespaceURI.SVG) {
36
+ return createElement(new SVGElement.SVGElement(), this, localName);
37
+ }
38
+ return createElement(new Element.Element(), this, localName, namespaceURI);
39
+ }
40
+ createTextNode(data) {
41
+ return createNode(new Text.Text(data), this);
42
+ }
43
+ createComment(data) {
44
+ return createNode(new Comment.Comment(data), this);
45
+ }
46
+ createDocumentFragment() {
47
+ return createNode(new DocumentFragment.DocumentFragment(), this);
48
+ }
49
+ createEvent() {
50
+ return new Event.Event('');
51
+ }
52
+ importNode(node, deep) {
53
+ return shared.cloneNode(node, deep, this);
54
+ }
55
+ adoptNode(node) {
56
+ if (node[constants.OWNER_DOCUMENT] === this) return node;
57
+ node.parentNode?.removeChild(node);
58
+ adoptNode(node, this);
59
+ return node;
60
+ }
61
+ }
62
+ function createNode(node, ownerDocument) {
63
+ Object.defineProperty(node, constants.OWNER_DOCUMENT, {
64
+ value: ownerDocument,
65
+ writable: true,
66
+ enumerable: false
67
+ });
68
+ return node;
69
+ }
70
+ function createElement(element, ownerDocument, name, namespace) {
71
+ createNode(element, ownerDocument);
72
+ Object.defineProperty(element, constants.NAME, {
73
+ value: name
74
+ });
75
+ if (namespace) {
76
+ Object.defineProperty(element, constants.NS, {
77
+ value: namespace
78
+ });
79
+ }
80
+ return element;
81
+ }
82
+ function adoptNode(node, document) {
83
+ node[constants.OWNER_DOCUMENT] = document;
84
+ if (shared.isParentNode(node)) {
85
+ for (const child of node.childNodes) {
86
+ adoptNode(child, document);
87
+ }
88
+ }
89
+ }
90
+
91
+ exports.Document = Document;
92
+ exports.adoptNode = adoptNode;
93
+ exports.createElement = createElement;
94
+ exports.createNode = createNode;
@@ -0,0 +1,12 @@
1
+ 'use strict';
2
+
3
+ var constants = require('./constants.cjs');
4
+ var ParentNode = require('./ParentNode.cjs');
5
+
6
+ class DocumentFragment extends ParentNode.ParentNode {
7
+ nodeType = constants.NodeType.DOCUMENT_FRAGMENT_NODE;
8
+ [constants.NAME] = '#document-fragment';
9
+ [constants.OWNER_DOCUMENT] = window.document;
10
+ }
11
+
12
+ exports.DocumentFragment = DocumentFragment;
@@ -0,0 +1,81 @@
1
+ 'use strict';
2
+
3
+ var constants = require('./constants.cjs');
4
+ var ParentNode = require('./ParentNode.cjs');
5
+ var NamedNodeMap = require('./NamedNodeMap.cjs');
6
+ var Attr = require('./Attr.cjs');
7
+ var serialization = require('./serialization.cjs');
8
+
9
+ class Element extends ParentNode.ParentNode {
10
+ nodeType = constants.NodeType.ELEMENT_NODE;
11
+ [constants.NS] = constants.NamespaceURI.XHTML;
12
+ [constants.SLOT] = '';
13
+ get namespaceURI() {
14
+ return this[constants.NS];
15
+ }
16
+ get tagName() {
17
+ return this.nodeName;
18
+ }
19
+ get slot() {
20
+ return this[constants.SLOT];
21
+ }
22
+ set slot(slot) {
23
+ const finalSlot = String(slot);
24
+ this[constants.SLOT] = finalSlot;
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
+ setAttribute(name, value) {
38
+ this.attributes.setNamedItem(new Attr.Attr(name, String(value)));
39
+ }
40
+ setAttributeNS(namespace, name, value) {
41
+ this.attributes.setNamedItemNS(new Attr.Attr(name, String(value), namespace));
42
+ }
43
+ getAttribute(name) {
44
+ const attr = this.attributes.getNamedItem(name);
45
+ return attr && attr.value;
46
+ }
47
+ getAttributeNS(namespace, name) {
48
+ const attr = this.attributes.getNamedItemNS(namespace, name);
49
+ return attr && attr.value;
50
+ }
51
+ hasAttribute(name) {
52
+ const attr = this.attributes.getNamedItem(name);
53
+ return attr != null;
54
+ }
55
+ hasAttributeNS(namespace, name) {
56
+ const attr = this.attributes.getNamedItemNS(namespace, name);
57
+ return attr != null;
58
+ }
59
+ removeAttribute(name) {
60
+ this.attributes.removeNamedItem(name);
61
+ }
62
+ removeAttributeNS(namespace, name) {
63
+ this.attributes.removeNamedItemNS(namespace, name);
64
+ }
65
+ get outerHTML() {
66
+ return serialization.serializeNode(this);
67
+ }
68
+ get innerHTML() {
69
+ return serialization.serializeChildren(this);
70
+ }
71
+ set innerHTML(html) {
72
+ if (html == null || html === '') {
73
+ this.replaceChildren();
74
+ } else {
75
+ const fragment = serialization.parseHtml(String(html), this);
76
+ this.replaceChildren(fragment);
77
+ }
78
+ }
79
+ }
80
+
81
+ exports.Element = Element;
@@ -0,0 +1,113 @@
1
+ 'use strict';
2
+
3
+ var constants = require('./constants.cjs');
4
+
5
+ let EventPhase = /*#__PURE__*/function (EventPhase) {
6
+ EventPhase[EventPhase["NONE"] = 0] = "NONE";
7
+ EventPhase[EventPhase["CAPTURING_PHASE"] = 1] = "CAPTURING_PHASE";
8
+ EventPhase[EventPhase["AT_TARGET"] = 2] = "AT_TARGET";
9
+ EventPhase[EventPhase["BUBBLING_PHASE"] = 3] = "BUBBLING_PHASE";
10
+ return EventPhase;
11
+ }({});
12
+ const CAPTURE_MARKER = '@';
13
+ const now = typeof performance === 'undefined' ? Date.now : performance.now.bind(performance);
14
+ class Event {
15
+ static NONE = EventPhase.NONE;
16
+ static CAPTURING_PHASE = EventPhase.CAPTURING_PHASE;
17
+ static AT_TARGET = EventPhase.AT_TARGET;
18
+ static BUBBLING_PHASE = EventPhase.BUBBLING_PHASE;
19
+
20
+ // NONE = EventPhase.NONE;
21
+ // CAPTURING_PHASE = EventPhase.CAPTURING_PHASE;
22
+ // AT_TARGET = EventPhase.AT_TARGET;
23
+ // BUBBLING_PHASE = EventPhase.BUBBLING_PHASE;
24
+
25
+ timeStamp = now();
26
+ target = null;
27
+ currentTarget = null;
28
+ srcElement = null;
29
+ bubbles = false;
30
+ cancelable = false;
31
+ composed = false;
32
+ defaultPrevented = false;
33
+ cancelBubble = false;
34
+ immediatePropagationStopped = false;
35
+ eventPhase = 0;
36
+ path = [];
37
+ // private inPassiveListener = false;
38
+
39
+ constructor(type, options) {
40
+ this.type = type;
41
+ Object.defineProperty(this, constants.IS_TRUSTED, {
42
+ writable: true,
43
+ value: false
44
+ });
45
+ if (options) {
46
+ if (options.bubbles) this.bubbles = options.bubbles;
47
+ if (options.cancelable) this.cancelable = options.cancelable;
48
+ if (options.composed) this.composed = options.composed;
49
+ }
50
+ }
51
+ get composedPath() {
52
+ return this.path;
53
+ }
54
+ get isTrusted() {
55
+ return this[constants.IS_TRUSTED];
56
+ }
57
+ stopPropagation() {
58
+ this.cancelBubble = true;
59
+ }
60
+ stopImmediatePropagation() {
61
+ this.immediatePropagationStopped = true;
62
+ }
63
+ preventDefault() {
64
+ this.defaultPrevented = true;
65
+ }
66
+ set returnValue(value) {
67
+ this.defaultPrevented = value;
68
+ }
69
+ get returnValue() {
70
+ return this.defaultPrevented;
71
+ }
72
+
73
+ /** @deprecated */
74
+ initEvent(type, bubbles, cancelable) {
75
+ this.type = type;
76
+ this.bubbles = Boolean(bubbles);
77
+ this.cancelable = Boolean(cancelable);
78
+ }
79
+ }
80
+ function fireEvent(event, target, phase) {
81
+ const listeners = target[constants.LISTENERS];
82
+ const list = listeners?.get(`${event.type}${phase === EventPhase.CAPTURING_PHASE ? CAPTURE_MARKER : ''}`);
83
+ if (!list) return false;
84
+ let defaultPrevented = false;
85
+ for (const listener of list) {
86
+ event.eventPhase = phase;
87
+ event.currentTarget = target;
88
+ try {
89
+ if (typeof listener === 'object') {
90
+ listener.handleEvent(event);
91
+ } else {
92
+ listener.call(target, event);
93
+ }
94
+ } catch (err) {
95
+ setTimeout(thrower, 0, err);
96
+ }
97
+ if (event.defaultPrevented === true) {
98
+ defaultPrevented = true;
99
+ }
100
+ if (event.immediatePropagationStopped) {
101
+ break;
102
+ }
103
+ }
104
+ return defaultPrevented;
105
+ }
106
+ function thrower(error) {
107
+ throw error;
108
+ }
109
+
110
+ exports.CAPTURE_MARKER = CAPTURE_MARKER;
111
+ exports.Event = Event;
112
+ exports.EventPhase = EventPhase;
113
+ exports.fireEvent = fireEvent;
@@ -0,0 +1,101 @@
1
+ 'use strict';
2
+
3
+ var hooks = require('./hooks.cjs');
4
+ var constants = require('./constants.cjs');
5
+ var Event = require('./Event.cjs');
6
+
7
+ const ONCE_LISTENERS = Symbol('onceListeners');
8
+ class EventTarget {
9
+ addEventListener(type, listener, options) {
10
+ if (listener == null) return;
11
+ const capture = options === true || options && options.capture === true;
12
+ const once = typeof options === 'object' && options.once === true;
13
+ const signal = typeof options === 'object' ? options.signal : undefined;
14
+ const key = `${type}${capture ? Event.CAPTURE_MARKER : ''}`;
15
+ let normalizedListener = listener;
16
+ if (once) {
17
+ normalizedListener = function normalizedListener(...args) {
18
+ this.removeEventListener(type, listener, options);
19
+ return typeof listener === 'object' ? listener.handleEvent(...args) : listener.call(this, ...args);
20
+ };
21
+ let onceListeners = this[ONCE_LISTENERS];
22
+ if (!onceListeners) {
23
+ onceListeners = new WeakMap();
24
+ this[ONCE_LISTENERS] = onceListeners;
25
+ }
26
+ onceListeners.set(listener, normalizedListener);
27
+ }
28
+ let listeners = this[constants.LISTENERS];
29
+ if (!listeners) {
30
+ listeners = new Map();
31
+ this[constants.LISTENERS] = listeners;
32
+ }
33
+ let list = listeners.get(key);
34
+ if (!list) {
35
+ list = new Set();
36
+ listeners.set(key, list);
37
+ }
38
+ list.add(normalizedListener);
39
+ signal?.addEventListener('abort', () => {
40
+ removeEventListener.call(this, type, listener, options);
41
+ }, {
42
+ once: true
43
+ });
44
+ hooks.hooks.addEventListener?.(this, type, listener, options);
45
+ }
46
+ removeEventListener(type, listener, options) {
47
+ return removeEventListener.call(this, type, listener, options);
48
+ }
49
+
50
+ // function isChildNode(node: EventTarget): node is ChildNode {
51
+ // return PARENT in node;
52
+ // }
53
+
54
+ dispatchEvent(event) {
55
+ const path = [];
56
+ // instanceof here is just to keep TypeScript happy
57
+ let target = this;
58
+ while (target != null) {
59
+ path.push(target);
60
+ target = target.parentNode;
61
+ }
62
+ // while (target instanceof Node && (target = target.parentNode)) {
63
+ // path.push(target);
64
+ // }
65
+ event.target = this;
66
+ event.srcElement = this;
67
+ event.path = path;
68
+ let defaultPrevented = false;
69
+ for (let i = path.length; --i;) {
70
+ if (Event.fireEvent(event, path[i], Event.EventPhase.CAPTURING_PHASE)) {
71
+ defaultPrevented = true;
72
+ }
73
+ }
74
+ if (Event.fireEvent(event, this, Event.EventPhase.AT_TARGET)) {
75
+ defaultPrevented = true;
76
+ }
77
+ for (let i = 1; i < path.length; i++) {
78
+ if (Event.fireEvent(event, path[i], Event.EventPhase.BUBBLING_PHASE)) {
79
+ defaultPrevented = true;
80
+ }
81
+ }
82
+ return !defaultPrevented;
83
+ }
84
+ }
85
+ function removeEventListener(type, listener, options) {
86
+ if (listener == null) return;
87
+ const onceListeners = this[ONCE_LISTENERS];
88
+ const normalizedListener = onceListeners?.get(listener) ?? listener;
89
+ onceListeners?.delete(listener);
90
+ const capture = options === true || options && options.capture === true;
91
+ const key = `${type}${capture ? Event.CAPTURE_MARKER : ''}`;
92
+ const list = this[constants.LISTENERS]?.get(key);
93
+ if (list) {
94
+ const deleted = list.delete(normalizedListener);
95
+ if (deleted) {
96
+ hooks.hooks.removeEventListener?.(this, type, listener, options);
97
+ }
98
+ }
99
+ }
100
+
101
+ exports.EventTarget = EventTarget;
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ var Element = require('./Element.cjs');
4
+
5
+ class HTMLElement extends Element.Element {}
6
+
7
+ exports.HTMLElement = HTMLElement;