Re: How to be notified when a node gets detached/reparented?
Jonas Sicking wrote: On Thu, Oct 11, 2012 at 6:04 AM, Paul Rouget p...@mozilla.com wrote: Paul Rouget wrote: Context: in the firefox devtools, we need to track some nodes and update different views based on what's happening to this node (show its parents, show its child, show its attributes, …). The new Mutation observers are very helpful. But there's one thing I am not really sure how to handle correctly . When a node gets detached (parent.removeChild(node)) or reparented, I need to be notified. My current idea is to listen to childList mutations from the parent, then, on this mutation, check if the node is still part of the children of the parent, if not, check if it has a parent, if so, the node has been *relocated*, then I need re-listen to a childList mutation from this new parent, if no parent, the node has been *detached*. I was wondering if there was any better way to do that. Another way to do it is to listen to subtree mutations from the documentElement, and then check if the targeted node is part of the removeNodes list. You also would have to check if any of the the node's ancestors is part of the removeNodes list. Indeed. I tested this approach yesterday, and ran into this issue. The other option is to listen to subtree+childList on the root node, and everytime the callback is called, check if the node is still attached (with compareDocumentPosition) Would that deteriorate the performance? That is obviously more work that has to be done both by the platform and by the webpage, so yes, it's worse performance. How much work I couldn't say offhand. It would be worth bringing this use-case to the webapps WG where MutationObservers are defined. Especially getting notifications when a node is moved in and out of a document seems like it would be worth having explicit notifications about. / Jonas -- Paul ___ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform
Re: How to be notified when a node gets detached/reparented?
smaug wrote: On 10/11/2012 02:40 PM, Paul Rouget wrote: Context: in the firefox devtools, we need to track some nodes and update different views based on what's happening to this node (show its parents, show its child, show its attributes, …). The new Mutation observers are very helpful. But there's one thing I am not really sure how to handle correctly . When a node gets detached (parent.removeChild(node)) or reparented, I need to be notified. My current idea is to listen to childList mutations from the parent, then, on this mutation, check if the node is still part of the children of the parent, if not, check if it has a parent, if so, the node has been *relocated*, then I need re-listen to a childList mutation from this new parent, if no parent, the node has been *detached*. Why do you need to re-listen anywhere? You get the node in a MutationRecord and when the callback is called you check where it is. ( node.contains can be useful and certainly faster than anything in JS. ) If the node doesn't have parent, it is detached. I didn't know about Node.contains. Something I didn't consider: what if an ancestor of the node is detached? So what if I do that: - listen to document.documentElement childList mutations - in the callback, check if the node is still attached (with compareDocumentPosition) the problem is that I'll need to call compareDocumentPosition every time there's a childList mutation in the whole document. -- Paul ___ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform
Re: How to be notified when a node gets detached/reparented?
Marcio Galli wrote: @Paul, What is your use case BTW? when you say update views based on mutations, is the goal is to let the user know what is going on? Or you actually performing other mutations back to the DOM or logging things or creating reports? For example: if you start the Firefox Inspector (in the tools menu), you have what we call a highlighter (a rectangle over the page that shows you what node is selected). I want to know when this node is removed and then hide the rectangle. m On Thu, Oct 11, 2012 at 4:27 PM, Jonas Sicking jo...@sicking.cc wrote: On Thu, Oct 11, 2012 at 6:04 AM, Paul Rouget p...@mozilla.com wrote: Paul Rouget wrote: Context: in the firefox devtools, we need to track some nodes and update different views based on what's happening to this node (show its parents, show its child, show its attributes, …). The new Mutation observers are very helpful. But there's one thing I am not really sure how to handle correctly . When a node gets detached (parent.removeChild(node)) or reparented, I need to be notified. My current idea is to listen to childList mutations from the parent, then, on this mutation, check if the node is still part of the children of the parent, if not, check if it has a parent, if so, the node has been *relocated*, then I need re-listen to a childList mutation from this new parent, if no parent, the node has been *detached*. I was wondering if there was any better way to do that. Another way to do it is to listen to subtree mutations from the documentElement, and then check if the targeted node is part of the removeNodes list. You also would have to check if any of the the node's ancestors is part of the removeNodes list. Would that deteriorate the performance? That is obviously more work that has to be done both by the platform and by the webpage, so yes, it's worse performance. How much work I couldn't say offhand. It would be worth bringing this use-case to the webapps WG where MutationObservers are defined. Especially getting notifications when a node is moved in and out of a document seems like it would be worth having explicit notifications about. / Jonas ___ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform -- www.telasocial.com -- Paul ___ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform
How to be notified when a node gets detached/reparented?
Context: in the firefox devtools, we need to track some nodes and update different views based on what's happening to this node (show its parents, show its child, show its attributes, …). The new Mutation observers are very helpful. But there's one thing I am not really sure how to handle correctly . When a node gets detached (parent.removeChild(node)) or reparented, I need to be notified. My current idea is to listen to childList mutations from the parent, then, on this mutation, check if the node is still part of the children of the parent, if not, check if it has a parent, if so, the node has been *relocated*, then I need re-listen to a childList mutation from this new parent, if no parent, the node has been *detached*. I was wondering if there was any better way to do that. Thanks, -- Paul ___ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform
Re: How to be notified when a node gets detached/reparented?
Marcio Galli wrote: Hi Paul, so this means we do not have anymore DOMnodeRemoved from the mutation events? There's no DOMNodeRemoved type: https://developer.mozilla.org/en-US/docs/DOM/MutationObserver#MutationObserverInit But there's a removedNodes from the mutation record. Maybe this array include the target if we listen to the subtree. I'll try. I find your use case sort of important specially now that I believe pages will suffer more changes based in template operations in the client. So detecting context is key for client apps to know where they were and what to do next. A more specific case is a 4x4 grid abcd that loses quadrant a. It is pretty important to signal the new state, let's say null,bcd to consumers — for example a grid rearrangement may take place. m On Thu, Oct 11, 2012 at 8:40 AM, Paul Rouget p...@mozilla.com wrote: Context: in the firefox devtools, we need to track some nodes and update different views based on what's happening to this node (show its parents, show its child, show its attributes, …). The new Mutation observers are very helpful. But there's one thing I am not really sure how to handle correctly . When a node gets detached (parent.removeChild(node)) or reparented, I need to be notified. My current idea is to listen to childList mutations from the parent, then, on this mutation, check if the node is still part of the children of the parent, if not, check if it has a parent, if so, the node has been *relocated*, then I need re-listen to a childList mutation from this new parent, if no parent, the node has been *detached*. I was wondering if there was any better way to do that. Thanks, -- Paul ___ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform -- www.telasocial.com -- Paul ___ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform
Re: How to be notified when a node gets detached/reparented?
Paul Rouget wrote: Marcio Galli wrote: Hi Paul, so this means we do not have anymore DOMnodeRemoved from the mutation events? There's no DOMNodeRemoved type: https://developer.mozilla.org/en-US/docs/DOM/MutationObserver#MutationObserverInit But there's a removedNodes from the mutation record. Maybe this array include the target if we listen to the subtree. I'll try. Nope. I find your use case sort of important specially now that I believe pages will suffer more changes based in template operations in the client. So detecting context is key for client apps to know where they were and what to do next. A more specific case is a 4x4 grid abcd that loses quadrant a. It is pretty important to signal the new state, let's say null,bcd to consumers — for example a grid rearrangement may take place. m On Thu, Oct 11, 2012 at 8:40 AM, Paul Rouget p...@mozilla.com wrote: Context: in the firefox devtools, we need to track some nodes and update different views based on what's happening to this node (show its parents, show its child, show its attributes, …). The new Mutation observers are very helpful. But there's one thing I am not really sure how to handle correctly . When a node gets detached (parent.removeChild(node)) or reparented, I need to be notified. My current idea is to listen to childList mutations from the parent, then, on this mutation, check if the node is still part of the children of the parent, if not, check if it has a parent, if so, the node has been *relocated*, then I need re-listen to a childList mutation from this new parent, if no parent, the node has been *detached*. I was wondering if there was any better way to do that. Thanks, -- Paul ___ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform -- www.telasocial.com -- Paul ___ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform -- Paul ___ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform
Re: How to be notified when a node gets detached/reparented?
Paul Rouget wrote: Context: in the firefox devtools, we need to track some nodes and update different views based on what's happening to this node (show its parents, show its child, show its attributes, …). The new Mutation observers are very helpful. But there's one thing I am not really sure how to handle correctly . When a node gets detached (parent.removeChild(node)) or reparented, I need to be notified. My current idea is to listen to childList mutations from the parent, then, on this mutation, check if the node is still part of the children of the parent, if not, check if it has a parent, if so, the node has been *relocated*, then I need re-listen to a childList mutation from this new parent, if no parent, the node has been *detached*. I was wondering if there was any better way to do that. Another way to do it is to listen to subtree mutations from the documentElement, and then check if the targeted node is part of the removeNodes list. Would that deteriorate the performance? -- Paul ___ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform
Re: How to be notified when a node gets detached/reparented?
On Thu, Oct 11, 2012 at 6:04 AM, Paul Rouget p...@mozilla.com wrote: Paul Rouget wrote: Context: in the firefox devtools, we need to track some nodes and update different views based on what's happening to this node (show its parents, show its child, show its attributes, …). The new Mutation observers are very helpful. But there's one thing I am not really sure how to handle correctly . When a node gets detached (parent.removeChild(node)) or reparented, I need to be notified. My current idea is to listen to childList mutations from the parent, then, on this mutation, check if the node is still part of the children of the parent, if not, check if it has a parent, if so, the node has been *relocated*, then I need re-listen to a childList mutation from this new parent, if no parent, the node has been *detached*. I was wondering if there was any better way to do that. Another way to do it is to listen to subtree mutations from the documentElement, and then check if the targeted node is part of the removeNodes list. You also would have to check if any of the the node's ancestors is part of the removeNodes list. Would that deteriorate the performance? That is obviously more work that has to be done both by the platform and by the webpage, so yes, it's worse performance. How much work I couldn't say offhand. It would be worth bringing this use-case to the webapps WG where MutationObservers are defined. Especially getting notifications when a node is moved in and out of a document seems like it would be worth having explicit notifications about. / Jonas ___ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform
Re: How to be notified when a node gets detached/reparented?
@Paul, What is your use case BTW? when you say update views based on mutations, is the goal is to let the user know what is going on? Or you actually performing other mutations back to the DOM or logging things or creating reports? m On Thu, Oct 11, 2012 at 4:27 PM, Jonas Sicking jo...@sicking.cc wrote: On Thu, Oct 11, 2012 at 6:04 AM, Paul Rouget p...@mozilla.com wrote: Paul Rouget wrote: Context: in the firefox devtools, we need to track some nodes and update different views based on what's happening to this node (show its parents, show its child, show its attributes, …). The new Mutation observers are very helpful. But there's one thing I am not really sure how to handle correctly . When a node gets detached (parent.removeChild(node)) or reparented, I need to be notified. My current idea is to listen to childList mutations from the parent, then, on this mutation, check if the node is still part of the children of the parent, if not, check if it has a parent, if so, the node has been *relocated*, then I need re-listen to a childList mutation from this new parent, if no parent, the node has been *detached*. I was wondering if there was any better way to do that. Another way to do it is to listen to subtree mutations from the documentElement, and then check if the targeted node is part of the removeNodes list. You also would have to check if any of the the node's ancestors is part of the removeNodes list. Would that deteriorate the performance? That is obviously more work that has to be done both by the platform and by the webpage, so yes, it's worse performance. How much work I couldn't say offhand. It would be worth bringing this use-case to the webapps WG where MutationObservers are defined. Especially getting notifications when a node is moved in and out of a document seems like it would be worth having explicit notifications about. / Jonas ___ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform -- www.telasocial.com ___ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform
Re: How to be notified when a node gets detached/reparented?
On 10/11/2012 02:40 PM, Paul Rouget wrote: Context: in the firefox devtools, we need to track some nodes and update different views based on what's happening to this node (show its parents, show its child, show its attributes, …). The new Mutation observers are very helpful. But there's one thing I am not really sure how to handle correctly . When a node gets detached (parent.removeChild(node)) or reparented, I need to be notified. My current idea is to listen to childList mutations from the parent, then, on this mutation, check if the node is still part of the children of the parent, if not, check if it has a parent, if so, the node has been *relocated*, then I need re-listen to a childList mutation from this new parent, if no parent, the node has been *detached*. Why do you need to re-listen anywhere? You get the node in a MutationRecord and when the callback is called you check where it is. ( node.contains can be useful and certainly faster than anything in JS. ) If the node doesn't have parent, it is detached. I was wondering if there was any better way to do that. Thanks, -- Paul ___ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform