Skip to content

Instantly share code, notes, and snippets.

@ernestlv
Last active August 29, 2015 14:27
Show Gist options
  • Save ernestlv/8e18006e8f6fd032102d to your computer and use it in GitHub Desktop.
Save ernestlv/8e18006e8f6fd032102d to your computer and use it in GitHub Desktop.
compare two DOM trees walking the dom
function compare(nodeA, nodeB) {
if ( !nodeA && !nodeB ) return true; //edge case
if ( nodeA.nodeType === nodeB.nodeType && nodeA.tagName === nodeB.tagName && nodeA.nodeValue === nodeB.nodeValue ){
var i = 0;
var childA = nodeA.childNodes[i];
var childB = nodeB.childNodes[i];
while(childA && childB){
if (!compare(childA, childB)) return false; //some child is not equal
i++;
childA = nodeA.childNodes[i];
childB = nodeB.childNodes[i];
}
return true; //all children are equal trees compare
}
return false; //nodes are not equal
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment