Dom: Wrong return type of Node::getRootNode()

Created on 20 Mar 2019  路  5Comments  路  Source: whatwg/dom

The WhatWG page on Node::getRootNode states that this function returns an object of type Node. Yet, shouldn't it state, instead, that this function returns an object of type ShadowRoot?

The Node interface lacks, for instance, the host property, which is documented to be a property of the return value of Node::getRootNode().

/see also: /mdn/sprints/issues/1204

Most helpful comment

It can also return any other kind of node, if the node or one of its ancestors isn't connected.

All 5 comments

which is documented to be a property of the return value of Node::getRootNode().

What documentation is that referring to? It's not in the spec, which says that the return value is of type Node.

Anyway, getRootNode()'s specification is correct. If you look at the algorithm, in https://dom.spec.whatwg.org/#dom-node-getrootnode , you'll see it can return the root node, which can be a Document or other class representing the document node. The document node is not a ShadowRoot.

Try it in your own browser: type document.body.getRootNode() and see what it returns.

I found all this:

Chrome and Firefox return valid values when evaluating this.getRootNode().host from within a web component having a Shadow DOM attached.

Yes, sometimes it does return a ShadowRoot. Sometimes it returns a Document, or HTMLDocument, or SVGDocument, or similar. All of these are different types of Node. That is why the return value is Node.

Ah, I see. Thank a lot for clarifying!

It can also return any other kind of node, if the node or one of its ancestors isn't connected.

Was this page helpful?
0 / 5 - 0 ratings