From 69893273c65b05de6b403f62bdca97b1719cb4b7 Mon Sep 17 00:00:00 2001 From: Olavo Santos Date: Wed, 7 Aug 2024 09:31:24 -0500 Subject: [PATCH] refactor: use OWNER_DOCUMENT symbol consistently to access the HOOKS --- packages/polyfill/source/Attr.ts | 3 ++- packages/polyfill/source/CharacterData.ts | 12 ++++++++---- packages/polyfill/source/NamedNodeMap.ts | 5 +++-- packages/polyfill/source/ParentNode.ts | 4 ++-- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/packages/polyfill/source/Attr.ts b/packages/polyfill/source/Attr.ts index 4f692b5b..df51ce86 100644 --- a/packages/polyfill/source/Attr.ts +++ b/packages/polyfill/source/Attr.ts @@ -7,6 +7,7 @@ import { NamespaceURI, NodeType, HOOKS, + OWNER_DOCUMENT, } from './constants.ts'; import {Node} from './Node.ts'; import type {Element} from './Element.ts'; @@ -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, diff --git a/packages/polyfill/source/CharacterData.ts b/packages/polyfill/source/CharacterData.ts index 0d17e63f..7d7e67c5 100644 --- a/packages/polyfill/source/CharacterData.ts +++ b/packages/polyfill/source/CharacterData.ts @@ -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 { @@ -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() { diff --git a/packages/polyfill/source/NamedNodeMap.ts b/packages/polyfill/source/NamedNodeMap.ts index 48fbbe8b..37c8c02f 100644 --- a/packages/polyfill/source/NamedNodeMap.ts +++ b/packages/polyfill/source/NamedNodeMap.ts @@ -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'; @@ -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, @@ -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, diff --git a/packages/polyfill/source/ParentNode.ts b/packages/polyfill/source/ParentNode.ts index daad6e65..26b48b57 100644 --- a/packages/polyfill/source/ParentNode.ts +++ b/packages/polyfill/source/ParentNode.ts @@ -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, @@ -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,