@remote-dom/polyfill 0.0.0-preview-20240219211553

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 (142) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/LICENSE.md +7 -0
  3. package/README.md +45 -0
  4. package/build/esm/Attr.mjs +52 -0
  5. package/build/esm/CharacterData.mjs +27 -0
  6. package/build/esm/ChildNode.mjs +42 -0
  7. package/build/esm/Comment.mjs +9 -0
  8. package/build/esm/CustomElementRegistry.mjs +33 -0
  9. package/build/esm/CustomEvent.mjs +26 -0
  10. package/build/esm/Document.mjs +89 -0
  11. package/build/esm/DocumentFragment.mjs +10 -0
  12. package/build/esm/Element.mjs +95 -0
  13. package/build/esm/Event.mjs +108 -0
  14. package/build/esm/EventTarget.mjs +99 -0
  15. package/build/esm/HTMLElement.mjs +5 -0
  16. package/build/esm/HTMLTemplateElement.mjs +24 -0
  17. package/build/esm/MutationObserver.mjs +5 -0
  18. package/build/esm/NamedNodeMap.mjs +121 -0
  19. package/build/esm/Node.mjs +100 -0
  20. package/build/esm/NodeList.mjs +7 -0
  21. package/build/esm/ParentNode.mjs +128 -0
  22. package/build/esm/SVGElement.mjs +17 -0
  23. package/build/esm/Text.mjs +9 -0
  24. package/build/esm/Window.mjs +66 -0
  25. package/build/esm/constants.mjs +39 -0
  26. package/build/esm/hooks.mjs +3 -0
  27. package/build/esm/index.mjs +2 -0
  28. package/build/esm/selectors.mjs +197 -0
  29. package/build/esm/serialization.mjs +92 -0
  30. package/build/esm/shared.mjs +55 -0
  31. package/build/esnext/Attr.esnext +57 -0
  32. package/build/esnext/CharacterData.esnext +27 -0
  33. package/build/esnext/ChildNode.esnext +47 -0
  34. package/build/esnext/Comment.esnext +9 -0
  35. package/build/esnext/CustomElementRegistry.esnext +34 -0
  36. package/build/esnext/CustomEvent.esnext +25 -0
  37. package/build/esnext/Document.esnext +86 -0
  38. package/build/esnext/DocumentFragment.esnext +10 -0
  39. package/build/esnext/Element.esnext +99 -0
  40. package/build/esnext/Event.esnext +109 -0
  41. package/build/esnext/EventTarget.esnext +99 -0
  42. package/build/esnext/HTMLElement.esnext +6 -0
  43. package/build/esnext/HTMLTemplateElement.esnext +26 -0
  44. package/build/esnext/MutationObserver.esnext +5 -0
  45. package/build/esnext/NamedNodeMap.esnext +131 -0
  46. package/build/esnext/Node.esnext +116 -0
  47. package/build/esnext/NodeList.esnext +7 -0
  48. package/build/esnext/ParentNode.esnext +138 -0
  49. package/build/esnext/SVGElement.esnext +17 -0
  50. package/build/esnext/Text.esnext +9 -0
  51. package/build/esnext/Window.esnext +59 -0
  52. package/build/esnext/constants.esnext +37 -0
  53. package/build/esnext/hooks.esnext +3 -0
  54. package/build/esnext/index.esnext +2 -0
  55. package/build/esnext/selectors.esnext +169 -0
  56. package/build/esnext/serialization.esnext +76 -0
  57. package/build/esnext/shared.esnext +59 -0
  58. package/build/tsconfig.tsbuildinfo +1 -0
  59. package/build/typescript/Attr.d.ts +24 -0
  60. package/build/typescript/Attr.d.ts.map +1 -0
  61. package/build/typescript/CharacterData.d.ts +10 -0
  62. package/build/typescript/CharacterData.d.ts.map +1 -0
  63. package/build/typescript/ChildNode.d.ts +10 -0
  64. package/build/typescript/ChildNode.d.ts.map +1 -0
  65. package/build/typescript/Comment.d.ts +7 -0
  66. package/build/typescript/Comment.d.ts.map +1 -0
  67. package/build/typescript/CustomElementRegistry.d.ts +9 -0
  68. package/build/typescript/CustomElementRegistry.d.ts.map +1 -0
  69. package/build/typescript/CustomEvent.d.ts +17 -0
  70. package/build/typescript/CustomEvent.d.ts.map +1 -0
  71. package/build/typescript/Document.d.ts +27 -0
  72. package/build/typescript/Document.d.ts.map +1 -0
  73. package/build/typescript/DocumentFragment.d.ts +8 -0
  74. package/build/typescript/DocumentFragment.d.ts.map +1 -0
  75. package/build/typescript/Element.d.ts +32 -0
  76. package/build/typescript/Element.d.ts.map +1 -0
  77. package/build/typescript/Event.d.ts +47 -0
  78. package/build/typescript/Event.d.ts.map +1 -0
  79. package/build/typescript/EventTarget.d.ts +12 -0
  80. package/build/typescript/EventTarget.d.ts.map +1 -0
  81. package/build/typescript/HTMLElement.d.ts +4 -0
  82. package/build/typescript/HTMLElement.d.ts.map +1 -0
  83. package/build/typescript/HTMLTemplateElement.d.ts +11 -0
  84. package/build/typescript/HTMLTemplateElement.d.ts.map +1 -0
  85. package/build/typescript/MutationObserver.d.ts +3 -0
  86. package/build/typescript/MutationObserver.d.ts.map +1 -0
  87. package/build/typescript/NamedNodeMap.d.ts +18 -0
  88. package/build/typescript/NamedNodeMap.d.ts.map +1 -0
  89. package/build/typescript/Node.d.ts +37 -0
  90. package/build/typescript/Node.d.ts.map +1 -0
  91. package/build/typescript/NodeList.d.ts +4 -0
  92. package/build/typescript/NodeList.d.ts.map +1 -0
  93. package/build/typescript/ParentNode.d.ts +18 -0
  94. package/build/typescript/ParentNode.d.ts.map +1 -0
  95. package/build/typescript/SVGElement.d.ts +7 -0
  96. package/build/typescript/SVGElement.d.ts.map +1 -0
  97. package/build/typescript/Text.d.ts +7 -0
  98. package/build/typescript/Text.d.ts.map +1 -0
  99. package/build/typescript/Window.d.ts +45 -0
  100. package/build/typescript/Window.d.ts.map +1 -0
  101. package/build/typescript/constants.d.ts +35 -0
  102. package/build/typescript/constants.d.ts.map +1 -0
  103. package/build/typescript/hooks.d.ts +13 -0
  104. package/build/typescript/hooks.d.ts.map +1 -0
  105. package/build/typescript/index.d.ts +3 -0
  106. package/build/typescript/index.d.ts.map +1 -0
  107. package/build/typescript/selectors.d.ts +32 -0
  108. package/build/typescript/selectors.d.ts.map +1 -0
  109. package/build/typescript/serialization.d.ts +6 -0
  110. package/build/typescript/serialization.d.ts.map +1 -0
  111. package/build/typescript/shared.d.ts +16 -0
  112. package/build/typescript/shared.d.ts.map +1 -0
  113. package/package.json +35 -0
  114. package/rollup.config.js +3 -0
  115. package/source/Attr.ts +70 -0
  116. package/source/CharacterData.ts +29 -0
  117. package/source/ChildNode.ts +45 -0
  118. package/source/Comment.ts +7 -0
  119. package/source/CustomElementRegistry.ts +52 -0
  120. package/source/CustomEvent.ts +30 -0
  121. package/source/Document.ts +112 -0
  122. package/source/DocumentFragment.ts +8 -0
  123. package/source/Element.ts +119 -0
  124. package/source/Event.ts +142 -0
  125. package/source/EventTarget.ts +139 -0
  126. package/source/HTMLElement.ts +3 -0
  127. package/source/HTMLTemplateElement.ts +28 -0
  128. package/source/MutationObserver.ts +3 -0
  129. package/source/NamedNodeMap.ts +156 -0
  130. package/source/Node.ts +139 -0
  131. package/source/NodeList.ts +5 -0
  132. package/source/ParentNode.ts +158 -0
  133. package/source/SVGElement.ts +16 -0
  134. package/source/Text.ts +7 -0
  135. package/source/Window.ts +64 -0
  136. package/source/constants.ts +37 -0
  137. package/source/hooks.ts +28 -0
  138. package/source/index.ts +2 -0
  139. package/source/selectors.ts +230 -0
  140. package/source/serialization.ts +110 -0
  141. package/source/shared.ts +80 -0
  142. package/tsconfig.json +10 -0
@@ -0,0 +1,138 @@
1
+ import { hooks } from './hooks.esnext';
2
+ import { PREV, NEXT, CHILD, NodeType, OWNER_DOCUMENT, PARENT } from './constants.esnext';
3
+ import { ChildNode, toNode } from './ChildNode.esnext';
4
+ import { NodeList } from './NodeList.esnext';
5
+ import { querySelectorAll, querySelector } from './selectors.esnext';
6
+
7
+ class ParentNode extends ChildNode {
8
+ childNodes = new NodeList();
9
+ children = new NodeList();
10
+ appendChild(child) {
11
+ this.insertInto(child, null);
12
+ }
13
+ insertBefore(child, ref) {
14
+ this.insertInto(child, ref || null);
15
+ }
16
+ append(...nodes) {
17
+ for (const child of nodes) {
18
+ if (child == null)
19
+ continue;
20
+ this.appendChild(toNode(this, child));
21
+ }
22
+ }
23
+ prepend(...nodes) {
24
+ const before = this.firstChild;
25
+ for (const child of nodes) {
26
+ if (child == null)
27
+ continue;
28
+ this.insertBefore(toNode(this, child), before);
29
+ }
30
+ }
31
+ replaceChildren(...nodes) {
32
+ let child;
33
+ while (child = this.firstChild) {
34
+ this.removeChild(child);
35
+ }
36
+ this.append(...nodes);
37
+ }
38
+ removeChild(child) {
39
+ if (child.parentNode !== this)
40
+ throw Error(`not a child of this node`);
41
+ const prev = child[PREV];
42
+ const next = child[NEXT];
43
+ if (prev)
44
+ prev[NEXT] = next;
45
+ else
46
+ this[CHILD] = next;
47
+ if (next)
48
+ next[PREV] = prev;
49
+ const childNodes = this.childNodes;
50
+ const childNodesIndex = childNodes.indexOf(child);
51
+ childNodes.splice(childNodesIndex, 1);
52
+ if (child.nodeType === 1) {
53
+ const children = this.children;
54
+ children.splice(children.indexOf(child), 1);
55
+ }
56
+ hooks.removeChild?.(this, child, childNodesIndex);
57
+ }
58
+ replaceChild(newChild, oldChild) {
59
+ if (oldChild.parentNode !== this) {
60
+ throw Error("reference node is not a child of this parent");
61
+ }
62
+ const next = oldChild[NEXT];
63
+ this.removeChild(oldChild);
64
+ this.insertInto(newChild, next);
65
+ }
66
+ querySelectorAll(selector) {
67
+ return querySelectorAll(this, selector);
68
+ }
69
+ querySelector(selector) {
70
+ return querySelector(this, selector);
71
+ }
72
+ insertInto(child, before) {
73
+ if (child.nodeType === NodeType.DOCUMENT_FRAGMENT_NODE) {
74
+ let node = child[CHILD];
75
+ while (node) {
76
+ const next = node[NEXT];
77
+ this.insertInto(node, before);
78
+ node = next;
79
+ }
80
+ return;
81
+ }
82
+ if (child.parentNode !== null) {
83
+ child.parentNode.removeChild(child);
84
+ }
85
+ if (before) {
86
+ if (before.parentNode !== this) {
87
+ throw Error("reference node is not a child of this parent");
88
+ }
89
+ child[NEXT] = before;
90
+ child[PREV] = before[PREV];
91
+ if (before[PREV] === null)
92
+ this[CHILD] = child;
93
+ before[PREV] = child;
94
+ } else {
95
+ child[NEXT] = null;
96
+ let last = this[CHILD];
97
+ if (last) {
98
+ let next;
99
+ while (next = last[NEXT])
100
+ last = next;
101
+ last[NEXT] = child;
102
+ child[PREV] = last;
103
+ } else {
104
+ this[CHILD] = child;
105
+ child[PREV] = null;
106
+ }
107
+ }
108
+ const ownerDocument = this[OWNER_DOCUMENT];
109
+ const isElement = child.nodeType === NodeType.ELEMENT_NODE;
110
+ child[PARENT] = this;
111
+ child[OWNER_DOCUMENT] = ownerDocument;
112
+ const childNodes = this.childNodes;
113
+ let insertIndex;
114
+ if (before) {
115
+ insertIndex = childNodes.indexOf(before);
116
+ childNodes.splice(insertIndex, 0, child);
117
+ if (isElement) {
118
+ const children = this.children;
119
+ let ref = before;
120
+ while (ref && ref.nodeType !== 1)
121
+ ref = ref[NEXT];
122
+ if (ref) {
123
+ children.splice(children.indexOf(ref), 0, child);
124
+ } else {
125
+ children.push(child);
126
+ }
127
+ }
128
+ } else {
129
+ insertIndex = childNodes.length;
130
+ childNodes.push(child);
131
+ if (isElement)
132
+ this.children.push(child);
133
+ }
134
+ hooks.insertChild?.(this, child, insertIndex);
135
+ }
136
+ }
137
+
138
+ export { ParentNode };
@@ -0,0 +1,17 @@
1
+ import { NS, NamespaceURI } from './constants.esnext';
2
+ import { Element } from './Element.esnext';
3
+
4
+ class SVGElement extends Element {
5
+ [NS] = NamespaceURI.SVG;
6
+ get ownerSVGElement() {
7
+ let root = null;
8
+ let parent = this.parentNode;
9
+ while (parent instanceof SVGElement) {
10
+ root = parent;
11
+ parent = parent.parentNode;
12
+ }
13
+ return root;
14
+ }
15
+ }
16
+
17
+ export { SVGElement };
@@ -0,0 +1,9 @@
1
+ import { NodeType, NAME } from './constants.esnext';
2
+ import { CharacterData } from './CharacterData.esnext';
3
+
4
+ class Text extends CharacterData {
5
+ nodeType = NodeType.TEXT_NODE;
6
+ [NAME] = "#text";
7
+ }
8
+
9
+ export { Text };
@@ -0,0 +1,59 @@
1
+ import { Document } from './Document.esnext';
2
+ import { Event } from './Event.esnext';
3
+ import { EventTarget } from './EventTarget.esnext';
4
+ import { CustomEvent } from './CustomEvent.esnext';
5
+ import { Node } from './Node.esnext';
6
+ import { ParentNode } from './ParentNode.esnext';
7
+ import { ChildNode } from './ChildNode.esnext';
8
+ import { Element } from './Element.esnext';
9
+ import { HTMLElement } from './HTMLElement.esnext';
10
+ import { SVGElement } from './SVGElement.esnext';
11
+ import { CharacterData } from './CharacterData.esnext';
12
+ import { Text } from './Text.esnext';
13
+ import { Comment } from './Comment.esnext';
14
+ import { DocumentFragment } from './DocumentFragment.esnext';
15
+ import { HTMLTemplateElement } from './HTMLTemplateElement.esnext';
16
+ import { CustomElementRegistryImplementation } from './CustomElementRegistry.esnext';
17
+ import { MutationObserver } from './MutationObserver.esnext';
18
+
19
+ class Window extends EventTarget {
20
+ name = "";
21
+ parent = this;
22
+ self = this;
23
+ top = this;
24
+ document = new Document(this);
25
+ customElements = new CustomElementRegistryImplementation();
26
+ location = globalThis.location;
27
+ navigator = globalThis.navigator;
28
+ Event = Event;
29
+ EventTarget = EventTarget;
30
+ CustomEvent = CustomEvent;
31
+ Node = Node;
32
+ ParentNode = ParentNode;
33
+ ChildNode = ChildNode;
34
+ DocumentFragment = DocumentFragment;
35
+ Document = Document;
36
+ CharacterData = CharacterData;
37
+ Comment = Comment;
38
+ Text = Text;
39
+ Element = Element;
40
+ HTMLElement = HTMLElement;
41
+ SVGElement = SVGElement;
42
+ HTMLTemplateElement = HTMLTemplateElement;
43
+ MutationObserver = MutationObserver;
44
+ static setGlobal(window) {
45
+ const properties = Object.getOwnPropertyDescriptors(window);
46
+ delete properties.self;
47
+ Object.defineProperties(globalThis, {
48
+ ...properties,
49
+ window: { value: window }
50
+ });
51
+ if (typeof self === "undefined") {
52
+ Object.defineProperty(globalThis, "self", { value: window });
53
+ } else {
54
+ Object.defineProperties(self, properties);
55
+ }
56
+ }
57
+ }
58
+
59
+ export { Window };
@@ -0,0 +1,37 @@
1
+ const NAME = Symbol("name");
2
+ const VALUE = Symbol("value");
3
+ const NS = Symbol("ns");
4
+ const OWNER_ELEMENT = Symbol("owner");
5
+ const OWNER_DOCUMENT = Symbol("owner_document");
6
+ const ATTRIBUTES = Symbol("attributes");
7
+ const PREV = Symbol("prev");
8
+ const NEXT = Symbol("next");
9
+ const CHILD = Symbol("child");
10
+ const PARENT = Symbol("parent");
11
+ const DATA = Symbol("data");
12
+ const LISTENERS = Symbol("listeners");
13
+ const IS_TRUSTED = Symbol("isTrusted");
14
+ const CONTENT = Symbol("content");
15
+ const SLOT = Symbol("slot");
16
+ var NodeType = /* @__PURE__ */ ((NodeType2) => {
17
+ NodeType2[NodeType2["NODE"] = 0] = "NODE";
18
+ NodeType2[NodeType2["ELEMENT_NODE"] = 1] = "ELEMENT_NODE";
19
+ NodeType2[NodeType2["ATTRIBUTE_NODE"] = 2] = "ATTRIBUTE_NODE";
20
+ NodeType2[NodeType2["TEXT_NODE"] = 3] = "TEXT_NODE";
21
+ NodeType2[NodeType2["CDATA_SECTION_NODE"] = 4] = "CDATA_SECTION_NODE";
22
+ NodeType2[NodeType2["ENTITY_REFERENCE_NODE"] = 5] = "ENTITY_REFERENCE_NODE";
23
+ NodeType2[NodeType2["ENTITY_NODE"] = 6] = "ENTITY_NODE";
24
+ NodeType2[NodeType2["PROCESSING_INSTRUCTION_NODE"] = 7] = "PROCESSING_INSTRUCTION_NODE";
25
+ NodeType2[NodeType2["COMMENT_NODE"] = 8] = "COMMENT_NODE";
26
+ NodeType2[NodeType2["DOCUMENT_NODE"] = 9] = "DOCUMENT_NODE";
27
+ NodeType2[NodeType2["DOCUMENT_TYPE_NODE"] = 10] = "DOCUMENT_TYPE_NODE";
28
+ NodeType2[NodeType2["DOCUMENT_FRAGMENT_NODE"] = 11] = "DOCUMENT_FRAGMENT_NODE";
29
+ return NodeType2;
30
+ })(NodeType || {});
31
+ var NamespaceURI = /* @__PURE__ */ ((NamespaceURI2) => {
32
+ NamespaceURI2["XHTML"] = "http://www.w3.org/1999/xhtml";
33
+ NamespaceURI2["SVG"] = "http://www.w3.org/2000/svg";
34
+ return NamespaceURI2;
35
+ })(NamespaceURI || {});
36
+
37
+ export { ATTRIBUTES, CHILD, CONTENT, DATA, IS_TRUSTED, LISTENERS, NAME, NEXT, NS, NamespaceURI, NodeType, OWNER_DOCUMENT, OWNER_ELEMENT, PARENT, PREV, SLOT, VALUE };
@@ -0,0 +1,3 @@
1
+ const hooks = {};
2
+
3
+ export { hooks };
@@ -0,0 +1,2 @@
1
+ export { hooks } from './hooks.esnext';
2
+ export { Window } from './Window.esnext';
@@ -0,0 +1,169 @@
1
+ import { CHILD, NEXT, PARENT, PREV } from './constants.esnext';
2
+ import { isElementNode } from './shared.esnext';
3
+
4
+ const ELEMENT_SELECTOR_TEST = /[a-z]/;
5
+ function querySelector(within, selector) {
6
+ const parts = parseSelector(selector);
7
+ let result = null;
8
+ const child = within[CHILD];
9
+ if (child && parts[0].matchers.length) {
10
+ walkNodesForSelector(child, parts, (node) => {
11
+ result = node;
12
+ return false;
13
+ });
14
+ }
15
+ return result;
16
+ }
17
+ function querySelectorAll(within, selector) {
18
+ const parts = parseSelector(selector);
19
+ const results = [];
20
+ const child = within[CHILD];
21
+ if (child && parts[0].matchers.length) {
22
+ walkNodesForSelector(child, parts, (node) => {
23
+ results.push(node);
24
+ });
25
+ }
26
+ return results;
27
+ }
28
+ function parseSelector(selector) {
29
+ let part = { combinator: 4 /* Inner */, matchers: [] };
30
+ const parts = [part];
31
+ const tokenizer = /\s*?([>\s+~]?)\s*?(?:(?:\[\s*([^\]=]+)(?:=(['"])(.*?)\3)?\s*\])|([#.]?)([^\s#.[>:]+)|:(\w+)(?:\((.*?)\))?)/gi;
32
+ let token;
33
+ while (token = tokenizer.exec(selector)) {
34
+ if (token[1]) {
35
+ if (token[1] === ">")
36
+ part.combinator = 1 /* Child */;
37
+ else if (token[1] === "+")
38
+ part.combinator = 3 /* Adjacent */;
39
+ else if (token[1] === "~")
40
+ part.combinator = 2 /* Sibling */;
41
+ else
42
+ part.combinator = 0 /* Descendant */;
43
+ part = { combinator: 4 /* Inner */, matchers: [] };
44
+ parts.push(part);
45
+ }
46
+ let type = 0 /* Unknown */;
47
+ if (token[2]) {
48
+ type = 4 /* Attribute */;
49
+ } else if (token[5]) {
50
+ type = token[5] === "#" ? 2 /* Id */ : 3 /* Class */;
51
+ } else if (token[7]) {
52
+ type = token[8] == null ? 5 /* Pseudo */ : 6 /* Function */;
53
+ } else if (token[6] && ELEMENT_SELECTOR_TEST.test(token[6])) {
54
+ type = 1 /* Element */;
55
+ }
56
+ part.matchers.push({
57
+ type,
58
+ name: token[2] || token[6] || token[7],
59
+ value: token[4] ?? token[6] ?? token[8]
60
+ });
61
+ }
62
+ return parts;
63
+ }
64
+ function matchesSelector(element, selector) {
65
+ const parsed = parseSelector(selector);
66
+ let part;
67
+ while (part = parsed.pop()) {
68
+ if (!matchesSelectorPart(element, part))
69
+ return false;
70
+ }
71
+ return true;
72
+ }
73
+ function walkNodesForSelector(node, parts, callback) {
74
+ if (isElementNode(node)) {
75
+ if (matchesSelectorRecursive(node, parts)) {
76
+ if (callback(node) === false)
77
+ return false;
78
+ }
79
+ const child = node[CHILD];
80
+ if (child && walkNodesForSelector(child, parts, callback) === false) {
81
+ return false;
82
+ }
83
+ }
84
+ const next = node[NEXT];
85
+ if (next && walkNodesForSelector(next, parts, callback) === false) {
86
+ return false;
87
+ }
88
+ return true;
89
+ }
90
+ function matchesSelectorRecursive(element, parts) {
91
+ const { combinator, matchers } = parts[parts.length - 1];
92
+ if (combinator === 4 /* Inner */) {
93
+ if (!matchesSelectorMatcher(element, matchers))
94
+ return false;
95
+ const pp = parts.slice(0, -1);
96
+ return pp.length === 0 || matchesSelectorRecursive(element, pp);
97
+ }
98
+ const link = combinator === 1 /* Child */ || combinator === 0 /* Descendant */ ? PARENT : PREV;
99
+ let ref = element[link];
100
+ if (!ref || !matchesSelectorMatcher(ref, matchers)) {
101
+ return false;
102
+ }
103
+ if (combinator === 0 /* Descendant */ || combinator === 2 /* Sibling */) {
104
+ while (ref = ref[link]) {
105
+ if (matchesSelectorMatcher(ref, matchers)) {
106
+ const pp = parts.slice(0, -1);
107
+ if (pp.length === 0)
108
+ return true;
109
+ if (matchesSelectorRecursive(element, pp))
110
+ return true;
111
+ }
112
+ }
113
+ }
114
+ return false;
115
+ }
116
+ function matchesSelectorPart(element, { combinator, matchers }) {
117
+ if (combinator === 4 /* Inner */) {
118
+ return matchesSelectorMatcher(element, matchers);
119
+ }
120
+ const link = combinator === 1 /* Child */ || combinator === 0 /* Descendant */ ? PARENT : PREV;
121
+ let ref = element[link];
122
+ if (!ref || !matchesSelectorMatcher(ref, matchers)) {
123
+ return false;
124
+ }
125
+ if (combinator === 0 /* Descendant */ || combinator === 2 /* Sibling */) {
126
+ while (ref = ref[link]) {
127
+ if (matchesSelectorMatcher(ref, matchers))
128
+ return true;
129
+ }
130
+ }
131
+ return false;
132
+ }
133
+ function matchesSelectorMatcher(element, matcher) {
134
+ if (!element)
135
+ return false;
136
+ if (Array.isArray(matcher)) {
137
+ for (const single of matcher) {
138
+ if (matchesSelectorMatcher(element, single) === false)
139
+ return false;
140
+ }
141
+ return true;
142
+ }
143
+ const { type, name, value } = matcher;
144
+ switch (type) {
145
+ case 1 /* Element */:
146
+ return element.localName === name;
147
+ case 2 /* Id */:
148
+ return element.getAttribute("id") === name;
149
+ case 3 /* Class */:
150
+ return element.getAttribute("class") === name;
151
+ case 4 /* Attribute */:
152
+ return value == null ? element.hasAttribute(name) : element.getAttribute(name) === value;
153
+ case 5 /* Pseudo */:
154
+ switch (name) {
155
+ default:
156
+ throw Error(`Pseudo :${name} not implemented`);
157
+ }
158
+ case 6 /* Function */:
159
+ switch (name) {
160
+ case "has":
161
+ return matchesSelector(element, value || "");
162
+ default:
163
+ throw Error(`Function :${name}(${value}) not implemented`);
164
+ }
165
+ }
166
+ return false;
167
+ }
168
+
169
+ export { parseSelector, querySelector, querySelectorAll };
@@ -0,0 +1,76 @@
1
+ import { CHILD, NEXT, NodeType, DATA, NAME, ATTRIBUTES, VALUE } from './constants.esnext';
2
+
3
+ const elementTokenizer = /(?:<([a-z][a-z0-9-:]*)((?:\s[^<>'"=\n\s]+(?:=(['"])[^\n]*?\3|=[^>'"\n\s]*|))*)\s*(\/?)\s*>|<\/([a-z][a-z0-9-:]*)>|<!--(.*?)-->|([^&<>]+))/gi;
4
+ const attributeTokenizer = /\s([^<>'"=\n\s]+)(?:=(['"])([^\n]*?)\2|=([^>'"\n\s]*)|)/g;
5
+ function parseHtml(html, contextNode) {
6
+ const document = contextNode.ownerDocument;
7
+ const root = document.createDocumentFragment();
8
+ const stack = [root];
9
+ let parent = root;
10
+ let token;
11
+ elementTokenizer.lastIndex = 0;
12
+ while (token = elementTokenizer.exec(html)) {
13
+ const tag = token[1];
14
+ if (tag) {
15
+ const node = document.createElement(tag);
16
+ const attrs = token[2];
17
+ attributeTokenizer.lastIndex = 0;
18
+ let t;
19
+ while (t = attributeTokenizer.exec(attrs)) {
20
+ node.setAttribute(t[1], t[3] || t[4] || "");
21
+ }
22
+ parent.append(node);
23
+ stack.push(parent);
24
+ parent = node;
25
+ } else if (token[5]) {
26
+ parent = stack.pop() || root;
27
+ } else if (token[6]) {
28
+ parent.append(document.createComment(token[6]));
29
+ } else {
30
+ parent.append(token[7]);
31
+ }
32
+ }
33
+ return root;
34
+ }
35
+ function serializeChildren(parentNode) {
36
+ let out = "";
37
+ let child = parentNode[CHILD];
38
+ while (child) {
39
+ out += serializeNode(child);
40
+ child = child[NEXT];
41
+ }
42
+ return out;
43
+ }
44
+ function serializeNode(node) {
45
+ switch (node.nodeType) {
46
+ case NodeType.ELEMENT_NODE: {
47
+ const el = node;
48
+ let out = `<${el[NAME]}`;
49
+ let attr = el[ATTRIBUTES]?.[CHILD];
50
+ while (attr) {
51
+ out += ` ${attr[NAME]}`;
52
+ let value = attr[VALUE];
53
+ if (value !== "") {
54
+ value = String(value).replace(/&/g, "&amp;").replace(/"/g, "&quot;");
55
+ out += `="${value}"`;
56
+ }
57
+ attr = attr[NEXT];
58
+ }
59
+ out += ">";
60
+ out += serializeChildren(el);
61
+ out += `</${el[NAME]}>`;
62
+ return out;
63
+ }
64
+ case NodeType.TEXT_NODE: {
65
+ const text = node;
66
+ return text[DATA].replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
67
+ }
68
+ case NodeType.COMMENT_NODE: {
69
+ const text = node;
70
+ return `<!--${text[DATA]}-->`;
71
+ }
72
+ }
73
+ return "";
74
+ }
75
+
76
+ export { parseHtml, serializeChildren, serializeNode };
@@ -0,0 +1,59 @@
1
+ import { DATA, ATTRIBUTES, OWNER_DOCUMENT, NodeType } from './constants.esnext';
2
+
3
+ function isCharacterData(node) {
4
+ return DATA in node;
5
+ }
6
+ function isTextNode(node) {
7
+ return node.nodeType === NodeType.TEXT_NODE;
8
+ }
9
+ function isCommentNode(node) {
10
+ return node.nodeType === NodeType.COMMENT_NODE;
11
+ }
12
+ function isElementNode(node) {
13
+ return node.nodeType === NodeType.ELEMENT_NODE;
14
+ }
15
+ function isDocumentFragmentNode(node) {
16
+ return node.nodeType === NodeType.DOCUMENT_FRAGMENT_NODE;
17
+ }
18
+ function isParentNode(node) {
19
+ return "appendChild" in node;
20
+ }
21
+ function cloneNode(node, deep, document = node.ownerDocument) {
22
+ if (isTextNode(node)) {
23
+ return document.createTextNode(node.data);
24
+ } else if (isCommentNode(node)) {
25
+ return document.createComment(node.data);
26
+ } else if (isElementNode(node)) {
27
+ const cloned = document.createElement(node.localName);
28
+ if (node[ATTRIBUTES]) {
29
+ for (let i = 0; i < node[ATTRIBUTES].length; i++) {
30
+ const attribute = node[ATTRIBUTES].item(i);
31
+ cloned.setAttributeNS(
32
+ attribute.namespaceURI,
33
+ attribute.name,
34
+ attribute.value
35
+ );
36
+ }
37
+ }
38
+ if (deep) {
39
+ for (const child of node.childNodes) {
40
+ cloned.appendChild(cloneNode(child, true, document));
41
+ }
42
+ }
43
+ return cloned;
44
+ } else if (isDocumentFragmentNode(node)) {
45
+ const fragment = document.createDocumentFragment();
46
+ if (deep) {
47
+ for (const child of node.childNodes) {
48
+ fragment.appendChild(cloneNode(child, true, document));
49
+ }
50
+ }
51
+ return fragment;
52
+ } else {
53
+ const cloned = new node.constructor();
54
+ cloned[OWNER_DOCUMENT] = document;
55
+ return cloned;
56
+ }
57
+ }
58
+
59
+ export { cloneNode, isCharacterData, isCommentNode, isDocumentFragmentNode, isElementNode, isParentNode, isTextNode };