This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 26837 - The spec has an example using cloneNode. It would be nice to have another example using just appendChild, similar to this one: http://www.html5rocks.com/en/tutorials/webcomponents/template/#toc-using
Summary: The spec has an example using cloneNode. It would be nice to have another exa...
Status: RESOLVED FIXED
Alias: None
Product: WHATWG
Classification: Unclassified
Component: HTML (show other bugs)
Version: unspecified
Hardware: Other other
: P3 normal
Target Milestone: Unsorted
Assignee: Ian 'Hixie' Hickson
QA Contact: contributor
URL: https://html.spec.whatwg.org/#the-tem...
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-09-17 19:01 UTC by contributor
Modified: 2014-09-29 21:18 UTC (History)
3 users (show)

See Also:


Attachments

Description contributor 2014-09-17 19:01:36 UTC
Specification: https://html.spec.whatwg.org/multipage/scripting.html
Multipage: https://html.spec.whatwg.org/multipage/#the-template-element
Complete: https://html.spec.whatwg.org/#the-template-element
Referrer: https://html.spec.whatwg.org/multipage/scripting-1.html

Comment:
The spec has an example using cloneNode. It would be nice to have another
example using just appendChild, similar to this one:
http://www.html5rocks.com/en/tutorials/webcomponents/template/#toc-using

Posted from: 187.1.80.162
User agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36
Comment 1 Ian 'Hixie' Hickson 2014-09-24 22:23:51 UTC
I assume you mean importNode(), not appendChild().

I don't really understand why you would use importNode(). It's exactly the same as cloneNode(), except requires more code. Can you elaborate?
Comment 2 Denilson Figueiredo de Sá 2014-09-24 22:49:16 UTC
Indeed, I meant importNode().

As a web developer, I usually read the specs to understand how to use the technologies correctly. And I got confused why cloneNode() is mentioned in the example, while importNode() is used in that other tutorial. I didn't know which one was correct, or if they were equivalent.

Considering the <template> element creates another Document to hold its contents, I got confused if importNode was required.

This is why I have suggested to add another example (or a comment) showing that they are equivalent.

Thanks for your concern!
Comment 3 Ian 'Hixie' Hickson 2014-09-25 18:03:03 UTC
I can add a note saying that some authors prefer importNode() but that they are actually identical, sure.

   target.appendChild(document.importNode(node, true))

...is exactly identical to:

   target.appendChild(node.cloneNode(true))

The only difference between cloneNode and importNode is that importNode also changes the ownerDocument, but the ownerDocument gets adjusted by appendChild anyway.
Comment 4 contributor 2014-09-29 21:18:50 UTC
Checked in as WHATWG revision r8825.
Check-in comment: Add a comment about cloneNode vs importNode in the example that clones a template, since some tutorials use importNode instead of cloneNode (they're identical in practice)
https://html5.org/tools/web-apps-tracker?from=8824&to=8825