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 27420 - [Custom]: need a hook for transfering data while cloning elements
Summary: [Custom]: need a hook for transfering data while cloning elements
Status: RESOLVED DUPLICATE of bug 24570
Alias: None
Product: WebAppsWG
Classification: Unclassified
Component: HISTORICAL - Component Model (show other bugs)
Version: unspecified
Hardware: PC Windows NT
: P2 normal
Target Milestone: ---
Assignee: Dimitri Glazkov
QA Contact: public-webapps-bugzilla
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 14968
  Show dependency treegraph
 
Reported: 2014-11-24 17:35 UTC by Domenic Denicola
Modified: 2014-11-24 19:19 UTC (History)
5 users (show)

See Also:


Attachments

Description Domenic Denicola 2014-11-24 17:35:16 UTC
Many elements have internal state that should be cloned when using `cloneNode()`. From https://dom.spec.whatwg.org/#concept-node-clone:

> Run any cloning steps defined for node in other applicable specifications and pass copy, node, document and the clone children flag if set, as parameters. 

For example in HTML the input element specifies:

> The cloning steps for input elements must propagate the value, dirty value flag, checkedness, and dirty checkedness flag from the node being cloned to the copy.

This behavior should be hookable by authors as well for their custom elements.


My proposal is that we introduce a clonedCallback(source, dest) that, for cloned nodes, is called after the createdCallback with the original as the source and the new clone as the dest.

Ideally (probably later) we should also redefine DOM to delegate to clonedCallback instead of to "other applicable specifications" and then HTML should specify the clonedCallback behavior instead of specifying "the cloning steps". That way if you e.g. create a custom element that extends an input element you can call `super.clonedCallback(source, dest)` inside your own `clonedCallback`.
Comment 1 Domenic Denicola 2014-11-24 17:50:53 UTC
Anne reminded me that my "ideally (probably later)" plan might not work so well for the built-in elements since clonedCallback would likely need to be delayed in some fashion whereas the built-ins try to do everything synchronously. Somewhat-related previous discussion in bug #25529.

We should still solve this for custom elements.
Comment 2 Boris Zbarsky 2014-11-24 17:53:09 UTC
> and then HTML should specify the clonedCallback behavior

There's a timing difference here, right?  The current cloning steps behavior means there are never script-exposed elements that don't have the relevant state propagated yet, while using clonedCallback for this would mean there are.

Whether this is a problem in practice probably depends on the element.  For example, for a <script> the cloning steps are what propagates the "don't execute again" flag, so this change would allow a single <script> element to execute multiple times...
Comment 3 Domenic Denicola 2014-11-24 17:57:06 UTC
(In reply to Boris Zbarsky from comment #2)

> There's a timing difference here, right?  The current cloning steps behavior
> means there are never script-exposed elements that don't have the relevant
> state propagated yet, while using clonedCallback for this would mean there
> are.

I don't think so? Note that how custom element callbacks work is that they're copied to an internal registry when you do document.registerElement; it doesn't do a run-time lookup of `theElement.clonedCallback`. So if you envision this as HTML "registering" all its elements ahead of time, the cloned callbacks would be registered with the system ahead of time and couldn't be intercepted or messed with.
Comment 4 Adam Klein 2014-11-24 18:26:12 UTC

*** This bug has been marked as a duplicate of bug 24570 ***
Comment 5 Boris Zbarsky 2014-11-24 19:19:23 UTC
> and couldn't be intercepted or messed with.

I'm not talking about interception.  I'm talking about interleaving of non-custom and custom callbacks and the fact that the custom ones would be able to observe some already-cloned nodes for which the custom callbacks had not happened yet.

Basically, the same issue you mention in comment 1.