Skip to content

Instantly share code, notes, and snippets.

@eagafonov
Last active September 29, 2015 13:50
Show Gist options
  • Save eagafonov/09896ecfdd326f0c5d75 to your computer and use it in GitHub Desktop.
Save eagafonov/09896ecfdd326f0c5d75 to your computer and use it in GitHub Desktop.
jQuery plugin to dump HTML tree
// playground http://jsfiddle.net/1ssfjxu1/1/
$.fn.htmldump = function() {
function walk(element, callback) {
if (callback) {
callback.apply(element);
}
$(element).children('*:not(script)')
.each(function(i, child) {walk(child, callback)});
};
function wipe_text_nodes() {
$(this).contents().filter(function(){return this.nodeType != 1}).remove();
}
var little_me = this.clone();
little_me.find('script,.htmldumper').remove();
little_me.find('svg').html('');
walk(little_me, wipe_text_nodes);
return little_me.html();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment