@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.
- package/CHANGELOG.md +88 -0
- package/README.md +51 -3
- package/build/cjs/Attr.cjs +33 -4
- package/build/cjs/CharacterData.cjs +10 -0
- package/build/cjs/ChildNode.cjs +79 -13
- package/build/cjs/Comment.cjs +1 -1
- package/build/cjs/CustomElementRegistry.cjs +27 -2
- package/build/cjs/CustomEvent.cjs +2 -2
- package/build/cjs/Document.cjs +56 -32
- package/build/cjs/DocumentFragment.cjs +6 -2
- package/build/cjs/Element.cjs +68 -8
- package/build/cjs/Event.cjs +29 -27
- package/build/cjs/EventTarget.cjs +128 -58
- package/build/cjs/HTMLTemplateElement.cjs +1 -0
- package/build/cjs/MutationObserver.cjs +167 -1
- package/build/cjs/NamedNodeMap.cjs +82 -45
- package/build/cjs/Node.cjs +47 -11
- package/build/cjs/NodeList.cjs +9 -1
- package/build/cjs/ParentNode.cjs +319 -77
- package/build/cjs/SVGElement.cjs +1 -1
- package/build/cjs/Text.cjs +1 -1
- package/build/cjs/Window.cjs +131 -38
- package/build/cjs/attribute-reactions.cjs +20 -0
- package/build/cjs/constants.cjs +48 -24
- package/build/cjs/custom-element-reactions.cjs +52 -0
- package/build/cjs/dom-exception.cjs +15 -0
- package/build/cjs/hook-effects.cjs +51 -0
- package/build/cjs/names.cjs +61 -0
- package/build/cjs/selectors.cjs +213 -122
- package/build/cjs/serialization.cjs +168 -65
- package/build/cjs/shared.cjs +177 -28
- package/build/esm/Attr.mjs +34 -5
- package/build/esm/CharacterData.mjs +10 -0
- package/build/esm/ChildNode.mjs +76 -15
- package/build/esm/Comment.mjs +2 -2
- package/build/esm/CustomElementRegistry.mjs +27 -2
- package/build/esm/CustomEvent.mjs +2 -2
- package/build/esm/Document.mjs +60 -35
- package/build/esm/DocumentFragment.mjs +7 -3
- package/build/esm/Element.mjs +69 -9
- package/build/esm/Event.mjs +27 -27
- package/build/esm/EventTarget.mjs +130 -60
- package/build/esm/HTMLTemplateElement.mjs +2 -1
- package/build/esm/MutationObserver.mjs +166 -2
- package/build/esm/NamedNodeMap.mjs +83 -46
- package/build/esm/Node.mjs +48 -12
- package/build/esm/NodeList.mjs +9 -1
- package/build/esm/ParentNode.mjs +322 -81
- package/build/esm/SVGElement.mjs +2 -2
- package/build/esm/Text.mjs +2 -2
- package/build/esm/Window.mjs +132 -39
- package/build/esm/attribute-reactions.mjs +16 -0
- package/build/esm/constants.mjs +28 -23
- package/build/esm/custom-element-reactions.mjs +47 -0
- package/build/esm/dom-exception.mjs +11 -0
- package/build/esm/hook-effects.mjs +46 -0
- package/build/esm/names.mjs +54 -0
- package/build/esm/selectors.mjs +202 -124
- package/build/esm/serialization.mjs +169 -66
- package/build/esm/shared.mjs +173 -30
- package/build/esnext/Attr.esnext +45 -5
- package/build/esnext/CharacterData.esnext +10 -0
- package/build/esnext/ChildNode.esnext +84 -15
- package/build/esnext/Comment.esnext +2 -2
- package/build/esnext/CustomElementRegistry.esnext +42 -0
- package/build/esnext/CustomEvent.esnext +2 -2
- package/build/esnext/Document.esnext +69 -32
- package/build/esnext/DocumentFragment.esnext +6 -2
- package/build/esnext/Element.esnext +78 -9
- package/build/esnext/Event.esnext +36 -37
- package/build/esnext/EventTarget.esnext +133 -61
- package/build/esnext/HTMLTemplateElement.esnext +3 -1
- package/build/esnext/MutationObserver.esnext +163 -2
- package/build/esnext/NamedNodeMap.esnext +110 -42
- package/build/esnext/Node.esnext +50 -12
- package/build/esnext/NodeList.esnext +1 -1
- package/build/esnext/ParentNode.esnext +352 -79
- package/build/esnext/SVGElement.esnext +2 -2
- package/build/esnext/Text.esnext +2 -2
- package/build/esnext/Window.esnext +120 -16
- package/build/esnext/attribute-reactions.esnext +21 -0
- package/build/esnext/constants.esnext +34 -21
- package/build/esnext/custom-element-reactions.esnext +49 -0
- package/build/esnext/dom-exception.esnext +11 -0
- package/build/esnext/hook-effects.esnext +46 -0
- package/build/esnext/names.esnext +61 -0
- package/build/esnext/selectors.esnext +202 -100
- package/build/esnext/serialization.esnext +171 -48
- package/build/esnext/shared.esnext +181 -34
- package/build/tsconfig.tsbuildinfo +1 -1
- package/build/typescript/Attr.d.ts +6 -4
- package/build/typescript/Attr.d.ts.map +1 -1
- package/build/typescript/CharacterData.d.ts.map +1 -1
- package/build/typescript/ChildNode.d.ts +5 -0
- package/build/typescript/ChildNode.d.ts.map +1 -1
- package/build/typescript/ClipboardEvent.d.ts.map +1 -1
- package/build/typescript/Comment.d.ts +2 -2
- package/build/typescript/Comment.d.ts.map +1 -1
- package/build/typescript/CustomElementRegistry.d.ts +1 -0
- package/build/typescript/CustomElementRegistry.d.ts.map +1 -1
- package/build/typescript/CustomEvent.d.ts.map +1 -1
- package/build/typescript/Document.d.ts +10 -5
- package/build/typescript/Document.d.ts.map +1 -1
- package/build/typescript/DocumentFragment.d.ts +2 -1
- package/build/typescript/DocumentFragment.d.ts.map +1 -1
- package/build/typescript/Element.d.ts +16 -7
- package/build/typescript/Element.d.ts.map +1 -1
- package/build/typescript/ErrorEvent.d.ts.map +1 -1
- package/build/typescript/Event.d.ts +9 -10
- package/build/typescript/Event.d.ts.map +1 -1
- package/build/typescript/EventTarget.d.ts +20 -3
- package/build/typescript/EventTarget.d.ts.map +1 -1
- package/build/typescript/FocusEvent.d.ts.map +1 -1
- package/build/typescript/HTMLTemplateElement.d.ts.map +1 -1
- package/build/typescript/MutationObserver.d.ts +42 -0
- package/build/typescript/MutationObserver.d.ts.map +1 -1
- package/build/typescript/NamedNodeMap.d.ts +5 -3
- package/build/typescript/NamedNodeMap.d.ts.map +1 -1
- package/build/typescript/Node.d.ts +4 -3
- package/build/typescript/Node.d.ts.map +1 -1
- package/build/typescript/NodeList.d.ts +3 -2
- package/build/typescript/NodeList.d.ts.map +1 -1
- package/build/typescript/ParentNode.d.ts +29 -10
- package/build/typescript/ParentNode.d.ts.map +1 -1
- package/build/typescript/PromiseRejectionEvent.d.ts.map +1 -1
- package/build/typescript/SVGElement.d.ts +1 -1
- package/build/typescript/SVGElement.d.ts.map +1 -1
- package/build/typescript/Text.d.ts +1 -1
- package/build/typescript/Text.d.ts.map +1 -1
- package/build/typescript/ToggleEvent.d.ts.map +1 -1
- package/build/typescript/Window.d.ts +5 -1
- package/build/typescript/Window.d.ts.map +1 -1
- package/build/typescript/attribute-reactions.d.ts +4 -0
- package/build/typescript/attribute-reactions.d.ts.map +1 -0
- package/build/typescript/constants.d.ts +27 -18
- package/build/typescript/constants.d.ts.map +1 -1
- package/build/typescript/custom-element-reactions.d.ts +6 -0
- package/build/typescript/custom-element-reactions.d.ts.map +1 -0
- package/build/typescript/dom-exception.d.ts +2 -0
- package/build/typescript/dom-exception.d.ts.map +1 -0
- package/build/typescript/extensions.d.ts +4 -0
- package/build/typescript/extensions.d.ts.map +1 -0
- package/build/typescript/hook-effects.d.ts +6 -0
- package/build/typescript/hook-effects.d.ts.map +1 -0
- package/build/typescript/hooks.d.ts +3 -3
- package/build/typescript/hooks.d.ts.map +1 -1
- package/build/typescript/index.d.ts +1 -0
- package/build/typescript/index.d.ts.map +1 -1
- package/build/typescript/names.d.ts +11 -0
- package/build/typescript/names.d.ts.map +1 -0
- package/build/typescript/selectors.d.ts +21 -22
- package/build/typescript/selectors.d.ts.map +1 -1
- package/build/typescript/serialization.d.ts.map +1 -1
- package/build/typescript/shared.d.ts +10 -0
- package/build/typescript/shared.d.ts.map +1 -1
- package/build/typescript/tests/ChildNode.test.d.ts +2 -0
- package/build/typescript/tests/ChildNode.test.d.ts.map +1 -0
- package/build/typescript/tests/MutationObserver.test.d.ts +2 -0
- package/build/typescript/tests/MutationObserver.test.d.ts.map +1 -0
- package/build/typescript/tests/adopt-inserted-subtrees.test.d.ts +2 -0
- package/build/typescript/tests/adopt-inserted-subtrees.test.d.ts.map +1 -0
- package/build/typescript/tests/attr-mutation.test.d.ts +2 -0
- package/build/typescript/tests/attr-mutation.test.d.ts.map +1 -0
- package/build/typescript/tests/child-node-before-after.test.d.ts +2 -0
- package/build/typescript/tests/child-node-before-after.test.d.ts.map +1 -0
- package/build/typescript/tests/child-node-replace-with.test.d.ts +2 -0
- package/build/typescript/tests/child-node-replace-with.test.d.ts.map +1 -0
- package/build/typescript/tests/clone-import-namespaces.test.d.ts +2 -0
- package/build/typescript/tests/clone-import-namespaces.test.d.ts.map +1 -0
- package/build/typescript/tests/constants.test.d.ts +2 -0
- package/build/typescript/tests/constants.test.d.ts.map +1 -0
- package/build/typescript/tests/create-element-namespaces.test.d.ts +2 -0
- package/build/typescript/tests/create-element-namespaces.test.d.ts.map +1 -0
- package/build/typescript/tests/create-text-node.test.d.ts +2 -0
- package/build/typescript/tests/create-text-node.test.d.ts.map +1 -0
- package/build/typescript/tests/custom-element-reactions.test.d.ts +2 -0
- package/build/typescript/tests/custom-element-reactions.test.d.ts.map +1 -0
- package/build/typescript/tests/custom-element-registry.test.d.ts +2 -0
- package/build/typescript/tests/custom-element-registry.test.d.ts.map +1 -0
- package/build/typescript/tests/custom-event.test.d.ts +2 -0
- package/build/typescript/tests/custom-event.test.d.ts.map +1 -0
- package/build/typescript/tests/document-clone-import-contract.test.d.ts +2 -0
- package/build/typescript/tests/document-clone-import-contract.test.d.ts.map +1 -0
- package/build/typescript/tests/document.test.d.ts +2 -0
- package/build/typescript/tests/document.test.d.ts.map +1 -0
- package/build/typescript/tests/dom-mutation-selector-errors.test.d.ts +2 -0
- package/build/typescript/tests/dom-mutation-selector-errors.test.d.ts.map +1 -0
- package/build/typescript/tests/event-dispatch.test.d.ts +2 -0
- package/build/typescript/tests/event-dispatch.test.d.ts.map +1 -0
- package/build/typescript/tests/event-target.test.d.ts +2 -0
- package/build/typescript/tests/event-target.test.d.ts.map +1 -0
- package/build/typescript/tests/event.test.d.ts +2 -0
- package/build/typescript/tests/event.test.d.ts.map +1 -0
- package/build/typescript/tests/extensions.test.d.ts +2 -0
- package/build/typescript/tests/extensions.test.d.ts.map +1 -0
- package/build/typescript/tests/get-elements-by-tag-name.test.d.ts +2 -0
- package/build/typescript/tests/get-elements-by-tag-name.test.d.ts.map +1 -0
- package/build/typescript/tests/getElementsByClassName.test.d.ts +2 -0
- package/build/typescript/tests/getElementsByClassName.test.d.ts.map +1 -0
- package/build/typescript/tests/hooks.test.d.ts +2 -0
- package/build/typescript/tests/hooks.test.d.ts.map +1 -0
- package/build/typescript/tests/inner-html-parsing.test.d.ts +2 -0
- package/build/typescript/tests/inner-html-parsing.test.d.ts.map +1 -0
- package/build/typescript/tests/is-default-namespace.test.d.ts +2 -0
- package/build/typescript/tests/is-default-namespace.test.d.ts.map +1 -0
- package/build/typescript/tests/name-and-namespace-normalization.test.d.ts +2 -0
- package/build/typescript/tests/name-and-namespace-normalization.test.d.ts.map +1 -0
- package/build/typescript/tests/named-node-map.test.d.ts +2 -0
- package/build/typescript/tests/named-node-map.test.d.ts.map +1 -0
- package/build/typescript/tests/namespaced-attribute-reactions.test.d.ts +2 -0
- package/build/typescript/tests/namespaced-attribute-reactions.test.d.ts.map +1 -0
- package/build/typescript/tests/node.test.d.ts +2 -0
- package/build/typescript/tests/node.test.d.ts.map +1 -0
- package/build/typescript/tests/parent-node-mutation-return-values.test.d.ts +2 -0
- package/build/typescript/tests/parent-node-mutation-return-values.test.d.ts.map +1 -0
- package/build/typescript/tests/parent-node-mutation-validation.test.d.ts +2 -0
- package/build/typescript/tests/parent-node-mutation-validation.test.d.ts.map +1 -0
- package/build/typescript/tests/parent-node.test.d.ts +2 -0
- package/build/typescript/tests/parent-node.test.d.ts.map +1 -0
- package/build/typescript/tests/serialization-template.test.d.ts +2 -0
- package/build/typescript/tests/serialization-template.test.d.ts.map +1 -0
- package/build/typescript/tests/slot-attribute.test.d.ts +2 -0
- package/build/typescript/tests/slot-attribute.test.d.ts.map +1 -0
- package/build/typescript/tests/text-content.test.d.ts +2 -0
- package/build/typescript/tests/text-content.test.d.ts.map +1 -0
- package/build/typescript/tests/toggle-attribute.test.d.ts +2 -0
- package/build/typescript/tests/toggle-attribute.test.d.ts.map +1 -0
- package/build/typescript/tests/wide-tree-traversal.test.d.ts +2 -0
- package/build/typescript/tests/wide-tree-traversal.test.d.ts.map +1 -0
- package/build/typescript/tests/window-set-global.test.d.ts +2 -0
- package/build/typescript/tests/window-set-global.test.d.ts.map +1 -0
- package/package.json +1 -1
- package/source/Attr.ts +60 -8
- package/source/CharacterData.ts +13 -0
- package/source/ChildNode.ts +107 -14
- package/source/Comment.ts +2 -2
- package/source/CustomElementRegistry.ts +56 -0
- package/source/CustomEvent.ts +2 -2
- package/source/Document.ts +104 -38
- package/source/DocumentFragment.ts +12 -2
- package/source/Element.ts +129 -14
- package/source/Event.ts +34 -32
- package/source/EventTarget.ts +203 -79
- package/source/HTMLTemplateElement.ts +2 -1
- package/source/MutationObserver.ts +279 -1
- package/source/NamedNodeMap.ts +141 -60
- package/source/Node.ts +70 -14
- package/source/NodeList.ts +13 -3
- package/source/ParentNode.ts +500 -88
- package/source/SVGElement.ts +2 -2
- package/source/Text.ts +2 -2
- package/source/Window.ts +156 -17
- package/source/attribute-reactions.ts +32 -0
- package/source/constants.ts +40 -17
- package/source/custom-element-reactions.ts +64 -0
- package/source/dom-exception.ts +14 -0
- package/source/extensions.ts +4 -0
- package/source/hook-effects.ts +56 -0
- package/source/hooks.ts +11 -3
- package/source/index.ts +1 -0
- package/source/names.ts +94 -0
- package/source/selectors.ts +301 -138
- package/source/serialization.ts +224 -66
- package/source/shared.ts +269 -40
- package/source/tests/ChildNode.test.ts +178 -0
- package/source/tests/MutationObserver.test.ts +303 -0
- package/source/tests/adopt-inserted-subtrees.test.ts +584 -0
- package/source/tests/attr-mutation.test.ts +112 -0
- package/source/tests/child-node-before-after.test.ts +421 -0
- package/source/tests/child-node-replace-with.test.ts +251 -0
- package/source/tests/clone-import-namespaces.test.ts +365 -0
- package/source/tests/constants.test.ts +46 -0
- package/source/tests/create-element-namespaces.test.ts +142 -0
- package/source/tests/create-text-node.test.ts +38 -0
- package/source/tests/custom-element-reactions.test.ts +600 -0
- package/source/tests/custom-element-registry.test.ts +137 -0
- package/source/tests/custom-event.test.ts +26 -0
- package/source/tests/document-clone-import-contract.test.ts +77 -0
- package/source/tests/document.test.ts +122 -0
- package/source/tests/dom-mutation-selector-errors.test.ts +484 -0
- package/source/tests/event-dispatch.test.ts +270 -0
- package/source/tests/event-target.test.ts +208 -0
- package/source/tests/event.test.ts +98 -0
- package/source/tests/extensions.test.ts +159 -0
- package/source/tests/get-elements-by-tag-name.test.ts +79 -0
- package/source/tests/getElementsByClassName.test.ts +109 -0
- package/source/tests/hooks.test.ts +43 -0
- package/source/tests/inner-html-parsing.test.ts +461 -0
- package/source/tests/is-default-namespace.test.ts +111 -0
- package/source/tests/name-and-namespace-normalization.test.ts +319 -0
- package/source/tests/named-node-map.test.ts +122 -0
- package/source/tests/namespaced-attribute-reactions.test.ts +141 -0
- package/source/tests/node.test.ts +56 -0
- package/source/tests/parent-node-mutation-return-values.test.ts +63 -0
- package/source/tests/parent-node-mutation-validation.test.ts +362 -0
- package/source/tests/parent-node.test.ts +169 -0
- package/source/tests/selectors.test.ts +533 -22
- package/source/tests/serialization-template.test.ts +232 -0
- package/source/tests/slot-attribute.test.ts +133 -0
- package/source/tests/text-content.test.ts +440 -0
- package/source/tests/toggle-attribute.test.ts +113 -0
- package/source/tests/wide-tree-traversal.test.ts +699 -0
- package/source/tests/window-set-global.test.ts +321 -0
- package/tsconfig.json +1 -1
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import {Window} from '../index.ts';
|
|
2
|
+
|
|
3
|
+
import {describe, it, expect, beforeEach} from 'vitest';
|
|
4
|
+
|
|
5
|
+
function siblingChain(parent: ParentNode) {
|
|
6
|
+
const names: string[] = [];
|
|
7
|
+
for (let node = parent.firstChild; node; node = node.nextSibling) {
|
|
8
|
+
names.push(node.nodeName.toLowerCase());
|
|
9
|
+
}
|
|
10
|
+
return names;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
describe('ChildNode', () => {
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
const window = new Window();
|
|
16
|
+
Window.setGlobalThis(window);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe('replaceWith', () => {
|
|
20
|
+
let first: Element;
|
|
21
|
+
let target: Element;
|
|
22
|
+
let last: Element;
|
|
23
|
+
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
first = document.createElement('first-child');
|
|
26
|
+
target = document.createElement('target-child');
|
|
27
|
+
last = document.createElement('last-child');
|
|
28
|
+
document.body.append(first, target, last);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('replaces a middle child with a single node', () => {
|
|
32
|
+
const replacement = document.createElement('replacement-child');
|
|
33
|
+
|
|
34
|
+
target.replaceWith(replacement);
|
|
35
|
+
|
|
36
|
+
expect(siblingChain(document.body)).toStrictEqual([
|
|
37
|
+
'first-child',
|
|
38
|
+
'replacement-child',
|
|
39
|
+
'last-child',
|
|
40
|
+
]);
|
|
41
|
+
expect(replacement.parentNode).toBe(document.body);
|
|
42
|
+
expect(target.parentNode).toBeNull();
|
|
43
|
+
expect(document.querySelector('replacement-child')).toBe(replacement);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('replaces the last child', () => {
|
|
47
|
+
const replacement = document.createElement('replacement-child');
|
|
48
|
+
|
|
49
|
+
last.replaceWith(replacement);
|
|
50
|
+
|
|
51
|
+
expect(siblingChain(document.body)).toStrictEqual([
|
|
52
|
+
'first-child',
|
|
53
|
+
'target-child',
|
|
54
|
+
'replacement-child',
|
|
55
|
+
]);
|
|
56
|
+
expect(document.body.lastChild).toBe(replacement);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('replaces the first child', () => {
|
|
60
|
+
const replacement = document.createElement('replacement-child');
|
|
61
|
+
|
|
62
|
+
first.replaceWith(replacement);
|
|
63
|
+
|
|
64
|
+
expect(siblingChain(document.body)).toStrictEqual([
|
|
65
|
+
'replacement-child',
|
|
66
|
+
'target-child',
|
|
67
|
+
'last-child',
|
|
68
|
+
]);
|
|
69
|
+
expect(document.body.firstChild).toBe(replacement);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('inserts multiple nodes in argument order', () => {
|
|
73
|
+
target.replaceWith(
|
|
74
|
+
document.createElement('replacement-one'),
|
|
75
|
+
document.createElement('replacement-two'),
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
expect(siblingChain(document.body)).toStrictEqual([
|
|
79
|
+
'first-child',
|
|
80
|
+
'replacement-one',
|
|
81
|
+
'replacement-two',
|
|
82
|
+
'last-child',
|
|
83
|
+
]);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('inserts strings as text nodes', () => {
|
|
87
|
+
target.replaceWith('replacement text');
|
|
88
|
+
|
|
89
|
+
expect(document.body.childNodes[1]!.nodeType).toBe(3);
|
|
90
|
+
expect(document.body.childNodes[1]!.textContent).toBe('replacement text');
|
|
91
|
+
expect(siblingChain(document.body)).toStrictEqual([
|
|
92
|
+
'first-child',
|
|
93
|
+
'#text',
|
|
94
|
+
'last-child',
|
|
95
|
+
]);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('mixes nodes and strings', () => {
|
|
99
|
+
target.replaceWith(
|
|
100
|
+
'before ',
|
|
101
|
+
document.createElement('mixed-child'),
|
|
102
|
+
' after',
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
expect(siblingChain(document.body)).toStrictEqual([
|
|
106
|
+
'first-child',
|
|
107
|
+
'#text',
|
|
108
|
+
'mixed-child',
|
|
109
|
+
'#text',
|
|
110
|
+
'last-child',
|
|
111
|
+
]);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('removes the node when called with no arguments', () => {
|
|
115
|
+
target.replaceWith();
|
|
116
|
+
|
|
117
|
+
expect(siblingChain(document.body)).toStrictEqual([
|
|
118
|
+
'first-child',
|
|
119
|
+
'last-child',
|
|
120
|
+
]);
|
|
121
|
+
expect(target.parentNode).toBeNull();
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('does nothing to a node without a parent', () => {
|
|
125
|
+
const detached = document.createElement('detached-child');
|
|
126
|
+
|
|
127
|
+
expect(() =>
|
|
128
|
+
detached.replaceWith(document.createElement('replacement-child')),
|
|
129
|
+
).not.toThrow();
|
|
130
|
+
expect(detached.parentNode).toBeNull();
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('moves a node that is already in the document', () => {
|
|
134
|
+
target.replaceWith(first);
|
|
135
|
+
|
|
136
|
+
expect(siblingChain(document.body)).toStrictEqual([
|
|
137
|
+
'first-child',
|
|
138
|
+
'last-child',
|
|
139
|
+
]);
|
|
140
|
+
expect(first.parentNode).toBe(document.body);
|
|
141
|
+
expect(target.parentNode).toBeNull();
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it('replaces a node with its own next sibling', () => {
|
|
145
|
+
target.replaceWith(last);
|
|
146
|
+
|
|
147
|
+
expect(siblingChain(document.body)).toStrictEqual([
|
|
148
|
+
'first-child',
|
|
149
|
+
'last-child',
|
|
150
|
+
]);
|
|
151
|
+
expect(target.parentNode).toBeNull();
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('replaces a node with itself', () => {
|
|
155
|
+
target.replaceWith(target);
|
|
156
|
+
|
|
157
|
+
expect(siblingChain(document.body)).toStrictEqual([
|
|
158
|
+
'first-child',
|
|
159
|
+
'target-child',
|
|
160
|
+
'last-child',
|
|
161
|
+
]);
|
|
162
|
+
expect(target.parentNode).toBe(document.body);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('keeps the sibling chain and childNodes in agreement', () => {
|
|
166
|
+
target.replaceWith(
|
|
167
|
+
document.createElement('replacement-one'),
|
|
168
|
+
document.createElement('replacement-two'),
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
expect(siblingChain(document.body)).toStrictEqual(
|
|
172
|
+
Array.from(document.body.childNodes, (node) =>
|
|
173
|
+
node.nodeName.toLowerCase(),
|
|
174
|
+
),
|
|
175
|
+
);
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
});
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
attributeObserversActive,
|
|
5
|
+
characterDataObserversActive,
|
|
6
|
+
childListObserversActive,
|
|
7
|
+
type MutationCallback,
|
|
8
|
+
} from '../MutationObserver.ts';
|
|
9
|
+
import {Window} from '../Window.ts';
|
|
10
|
+
|
|
11
|
+
describe('MutationObserver', () => {
|
|
12
|
+
let window: Window;
|
|
13
|
+
let observers: InstanceType<Window['MutationObserver']>[];
|
|
14
|
+
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
window = new Window();
|
|
17
|
+
observers = [];
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
afterEach(() => {
|
|
21
|
+
for (const observer of observers) observer.disconnect();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
function createObserver(callback: MutationCallback) {
|
|
25
|
+
const observer = new window.MutationObserver(callback);
|
|
26
|
+
observers.push(observer);
|
|
27
|
+
return observer;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
it('only enables tracking for mutation types that are observed', () => {
|
|
31
|
+
const first = createObserver(() => {});
|
|
32
|
+
const second = createObserver(() => {});
|
|
33
|
+
const target = window.document.createElement('div');
|
|
34
|
+
|
|
35
|
+
expect(attributeObserversActive).toBe(false);
|
|
36
|
+
expect(characterDataObserversActive).toBe(false);
|
|
37
|
+
expect(childListObserversActive).toBe(false);
|
|
38
|
+
|
|
39
|
+
first.observe(target, {attributes: true});
|
|
40
|
+
first.observe(target, {attributes: true, attributeOldValue: true});
|
|
41
|
+
expect(attributeObserversActive).toBe(true);
|
|
42
|
+
expect(characterDataObserversActive).toBe(false);
|
|
43
|
+
expect(childListObserversActive).toBe(false);
|
|
44
|
+
|
|
45
|
+
first.observe(target, {childList: true});
|
|
46
|
+
second.observe(target, {characterData: true});
|
|
47
|
+
expect(attributeObserversActive).toBe(false);
|
|
48
|
+
expect(characterDataObserversActive).toBe(true);
|
|
49
|
+
expect(childListObserversActive).toBe(true);
|
|
50
|
+
|
|
51
|
+
first.disconnect();
|
|
52
|
+
expect(characterDataObserversActive).toBe(true);
|
|
53
|
+
expect(childListObserversActive).toBe(false);
|
|
54
|
+
|
|
55
|
+
second.disconnect();
|
|
56
|
+
expect(attributeObserversActive).toBe(false);
|
|
57
|
+
expect(characterDataObserversActive).toBe(false);
|
|
58
|
+
expect(childListObserversActive).toBe(false);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('observes and batches child-list changes', async () => {
|
|
62
|
+
const callback = vi.fn();
|
|
63
|
+
const observer = createObserver(callback);
|
|
64
|
+
const parent = window.document.createElement('div');
|
|
65
|
+
const first = window.document.createElement('span');
|
|
66
|
+
const second = window.document.createElement('span');
|
|
67
|
+
|
|
68
|
+
observer.observe(parent, {childList: true});
|
|
69
|
+
parent.append(first, second);
|
|
70
|
+
parent.removeChild(first);
|
|
71
|
+
|
|
72
|
+
expect(callback).not.toHaveBeenCalled();
|
|
73
|
+
await Promise.resolve();
|
|
74
|
+
|
|
75
|
+
expect(callback).toHaveBeenCalledOnce();
|
|
76
|
+
expect(callback).toHaveBeenCalledWith(
|
|
77
|
+
[
|
|
78
|
+
expect.objectContaining({
|
|
79
|
+
type: 'childList',
|
|
80
|
+
target: parent,
|
|
81
|
+
addedNodes: [first],
|
|
82
|
+
removedNodes: [],
|
|
83
|
+
}),
|
|
84
|
+
expect.objectContaining({
|
|
85
|
+
type: 'childList',
|
|
86
|
+
target: parent,
|
|
87
|
+
addedNodes: [second],
|
|
88
|
+
removedNodes: [],
|
|
89
|
+
previousSibling: first,
|
|
90
|
+
}),
|
|
91
|
+
expect.objectContaining({
|
|
92
|
+
type: 'childList',
|
|
93
|
+
target: parent,
|
|
94
|
+
addedNodes: [],
|
|
95
|
+
removedNodes: [first],
|
|
96
|
+
nextSibling: second,
|
|
97
|
+
}),
|
|
98
|
+
],
|
|
99
|
+
observer,
|
|
100
|
+
);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('observes transactional document-fragment insertion', async () => {
|
|
104
|
+
const callback = vi.fn();
|
|
105
|
+
const observer = createObserver(callback);
|
|
106
|
+
const parent = window.document.createElement('div');
|
|
107
|
+
const fragment = window.document.createDocumentFragment();
|
|
108
|
+
const first = window.document.createElement('span');
|
|
109
|
+
const second = window.document.createElement('span');
|
|
110
|
+
fragment.append(first, second);
|
|
111
|
+
|
|
112
|
+
observer.observe(parent, {childList: true});
|
|
113
|
+
parent.appendChild(fragment);
|
|
114
|
+
await Promise.resolve();
|
|
115
|
+
|
|
116
|
+
expect(callback.mock.calls[0]![0]).toEqual([
|
|
117
|
+
expect.objectContaining({
|
|
118
|
+
type: 'childList',
|
|
119
|
+
target: parent,
|
|
120
|
+
addedNodes: [first],
|
|
121
|
+
removedNodes: [],
|
|
122
|
+
}),
|
|
123
|
+
expect.objectContaining({
|
|
124
|
+
type: 'childList',
|
|
125
|
+
target: parent,
|
|
126
|
+
addedNodes: [second],
|
|
127
|
+
removedNodes: [],
|
|
128
|
+
previousSibling: first,
|
|
129
|
+
}),
|
|
130
|
+
]);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('observes both sides of a transactional node move', async () => {
|
|
134
|
+
const callback = vi.fn();
|
|
135
|
+
const observer = createObserver(callback);
|
|
136
|
+
const source = window.document.createElement('div');
|
|
137
|
+
const destination = window.document.createElement('div');
|
|
138
|
+
const child = window.document.createElement('span');
|
|
139
|
+
source.appendChild(child);
|
|
140
|
+
|
|
141
|
+
observer.observe(source, {childList: true});
|
|
142
|
+
observer.observe(destination, {childList: true});
|
|
143
|
+
destination.appendChild(child);
|
|
144
|
+
await Promise.resolve();
|
|
145
|
+
|
|
146
|
+
expect(callback.mock.calls[0]![0]).toEqual([
|
|
147
|
+
expect.objectContaining({
|
|
148
|
+
type: 'childList',
|
|
149
|
+
target: source,
|
|
150
|
+
addedNodes: [],
|
|
151
|
+
removedNodes: [child],
|
|
152
|
+
}),
|
|
153
|
+
expect.objectContaining({
|
|
154
|
+
type: 'childList',
|
|
155
|
+
target: destination,
|
|
156
|
+
addedNodes: [child],
|
|
157
|
+
removedNodes: [],
|
|
158
|
+
}),
|
|
159
|
+
]);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('observes transactional node replacement', async () => {
|
|
163
|
+
const callback = vi.fn();
|
|
164
|
+
const observer = createObserver(callback);
|
|
165
|
+
const parent = window.document.createElement('div');
|
|
166
|
+
const oldChild = window.document.createElement('span');
|
|
167
|
+
const newChild = window.document.createElement('strong');
|
|
168
|
+
parent.appendChild(oldChild);
|
|
169
|
+
|
|
170
|
+
observer.observe(parent, {childList: true});
|
|
171
|
+
parent.replaceChild(newChild, oldChild);
|
|
172
|
+
await Promise.resolve();
|
|
173
|
+
|
|
174
|
+
expect(callback.mock.calls[0]![0]).toEqual([
|
|
175
|
+
expect.objectContaining({
|
|
176
|
+
type: 'childList',
|
|
177
|
+
target: parent,
|
|
178
|
+
addedNodes: [],
|
|
179
|
+
removedNodes: [oldChild],
|
|
180
|
+
}),
|
|
181
|
+
expect.objectContaining({
|
|
182
|
+
type: 'childList',
|
|
183
|
+
target: parent,
|
|
184
|
+
addedNodes: [newChild],
|
|
185
|
+
removedNodes: [],
|
|
186
|
+
}),
|
|
187
|
+
]);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it('does not observe a rejected transactional insertion', async () => {
|
|
191
|
+
const callback = vi.fn();
|
|
192
|
+
const observer = createObserver(callback);
|
|
193
|
+
const parent = window.document.createElement('div');
|
|
194
|
+
const child = window.document.createElement('span');
|
|
195
|
+
parent.appendChild(child);
|
|
196
|
+
observer.observe(parent, {childList: true, subtree: true});
|
|
197
|
+
|
|
198
|
+
expect(() => child.appendChild(parent)).toThrow();
|
|
199
|
+
await Promise.resolve();
|
|
200
|
+
|
|
201
|
+
expect(callback).not.toHaveBeenCalled();
|
|
202
|
+
expect(parent.firstChild).toBe(child);
|
|
203
|
+
expect(child.parentNode).toBe(parent);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('observes filtered attributes in a subtree with old values', async () => {
|
|
207
|
+
const callback = vi.fn();
|
|
208
|
+
const observer = createObserver(callback);
|
|
209
|
+
const parent = window.document.createElement('div');
|
|
210
|
+
const child = window.document.createElement('span');
|
|
211
|
+
parent.appendChild(child);
|
|
212
|
+
|
|
213
|
+
observer.observe(parent, {
|
|
214
|
+
attributes: true,
|
|
215
|
+
attributeFilter: ['data-state'],
|
|
216
|
+
attributeOldValue: true,
|
|
217
|
+
subtree: true,
|
|
218
|
+
});
|
|
219
|
+
child.setAttribute('data-state', 'loading');
|
|
220
|
+
child.setAttribute('data-state', 'ready');
|
|
221
|
+
child.setAttribute('ignored', 'value');
|
|
222
|
+
|
|
223
|
+
await Promise.resolve();
|
|
224
|
+
|
|
225
|
+
expect(callback).toHaveBeenCalledWith(
|
|
226
|
+
[
|
|
227
|
+
expect.objectContaining({
|
|
228
|
+
type: 'attributes',
|
|
229
|
+
target: child,
|
|
230
|
+
attributeName: 'data-state',
|
|
231
|
+
oldValue: null,
|
|
232
|
+
}),
|
|
233
|
+
expect.objectContaining({
|
|
234
|
+
type: 'attributes',
|
|
235
|
+
target: child,
|
|
236
|
+
attributeName: 'data-state',
|
|
237
|
+
oldValue: 'loading',
|
|
238
|
+
}),
|
|
239
|
+
],
|
|
240
|
+
observer,
|
|
241
|
+
);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('observes character data and only exposes old values when requested', async () => {
|
|
245
|
+
const withOldValue = vi.fn();
|
|
246
|
+
const withoutOldValue = vi.fn();
|
|
247
|
+
const text = window.document.createTextNode('before');
|
|
248
|
+
|
|
249
|
+
createObserver(withOldValue).observe(text, {
|
|
250
|
+
characterData: true,
|
|
251
|
+
characterDataOldValue: true,
|
|
252
|
+
});
|
|
253
|
+
createObserver(withoutOldValue).observe(text, {
|
|
254
|
+
characterData: true,
|
|
255
|
+
});
|
|
256
|
+
text.data = 'after';
|
|
257
|
+
|
|
258
|
+
await Promise.resolve();
|
|
259
|
+
|
|
260
|
+
expect(withOldValue.mock.calls[0]![0][0].oldValue).toBe('before');
|
|
261
|
+
expect(withoutOldValue.mock.calls[0]![0][0].oldValue).toBeNull();
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it('supports takeRecords() and disconnect()', async () => {
|
|
265
|
+
const callback = vi.fn();
|
|
266
|
+
const observer = createObserver(callback);
|
|
267
|
+
const parent = window.document.createElement('div');
|
|
268
|
+
|
|
269
|
+
observer.observe(parent, {childList: true});
|
|
270
|
+
parent.append('first');
|
|
271
|
+
|
|
272
|
+
expect(observer.takeRecords()).toHaveLength(1);
|
|
273
|
+
|
|
274
|
+
parent.append('second');
|
|
275
|
+
observer.disconnect();
|
|
276
|
+
parent.append('third');
|
|
277
|
+
await Promise.resolve();
|
|
278
|
+
|
|
279
|
+
expect(callback).not.toHaveBeenCalled();
|
|
280
|
+
expect(observer.takeRecords()).toEqual([]);
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it('validates the callback and observation options', () => {
|
|
284
|
+
expect(() => new window.MutationObserver(null as any)).toThrow(TypeError);
|
|
285
|
+
|
|
286
|
+
const observer = createObserver(() => {});
|
|
287
|
+
const target = window.document.createElement('div');
|
|
288
|
+
|
|
289
|
+
expect(() => observer.observe(target)).toThrow(TypeError);
|
|
290
|
+
expect(() =>
|
|
291
|
+
observer.observe(target, {
|
|
292
|
+
attributes: false,
|
|
293
|
+
attributeOldValue: true,
|
|
294
|
+
}),
|
|
295
|
+
).toThrow(TypeError);
|
|
296
|
+
expect(() =>
|
|
297
|
+
observer.observe(target, {
|
|
298
|
+
characterData: false,
|
|
299
|
+
characterDataOldValue: true,
|
|
300
|
+
}),
|
|
301
|
+
).toThrow(TypeError);
|
|
302
|
+
});
|
|
303
|
+
});
|