@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,321 @@
|
|
|
1
|
+
import {afterEach, describe, expect, it, vi} from 'vitest';
|
|
2
|
+
|
|
3
|
+
import {HOOKS} from '../constants.ts';
|
|
4
|
+
import {ErrorEvent} from '../ErrorEvent.ts';
|
|
5
|
+
import {Event} from '../Event.ts';
|
|
6
|
+
import {PromiseRejectionEvent} from '../PromiseRejectionEvent.ts';
|
|
7
|
+
import {Window} from '../Window.ts';
|
|
8
|
+
|
|
9
|
+
const originalGlobalProperties = Object.getOwnPropertyDescriptors(globalThis);
|
|
10
|
+
|
|
11
|
+
afterEach(() => {
|
|
12
|
+
for (const property of Reflect.ownKeys(globalThis)) {
|
|
13
|
+
if (
|
|
14
|
+
!Object.prototype.hasOwnProperty.call(originalGlobalProperties, property)
|
|
15
|
+
) {
|
|
16
|
+
Reflect.deleteProperty(globalThis, property);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
Object.defineProperties(globalThis, originalGlobalProperties);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
function expectCompleteEventTarget(target: any, window: Window) {
|
|
24
|
+
const listener = vi.fn();
|
|
25
|
+
target.addEventListener('test', listener);
|
|
26
|
+
target.dispatchEvent(new Event('test'));
|
|
27
|
+
expect(listener).toHaveBeenCalledOnce();
|
|
28
|
+
|
|
29
|
+
target.removeEventListener('test', listener);
|
|
30
|
+
target.dispatchEvent(new Event('test'));
|
|
31
|
+
expect(listener).toHaveBeenCalledOnce();
|
|
32
|
+
|
|
33
|
+
const errorHandler = vi.fn();
|
|
34
|
+
target.onerror = errorHandler;
|
|
35
|
+
expect(target.onerror).toBe(errorHandler);
|
|
36
|
+
expect(window.onerror).toBe(errorHandler);
|
|
37
|
+
|
|
38
|
+
target.dispatchEvent(new ErrorEvent('error', {message: 'test'}));
|
|
39
|
+
expect(errorHandler).toHaveBeenCalledOnce();
|
|
40
|
+
|
|
41
|
+
const rejectionHandler = vi.fn();
|
|
42
|
+
target.onunhandledrejection = rejectionHandler;
|
|
43
|
+
expect(target.onunhandledrejection).toBe(rejectionHandler);
|
|
44
|
+
expect(window.onunhandledrejection).toBe(rejectionHandler);
|
|
45
|
+
|
|
46
|
+
target.dispatchEvent(
|
|
47
|
+
new PromiseRejectionEvent('unhandledrejection', {
|
|
48
|
+
promise: Promise.resolve(),
|
|
49
|
+
reason: new Error('test'),
|
|
50
|
+
}),
|
|
51
|
+
);
|
|
52
|
+
expect(rejectionHandler).toHaveBeenCalledOnce();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
describe('Window global installation', () => {
|
|
56
|
+
describe('setGlobal()', () => {
|
|
57
|
+
it('installs and reinstalls all globals when self does not exist', () => {
|
|
58
|
+
delete (globalThis as any).self;
|
|
59
|
+
|
|
60
|
+
const firstWindow = new Window();
|
|
61
|
+
const firstDocument = firstWindow.document;
|
|
62
|
+
Window.setGlobal(firstWindow);
|
|
63
|
+
|
|
64
|
+
expect(globalThis.window).toBe(firstWindow);
|
|
65
|
+
expect(globalThis.self).toBe(firstWindow);
|
|
66
|
+
expectCompleteEventTarget(globalThis, firstWindow);
|
|
67
|
+
|
|
68
|
+
const secondWindow = new Window();
|
|
69
|
+
Window.setGlobal(secondWindow);
|
|
70
|
+
|
|
71
|
+
expect(globalThis.window).toBe(secondWindow);
|
|
72
|
+
expect(globalThis.self).toBe(secondWindow);
|
|
73
|
+
expect(globalThis.document).toBe(secondWindow.document);
|
|
74
|
+
expectCompleteEventTarget(globalThis, secondWindow);
|
|
75
|
+
|
|
76
|
+
expect(firstWindow.window).toBe(firstWindow);
|
|
77
|
+
expect(firstWindow.self).toBe(firstWindow);
|
|
78
|
+
expect(firstWindow.document).toBe(firstDocument);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('preserves and updates an existing self across installations', () => {
|
|
82
|
+
const existingSelf = {} as any;
|
|
83
|
+
Object.defineProperty(globalThis, 'self', {
|
|
84
|
+
configurable: true,
|
|
85
|
+
value: existingSelf,
|
|
86
|
+
writable: true,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const firstWindow = new Window();
|
|
90
|
+
Window.setGlobal(firstWindow);
|
|
91
|
+
|
|
92
|
+
expect(globalThis.self).toBe(existingSelf);
|
|
93
|
+
expect(existingSelf.window).toBe(firstWindow);
|
|
94
|
+
expect(existingSelf.document).toBe(firstWindow.document);
|
|
95
|
+
expectCompleteEventTarget(existingSelf, firstWindow);
|
|
96
|
+
|
|
97
|
+
const secondWindow = new Window();
|
|
98
|
+
Window.setGlobal(secondWindow);
|
|
99
|
+
|
|
100
|
+
expect(globalThis.self).toBe(existingSelf);
|
|
101
|
+
expect(existingSelf.window).toBe(secondWindow);
|
|
102
|
+
expect(existingSelf.document).toBe(secondWindow.document);
|
|
103
|
+
expectCompleteEventTarget(existingSelf, secondWindow);
|
|
104
|
+
expectCompleteEventTarget(globalThis, secondWindow);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('preserves native event properties on global and worker targets', () => {
|
|
108
|
+
const globalAddEventListener = vi.fn();
|
|
109
|
+
const globalRemoveEventListener = vi.fn();
|
|
110
|
+
const globalDispatchEvent = vi.fn();
|
|
111
|
+
let globalOnerror: unknown = null;
|
|
112
|
+
let globalOnunhandledrejection: unknown = null;
|
|
113
|
+
|
|
114
|
+
Object.defineProperties(globalThis, {
|
|
115
|
+
addEventListener: {
|
|
116
|
+
configurable: true,
|
|
117
|
+
value: globalAddEventListener,
|
|
118
|
+
},
|
|
119
|
+
removeEventListener: {
|
|
120
|
+
configurable: true,
|
|
121
|
+
value: globalRemoveEventListener,
|
|
122
|
+
},
|
|
123
|
+
dispatchEvent: {configurable: true, value: globalDispatchEvent},
|
|
124
|
+
onerror: {
|
|
125
|
+
configurable: true,
|
|
126
|
+
get: () => globalOnerror,
|
|
127
|
+
set: (handler) => {
|
|
128
|
+
globalOnerror = handler;
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
onunhandledrejection: {
|
|
132
|
+
configurable: true,
|
|
133
|
+
get: () => globalOnunhandledrejection,
|
|
134
|
+
set: (handler) => {
|
|
135
|
+
globalOnunhandledrejection = handler;
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
const workerAddEventListener = vi.fn();
|
|
141
|
+
const workerRemoveEventListener = vi.fn();
|
|
142
|
+
const workerDispatchEvent = vi.fn();
|
|
143
|
+
let workerOnerror: unknown = null;
|
|
144
|
+
let workerOnunhandledrejection: unknown = null;
|
|
145
|
+
const workerPrototype = {};
|
|
146
|
+
|
|
147
|
+
Object.defineProperties(workerPrototype, {
|
|
148
|
+
addEventListener: {
|
|
149
|
+
configurable: true,
|
|
150
|
+
value: workerAddEventListener,
|
|
151
|
+
},
|
|
152
|
+
removeEventListener: {
|
|
153
|
+
configurable: true,
|
|
154
|
+
value: workerRemoveEventListener,
|
|
155
|
+
},
|
|
156
|
+
dispatchEvent: {configurable: true, value: workerDispatchEvent},
|
|
157
|
+
onerror: {
|
|
158
|
+
configurable: true,
|
|
159
|
+
get: () => workerOnerror,
|
|
160
|
+
set: (handler) => {
|
|
161
|
+
workerOnerror = handler;
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
onunhandledrejection: {
|
|
165
|
+
configurable: true,
|
|
166
|
+
get: () => workerOnunhandledrejection,
|
|
167
|
+
set: (handler) => {
|
|
168
|
+
workerOnunhandledrejection = handler;
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
const workerSelf = Object.create(workerPrototype);
|
|
174
|
+
Object.defineProperty(globalThis, 'self', {
|
|
175
|
+
configurable: true,
|
|
176
|
+
value: workerSelf,
|
|
177
|
+
writable: true,
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
const firstWindow = new Window();
|
|
181
|
+
Window.setGlobal(firstWindow);
|
|
182
|
+
|
|
183
|
+
expect(globalThis.addEventListener).toBe(globalAddEventListener);
|
|
184
|
+
expect(globalThis.removeEventListener).toBe(globalRemoveEventListener);
|
|
185
|
+
expect(globalThis.dispatchEvent).toBe(globalDispatchEvent);
|
|
186
|
+
expect(workerSelf.addEventListener).toBe(workerAddEventListener);
|
|
187
|
+
expect(workerSelf.removeEventListener).toBe(workerRemoveEventListener);
|
|
188
|
+
expect(workerSelf.dispatchEvent).toBe(workerDispatchEvent);
|
|
189
|
+
|
|
190
|
+
for (const property of [
|
|
191
|
+
'addEventListener',
|
|
192
|
+
'removeEventListener',
|
|
193
|
+
'dispatchEvent',
|
|
194
|
+
'onerror',
|
|
195
|
+
'onunhandledrejection',
|
|
196
|
+
]) {
|
|
197
|
+
expect(Object.hasOwn(workerSelf, property)).toBe(false);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const globalListener = vi.fn();
|
|
201
|
+
globalThis.addEventListener('message', globalListener);
|
|
202
|
+
globalThis.removeEventListener('message', globalListener);
|
|
203
|
+
(globalThis.dispatchEvent as any)(new Event('message'));
|
|
204
|
+
expect(globalAddEventListener).toHaveBeenCalledWith(
|
|
205
|
+
'message',
|
|
206
|
+
globalListener,
|
|
207
|
+
);
|
|
208
|
+
expect(globalRemoveEventListener).toHaveBeenCalledWith(
|
|
209
|
+
'message',
|
|
210
|
+
globalListener,
|
|
211
|
+
);
|
|
212
|
+
expect(globalDispatchEvent).toHaveBeenCalledOnce();
|
|
213
|
+
|
|
214
|
+
const workerListener = vi.fn();
|
|
215
|
+
workerSelf.addEventListener('message', workerListener);
|
|
216
|
+
workerSelf.removeEventListener('message', workerListener);
|
|
217
|
+
workerSelf.dispatchEvent(new Event('message'));
|
|
218
|
+
expect(workerAddEventListener).toHaveBeenCalledWith(
|
|
219
|
+
'message',
|
|
220
|
+
workerListener,
|
|
221
|
+
);
|
|
222
|
+
expect(workerRemoveEventListener).toHaveBeenCalledWith(
|
|
223
|
+
'message',
|
|
224
|
+
workerListener,
|
|
225
|
+
);
|
|
226
|
+
expect(workerDispatchEvent).toHaveBeenCalledOnce();
|
|
227
|
+
|
|
228
|
+
const globalErrorHandler = vi.fn();
|
|
229
|
+
const workerErrorHandler = vi.fn();
|
|
230
|
+
(globalThis as any).onerror = globalErrorHandler;
|
|
231
|
+
workerSelf.onerror = workerErrorHandler;
|
|
232
|
+
expect(globalOnerror).toBe(globalErrorHandler);
|
|
233
|
+
expect(workerOnerror).toBe(workerErrorHandler);
|
|
234
|
+
expect(firstWindow.onerror).toBe(null);
|
|
235
|
+
|
|
236
|
+
const globalRejectionHandler = vi.fn();
|
|
237
|
+
const workerRejectionHandler = vi.fn();
|
|
238
|
+
(globalThis as any).onunhandledrejection = globalRejectionHandler;
|
|
239
|
+
workerSelf.onunhandledrejection = workerRejectionHandler;
|
|
240
|
+
expect(globalOnunhandledrejection).toBe(globalRejectionHandler);
|
|
241
|
+
expect(workerOnunhandledrejection).toBe(workerRejectionHandler);
|
|
242
|
+
expect(firstWindow.onunhandledrejection).toBe(null);
|
|
243
|
+
|
|
244
|
+
const secondWindow = new Window();
|
|
245
|
+
Window.setGlobal(secondWindow);
|
|
246
|
+
|
|
247
|
+
expect(globalThis.addEventListener).toBe(globalAddEventListener);
|
|
248
|
+
expect(workerSelf.addEventListener).toBe(workerAddEventListener);
|
|
249
|
+
expect(workerSelf.document).toBe(secondWindow.document);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it('reinstalls globals from a distinct Window module copy', async () => {
|
|
253
|
+
delete (globalThis as any).self;
|
|
254
|
+
vi.resetModules();
|
|
255
|
+
const {Window: FirstWindow} = await import('../Window.ts');
|
|
256
|
+
vi.resetModules();
|
|
257
|
+
const {Window: SecondWindow} = await import('../Window.ts');
|
|
258
|
+
|
|
259
|
+
expect(SecondWindow).not.toBe(FirstWindow);
|
|
260
|
+
|
|
261
|
+
const firstWindow = new FirstWindow();
|
|
262
|
+
const firstDocument = firstWindow.document;
|
|
263
|
+
FirstWindow.setGlobal(firstWindow);
|
|
264
|
+
|
|
265
|
+
const secondWindow = new SecondWindow();
|
|
266
|
+
SecondWindow.setGlobal(secondWindow);
|
|
267
|
+
|
|
268
|
+
expect(globalThis.window).toBe(secondWindow);
|
|
269
|
+
expect(globalThis.self).toBe(secondWindow);
|
|
270
|
+
expect(globalThis.document).toBe(secondWindow.document);
|
|
271
|
+
expect(firstWindow.window).toBe(firstWindow);
|
|
272
|
+
expect(firstWindow.self).toBe(firstWindow);
|
|
273
|
+
expect(firstWindow.document).toBe(firstDocument);
|
|
274
|
+
|
|
275
|
+
const listener = vi.fn();
|
|
276
|
+
globalThis.addEventListener('test', listener);
|
|
277
|
+
secondWindow.dispatchEvent(new secondWindow.Event('test'));
|
|
278
|
+
expect(listener).toHaveBeenCalledOnce();
|
|
279
|
+
|
|
280
|
+
const errorHandler = vi.fn();
|
|
281
|
+
globalThis.onerror = errorHandler;
|
|
282
|
+
expect(secondWindow.onerror).toBe(errorHandler);
|
|
283
|
+
expect(firstWindow.onerror).toBe(null);
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
describe('setGlobalThis()', () => {
|
|
288
|
+
it('installs and reinstalls the same complete global surface', () => {
|
|
289
|
+
const existingSelf = {};
|
|
290
|
+
Object.defineProperty(globalThis, 'self', {
|
|
291
|
+
configurable: true,
|
|
292
|
+
value: existingSelf,
|
|
293
|
+
writable: true,
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
const firstWindow = new Window();
|
|
297
|
+
Window.setGlobalThis(firstWindow);
|
|
298
|
+
|
|
299
|
+
expect(globalThis.window).toBe(globalThis);
|
|
300
|
+
expect(globalThis.self).toBe(globalThis);
|
|
301
|
+
expectCompleteEventTarget(globalThis, firstWindow);
|
|
302
|
+
|
|
303
|
+
const secondWindow = new Window();
|
|
304
|
+
Window.setGlobalThis(secondWindow);
|
|
305
|
+
|
|
306
|
+
expect(globalThis.window).toBe(globalThis);
|
|
307
|
+
expect(globalThis.self).toBe(globalThis);
|
|
308
|
+
expect(globalThis.document).toBe(secondWindow.document);
|
|
309
|
+
expectCompleteEventTarget(globalThis, secondWindow);
|
|
310
|
+
});
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
it('installs symbol-keyed Window state', () => {
|
|
314
|
+
Window.setGlobal(new Window());
|
|
315
|
+
expect(Object.hasOwn(globalThis, HOOKS)).toBe(true);
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it('cleans symbol-keyed Window state between tests', () => {
|
|
319
|
+
expect(Object.hasOwn(globalThis, HOOKS)).toBe(false);
|
|
320
|
+
});
|
|
321
|
+
});
|
package/tsconfig.json
CHANGED