A problem I ran into implementing my MutationObserver shimmed interface is being able to save the state of an elements children for later use. Naively I started by just using $oldnode = $node.cloneNode(true)
which would give me an atomic node at a previous state. The problem with using cloneNode
is when it comes time to compare. Any children in $oldnode
will be !== to the same child in $node
.
I addressed this issue by assigning a unique id to all elements and checking if the unique id of an old node is the same as from an earlier state. This is ugly and noone wants me adding my own unique id property on their elements.
var counter = 0;
var getId = function($ele) {
var id = $ele.nodeType === 3 ? $ele.nodeValue ://text node id is the text content