Skip to content

Commit 7a0c21e

Browse files
authored
fix: getRootNode() bug (#213)
Fixed wrong returned node when used.
1 parent e8dc9de commit 7a0c21e

File tree

7 files changed

+27
-7
lines changed

7 files changed

+27
-7
lines changed

cjs/interface/node.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,16 @@ class Node extends EventTarget {
188188
return this.parentNode;
189189
}
190190

191+
/**
192+
* Calling it on an element inside a standard web page will return an HTMLDocument object representing the entire page (or <iframe>).
193+
* Calling it on an element inside a shadow DOM will return the associated ShadowRoot.
194+
* @return {ShadowRoot | HTMLDocument}
195+
*/
191196
getRootNode() {
192197
let root = this;
193198
while (root.parentNode)
194199
root = root.parentNode;
195-
return root.nodeType === DOCUMENT_NODE ? root.documentElement : root;
200+
return root;
196201
}
197202
}
198203
exports.Node = Node

esm/interface/node.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,15 @@ export class Node extends EventTarget {
187187
return this.parentNode;
188188
}
189189

190+
/**
191+
* Calling it on an element inside a standard web page will return an HTMLDocument object representing the entire page (or <iframe>).
192+
* Calling it on an element inside a shadow DOM will return the associated ShadowRoot.
193+
* @return {ShadowRoot | HTMLDocument}
194+
*/
190195
getRootNode() {
191196
let root = this;
192197
while (root.parentNode)
193198
root = root.parentNode;
194-
return root.nodeType === DOCUMENT_NODE ? root.documentElement : root;
199+
return root;
195200
}
196201
}

test/interface/text.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ assert(text.isConnected, false, '!isConnected');
2020
assert(text.parentElement, null, '!parentElement');
2121
assert(node.contains(text), false, '!contains');
2222
node.firstChild.appendChild(text);
23-
assert(text.getRootNode(), document.documentElement, 'getRootNode as html');
23+
assert(text.getRootNode(), document, 'getRootNode as document');
2424
assert(node.contains(text), true, 'contains');
2525
assert(text.isConnected, true, 'isConnected');
2626
assert(text.parentElement, node.firstChild, 'parentElement');

types/esm/interface/image.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ export function ImageClass(ownerDocument: any): {
184184
compareDocumentPosition(target: any): number;
185185
isEqualNode(node: any): boolean;
186186
_getParent(): any;
187-
getRootNode(): any;
187+
getRootNode(): ShadowRoot | HTMLDocument;
188188
[PREV]: any;
189189
addEventListener(type: any, listener: any, options: any): void;
190190
removeEventListener(type: any, listener: any): void;

types/esm/interface/node.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@ export class Node extends EventTarget implements globalThis.Node {
6969
isSameNode(node: any): boolean;
7070
compareDocumentPosition(target: any): number;
7171
isEqualNode(node: any): boolean;
72-
getRootNode(): any;
72+
/**
73+
* Calling it on an element inside a standard web page will return an HTMLDocument object representing the entire page (or <iframe>).
74+
* Calling it on an element inside a shadow DOM will return the associated ShadowRoot.
75+
* @return {ShadowRoot | HTMLDocument}
76+
*/
77+
getRootNode(): ShadowRoot | HTMLDocument;
7378
[NEXT]: any;
7479
[PREV]: any;
7580
}

types/esm/shared/node.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function parentElement({ parentNode }: {
66
parentNode: any;
77
}): any;
88
export function previousSibling({ [PREV]: prev }: {
9-
"__@PREV@22226": any;
9+
"__@PREV@22229": any;
1010
}): any;
1111
export function nextSibling(node: any): any;
1212
import { PREV } from "./symbols.js";

worker.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4405,11 +4405,16 @@ let Node$1 = class Node extends DOMEventTarget {
44054405
return this.parentNode;
44064406
}
44074407

4408+
/**
4409+
* Calling it on an element inside a standard web page will return an HTMLDocument object representing the entire page (or <iframe>).
4410+
* Calling it on an element inside a shadow DOM will return the associated ShadowRoot.
4411+
* @return {ShadowRoot | HTMLDocument}
4412+
*/
44084413
getRootNode() {
44094414
let root = this;
44104415
while (root.parentNode)
44114416
root = root.parentNode;
4412-
return root.nodeType === DOCUMENT_NODE ? root.documentElement : root;
4417+
return root;
44134418
}
44144419
};
44154420

0 commit comments

Comments
 (0)