Skip to content

Instantly share code, notes, and snippets.

@hallvors
Last active December 24, 2015 20:18
Show Gist options
  • Save hallvors/6856362 to your computer and use it in GitHub Desktop.
Save hallvors/6856362 to your computer and use it in GitHub Desktop.
This strips out HREF, VALUE and SRC attributes and comment nodes to be able to serialize the DOM and compare mostly the structure. This helps because HREF, VALUE and SRC tends to contain session identifiers with random data that give false positives when trying to diff the HTML code.
(function(){
function removeAttr(attr){
var xpElms = document.evaluate('//*[@'+attr+']', document.documentElement, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE , null );
var elm;
for(var i=0; elm = xpElms.snapshotItem(i); i++){
elm.removeAttribute(attr)
}
}
removeAttr('href');
removeAttr('src');
removeAttr('value');
// remove <!-- comment --> and text nodes
var xpElms = document.evaluate('//comment()|//text()', document.documentElement, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE , null );
for(var i=0; elm = xpElms.snapshotItem(i); i++){
if(!(elm.parentElement.tagName in {'SCRIPT':1,'STYLE':1}))
elm.parentElement.removeChild(elm)
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment