Skip to content

Commit

Permalink
fix: getRootNode() bug
Browse files Browse the repository at this point in the history
  • Loading branch information
luwes committed Jul 19, 2023
1 parent e8dc9de commit 6d391c9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 6 additions & 1 deletion esm/interface/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,15 @@ export class Node extends EventTarget {
return this.parentNode;
}

/**
* Calling it on an element inside a standard web page will return an HTMLDocument object representing the entire page (or <iframe>).
* Calling it on an element inside a shadow DOM will return the associated ShadowRoot.
* @return {ShadowRoot | HTMLDocument}
*/
getRootNode() {
let root = this;
while (root.parentNode)
root = root.parentNode;
return root.nodeType === DOCUMENT_NODE ? root.documentElement : root;
return root;
}
}
2 changes: 1 addition & 1 deletion test/interface/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ assert(text.isConnected, false, '!isConnected');
assert(text.parentElement, null, '!parentElement');
assert(node.contains(text), false, '!contains');
node.firstChild.appendChild(text);
assert(text.getRootNode(), document.documentElement, 'getRootNode as html');
assert(text.getRootNode(), document, 'getRootNode as document');
assert(node.contains(text), true, 'contains');
assert(text.isConnected, true, 'isConnected');
assert(text.parentElement, node.firstChild, 'parentElement');
Expand Down

0 comments on commit 6d391c9

Please sign in to comment.