Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dom-is-connected.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clack/ui': patch
---

Add Node.isConnected by walking the node's ancestry to its owner document's root.
11 changes: 11 additions & 0 deletions packages/ui/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,17 @@ export abstract class Node extends EventTarget {
return null;
}

get isConnected(): boolean {
const { documentElement } = this.ownerDocument;
// oxlint-disable-next-line typescript/no-this-alias -- This cursor walks the node's ancestry.
for (let current: Node | null = this; current; current = current.parentNode) {
if (current === documentElement) {
return true;
}
}
return false;
}

appendChild(node: Node): Node {
return this.insertBefore(node, null);
}
Expand Down
1 change: 1 addition & 0 deletions packages/ui/test/wpt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ const config: WPTConfig = {
'Attribute with prefix in local name',
'getAttributeNames tests',
],
'dom/nodes/Node-isConnected.html': true,
'dom/nodes/Node-nodeValue.html': ['Text.nodeValue', 'Comment.nodeValue'],
'dom/nodes/Node-textContent.html': [
'For an empty Text, textContent should be the empty string',
Expand Down
Loading