From 1473a3c521e8d4d44d50c2f15680f28997270dc8 Mon Sep 17 00:00:00 2001 From: Jake Archibald Date: Tue, 29 Oct 2024 11:09:58 +0000 Subject: [PATCH] Fix references after node removal (#472) --- .changeset/serious-ladybugs-bow.md | 5 +++++ .changeset/warm-oranges-rest.md | 5 +++++ packages/polyfill/source/Node.ts | 8 ++++++++ packages/polyfill/source/ParentNode.ts | 4 ++++ 4 files changed, 22 insertions(+) create mode 100644 .changeset/serious-ladybugs-bow.md create mode 100644 .changeset/warm-oranges-rest.md diff --git a/.changeset/serious-ladybugs-bow.md b/.changeset/serious-ladybugs-bow.md new file mode 100644 index 00000000..328649b2 --- /dev/null +++ b/.changeset/serious-ladybugs-bow.md @@ -0,0 +1,5 @@ +--- +'@remote-dom/polyfill': patch +--- + +Fix removeChild so it clears parent/sibling references diff --git a/.changeset/warm-oranges-rest.md b/.changeset/warm-oranges-rest.md new file mode 100644 index 00000000..1f106ead --- /dev/null +++ b/.changeset/warm-oranges-rest.md @@ -0,0 +1,5 @@ +--- +'@remote-dom/polyfill': patch +--- + +Add node.parentElement diff --git a/packages/polyfill/source/Node.ts b/packages/polyfill/source/Node.ts index 22026d33..af1a5705 100644 --- a/packages/polyfill/source/Node.ts +++ b/packages/polyfill/source/Node.ts @@ -62,6 +62,14 @@ export class Node extends EventTarget { set parentNode(_readonly) {} + get parentElement(): ParentNode | null { + const parent = this[PARENT]; + if (!parent || parent.nodeType !== 1) return null; + return parent; + } + + set parentElement(_readonly) {} + get previousSibling() { return this[PREV]; } diff --git a/packages/polyfill/source/ParentNode.ts b/packages/polyfill/source/ParentNode.ts index b3aed87d..b40d0c34 100644 --- a/packages/polyfill/source/ParentNode.ts +++ b/packages/polyfill/source/ParentNode.ts @@ -68,6 +68,10 @@ export class ParentNode extends ChildNode { children.splice(children.indexOf(child), 1); } + child[PARENT] = null; + child[NEXT] = null; + child[PREV] = null; + if (this[IS_CONNECTED]) { for (const node of selfAndDescendants(child)) { node[IS_CONNECTED] = false;