Skip to content

Commit b45b97c

Browse files
committed
Add Node.isConnected with native WPT coverage
1 parent 4789719 commit b45b97c

3 files changed

Lines changed: 16 additions & 0 deletions

File tree

‎.changeset/dom-is-connected.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clack/ui': patch
3+
---
4+
5+
Add Node.isConnected by walking the node's ancestry to a document.

‎packages/ui/src/dom.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ export abstract class Node extends EventTarget {
151151
//ignore
152152
}
153153

154+
get isConnected(): boolean {
155+
// oxlint-disable-next-line typescript/no-this-alias -- This cursor walks the node's ancestry.
156+
for (let current: Node | null = this; current; current = current.parentNode) {
157+
if (current instanceof Document) {
158+
return true;
159+
}
160+
}
161+
return false;
162+
}
163+
154164
appendChild(node: Node): Node {
155165
return this.insertBefore(node, null);
156166
}

‎packages/ui/wpt.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ const config: WPTConfig = {
169169
'Attribute with prefix in local name',
170170
'getAttributeNames tests',
171171
],
172+
'dom/nodes/Node-isConnected.html': ['Test with ordinary child nodes'],
172173
'dom/nodes/Node-nodeValue.html': ['Text.nodeValue', 'Comment.nodeValue'],
173174
'dom/nodes/Node-textContent.html': [
174175
'For an empty Text, textContent should be the empty string',

0 commit comments

Comments
 (0)