@remote-dom/polyfill 1.3.1 → 1.4.1

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 (50) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/build/cjs/Attr.cjs +0 -1
  3. package/build/cjs/Document.cjs +2 -0
  4. package/build/cjs/EventTarget.cjs +4 -1
  5. package/build/cjs/Node.cjs +11 -7
  6. package/build/cjs/ParentNode.cjs +22 -4
  7. package/build/cjs/Window.cjs +8 -22
  8. package/build/cjs/constants.cjs +2 -0
  9. package/build/cjs/shared.cjs +20 -0
  10. package/build/esm/Attr.mjs +0 -1
  11. package/build/esm/Document.mjs +3 -1
  12. package/build/esm/EventTarget.mjs +4 -1
  13. package/build/esm/Node.mjs +13 -9
  14. package/build/esm/ParentNode.mjs +23 -5
  15. package/build/esm/Window.mjs +8 -22
  16. package/build/esm/constants.mjs +2 -1
  17. package/build/esm/shared.mjs +20 -2
  18. package/build/esnext/Attr.esnext +0 -1
  19. package/build/esnext/Document.esnext +3 -1
  20. package/build/esnext/EventTarget.esnext +3 -3
  21. package/build/esnext/Node.esnext +14 -9
  22. package/build/esnext/ParentNode.esnext +23 -5
  23. package/build/esnext/Window.esnext +10 -19
  24. package/build/esnext/constants.esnext +2 -1
  25. package/build/esnext/shared.esnext +20 -2
  26. package/build/tsconfig.tsbuildinfo +1 -1
  27. package/build/typescript/Attr.d.ts +0 -1
  28. package/build/typescript/Attr.d.ts.map +1 -1
  29. package/build/typescript/Document.d.ts +2 -1
  30. package/build/typescript/Document.d.ts.map +1 -1
  31. package/build/typescript/EventTarget.d.ts +3 -3
  32. package/build/typescript/EventTarget.d.ts.map +1 -1
  33. package/build/typescript/Node.d.ts +5 -1
  34. package/build/typescript/Node.d.ts.map +1 -1
  35. package/build/typescript/ParentNode.d.ts.map +1 -1
  36. package/build/typescript/Window.d.ts +1 -0
  37. package/build/typescript/Window.d.ts.map +1 -1
  38. package/build/typescript/constants.d.ts +1 -0
  39. package/build/typescript/constants.d.ts.map +1 -1
  40. package/build/typescript/shared.d.ts +2 -0
  41. package/build/typescript/shared.d.ts.map +1 -1
  42. package/package.json +1 -1
  43. package/source/Attr.ts +0 -1
  44. package/source/Document.ts +3 -0
  45. package/source/EventTarget.ts +9 -3
  46. package/source/Node.ts +18 -7
  47. package/source/ParentNode.ts +26 -4
  48. package/source/Window.ts +12 -25
  49. package/source/constants.ts +1 -0
  50. package/source/shared.ts +28 -1
package/source/Node.ts CHANGED
@@ -8,6 +8,7 @@ import {
8
8
  NamespaceURI,
9
9
  NodeType,
10
10
  HOOKS,
11
+ IS_CONNECTED,
11
12
  } from './constants.ts';
12
13
  import type {Document} from './Document.ts';
13
14
  import type {ParentNode} from './ParentNode.ts';
@@ -17,6 +18,7 @@ import {
17
18
  isParentNode,
18
19
  isTextNode,
19
20
  cloneNode,
21
+ descendants,
20
22
  } from './shared.ts';
21
23
 
22
24
  export class Node extends EventTarget {
@@ -28,6 +30,7 @@ export class Node extends EventTarget {
28
30
  [CHILD]: Node | null = null;
29
31
  [PREV]: Node | null = null;
30
32
  [NEXT]: Node | null = null;
33
+ [IS_CONNECTED] = false;
31
34
 
32
35
  protected get [HOOKS]() {
33
36
  return this[OWNER_DOCUMENT].defaultView[HOOKS];
@@ -45,6 +48,10 @@ export class Node extends EventTarget {
45
48
  return this[OWNER_DOCUMENT];
46
49
  }
47
50
 
51
+ get isConnected() {
52
+ return this[IS_CONNECTED];
53
+ }
54
+
48
55
  isDefaultNamespace(namespace: string) {
49
56
  return namespace === NamespaceURI.XHTML;
50
57
  }
@@ -55,6 +62,14 @@ export class Node extends EventTarget {
55
62
 
56
63
  set parentNode(_readonly) {}
57
64
 
65
+ get parentElement(): ParentNode | null {
66
+ const parent = this[PARENT];
67
+ if (!parent || parent.nodeType !== 1) return null;
68
+ return parent;
69
+ }
70
+
71
+ set parentElement(_readonly) {}
72
+
58
73
  get previousSibling() {
59
74
  return this[PREV];
60
75
  }
@@ -113,17 +128,13 @@ export class Node extends EventTarget {
113
128
  get textContent(): string | null {
114
129
  if (isCharacterData(this)) return this.data;
115
130
  let text = '';
116
- function walk(node: Node) {
131
+
132
+ for (const node of descendants(this)) {
117
133
  if (isTextNode(node)) {
118
134
  text += node.data;
119
135
  }
120
- const child = node[CHILD];
121
- if (child) walk(child);
122
- const sibling = node[NEXT];
123
- if (sibling) walk(sibling);
124
136
  }
125
- const child = this[CHILD];
126
- if (child) walk(child);
137
+
127
138
  return text;
128
139
  }
129
140
 
@@ -6,11 +6,13 @@ import {
6
6
  OWNER_DOCUMENT,
7
7
  NodeType,
8
8
  HOOKS,
9
+ IS_CONNECTED,
9
10
  } from './constants.ts';
10
11
  import type {Node} from './Node.ts';
11
12
  import {ChildNode, toNode} from './ChildNode.ts';
12
13
  import {NodeList} from './NodeList.ts';
13
14
  import {querySelectorAll, querySelector} from './selectors.ts';
15
+ import {selfAndDescendants} from './shared.ts';
14
16
 
15
17
  export class ParentNode extends ChildNode {
16
18
  readonly childNodes = new NodeList();
@@ -66,8 +68,20 @@ export class ParentNode extends ChildNode {
66
68
  children.splice(children.indexOf(child), 1);
67
69
  }
68
70
 
69
- (child as any).disconnectedCallback?.();
70
- this[HOOKS].removeChild?.(this as any, child as any, childNodesIndex);
71
+ child[PARENT] = null;
72
+ child[NEXT] = null;
73
+ child[PREV] = null;
74
+
75
+ if (this[IS_CONNECTED]) {
76
+ for (const node of selfAndDescendants(child)) {
77
+ node[IS_CONNECTED] = false;
78
+ (node as any).disconnectedCallback?.();
79
+ }
80
+ }
81
+
82
+ if (this.nodeType === NodeType.ELEMENT_NODE) {
83
+ this[HOOKS].removeChild?.(this as any, child as any, childNodesIndex);
84
+ }
71
85
  }
72
86
 
73
87
  replaceChild(newChild: Node, oldChild: Node) {
@@ -154,7 +168,15 @@ export class ParentNode extends ChildNode {
154
168
  if (isElement) this.children.push(child);
155
169
  }
156
170
 
157
- (child as any).connectedCallback?.();
158
- this[HOOKS].insertChild?.(this as any, child as any, insertIndex);
171
+ if (this[IS_CONNECTED]) {
172
+ for (const node of selfAndDescendants(child)) {
173
+ node[IS_CONNECTED] = true;
174
+ (node as any).connectedCallback?.();
175
+ }
176
+ }
177
+
178
+ if (this.nodeType === NodeType.ELEMENT_NODE) {
179
+ this[HOOKS].insertChild?.(this as any, child as any, insertIndex);
180
+ }
159
181
  }
160
182
  }
package/source/Window.ts CHANGED
@@ -21,6 +21,7 @@ import type {Hooks} from './hooks.ts';
21
21
  export class Window extends EventTarget {
22
22
  [HOOKS]: Partial<Hooks> = {};
23
23
  name = '';
24
+ window = this;
24
25
  parent = this;
25
26
  self = this;
26
27
  top = this;
@@ -46,32 +47,18 @@ export class Window extends EventTarget {
46
47
  MutationObserver = MutationObserver;
47
48
 
48
49
  static setGlobal(window: Window) {
49
- const properties = Object.getOwnPropertyDescriptors(window);
50
-
51
- delete (properties as any).self;
50
+ for (const property in window) {
51
+ if ((window as any)[property] === window) {
52
+ (window as any)[property] = globalThis;
53
+ }
54
+ }
52
55
 
53
- Object.defineProperties(globalThis, {
54
- ...properties,
55
- window: {
56
- value: window,
57
- configurable: true,
58
- writable: true,
59
- enumerable: true,
60
- },
61
- });
56
+ const eventTargetPrototypeProperties = Object.getOwnPropertyDescriptors(
57
+ EventTarget.prototype,
58
+ );
59
+ const properties = Object.getOwnPropertyDescriptors(window);
62
60
 
63
- if (typeof self === 'undefined') {
64
- Object.defineProperty(globalThis, 'self', {
65
- value: window,
66
- configurable: true,
67
- writable: true,
68
- enumerable: true,
69
- });
70
- } else {
71
- // There can already be a `self`, like when polyfilling the DOM
72
- // in a Web Worker. In those cases, just mirror all the `Window`
73
- // properties onto `self`, rather than wholly redefining it.
74
- Object.defineProperties(self, properties);
75
- }
61
+ Object.defineProperties(globalThis, eventTargetPrototypeProperties);
62
+ Object.defineProperties(globalThis, properties);
76
63
  }
77
64
  }
@@ -16,6 +16,7 @@ export const PATH = Symbol('path');
16
16
  export const STOP_IMMEDIATE_PROPAGATION = Symbol('stop_immediate_propagation');
17
17
  export const CONTENT = Symbol('content');
18
18
  export const HOOKS = Symbol('hooks');
19
+ export const IS_CONNECTED = Symbol('is_connected');
19
20
 
20
21
  // @TODO remove explicit values
21
22
  export const enum NodeType {
package/source/shared.ts CHANGED
@@ -1,4 +1,11 @@
1
- import {DATA, OWNER_DOCUMENT, ATTRIBUTES, NodeType} from './constants.ts';
1
+ import {
2
+ DATA,
3
+ OWNER_DOCUMENT,
4
+ ATTRIBUTES,
5
+ NodeType,
6
+ CHILD,
7
+ NEXT,
8
+ } from './constants.ts';
2
9
  import type {Document} from './Document.ts';
3
10
  import type {DocumentFragment} from './DocumentFragment.ts';
4
11
  import type {Node} from './Node.ts';
@@ -78,3 +85,23 @@ export function cloneNode(
78
85
  return cloned;
79
86
  }
80
87
  }
88
+
89
+ export function descendants(node: Node) {
90
+ const nodes: Node[] = [];
91
+ const walk = (node: Node) => {
92
+ nodes.push(node);
93
+ const child = node[CHILD];
94
+ if (child) walk(child);
95
+ const sibling = node[NEXT];
96
+ if (sibling) walk(sibling);
97
+ };
98
+ const child = node[CHILD];
99
+ if (child) walk(child);
100
+ return nodes;
101
+ }
102
+
103
+ export function selfAndDescendants(node: Node) {
104
+ const nodes = descendants(node);
105
+ nodes.unshift(node);
106
+ return nodes;
107
+ }