@remote-dom/polyfill 1.5.1 → 1.6.0

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 (304) hide show
  1. package/CHANGELOG.md +88 -0
  2. package/README.md +51 -3
  3. package/build/cjs/Attr.cjs +33 -4
  4. package/build/cjs/CharacterData.cjs +10 -0
  5. package/build/cjs/ChildNode.cjs +79 -13
  6. package/build/cjs/Comment.cjs +1 -1
  7. package/build/cjs/CustomElementRegistry.cjs +27 -2
  8. package/build/cjs/CustomEvent.cjs +2 -2
  9. package/build/cjs/Document.cjs +56 -32
  10. package/build/cjs/DocumentFragment.cjs +6 -2
  11. package/build/cjs/Element.cjs +68 -8
  12. package/build/cjs/Event.cjs +29 -27
  13. package/build/cjs/EventTarget.cjs +128 -58
  14. package/build/cjs/HTMLTemplateElement.cjs +1 -0
  15. package/build/cjs/MutationObserver.cjs +167 -1
  16. package/build/cjs/NamedNodeMap.cjs +82 -45
  17. package/build/cjs/Node.cjs +47 -11
  18. package/build/cjs/NodeList.cjs +9 -1
  19. package/build/cjs/ParentNode.cjs +319 -77
  20. package/build/cjs/SVGElement.cjs +1 -1
  21. package/build/cjs/Text.cjs +1 -1
  22. package/build/cjs/Window.cjs +131 -38
  23. package/build/cjs/attribute-reactions.cjs +20 -0
  24. package/build/cjs/constants.cjs +48 -24
  25. package/build/cjs/custom-element-reactions.cjs +52 -0
  26. package/build/cjs/dom-exception.cjs +15 -0
  27. package/build/cjs/hook-effects.cjs +51 -0
  28. package/build/cjs/names.cjs +61 -0
  29. package/build/cjs/selectors.cjs +213 -122
  30. package/build/cjs/serialization.cjs +168 -65
  31. package/build/cjs/shared.cjs +177 -28
  32. package/build/esm/Attr.mjs +34 -5
  33. package/build/esm/CharacterData.mjs +10 -0
  34. package/build/esm/ChildNode.mjs +76 -15
  35. package/build/esm/Comment.mjs +2 -2
  36. package/build/esm/CustomElementRegistry.mjs +27 -2
  37. package/build/esm/CustomEvent.mjs +2 -2
  38. package/build/esm/Document.mjs +60 -35
  39. package/build/esm/DocumentFragment.mjs +7 -3
  40. package/build/esm/Element.mjs +69 -9
  41. package/build/esm/Event.mjs +27 -27
  42. package/build/esm/EventTarget.mjs +130 -60
  43. package/build/esm/HTMLTemplateElement.mjs +2 -1
  44. package/build/esm/MutationObserver.mjs +166 -2
  45. package/build/esm/NamedNodeMap.mjs +83 -46
  46. package/build/esm/Node.mjs +48 -12
  47. package/build/esm/NodeList.mjs +9 -1
  48. package/build/esm/ParentNode.mjs +322 -81
  49. package/build/esm/SVGElement.mjs +2 -2
  50. package/build/esm/Text.mjs +2 -2
  51. package/build/esm/Window.mjs +132 -39
  52. package/build/esm/attribute-reactions.mjs +16 -0
  53. package/build/esm/constants.mjs +28 -23
  54. package/build/esm/custom-element-reactions.mjs +47 -0
  55. package/build/esm/dom-exception.mjs +11 -0
  56. package/build/esm/hook-effects.mjs +46 -0
  57. package/build/esm/names.mjs +54 -0
  58. package/build/esm/selectors.mjs +202 -124
  59. package/build/esm/serialization.mjs +169 -66
  60. package/build/esm/shared.mjs +173 -30
  61. package/build/esnext/Attr.esnext +45 -5
  62. package/build/esnext/CharacterData.esnext +10 -0
  63. package/build/esnext/ChildNode.esnext +84 -15
  64. package/build/esnext/Comment.esnext +2 -2
  65. package/build/esnext/CustomElementRegistry.esnext +42 -0
  66. package/build/esnext/CustomEvent.esnext +2 -2
  67. package/build/esnext/Document.esnext +69 -32
  68. package/build/esnext/DocumentFragment.esnext +6 -2
  69. package/build/esnext/Element.esnext +78 -9
  70. package/build/esnext/Event.esnext +36 -37
  71. package/build/esnext/EventTarget.esnext +133 -61
  72. package/build/esnext/HTMLTemplateElement.esnext +3 -1
  73. package/build/esnext/MutationObserver.esnext +163 -2
  74. package/build/esnext/NamedNodeMap.esnext +110 -42
  75. package/build/esnext/Node.esnext +50 -12
  76. package/build/esnext/NodeList.esnext +1 -1
  77. package/build/esnext/ParentNode.esnext +352 -79
  78. package/build/esnext/SVGElement.esnext +2 -2
  79. package/build/esnext/Text.esnext +2 -2
  80. package/build/esnext/Window.esnext +120 -16
  81. package/build/esnext/attribute-reactions.esnext +21 -0
  82. package/build/esnext/constants.esnext +34 -21
  83. package/build/esnext/custom-element-reactions.esnext +49 -0
  84. package/build/esnext/dom-exception.esnext +11 -0
  85. package/build/esnext/hook-effects.esnext +46 -0
  86. package/build/esnext/names.esnext +61 -0
  87. package/build/esnext/selectors.esnext +202 -100
  88. package/build/esnext/serialization.esnext +171 -48
  89. package/build/esnext/shared.esnext +181 -34
  90. package/build/tsconfig.tsbuildinfo +1 -1
  91. package/build/typescript/Attr.d.ts +6 -4
  92. package/build/typescript/Attr.d.ts.map +1 -1
  93. package/build/typescript/CharacterData.d.ts.map +1 -1
  94. package/build/typescript/ChildNode.d.ts +5 -0
  95. package/build/typescript/ChildNode.d.ts.map +1 -1
  96. package/build/typescript/ClipboardEvent.d.ts.map +1 -1
  97. package/build/typescript/Comment.d.ts +2 -2
  98. package/build/typescript/Comment.d.ts.map +1 -1
  99. package/build/typescript/CustomElementRegistry.d.ts +1 -0
  100. package/build/typescript/CustomElementRegistry.d.ts.map +1 -1
  101. package/build/typescript/CustomEvent.d.ts.map +1 -1
  102. package/build/typescript/Document.d.ts +10 -5
  103. package/build/typescript/Document.d.ts.map +1 -1
  104. package/build/typescript/DocumentFragment.d.ts +2 -1
  105. package/build/typescript/DocumentFragment.d.ts.map +1 -1
  106. package/build/typescript/Element.d.ts +16 -7
  107. package/build/typescript/Element.d.ts.map +1 -1
  108. package/build/typescript/ErrorEvent.d.ts.map +1 -1
  109. package/build/typescript/Event.d.ts +9 -10
  110. package/build/typescript/Event.d.ts.map +1 -1
  111. package/build/typescript/EventTarget.d.ts +20 -3
  112. package/build/typescript/EventTarget.d.ts.map +1 -1
  113. package/build/typescript/FocusEvent.d.ts.map +1 -1
  114. package/build/typescript/HTMLTemplateElement.d.ts.map +1 -1
  115. package/build/typescript/MutationObserver.d.ts +42 -0
  116. package/build/typescript/MutationObserver.d.ts.map +1 -1
  117. package/build/typescript/NamedNodeMap.d.ts +5 -3
  118. package/build/typescript/NamedNodeMap.d.ts.map +1 -1
  119. package/build/typescript/Node.d.ts +4 -3
  120. package/build/typescript/Node.d.ts.map +1 -1
  121. package/build/typescript/NodeList.d.ts +3 -2
  122. package/build/typescript/NodeList.d.ts.map +1 -1
  123. package/build/typescript/ParentNode.d.ts +29 -10
  124. package/build/typescript/ParentNode.d.ts.map +1 -1
  125. package/build/typescript/PromiseRejectionEvent.d.ts.map +1 -1
  126. package/build/typescript/SVGElement.d.ts +1 -1
  127. package/build/typescript/SVGElement.d.ts.map +1 -1
  128. package/build/typescript/Text.d.ts +1 -1
  129. package/build/typescript/Text.d.ts.map +1 -1
  130. package/build/typescript/ToggleEvent.d.ts.map +1 -1
  131. package/build/typescript/Window.d.ts +5 -1
  132. package/build/typescript/Window.d.ts.map +1 -1
  133. package/build/typescript/attribute-reactions.d.ts +4 -0
  134. package/build/typescript/attribute-reactions.d.ts.map +1 -0
  135. package/build/typescript/constants.d.ts +27 -18
  136. package/build/typescript/constants.d.ts.map +1 -1
  137. package/build/typescript/custom-element-reactions.d.ts +6 -0
  138. package/build/typescript/custom-element-reactions.d.ts.map +1 -0
  139. package/build/typescript/dom-exception.d.ts +2 -0
  140. package/build/typescript/dom-exception.d.ts.map +1 -0
  141. package/build/typescript/extensions.d.ts +4 -0
  142. package/build/typescript/extensions.d.ts.map +1 -0
  143. package/build/typescript/hook-effects.d.ts +6 -0
  144. package/build/typescript/hook-effects.d.ts.map +1 -0
  145. package/build/typescript/hooks.d.ts +3 -3
  146. package/build/typescript/hooks.d.ts.map +1 -1
  147. package/build/typescript/index.d.ts +1 -0
  148. package/build/typescript/index.d.ts.map +1 -1
  149. package/build/typescript/names.d.ts +11 -0
  150. package/build/typescript/names.d.ts.map +1 -0
  151. package/build/typescript/selectors.d.ts +21 -22
  152. package/build/typescript/selectors.d.ts.map +1 -1
  153. package/build/typescript/serialization.d.ts.map +1 -1
  154. package/build/typescript/shared.d.ts +10 -0
  155. package/build/typescript/shared.d.ts.map +1 -1
  156. package/build/typescript/tests/ChildNode.test.d.ts +2 -0
  157. package/build/typescript/tests/ChildNode.test.d.ts.map +1 -0
  158. package/build/typescript/tests/MutationObserver.test.d.ts +2 -0
  159. package/build/typescript/tests/MutationObserver.test.d.ts.map +1 -0
  160. package/build/typescript/tests/adopt-inserted-subtrees.test.d.ts +2 -0
  161. package/build/typescript/tests/adopt-inserted-subtrees.test.d.ts.map +1 -0
  162. package/build/typescript/tests/attr-mutation.test.d.ts +2 -0
  163. package/build/typescript/tests/attr-mutation.test.d.ts.map +1 -0
  164. package/build/typescript/tests/child-node-before-after.test.d.ts +2 -0
  165. package/build/typescript/tests/child-node-before-after.test.d.ts.map +1 -0
  166. package/build/typescript/tests/child-node-replace-with.test.d.ts +2 -0
  167. package/build/typescript/tests/child-node-replace-with.test.d.ts.map +1 -0
  168. package/build/typescript/tests/clone-import-namespaces.test.d.ts +2 -0
  169. package/build/typescript/tests/clone-import-namespaces.test.d.ts.map +1 -0
  170. package/build/typescript/tests/constants.test.d.ts +2 -0
  171. package/build/typescript/tests/constants.test.d.ts.map +1 -0
  172. package/build/typescript/tests/create-element-namespaces.test.d.ts +2 -0
  173. package/build/typescript/tests/create-element-namespaces.test.d.ts.map +1 -0
  174. package/build/typescript/tests/create-text-node.test.d.ts +2 -0
  175. package/build/typescript/tests/create-text-node.test.d.ts.map +1 -0
  176. package/build/typescript/tests/custom-element-reactions.test.d.ts +2 -0
  177. package/build/typescript/tests/custom-element-reactions.test.d.ts.map +1 -0
  178. package/build/typescript/tests/custom-element-registry.test.d.ts +2 -0
  179. package/build/typescript/tests/custom-element-registry.test.d.ts.map +1 -0
  180. package/build/typescript/tests/custom-event.test.d.ts +2 -0
  181. package/build/typescript/tests/custom-event.test.d.ts.map +1 -0
  182. package/build/typescript/tests/document-clone-import-contract.test.d.ts +2 -0
  183. package/build/typescript/tests/document-clone-import-contract.test.d.ts.map +1 -0
  184. package/build/typescript/tests/document.test.d.ts +2 -0
  185. package/build/typescript/tests/document.test.d.ts.map +1 -0
  186. package/build/typescript/tests/dom-mutation-selector-errors.test.d.ts +2 -0
  187. package/build/typescript/tests/dom-mutation-selector-errors.test.d.ts.map +1 -0
  188. package/build/typescript/tests/event-dispatch.test.d.ts +2 -0
  189. package/build/typescript/tests/event-dispatch.test.d.ts.map +1 -0
  190. package/build/typescript/tests/event-target.test.d.ts +2 -0
  191. package/build/typescript/tests/event-target.test.d.ts.map +1 -0
  192. package/build/typescript/tests/event.test.d.ts +2 -0
  193. package/build/typescript/tests/event.test.d.ts.map +1 -0
  194. package/build/typescript/tests/extensions.test.d.ts +2 -0
  195. package/build/typescript/tests/extensions.test.d.ts.map +1 -0
  196. package/build/typescript/tests/get-elements-by-tag-name.test.d.ts +2 -0
  197. package/build/typescript/tests/get-elements-by-tag-name.test.d.ts.map +1 -0
  198. package/build/typescript/tests/getElementsByClassName.test.d.ts +2 -0
  199. package/build/typescript/tests/getElementsByClassName.test.d.ts.map +1 -0
  200. package/build/typescript/tests/hooks.test.d.ts +2 -0
  201. package/build/typescript/tests/hooks.test.d.ts.map +1 -0
  202. package/build/typescript/tests/inner-html-parsing.test.d.ts +2 -0
  203. package/build/typescript/tests/inner-html-parsing.test.d.ts.map +1 -0
  204. package/build/typescript/tests/is-default-namespace.test.d.ts +2 -0
  205. package/build/typescript/tests/is-default-namespace.test.d.ts.map +1 -0
  206. package/build/typescript/tests/name-and-namespace-normalization.test.d.ts +2 -0
  207. package/build/typescript/tests/name-and-namespace-normalization.test.d.ts.map +1 -0
  208. package/build/typescript/tests/named-node-map.test.d.ts +2 -0
  209. package/build/typescript/tests/named-node-map.test.d.ts.map +1 -0
  210. package/build/typescript/tests/namespaced-attribute-reactions.test.d.ts +2 -0
  211. package/build/typescript/tests/namespaced-attribute-reactions.test.d.ts.map +1 -0
  212. package/build/typescript/tests/node.test.d.ts +2 -0
  213. package/build/typescript/tests/node.test.d.ts.map +1 -0
  214. package/build/typescript/tests/parent-node-mutation-return-values.test.d.ts +2 -0
  215. package/build/typescript/tests/parent-node-mutation-return-values.test.d.ts.map +1 -0
  216. package/build/typescript/tests/parent-node-mutation-validation.test.d.ts +2 -0
  217. package/build/typescript/tests/parent-node-mutation-validation.test.d.ts.map +1 -0
  218. package/build/typescript/tests/parent-node.test.d.ts +2 -0
  219. package/build/typescript/tests/parent-node.test.d.ts.map +1 -0
  220. package/build/typescript/tests/serialization-template.test.d.ts +2 -0
  221. package/build/typescript/tests/serialization-template.test.d.ts.map +1 -0
  222. package/build/typescript/tests/slot-attribute.test.d.ts +2 -0
  223. package/build/typescript/tests/slot-attribute.test.d.ts.map +1 -0
  224. package/build/typescript/tests/text-content.test.d.ts +2 -0
  225. package/build/typescript/tests/text-content.test.d.ts.map +1 -0
  226. package/build/typescript/tests/toggle-attribute.test.d.ts +2 -0
  227. package/build/typescript/tests/toggle-attribute.test.d.ts.map +1 -0
  228. package/build/typescript/tests/wide-tree-traversal.test.d.ts +2 -0
  229. package/build/typescript/tests/wide-tree-traversal.test.d.ts.map +1 -0
  230. package/build/typescript/tests/window-set-global.test.d.ts +2 -0
  231. package/build/typescript/tests/window-set-global.test.d.ts.map +1 -0
  232. package/package.json +1 -1
  233. package/source/Attr.ts +60 -8
  234. package/source/CharacterData.ts +13 -0
  235. package/source/ChildNode.ts +107 -14
  236. package/source/Comment.ts +2 -2
  237. package/source/CustomElementRegistry.ts +56 -0
  238. package/source/CustomEvent.ts +2 -2
  239. package/source/Document.ts +104 -38
  240. package/source/DocumentFragment.ts +12 -2
  241. package/source/Element.ts +129 -14
  242. package/source/Event.ts +34 -32
  243. package/source/EventTarget.ts +203 -79
  244. package/source/HTMLTemplateElement.ts +2 -1
  245. package/source/MutationObserver.ts +279 -1
  246. package/source/NamedNodeMap.ts +141 -60
  247. package/source/Node.ts +70 -14
  248. package/source/NodeList.ts +13 -3
  249. package/source/ParentNode.ts +500 -88
  250. package/source/SVGElement.ts +2 -2
  251. package/source/Text.ts +2 -2
  252. package/source/Window.ts +156 -17
  253. package/source/attribute-reactions.ts +32 -0
  254. package/source/constants.ts +40 -17
  255. package/source/custom-element-reactions.ts +64 -0
  256. package/source/dom-exception.ts +14 -0
  257. package/source/extensions.ts +4 -0
  258. package/source/hook-effects.ts +56 -0
  259. package/source/hooks.ts +11 -3
  260. package/source/index.ts +1 -0
  261. package/source/names.ts +94 -0
  262. package/source/selectors.ts +301 -138
  263. package/source/serialization.ts +224 -66
  264. package/source/shared.ts +269 -40
  265. package/source/tests/ChildNode.test.ts +178 -0
  266. package/source/tests/MutationObserver.test.ts +303 -0
  267. package/source/tests/adopt-inserted-subtrees.test.ts +584 -0
  268. package/source/tests/attr-mutation.test.ts +112 -0
  269. package/source/tests/child-node-before-after.test.ts +421 -0
  270. package/source/tests/child-node-replace-with.test.ts +251 -0
  271. package/source/tests/clone-import-namespaces.test.ts +365 -0
  272. package/source/tests/constants.test.ts +46 -0
  273. package/source/tests/create-element-namespaces.test.ts +142 -0
  274. package/source/tests/create-text-node.test.ts +38 -0
  275. package/source/tests/custom-element-reactions.test.ts +600 -0
  276. package/source/tests/custom-element-registry.test.ts +137 -0
  277. package/source/tests/custom-event.test.ts +26 -0
  278. package/source/tests/document-clone-import-contract.test.ts +77 -0
  279. package/source/tests/document.test.ts +122 -0
  280. package/source/tests/dom-mutation-selector-errors.test.ts +484 -0
  281. package/source/tests/event-dispatch.test.ts +270 -0
  282. package/source/tests/event-target.test.ts +208 -0
  283. package/source/tests/event.test.ts +98 -0
  284. package/source/tests/extensions.test.ts +159 -0
  285. package/source/tests/get-elements-by-tag-name.test.ts +79 -0
  286. package/source/tests/getElementsByClassName.test.ts +109 -0
  287. package/source/tests/hooks.test.ts +43 -0
  288. package/source/tests/inner-html-parsing.test.ts +461 -0
  289. package/source/tests/is-default-namespace.test.ts +111 -0
  290. package/source/tests/name-and-namespace-normalization.test.ts +319 -0
  291. package/source/tests/named-node-map.test.ts +122 -0
  292. package/source/tests/namespaced-attribute-reactions.test.ts +141 -0
  293. package/source/tests/node.test.ts +56 -0
  294. package/source/tests/parent-node-mutation-return-values.test.ts +63 -0
  295. package/source/tests/parent-node-mutation-validation.test.ts +362 -0
  296. package/source/tests/parent-node.test.ts +169 -0
  297. package/source/tests/selectors.test.ts +533 -22
  298. package/source/tests/serialization-template.test.ts +232 -0
  299. package/source/tests/slot-attribute.test.ts +133 -0
  300. package/source/tests/text-content.test.ts +440 -0
  301. package/source/tests/toggle-attribute.test.ts +113 -0
  302. package/source/tests/wide-tree-traversal.test.ts +699 -0
  303. package/source/tests/window-set-global.test.ts +321 -0
  304. package/tsconfig.json +1 -1
@@ -7,69 +7,112 @@ const ChildNode = require('./ChildNode.cjs');
7
7
  const NodeList = require('./NodeList.cjs');
8
8
  const selectors = require('./selectors.cjs');
9
9
  const shared = require('./shared.cjs');
10
+ const MutationObserver = require('./MutationObserver.cjs');
11
+ const customElementReactions = require('./custom-element-reactions.cjs');
12
+ const hookEffects = require('./hook-effects.cjs');
13
+ const domException = require('./dom-exception.cjs');
10
14
 
11
15
  class ParentNode extends ChildNode.ChildNode {
12
- childNodes = (() => new NodeList.NodeList())();
13
- children = (() => new NodeList.NodeList())();
16
+ childNodes = new NodeList.NodeList();
17
+ children = new NodeList.NodeList();
14
18
  appendChild(child) {
15
- this.insertInto(child, null);
19
+ return customElementReactions.performWithCustomElementReactions(() => this[ChildNode.INSERT_NODE](child, null));
16
20
  }
17
21
  insertBefore(child, ref) {
18
- this.insertInto(child, ref || null);
22
+ return customElementReactions.performWithCustomElementReactions(() => this[ChildNode.INSERT_NODE](child, ref || null));
19
23
  }
20
24
  append(...nodes) {
21
- for (const child of nodes) {
22
- if (child == null) continue;
23
- this.appendChild(ChildNode.toNode(this, child));
24
- }
25
+ return customElementReactions.performWithCustomElementReactions(() => {
26
+ const staged = ChildNode.stageNodes(nodes.filter(node => node != null));
27
+ if (staged.length > 1) ChildNode.validateInsertionNodes(this, staged);
28
+ for (const child of staged) {
29
+ this[ChildNode.INSERT_NODE](ChildNode.toNode(this, child), null);
30
+ }
31
+ });
25
32
  }
26
33
  prepend(...nodes) {
27
- const before = this.firstChild;
28
- for (const child of nodes) {
29
- if (child == null) continue;
30
- this.insertBefore(ChildNode.toNode(this, child), before);
31
- }
34
+ return customElementReactions.performWithCustomElementReactions(() => {
35
+ const staged = ChildNode.stageNodes(nodes.filter(node => node != null));
36
+ if (staged.length > 1) ChildNode.validateInsertionNodes(this, staged);
37
+ const before = this.firstChild;
38
+ for (const child of staged) {
39
+ this[ChildNode.INSERT_NODE](ChildNode.toNode(this, child), before);
40
+ }
41
+ });
32
42
  }
33
43
  replaceChildren(...nodes) {
34
- let child;
35
- while (child = this.firstChild) {
36
- this.removeChild(child);
37
- }
38
- this.append(...nodes);
44
+ return customElementReactions.performWithCustomElementReactions(() => {
45
+ const staged = ChildNode.stageNodes(nodes.filter(node => node != null));
46
+ ChildNode.validateInsertionNodes(this, staged);
47
+ let child;
48
+ while (child = this.firstChild) {
49
+ this.removeChildImmediately(child);
50
+ }
51
+ for (const node of staged) {
52
+ this[ChildNode.INSERT_NODE](ChildNode.toNode(this, node), null);
53
+ }
54
+ });
39
55
  }
40
56
  removeChild(child) {
41
- if (child.parentNode !== this) throw Error(`not a child of this node`);
42
- const prev = child[constants.PREV];
43
- const next = child[constants.NEXT];
44
- if (prev) prev[constants.NEXT] = next;else this[constants.CHILD] = next;
45
- if (next) next[constants.PREV] = prev;
46
- const childNodes = this.childNodes;
47
- const childNodesIndex = childNodes.indexOf(child);
48
- childNodes.splice(childNodesIndex, 1);
49
- if (child.nodeType === 1) {
50
- const children = this.children;
51
- children.splice(children.indexOf(child), 1);
52
- }
53
- child[constants.PARENT] = null;
54
- child[constants.NEXT] = null;
55
- child[constants.PREV] = null;
56
- if (this[constants.IS_CONNECTED]) {
57
- for (const node of shared.selfAndDescendants(child)) {
58
- node[constants.IS_CONNECTED] = false;
59
- node.disconnectedCallback?.();
60
- }
57
+ return customElementReactions.performWithCustomElementReactions(() => {
58
+ this.removeChildImmediately(child);
59
+ return child;
60
+ });
61
+ }
62
+ removeChildImmediately(child) {
63
+ if (child.parentNode !== this) {
64
+ throw domException.createDOMException('The node is not a child of this node', 'NotFoundError');
65
+ }
66
+ const disconnectedNodes = this[constants.IS_CONNECTED] ? shared.selfAndDescendants(child) : undefined;
67
+ const previousSibling = child[constants.PREV];
68
+ const nextSibling = child[constants.NEXT];
69
+ const childNodesIndex = detachChild(this, child);
70
+ if (disconnectedNodes) {
71
+ for (const node of disconnectedNodes) node[constants.IS_CONNECTED] = false;
61
72
  }
62
- if (this.nodeType === constants.NodeType.ELEMENT_NODE) {
63
- this[constants.HOOKS].removeChild?.(this, child, childNodesIndex);
73
+ this.queueRemovalMutationRecord(this, child, previousSibling, nextSibling);
74
+ if (disconnectedNodes) {
75
+ this.enqueueTreeReactions(disconnectedNodes, 'disconnectedCallback');
64
76
  }
77
+ hookEffects.performHookEffects(this.collectRemovalHookEffects(child, childNodesIndex));
65
78
  }
66
79
  replaceChild(newChild, oldChild) {
80
+ return customElementReactions.performWithCustomElementReactions(() => this[ChildNode.REPLACE_NODE](newChild, oldChild));
81
+ }
82
+ [ChildNode.REPLACE_NODE](newChild, oldChild, hookEffects) {
83
+ ChildNode.validateInsertionNodes(this, [newChild]);
67
84
  if (oldChild.parentNode !== this) {
68
- throw Error('reference node is not a child of this parent');
85
+ throw domException.createDOMException('The reference node is not a child of this parent', 'NotFoundError');
69
86
  }
87
+ const previous = oldChild[constants.PREV];
70
88
  const next = oldChild[constants.NEXT];
71
- this.removeChild(oldChild);
72
- this.insertInto(newChild, next);
89
+ const insertion = this.prepareInsertion(newChild);
90
+ const removedNodes = this[constants.IS_CONNECTED] ? shared.selfAndDescendants(oldChild) : undefined;
91
+ const insertionRoots = new Set(insertion.map(({
92
+ node
93
+ }) => node));
94
+ let before = next;
95
+ while (before && insertionRoots.has(before)) before = before[constants.NEXT];
96
+ const oldChildIndex = detachChild(this, oldChild);
97
+ if (removedNodes) {
98
+ for (const node of removedNodes) node[constants.IS_CONNECTED] = false;
99
+ const removedNodeSet = new Set(removedNodes);
100
+ for (const prepared of insertion) {
101
+ if (removedNodeSet.has(prepared.node)) {
102
+ prepared.shouldDisconnect = false;
103
+ }
104
+ }
105
+ }
106
+ this.commitInsertion(insertion, before);
107
+ const destinationIsConnected = this[constants.IS_CONNECTED];
108
+ this.queueRemovalMutationRecord(this, oldChild, previous, next);
109
+ this.queueInsertionMutationRecords(insertion);
110
+ if (removedNodes) {
111
+ this.enqueueTreeReactions(removedNodes, 'disconnectedCallback');
112
+ }
113
+ this.enqueueInsertionReactions(insertion, destinationIsConnected);
114
+ this.performOrCollectHookEffects([...this.collectRemovalHookEffects(oldChild, oldChildIndex), ...this.collectInsertionHookEffects(insertion)], hookEffects);
115
+ return oldChild;
73
116
  }
74
117
  querySelectorAll(selector) {
75
118
  return selectors.querySelectorAll(this, selector);
@@ -77,27 +120,111 @@ class ParentNode extends ChildNode.ChildNode {
77
120
  querySelector(selector) {
78
121
  return selectors.querySelector(this, selector);
79
122
  }
80
- insertInto(child, before) {
81
- // append the children of a DocumentFragment:
82
- if (child.nodeType === constants.NodeType.DOCUMENT_FRAGMENT_NODE) {
123
+ [ChildNode.INSERT_NODE](child, before, hookEffects) {
124
+ this.validateInsertion(child, before);
125
+ this.insertIntoValidated(child, before, hookEffects);
126
+ return child;
127
+ }
128
+ [ChildNode.PREFLIGHT_INSERTIONS](children) {
129
+ for (const child of children) {
130
+ this.validateInsertion(child, null);
131
+ this.prepareInsertion(child);
132
+ }
133
+ }
134
+ validateInsertion(child, before) {
135
+ ChildNode.validateInsertionNodes(this, [child]);
136
+ if (before && before.parentNode !== this) {
137
+ throw domException.createDOMException('The reference node is not a child of this parent', 'NotFoundError');
138
+ }
139
+ }
140
+ insertIntoValidated(child, before, hookEffects) {
141
+ if (child === before) before = child[constants.NEXT];
142
+ const insertion = this.prepareInsertion(child);
143
+ this.commitInsertion(insertion, before);
144
+ const destinationIsConnected = this[constants.IS_CONNECTED];
145
+ this.queueInsertionMutationRecords(insertion);
146
+ this.enqueueInsertionReactions(insertion, destinationIsConnected);
147
+ this.performOrCollectHookEffects(this.collectInsertionHookEffects(insertion), hookEffects);
148
+ }
149
+ performOrCollectHookEffects(effects, hookEffects$1) {
150
+ if (hookEffects$1) hookEffects$1.push(...effects);else hookEffects.performHookEffects(effects);
151
+ }
152
+ prepareInsertion(child) {
153
+ const roots = [];
154
+ if (child.nodeType === constants.NODE_TYPE_DOCUMENT_FRAGMENT) {
83
155
  let node = child[constants.CHILD];
84
156
  while (node) {
85
- const next = node[constants.NEXT];
86
- this.insertInto(node, before);
87
- node = next;
157
+ roots.push(node);
158
+ node = node[constants.NEXT];
88
159
  }
89
- return;
160
+ } else {
161
+ roots.push(child);
90
162
  }
91
- if (child.parentNode !== null) {
92
- child.parentNode.removeChild(child);
163
+ const destinationIsConnected = this[constants.IS_CONNECTED];
164
+ const ownerDocument = this[constants.OWNER_DOCUMENT];
165
+ const insertion = [];
166
+ for (const node of roots) {
167
+ const wasConnected = node[constants.IS_CONNECTED];
168
+ const shouldTraverse = wasConnected || destinationIsConnected;
169
+ let nodes;
170
+ let adoption;
171
+ if (node[constants.OWNER_DOCUMENT] === ownerDocument) {
172
+ if (shouldTraverse) nodes = shared.selfAndDescendants(node);
173
+ } else {
174
+ const snapshot = shared.collectAdoptionSnapshot(node);
175
+ adoption = snapshot.nodes;
176
+ if (shouldTraverse) nodes = snapshot.treeNodes;
177
+ }
178
+ insertion.push({
179
+ node,
180
+ nodes,
181
+ adoption,
182
+ shouldDisconnect: wasConnected
183
+ });
93
184
  }
94
- if (before) {
95
- if (before.parentNode !== this) {
96
- throw Error('reference node is not a child of this parent');
185
+ return insertion;
186
+ }
187
+ commitInsertion(insertion, before) {
188
+ for (const prepared of insertion) {
189
+ const sourceParent = prepared.node[constants.PARENT];
190
+ if (sourceParent) {
191
+ const previousSibling = prepared.node[constants.PREV];
192
+ const nextSibling = prepared.node[constants.NEXT];
193
+ prepared.source = {
194
+ parent: sourceParent,
195
+ index: detachChild(sourceParent, prepared.node),
196
+ previousSibling,
197
+ nextSibling
198
+ };
97
199
  }
200
+ }
201
+ for (const prepared of insertion) {
202
+ const attached = this.attachChild(prepared.node, before);
203
+ prepared.insertIndex = attached.index;
204
+ prepared.previousSibling = attached.previousSibling;
205
+ prepared.nextSibling = attached.nextSibling;
206
+ }
207
+ const ownerDocument = this[constants.OWNER_DOCUMENT];
208
+ for (const {
209
+ adoption
210
+ } of insertion) {
211
+ if (adoption) shared.adoptNodes(adoption, ownerDocument);
212
+ }
213
+ const isConnected = this[constants.IS_CONNECTED];
214
+ for (const {
215
+ nodes
216
+ } of insertion) {
217
+ if (nodes) {
218
+ for (const node of nodes) node[constants.IS_CONNECTED] = isConnected;
219
+ }
220
+ }
221
+ }
222
+ attachChild(child, before) {
223
+ if (before) {
224
+ const previous = before[constants.PREV];
98
225
  child[constants.NEXT] = before;
99
- child[constants.PREV] = before[constants.PREV];
100
- if (before[constants.PREV] === null) this[constants.CHILD] = child;
226
+ child[constants.PREV] = previous;
227
+ if (previous) previous[constants.NEXT] = child;else this[constants.CHILD] = child;
101
228
  before[constants.PREV] = child;
102
229
  } else {
103
230
  child[constants.NEXT] = null;
@@ -112,40 +239,155 @@ class ParentNode extends ChildNode.ChildNode {
112
239
  child[constants.PREV] = null;
113
240
  }
114
241
  }
115
- const ownerDocument = this[constants.OWNER_DOCUMENT];
116
- const isElement = child.nodeType === constants.NodeType.ELEMENT_NODE;
242
+ const isElement = shared.isElementNode(child);
117
243
  child[constants.PARENT] = this;
118
- child[constants.OWNER_DOCUMENT] = ownerDocument;
119
- const childNodes = this.childNodes;
120
244
  let insertIndex;
121
245
  if (before) {
122
- insertIndex = childNodes.indexOf(before);
123
- childNodes.splice(insertIndex, 0, child);
246
+ insertIndex = this.childNodes.indexOf(before);
247
+ this.childNodes.splice(insertIndex, 0, child);
124
248
  if (isElement) {
125
- const children = this.children;
126
- let ref = before;
127
- while (ref && ref.nodeType !== 1) ref = ref[constants.NEXT];
128
- if (ref) {
129
- children.splice(children.indexOf(ref), 0, child);
249
+ let reference = before;
250
+ while (reference && !shared.isElementNode(reference)) {
251
+ reference = reference[constants.NEXT];
252
+ }
253
+ if (reference) {
254
+ this.children.splice(this.children.indexOf(reference), 0, child);
130
255
  } else {
131
- children.push(child);
256
+ this.children.push(child);
132
257
  }
133
258
  }
134
259
  } else {
135
- insertIndex = childNodes.length;
136
- childNodes.push(child);
260
+ insertIndex = this.childNodes.length;
261
+ this.childNodes.push(child);
137
262
  if (isElement) this.children.push(child);
138
263
  }
139
- if (this[constants.IS_CONNECTED]) {
140
- for (const node of shared.selfAndDescendants(child)) {
141
- node[constants.IS_CONNECTED] = true;
142
- node.connectedCallback?.();
264
+ return {
265
+ index: insertIndex,
266
+ previousSibling: child[constants.PREV],
267
+ nextSibling: child[constants.NEXT]
268
+ };
269
+ }
270
+ queueRemovalMutationRecord(parent, child, previousSibling, nextSibling) {
271
+ if (!MutationObserver.childListObserversActive) return;
272
+ MutationObserver.queueMutationRecord({
273
+ type: 'childList',
274
+ target: parent,
275
+ removedNodes: MutationObserver.mutationNodeList(child),
276
+ previousSibling,
277
+ nextSibling
278
+ });
279
+ }
280
+ queueInsertionMutationRecords(insertion) {
281
+ if (!MutationObserver.childListObserversActive) return;
282
+ for (const prepared of insertion) {
283
+ const {
284
+ node,
285
+ source
286
+ } = prepared;
287
+ if (source) {
288
+ this.queueRemovalMutationRecord(source.parent, node, source.previousSibling, source.nextSibling);
289
+ }
290
+ MutationObserver.queueMutationRecord({
291
+ type: 'childList',
292
+ target: this,
293
+ addedNodes: MutationObserver.mutationNodeList(node),
294
+ previousSibling: prepared.previousSibling,
295
+ nextSibling: prepared.nextSibling
296
+ });
297
+ }
298
+ }
299
+ collectRemovalHookEffects(child, childNodesIndex) {
300
+ if (this.nodeType !== constants.NODE_TYPE_ELEMENT || hookEffects.isCoveredByPendingPublication(this)) {
301
+ return [];
302
+ }
303
+ return [[() => this[constants.HOOKS].removeChild?.(this, child, childNodesIndex)]];
304
+ }
305
+ collectInsertionHookEffects(insertion) {
306
+ const effects = [];
307
+ for (const prepared of insertion) {
308
+ const {
309
+ node,
310
+ source
311
+ } = prepared;
312
+ if (source?.parent.nodeType === constants.NODE_TYPE_ELEMENT && !hookEffects.isCoveredByPendingPublication(source.parent)) {
313
+ effects.push([() => source.parent[constants.HOOKS].removeChild?.(source.parent, node, source.index)]);
314
+ }
315
+ if (this.nodeType === constants.NODE_TYPE_ELEMENT && !hookEffects.isCoveredByPendingPublication(this)) {
316
+ effects.push([() => this[constants.HOOKS].insertChild?.(this, node, prepared.insertIndex), node]);
317
+ }
318
+ }
319
+ return effects;
320
+ }
321
+ enqueueInsertionReactions(insertion, destinationIsConnected) {
322
+ for (const prepared of insertion) {
323
+ const {
324
+ nodes
325
+ } = prepared;
326
+ if (nodes) {
327
+ if (prepared.shouldDisconnect) {
328
+ this.enqueueTreeReactions(nodes, 'disconnectedCallback');
329
+ }
330
+ if (destinationIsConnected) {
331
+ this.enqueueTreeReactions(nodes, 'connectedCallback');
332
+ }
143
333
  }
144
334
  }
145
- if (this.nodeType === constants.NodeType.ELEMENT_NODE) {
146
- this[constants.HOOKS].insertChild?.(this, child, insertIndex);
335
+ }
336
+ enqueueTreeReactions(nodes, callbackName) {
337
+ enqueueTreeReactions(nodes, callbackName);
338
+ }
339
+ }
340
+ function removeChildForAdoption(parent, child, adoption, destination) {
341
+ if (child.parentNode !== parent) throw Error(`not a child of this node`);
342
+ const disconnectedNodes = parent[constants.IS_CONNECTED] ? adoption.treeNodes : undefined;
343
+ const previousSibling = child[constants.PREV];
344
+ const nextSibling = child[constants.NEXT];
345
+ const childNodesIndex = detachChild(parent, child);
346
+ if (disconnectedNodes) {
347
+ for (const node of disconnectedNodes) node[constants.IS_CONNECTED] = false;
348
+ }
349
+ shared.adoptNodes(adoption.nodes, destination);
350
+ if (MutationObserver.childListObserversActive) {
351
+ MutationObserver.queueMutationRecord({
352
+ type: 'childList',
353
+ target: parent,
354
+ removedNodes: MutationObserver.mutationNodeList(child),
355
+ previousSibling,
356
+ nextSibling
357
+ });
358
+ }
359
+ if (disconnectedNodes) {
360
+ enqueueTreeReactions(disconnectedNodes, 'disconnectedCallback');
361
+ }
362
+ const effects = [];
363
+ if (parent.nodeType === constants.NODE_TYPE_ELEMENT && !hookEffects.isCoveredByPendingPublication(parent)) {
364
+ effects.push([() => parent[constants.HOOKS].removeChild?.(parent, child, childNodesIndex)]);
365
+ }
366
+ hookEffects.performHookEffects(effects);
367
+ }
368
+ function detachChild(parent, child) {
369
+ const previous = child[constants.PREV];
370
+ const next = child[constants.NEXT];
371
+ if (previous) previous[constants.NEXT] = next;else parent[constants.CHILD] = next;
372
+ if (next) next[constants.PREV] = previous;
373
+ const childNodesIndex = parent.childNodes.indexOf(child);
374
+ parent.childNodes.splice(childNodesIndex, 1);
375
+ if (shared.isElementNode(child)) {
376
+ parent.children.splice(parent.children.indexOf(child), 1);
377
+ }
378
+ child[constants.PARENT] = null;
379
+ child[constants.NEXT] = null;
380
+ child[constants.PREV] = null;
381
+ return childNodesIndex;
382
+ }
383
+ function enqueueTreeReactions(nodes, callbackName) {
384
+ for (const node of nodes) {
385
+ const callback = node[callbackName];
386
+ if (typeof callback === 'function') {
387
+ customElementReactions.enqueueCustomElementReaction(node, () => callback.call(node));
147
388
  }
148
389
  }
149
390
  }
150
391
 
151
392
  exports.ParentNode = ParentNode;
393
+ exports.removeChildForAdoption = removeChildForAdoption;
@@ -6,7 +6,7 @@ const constants = require('./constants.cjs');
6
6
  const Element = require('./Element.cjs');
7
7
 
8
8
  class SVGElement extends Element.Element {
9
- [constants.NS] = (() => constants.NamespaceURI.SVG)();
9
+ [constants.NS] = constants.SVG_NAMESPACE;
10
10
  get ownerSVGElement() {
11
11
  let root = null;
12
12
  let parent = this.parentNode;
@@ -6,7 +6,7 @@ const constants = require('./constants.cjs');
6
6
  const CharacterData = require('./CharacterData.cjs');
7
7
 
8
8
  class Text extends CharacterData.CharacterData {
9
- nodeType = (() => constants.NodeType.TEXT_NODE)();
9
+ nodeType = constants.NODE_TYPE_TEXT;
10
10
  [constants.NAME] = '#text';
11
11
  }
12
12