Last active
August 29, 2015 14:27
-
-
Save ernestlv/8e18006e8f6fd032102d to your computer and use it in GitHub Desktop.
compare two DOM trees walking the dom
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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