Skip to content

Commit

Permalink
refactor: use OWNER_DOCUMENT symbol consistently to access the HOOKS
Browse files Browse the repository at this point in the history
  • Loading branch information
olavoasantos committed Aug 8, 2024
1 parent b58dd29 commit 6989327
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
3 changes: 2 additions & 1 deletion packages/polyfill/source/Attr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
NamespaceURI,
NodeType,
HOOKS,
OWNER_DOCUMENT,
} from './constants.ts';
import {Node} from './Node.ts';
import type {Element} from './Element.ts';
Expand Down Expand Up @@ -45,7 +46,7 @@ export class Attr extends Node {
this[VALUE] = str;
const ownerElement = this[OWNER_ELEMENT];
if (!ownerElement) return;
this.ownerElement?.ownerDocument.defaultView[HOOKS].setAttribute?.(
this[OWNER_DOCUMENT].defaultView[HOOKS].setAttribute?.(
ownerElement as any,
this[NAME],
str,
Expand Down
12 changes: 8 additions & 4 deletions packages/polyfill/source/CharacterData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {DATA, HOOKS} from './constants.ts';
import {DATA, HOOKS, OWNER_DOCUMENT} from './constants.ts';
import {ChildNode} from './ChildNode.ts';

export class CharacterData extends ChildNode {
Expand All @@ -15,9 +15,13 @@ export class CharacterData extends ChildNode {
str = typeof data === 'string' ? data : String(data);
}
this[DATA] = str;
setTimeout(() =>
this.ownerDocument.defaultView[HOOKS].setText?.(this as any, str),
);
if (this[OWNER_DOCUMENT]) {
this[OWNER_DOCUMENT].defaultView[HOOKS].setText?.(this as any, str);
} else {
setTimeout(() =>
this[OWNER_DOCUMENT].defaultView[HOOKS].setText?.(this as any, str),
);
}
}

get data() {
Expand Down
5 changes: 3 additions & 2 deletions packages/polyfill/source/NamedNodeMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
NEXT,
NamespaceURI,
HOOKS,
OWNER_DOCUMENT,
} from './constants.ts';
import type {Attr} from './Attr.ts';
import type {Element} from './Element.ts';
Expand Down Expand Up @@ -66,7 +67,7 @@ export class NamedNodeMap {
if (prev) prev[NEXT] = attr[NEXT];
if (this[CHILD] === attr) this[CHILD] = attr[NEXT];
updateElementAttribute(ownerElement, attr.name, attr.value, null);
ownerElement.ownerDocument.defaultView[HOOKS].removeAttribute?.(
ownerElement[OWNER_DOCUMENT].defaultView[HOOKS].removeAttribute?.(
ownerElement as any,
name,
namespaceURI,
Expand Down Expand Up @@ -118,7 +119,7 @@ export class NamedNodeMap {
attr.value,
);

ownerElement.ownerDocument.defaultView[HOOKS].setAttribute?.(
ownerElement[OWNER_DOCUMENT].defaultView[HOOKS].setAttribute?.(
ownerElement as any,
attr.name,
attr.value,
Expand Down
4 changes: 2 additions & 2 deletions packages/polyfill/source/ParentNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class ParentNode extends ChildNode {
children.splice(children.indexOf(child), 1);
}

this.ownerDocument.defaultView[HOOKS].removeChild?.(
this[OWNER_DOCUMENT].defaultView[HOOKS].removeChild?.(
this as any,
child as any,
childNodesIndex,
Expand Down Expand Up @@ -157,7 +157,7 @@ export class ParentNode extends ChildNode {
if (isElement) this.children.push(child);
}

this.ownerDocument.defaultView[HOOKS].insertChild?.(
this[OWNER_DOCUMENT].defaultView[HOOKS].insertChild?.(
this as any,
child as any,
insertIndex,
Expand Down

0 comments on commit 6989327

Please sign in to comment.