@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
@@ -1,10 +1,11 @@
1
- import { LISTENERS, OWNER_DOCUMENT, HOOKS, PATH } from './constants.esnext';
2
- import { fireEvent, EventPhase, CAPTURE_MARKER } from './Event.esnext';
1
+ import { LISTENERS, OWNER_DOCUMENT, HOOKS_DISPATCH, DISPATCHING, PATH, STOP_IMMEDIATE_PROPAGATION } from './constants.esnext';
2
+ import { createDOMException } from './dom-exception.esnext';
3
+ import { fireEvent, EVENT_PHASE_CAPTURING, EVENT_PHASE_BUBBLING, EVENT_PHASE_NONE } from './Event.esnext';
3
4
 
4
- const ONCE_LISTENERS = Symbol("onceListeners");
5
+ const LISTENER_REGISTRATIONS = Symbol("listenerRegistrations");
5
6
  class EventTarget {
6
7
  [LISTENERS] = void 0;
7
- [ONCE_LISTENERS] = void 0;
8
+ [LISTENER_REGISTRATIONS] = void 0;
8
9
  /**
9
10
  * Property set by entities that extend this class that are part of the DOM tree.
10
11
  * @internal
@@ -12,43 +13,71 @@ class EventTarget {
12
13
  [OWNER_DOCUMENT] = void 0;
13
14
  addEventListener(type, listener, options) {
14
15
  if (listener == null) return;
15
- const capture = options === true || options && options.capture === true;
16
+ const capture = options === true || typeof options === "object" && options.capture === true;
16
17
  const once = typeof options === "object" && options.once === true;
17
18
  const signal = typeof options === "object" ? options.signal : void 0;
18
- const key = `${type}${capture ? CAPTURE_MARKER : ""}`;
19
- let normalizedListener = listener;
20
- if (once) {
21
- normalizedListener = function normalizedListener2(...args) {
22
- this.removeEventListener(type, listener, options);
23
- return typeof listener === "object" ? listener.handleEvent(...args) : listener.call(this, ...args);
19
+ if (signal?.aborted) return;
20
+ const phase = listenerPhase(capture);
21
+ let registrations = this[LISTENER_REGISTRATIONS];
22
+ if (!registrations) {
23
+ registrations = /* @__PURE__ */ new Map();
24
+ this[LISTENER_REGISTRATIONS] = registrations;
25
+ }
26
+ let registrationsForType = registrations.get(type);
27
+ if (!registrationsForType) {
28
+ registrationsForType = {};
29
+ registrations.set(type, registrationsForType);
30
+ }
31
+ let registrationsForPhase = registrationsForType[phase];
32
+ if (!registrationsForPhase) {
33
+ registrationsForPhase = /* @__PURE__ */ new Map();
34
+ registrationsForType[phase] = registrationsForPhase;
35
+ }
36
+ if (registrationsForPhase.has(listener)) return;
37
+ const target = this;
38
+ const registration = {
39
+ type,
40
+ listener,
41
+ capture,
42
+ once,
43
+ normalizedListener: listener,
44
+ signal
45
+ };
46
+ if (once || signal) {
47
+ registration.normalizedListener = function normalizedListener(...args) {
48
+ if (registration.signal?.aborted) {
49
+ removeListenerRegistration(target, registration);
50
+ return;
51
+ }
52
+ if (registration.once) removeListenerRegistration(target, registration);
53
+ return typeof registration.listener === "object" ? registration.listener.handleEvent(...args) : registration.listener.call(this, ...args);
24
54
  };
25
- let onceListeners = this[ONCE_LISTENERS];
26
- if (!onceListeners) {
27
- onceListeners = /* @__PURE__ */ new WeakMap();
28
- this[ONCE_LISTENERS] = onceListeners;
29
- }
30
- onceListeners.set(listener, normalizedListener);
31
55
  }
32
56
  let listeners = this[LISTENERS];
33
57
  if (!listeners) {
34
58
  listeners = /* @__PURE__ */ new Map();
35
59
  this[LISTENERS] = listeners;
36
60
  }
37
- let list = listeners.get(key);
61
+ let listenersForType = listeners.get(type);
62
+ if (!listenersForType) {
63
+ listenersForType = {};
64
+ listeners.set(type, listenersForType);
65
+ }
66
+ let list = listenersForType[phase];
38
67
  if (!list) {
39
68
  list = /* @__PURE__ */ new Set();
40
- listeners.set(key, list);
69
+ listenersForType[phase] = list;
41
70
  }
42
- if (list.has(normalizedListener)) return;
43
- signal?.addEventListener(
44
- "abort",
45
- () => {
46
- removeEventListener.call(this, type, listener, options);
47
- },
48
- { once: true }
49
- );
50
- list.add(normalizedListener);
51
- this[OWNER_DOCUMENT]?.defaultView[HOOKS].addEventListener?.(
71
+ registrationsForPhase.set(listener, registration);
72
+ list.add(registration);
73
+ if (signal) {
74
+ const abortListener = () => {
75
+ removeListenerRegistration(target, registration);
76
+ };
77
+ registration.abortListener = abortListener;
78
+ signal.addEventListener("abort", abortListener, { once: true });
79
+ }
80
+ this[OWNER_DOCUMENT]?.defaultView[HOOKS_DISPATCH].addEventListener?.(
52
81
  this,
53
82
  type,
54
83
  listener,
@@ -62,46 +91,89 @@ class EventTarget {
62
91
  // return PARENT in node;
63
92
  // }
64
93
  dispatchEvent(event) {
65
- const path = [];
66
- let target = this;
67
- while (target != null) {
68
- path.push(target);
69
- target = target.parentNode;
70
- }
71
- event.target = this;
72
- event.srcElement = this;
73
- event[PATH] = path;
74
- for (let i = path.length; i--; ) {
75
- fireEvent(event, path[i], EventPhase.CAPTURING_PHASE);
76
- if (event.cancelBubble) return event.defaultPrevented;
94
+ if (event[DISPATCHING]) {
95
+ throw createDOMException(
96
+ `Failed to execute 'dispatchEvent' on 'EventTarget': The event is already being dispatched.`,
97
+ "InvalidStateError"
98
+ );
77
99
  }
78
- const bubblePath = event.bubbles ? path : path.slice(0, 1);
79
- for (let i = 0; i < bubblePath.length; i++) {
80
- fireEvent(event, bubblePath[i], EventPhase.BUBBLING_PHASE);
81
- if (event.cancelBubble) return event.defaultPrevented;
100
+ event[DISPATCHING] = true;
101
+ try {
102
+ const path = [];
103
+ let target = this;
104
+ while (target != null) {
105
+ path.push(target);
106
+ target = target.parentNode;
107
+ }
108
+ event.target = this;
109
+ event.srcElement = this;
110
+ event[PATH] = path;
111
+ for (let i = path.length; i--; ) {
112
+ fireEvent(event, path[i], EVENT_PHASE_CAPTURING);
113
+ if (event.cancelBubble) return !event.defaultPrevented;
114
+ }
115
+ const bubblePath = event.bubbles ? path : path.slice(0, 1);
116
+ for (let i = 0; i < bubblePath.length; i++) {
117
+ fireEvent(event, bubblePath[i], EVENT_PHASE_BUBBLING);
118
+ if (event.cancelBubble) return !event.defaultPrevented;
119
+ }
120
+ return !event.defaultPrevented;
121
+ } finally {
122
+ event[DISPATCHING] = false;
123
+ event.cancelBubble = false;
124
+ event[STOP_IMMEDIATE_PROPAGATION] = false;
125
+ event.eventPhase = EVENT_PHASE_NONE;
126
+ event.currentTarget = null;
127
+ event[PATH] = [];
82
128
  }
83
- return event.defaultPrevented;
84
129
  }
85
130
  }
86
131
  function removeEventListener(type, listener, options) {
87
132
  if (listener == null) return;
88
- const onceListeners = this[ONCE_LISTENERS];
89
- const normalizedListener = onceListeners?.get(listener) ?? listener;
90
- onceListeners?.delete(listener);
91
133
  const capture = options === true || options && options.capture === true;
92
- const key = `${type}${capture ? CAPTURE_MARKER : ""}`;
93
- const list = this[LISTENERS]?.get(key);
94
- if (list) {
95
- const deleted = list.delete(normalizedListener);
96
- if (deleted) {
97
- this[OWNER_DOCUMENT]?.defaultView[HOOKS].removeEventListener?.(
98
- this,
99
- type,
100
- listener,
101
- options
102
- );
103
- }
134
+ const phase = listenerPhase(Boolean(capture));
135
+ const registration = this[LISTENER_REGISTRATIONS]?.get(type)?.[phase]?.get(listener);
136
+ if (!registration) return;
137
+ removeListenerRegistration(this, registration);
138
+ }
139
+ function listenerPhase(capture) {
140
+ return capture ? "capture" : "bubble";
141
+ }
142
+ function removeListenerRegistration(target, registration) {
143
+ const phase = listenerPhase(registration.capture);
144
+ const registrations = target[LISTENER_REGISTRATIONS];
145
+ const registrationsForType = registrations?.get(registration.type);
146
+ if (!registrationsForType) return;
147
+ const registrationsForPhase = registrationsForType[phase];
148
+ if (!registrationsForPhase || registrationsForPhase.get(registration.listener) !== registration) {
149
+ return;
150
+ }
151
+ registrationsForPhase.delete(registration.listener);
152
+ if (registrationsForPhase.size === 0) delete registrationsForType[phase];
153
+ if (!registrationsForType.bubble && !registrationsForType.capture) {
154
+ registrations?.delete(registration.type);
155
+ }
156
+ if (registration.signal && registration.abortListener) {
157
+ registration.signal.removeEventListener(
158
+ "abort",
159
+ registration.abortListener
160
+ );
161
+ }
162
+ const listeners = target[LISTENERS];
163
+ const listenersForType = listeners?.get(registration.type);
164
+ if (!listenersForType) return;
165
+ const list = listenersForType[phase];
166
+ if (!list?.delete(registration)) return;
167
+ if (list.size === 0) delete listenersForType[phase];
168
+ if (!listenersForType.bubble && !listenersForType.capture) {
169
+ listeners?.delete(registration.type);
104
170
  }
171
+ target[OWNER_DOCUMENT]?.defaultView[HOOKS_DISPATCH].removeEventListener?.(
172
+ target,
173
+ registration.type,
174
+ registration.listener,
175
+ registration.capture
176
+ );
105
177
  }
106
178
 
107
179
  export { EventTarget };
@@ -1,4 +1,5 @@
1
- import { CONTENT } from './constants.esnext';
1
+ import { CONTENT, HOST } from './constants.esnext';
2
+ import './Event.esnext';
2
3
  import { Element } from './Element.esnext';
3
4
  import { parseHtml, serializeChildren } from './serialization.esnext';
4
5
 
@@ -8,6 +9,7 @@ class HTMLTemplateElement extends Element {
8
9
  let content = this[CONTENT];
9
10
  if (!content) {
10
11
  content = this.ownerDocument.createDocumentFragment();
12
+ content[HOST] = this;
11
13
  this[CONTENT] = content;
12
14
  }
13
15
  return content;
@@ -1,5 +1,166 @@
1
+ import { NodeList } from './NodeList.esnext';
2
+
3
+ let registrations;
4
+ let registrationCount = 0;
5
+ let attributeRegistrationCount = 0;
6
+ let characterDataRegistrationCount = 0;
7
+ let childListRegistrationCount = 0;
8
+ let attributeObserversActive = false;
9
+ let characterDataObserversActive = false;
10
+ let childListObserversActive = false;
1
11
  class MutationObserver {
2
- // stub implementation
12
+ #callback;
13
+ #records = [];
14
+ #targets = /* @__PURE__ */ new Set();
15
+ #scheduled = false;
16
+ constructor(callback) {
17
+ if (typeof callback !== "function") {
18
+ throw new TypeError("MutationObserver callback must be a function");
19
+ }
20
+ this.#callback = callback;
21
+ }
22
+ observe(target, options = {}) {
23
+ const normalizedOptions = normalizeOptions(options);
24
+ const currentRegistrations = registrations ??= /* @__PURE__ */ new WeakMap();
25
+ let targetRegistrations = currentRegistrations.get(target);
26
+ if (targetRegistrations == null) {
27
+ targetRegistrations = /* @__PURE__ */ new Map();
28
+ currentRegistrations.set(target, targetRegistrations);
29
+ }
30
+ const previousOptions = targetRegistrations.get(this);
31
+ if (previousOptions == null) {
32
+ registrationCount++;
33
+ } else {
34
+ updateRegistrationCounts(previousOptions, -1);
35
+ }
36
+ targetRegistrations.set(this, normalizedOptions);
37
+ updateRegistrationCounts(normalizedOptions, 1);
38
+ this.#targets.add(target);
39
+ }
40
+ disconnect() {
41
+ const currentRegistrations = registrations;
42
+ if (currentRegistrations == null) return;
43
+ for (const target of this.#targets) {
44
+ const targetRegistrations = currentRegistrations.get(target);
45
+ const options = targetRegistrations?.get(this);
46
+ if (options != null) {
47
+ targetRegistrations.delete(this);
48
+ registrationCount--;
49
+ updateRegistrationCounts(options, -1);
50
+ }
51
+ if (targetRegistrations?.size === 0) {
52
+ currentRegistrations.delete(target);
53
+ }
54
+ }
55
+ this.#targets.clear();
56
+ this.#records.length = 0;
57
+ if (registrationCount === 0) {
58
+ registrations = void 0;
59
+ }
60
+ }
61
+ takeRecords() {
62
+ return this.#records.splice(0);
63
+ }
64
+ /** @internal */
65
+ enqueue(record) {
66
+ this.#records.push(record);
67
+ if (this.#scheduled) return;
68
+ this.#scheduled = true;
69
+ queueMicrotask(() => {
70
+ this.#scheduled = false;
71
+ const records = this.takeRecords();
72
+ if (records.length > 0) {
73
+ this.#callback.call(this, records, this);
74
+ }
75
+ });
76
+ }
77
+ }
78
+ function queueMutationRecord(init) {
79
+ const currentRegistrations = registrations;
80
+ if (currentRegistrations == null) return;
81
+ const interestedObservers = /* @__PURE__ */ new Map();
82
+ let node = init.target;
83
+ while (node) {
84
+ const targetRegistrations = currentRegistrations.get(node);
85
+ if (targetRegistrations) {
86
+ for (const [observer, options] of targetRegistrations) {
87
+ if (node !== init.target && !options.subtree) continue;
88
+ let includeOldValue = false;
89
+ if (init.type === "attributes") {
90
+ if (!options.attributes) continue;
91
+ if (options.attributeFilter && !options.attributeFilter.has(init.attributeName ?? "")) {
92
+ continue;
93
+ }
94
+ includeOldValue = options.attributeOldValue;
95
+ } else if (init.type === "characterData") {
96
+ if (!options.characterData) continue;
97
+ includeOldValue = options.characterDataOldValue;
98
+ } else if (!options.childList) {
99
+ continue;
100
+ }
101
+ interestedObservers.set(
102
+ observer,
103
+ interestedObservers.get(observer) === true || includeOldValue
104
+ );
105
+ }
106
+ }
107
+ node = node.parentNode;
108
+ }
109
+ for (const [observer, includeOldValue] of interestedObservers) {
110
+ observer.enqueue({
111
+ addedNodes: init.addedNodes ?? new NodeList(),
112
+ attributeName: init.attributeName ?? null,
113
+ attributeNamespace: init.attributeNamespace ?? null,
114
+ nextSibling: init.nextSibling ?? null,
115
+ oldValue: includeOldValue ? init.oldValue ?? null : null,
116
+ previousSibling: init.previousSibling ?? null,
117
+ removedNodes: init.removedNodes ?? new NodeList(),
118
+ target: init.target,
119
+ type: init.type
120
+ });
121
+ }
122
+ }
123
+ function mutationNodeList(...nodes) {
124
+ const list = new NodeList();
125
+ list.push(...nodes);
126
+ return list;
127
+ }
128
+ function normalizeOptions(options) {
129
+ if (options.attributes === false && (options.attributeOldValue || options.attributeFilter != null)) {
130
+ throw new TypeError(
131
+ "attributeOldValue and attributeFilter require attributes to be enabled"
132
+ );
133
+ }
134
+ if (options.characterData === false && options.characterDataOldValue) {
135
+ throw new TypeError(
136
+ "characterDataOldValue requires characterData to be enabled"
137
+ );
138
+ }
139
+ const attributes = options.attributes ?? (options.attributeOldValue === true || options.attributeFilter != null);
140
+ const characterData = options.characterData ?? options.characterDataOldValue === true;
141
+ const childList = options.childList === true;
142
+ if (!attributes && !characterData && !childList) {
143
+ throw new TypeError(
144
+ "MutationObserver options must enable childList, attributes, or characterData"
145
+ );
146
+ }
147
+ return {
148
+ attributes,
149
+ attributeFilter: options.attributeFilter == null ? void 0 : new Set(options.attributeFilter.map(String)),
150
+ attributeOldValue: options.attributeOldValue === true,
151
+ characterData,
152
+ characterDataOldValue: options.characterDataOldValue === true,
153
+ childList,
154
+ subtree: options.subtree === true
155
+ };
156
+ }
157
+ function updateRegistrationCounts(options, delta) {
158
+ if (options.attributes) attributeRegistrationCount += delta;
159
+ if (options.characterData) characterDataRegistrationCount += delta;
160
+ if (options.childList) childListRegistrationCount += delta;
161
+ attributeObserversActive = attributeRegistrationCount > 0;
162
+ characterDataObserversActive = characterDataRegistrationCount > 0;
163
+ childListObserversActive = childListRegistrationCount > 0;
3
164
  }
4
165
 
5
- export { MutationObserver };
166
+ export { MutationObserver, attributeObserversActive, characterDataObserversActive, childListObserversActive, mutationNodeList, queueMutationRecord };
@@ -1,4 +1,9 @@
1
- import { CHILD, OWNER_ELEMENT, NS, NEXT, HOOKS } from './constants.esnext';
1
+ import { CHILD, OWNER_ELEMENT, NS, HTML_NAMESPACE, asciiLowercase, NEXT, HOOKS, OWNER_DOCUMENT } from './constants.esnext';
2
+ import { normalizeNamespace } from './names.esnext';
3
+ import { attributeObserversActive, queueMutationRecord } from './MutationObserver.esnext';
4
+ import { performWithCustomElementReactions } from './custom-element-reactions.esnext';
5
+ import { enqueueAttributeReaction } from './attribute-reactions.esnext';
6
+ import { createDOMException } from './dom-exception.esnext';
2
7
 
3
8
  class NamedNodeMap {
4
9
  [CHILD] = null;
@@ -7,12 +12,21 @@ class NamedNodeMap {
7
12
  this[OWNER_ELEMENT] = ownerElement;
8
13
  }
9
14
  getNamedItem(name) {
10
- return this.getNamedItemNS(null, name);
15
+ const qualifiedName = String(name);
16
+ const normalizedName = this[OWNER_ELEMENT][NS] === HTML_NAMESPACE ? asciiLowercase(qualifiedName) : qualifiedName;
17
+ let attr = this[CHILD];
18
+ while (attr) {
19
+ if (attr.name === normalizedName) return attr;
20
+ attr = attr[NEXT];
21
+ }
22
+ return null;
11
23
  }
12
- getNamedItemNS(namespaceURI, name) {
24
+ getNamedItemNS(namespaceURI, localName) {
25
+ const namespace = normalizeNamespace(namespaceURI);
26
+ const normalizedLocalName = String(localName);
13
27
  let attr = this[CHILD];
14
28
  while (attr) {
15
- if (attr.name === name && attr[NS] == namespaceURI) {
29
+ if (attr.localName === normalizedLocalName && attr[NS] === namespace) {
16
30
  return attr;
17
31
  }
18
32
  attr = attr[NEXT];
@@ -38,21 +52,52 @@ class NamedNodeMap {
38
52
  return index;
39
53
  }
40
54
  removeNamedItem(name) {
41
- return this.removeNamedItemNS(null, name);
55
+ const qualifiedName = String(name);
56
+ const normalizedName = this[OWNER_ELEMENT][NS] === HTML_NAMESPACE ? asciiLowercase(qualifiedName) : qualifiedName;
57
+ return performWithCustomElementReactions(
58
+ () => this.removeNamedItemImmediately((attr) => attr.name === normalizedName)
59
+ );
60
+ }
61
+ removeNamedItemNS(namespaceURI, localName) {
62
+ const namespace = normalizeNamespace(namespaceURI);
63
+ const normalizedLocalName = String(localName);
64
+ return performWithCustomElementReactions(
65
+ () => this.removeNamedItemImmediately(
66
+ (attr) => attr.localName === normalizedLocalName && attr[NS] === namespace
67
+ )
68
+ );
42
69
  }
43
- removeNamedItemNS(namespaceURI, name) {
70
+ removeNamedItemImmediately(matches) {
44
71
  const ownerElement = this[OWNER_ELEMENT];
45
72
  let attr = this[CHILD];
46
73
  let prev = null;
47
74
  while (attr != null) {
48
- if (attr.name === name && attr[NS] == namespaceURI) {
75
+ if (matches(attr)) {
49
76
  if (prev) prev[NEXT] = attr[NEXT];
50
77
  if (this[CHILD] === attr) this[CHILD] = attr[NEXT];
51
- updateElementAttribute(ownerElement, attr.name, attr.value, null);
78
+ const oldValue = attr.value;
79
+ attr[NEXT] = null;
80
+ attr[OWNER_ELEMENT] = null;
81
+ if (attributeObserversActive) {
82
+ queueMutationRecord({
83
+ type: "attributes",
84
+ target: ownerElement,
85
+ attributeName: attr.name,
86
+ attributeNamespace: attr[NS],
87
+ oldValue
88
+ });
89
+ }
52
90
  ownerElement[HOOKS].removeAttribute?.(
53
91
  ownerElement,
54
- name,
55
- namespaceURI
92
+ attr.name,
93
+ attr[NS]
94
+ );
95
+ enqueueAttributeReaction(
96
+ ownerElement,
97
+ attr.localName,
98
+ oldValue,
99
+ null,
100
+ attr[NS]
56
101
  );
57
102
  return attr;
58
103
  }
@@ -62,36 +107,61 @@ class NamedNodeMap {
62
107
  return null;
63
108
  }
64
109
  setNamedItem(attr) {
110
+ return performWithCustomElementReactions(
111
+ () => this.setNamedItemImmediately(attr)
112
+ );
113
+ }
114
+ setNamedItemImmediately(attr) {
65
115
  const ownerElement = this[OWNER_ELEMENT];
116
+ const currentOwner = attr[OWNER_ELEMENT];
117
+ if (currentOwner != null && currentOwner !== ownerElement) {
118
+ throw createDOMException(
119
+ "The attribute is already in use by another element.",
120
+ "InUseAttributeError"
121
+ );
122
+ }
66
123
  let old = null;
67
124
  let child = this[CHILD];
68
- attr[OWNER_ELEMENT] = ownerElement;
69
- if (child == null) {
70
- this[CHILD] = attr;
71
- } else {
72
- let prev;
73
- while (child) {
74
- if (child.name === attr.name && child[NS] == attr[NS]) {
75
- if (prev) prev[NEXT] = attr;
76
- else this[CHILD] = attr;
77
- attr[NEXT] = child[NEXT];
78
- child[NEXT] = null;
79
- old = child;
80
- break;
81
- }
82
- prev = child;
83
- child = child[NEXT];
125
+ let prev = null;
126
+ while (child) {
127
+ if (child.localName === attr.localName && child[NS] === attr[NS]) {
128
+ if (child === attr) return child;
129
+ if (prev) prev[NEXT] = attr;
130
+ else this[CHILD] = attr;
131
+ attr[NEXT] = child[NEXT];
132
+ child[NEXT] = null;
133
+ old = child;
134
+ break;
84
135
  }
136
+ prev = child;
137
+ child = child[NEXT];
138
+ }
139
+ if (old == null) {
85
140
  if (prev) prev[NEXT] = attr;
86
141
  else this[CHILD] = attr;
87
142
  }
88
- if (!old || old.value !== attr.value) {
89
- updateElementAttribute(
143
+ attr[OWNER_ELEMENT] = ownerElement;
144
+ attr[OWNER_DOCUMENT] = ownerElement[OWNER_DOCUMENT];
145
+ if (old) old[OWNER_ELEMENT] = null;
146
+ const valueChanged = old == null || old.value !== attr.value;
147
+ const qualifiedNameChanged = old != null && old.name !== attr.name;
148
+ if (valueChanged && attributeObserversActive) {
149
+ queueMutationRecord({
150
+ type: "attributes",
151
+ target: ownerElement,
152
+ attributeName: attr.name,
153
+ attributeNamespace: attr[NS],
154
+ oldValue: old?.value ?? null
155
+ });
156
+ }
157
+ if (qualifiedNameChanged) {
158
+ ownerElement[HOOKS].removeAttribute?.(
90
159
  ownerElement,
91
- attr.name,
92
- old?.value ?? null,
93
- attr.value
160
+ old.name,
161
+ old[NS]
94
162
  );
163
+ }
164
+ if (valueChanged || qualifiedNameChanged) {
95
165
  ownerElement[HOOKS].setAttribute?.(
96
166
  ownerElement,
97
167
  attr.name,
@@ -99,6 +169,15 @@ class NamedNodeMap {
99
169
  attr[NS]
100
170
  );
101
171
  }
172
+ if (valueChanged) {
173
+ enqueueAttributeReaction(
174
+ ownerElement,
175
+ attr.localName,
176
+ old?.value ?? null,
177
+ attr.value,
178
+ attr[NS]
179
+ );
180
+ }
102
181
  return old;
103
182
  }
104
183
  setNamedItemNS(attr) {
@@ -112,16 +191,5 @@ class NamedNodeMap {
112
191
  }
113
192
  }
114
193
  }
115
- function updateElementAttribute(element, name, oldValue, newValue) {
116
- const { observedAttributes } = element.constructor;
117
- const { attributeChangedCallback } = element;
118
- if (name === "slot") {
119
- element.slot = newValue ?? "";
120
- }
121
- if (attributeChangedCallback == null || observedAttributes == null || !observedAttributes.includes(name)) {
122
- return;
123
- }
124
- return attributeChangedCallback.call(element, name, oldValue, newValue);
125
- }
126
194
 
127
195
  export { NamedNodeMap };