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 23250 - No way with mutation observers to watch mutations to an element's parent
Summary: No way with mutation observers to watch mutations to an element's parent
Status: RESOLVED WONTFIX
Alias: None
Product: WebAppsWG
Classification: Unclassified
Component: DOM (show other bugs)
Version: unspecified
Hardware: PC All
: P2 enhancement
Target Milestone: ---
Assignee: Anne
QA Contact: public-webapps-bugzilla
URL:
Whiteboard: blocked on detailed implementer input
Keywords:
Depends on:
Blocks:
 
Reported: 2013-09-15 22:16 UTC by Ian 'Hixie' Hickson
Modified: 2016-04-15 13:39 UTC (History)
13 users (show)

See Also:


Attachments

Description Ian 'Hixie' Hickson 2013-09-15 22:16:05 UTC
http://dom.spec.whatwg.org/#mutation-observers

I'd like to catch the moment where an element is inserted into another element, and then later catch the moment where that element is removed from an element.

Right now, there's no way to do the first part that I can see, an doing the second part requires a lot more code since you have to filter out all the uninteresting changes to the siblings.
Comment 1 Olli Pettay 2013-09-15 22:49:39 UTC
So it would be "parentChainChanged" or some such.
I can see that being useful, especially if it would catch any ancestor change, not just parent change.
Comment 2 Olli Pettay 2013-09-15 23:04:37 UTC
Not sure though what kind of MutationRecord would be needed.
Do we need to report which element was attached to which element, or
would just telling the new subtree root be better?
The latter is probably more useful.
(Fast/simple access to subtree root might be useful in any case)
Comment 3 Jonas Sicking (Not reading bugmail) 2013-09-17 17:30:31 UTC
There are three types of changes possible here:

* Parent change
* Ancestor change
* Document change

I would expect a "parent change" notification to be pretty cheap to implement on most platforms.

"document change" is pretty cheap to implement in Gecko currently since we already have an internal notification for that. However that internal notification comes at a pretty high overhead so I'm not sure how cheap it would be to implement elsewhere. And i'm not sure that the internal notification which makes it cheap to implement this in gecko is something that we'll keep around forever.

"ancestor change" I think would be pretty expensive to implement in gecko.


I definitely see value in all three notifications here. But I think that we might want to enable separating expensive notifications from cheap ones. So that you don't incur high costs just because you are interested in for example "parent change".
Comment 4 Jonas Sicking (Not reading bugmail) 2013-09-17 17:31:13 UTC
There's technically also "owner document change". I'm not sure if there are use cases for that though?
Comment 5 Olli Pettay 2013-09-17 17:36:44 UTC
In gecko we have ancestor change internally, so it would be rather cheap ;)
Comment 6 Travis Leithead [MSFT] 2013-09-17 17:38:06 UTC
I'm reminded of the request that comes up every so often to add a "parent" combinator to CSS Selectors. Somehow Selectors are still super-useful and in nearly every case, you can rearrange your selector logic to avoid needing to do this.

The same seems possible in the scenario Ian provided. I can just observe the document or some scoped parent node to see when my element is inserted.

I suppose there is definitely convenience to observing the ancestor chain changes, but bottom-up matching/tracking is just not a typical pattern used in traversing the DOM.
Comment 7 Rafael Weinstein 2013-09-17 18:02:40 UTC
@Ian, MutationSummary (https://code.google.com/p/mutation-summary/) can provide you exactly the information you are wanting (and do it efficiently) using the existing MutationRecord types.

The idea with MutationObservers was to provide sufficient underlying information so that script can efficiently answer more complex questions.

I'm open to adding more mutation types, but I'm not sure we have a good justification yet.

Also, FWIW, In Blink, document changes are tracked internally at each node. Ancestor changes are not.
Comment 8 Ian 'Hixie' Hickson 2013-10-07 21:17:25 UTC
I don't understand how I can tell when the element is inserted into another, when that element is either not in a document, or not in a document I know about, which are the cases I am particularly interested in. Also I shouldn't need a 1600+ line library to do this. :-)

The specific use case was having an element that manipulates the parent element, e.g. the way that animations do in SVG. It needs to know when its parent changes, so it can hook in when it's added, and unhook when it's removed.
Comment 9 Anne 2014-11-10 13:29:15 UTC
Ian, it seems custom elements have these hooks. Can we find something that brings that and mutation observers together?
Comment 10 Ian 'Hixie' Hickson 2014-11-21 23:33:09 UTC
Maybe? I dunno. Neither of those are really in my area of expertise. :-)
Comment 11 Anne 2015-08-03 13:28:26 UTC
The custom element callbacks will provide for this. Perhaps there should be a way to generalize them to builtin elements: https://github.com/w3c/webcomponents/issues/291
Comment 12 Anne 2016-03-14 16:04:52 UTC
Rafael, could you reply to comment 8? Or maybe Adam can?
Comment 13 Adam Klein 2016-03-14 18:33:42 UTC
Re: #8, there is no way to do that with MutationObservers, indeed.

If we want to add something here it does seem like a good idea to add something analogous to what was added for custom elements; adding Domenic who is better-aware of the state of the element lifecycle callbacks (although Anne, you might know too).
Comment 14 Domenic Denicola 2016-03-14 21:06:13 UTC
We decided this capability was too low-level for custom elements, which now have only (dis)connectedCallback which is about insertions/removals that change an elements in-a-document status. A true inserted/removed hook would be run too often and is not needed nearly as often.

It seems possible to add one in the future (maybe parentChangedCallback?), but it's not clear the use cases are compelling enough. We have one request from Hixie based on an analogy to SVG animations; have we heard authors asking for this?
Comment 15 Adam Klein 2016-03-14 21:07:27 UTC
I don't recall ever hearing author requests for this feature, for whatever that's worth.
Comment 16 Ryosuke Niwa 2016-03-14 21:14:01 UTC
(In reply to Domenic Denicola from comment #14)
> It seems possible to add one in the future (maybe parentChangedCallback?),
> but it's not clear the use cases are compelling enough. We have one request
> from Hixie based on an analogy to SVG animations; have we heard authors
> asking for this?

This is a feature request for MutationObservers, not custom elements, so it can't just be a callback on custom element interface as that would not address use cases that invoke non-custom elements.
Comment 17 Anne 2016-04-15 13:39:52 UTC
Going to mark this WONTFIX based on lack of demand. When demand rises, let me know!