-
Notifications
You must be signed in to change notification settings - Fork 23k
Change cloneNode references to importNode #41441
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
If this is important, could you add a sentence to |
How does that look? |
|
If your argument for using |
I’m under the impression from the community that yes, we would like all these updated and that importNode should be the default. I’ll see if I can get some people to chime in here. |
|
Hi @frehner, any updates? Do you want to propose an updated version first so we can get other people to look at it? Perhaps someone from WHATWG? |
This comment was marked as spam.
This comment was marked as spam.
|
I think this is an important change to make. If you don't know exactly what's in your template (like many libraries that accept or produce user-defined templates), then |
|
I would also suggest clearly explaining that the nodes in Otherwise you get fairly unintuitive behavior that could manifest in odd ways. This could also be addressed in the pitfalls section. const clone = template.content.cloneNode(true);
console.assert(clone.firstElementChild.ownerDocument === document) // fails.
const imported = document.importNode(template.content, true);
console.assert(imported.firstElementChild.ownerDocument === document) // works. |
|
Thanks, both. Would you agree to globally replace |
Josh-Cena
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Waiting until the note gets rewritten
|
Hi @frehner @justinfagnani @sorvell I've committed an updated version of the note:
WDYT? |
|
|
||
| const shadowRoot = this.attachShadow({ mode: "open" }); | ||
| shadowRoot.appendChild(templateContent.cloneNode(true)); | ||
| shadowRoot.appendChild(document.importNode(templateContent, true)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For cases like this, I think it's actually fine to use cloneNode because it's immediately appended, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be fine, but IMO it's a lot easier to be consistent and use importNode everywhere.
|
I've done a global replacement for all |
Description
Updated examples to use
document.importNode()exclusively, and references to refer to bothimportNodeandNode.cloneNode.Motivation
document.importNodeandNode.cloneNodeare largely similar, but have one important difference; the timing of when the node is adopted. When usingNode.cloneNode(), the content isn't adopted until it's either explicitly adopted or when it's appended to the document, so at a later point after callingcloneNode(). This falls into an edge-case annoyance where calling upgrade on nodes that aren't yet adopted causes them to not actually get upgraded. This real example from the Web Components discord demonstrates the edge-case:If you change from
content.cloneNode()todocument.importNode()then the constructor will run and the content is upgraded correctly.So we would like to update the examples to show
document.importNode()being used by default, and also update references/links to show it as an option next to cloneNode.Additional details
Web components discord discussion https://discord.com/channels/767813449048260658/767813449048260661/1421037073829199994
Related issues and pull requests