-
-
Save coffeant/965774 to your computer and use it in GitHub Desktop.
Small Walker: A small and simple JavaScript DOM walker
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
/*! | |
* Small Walker - v0.1.1 - 5/5/2011 | |
* http://benalman.com/ | |
* | |
* Copyright (c) 2011 "Cowboy" Ben Alman | |
* Dual licensed under the MIT and GPL licenses. | |
* http://benalman.com/about/license/ | |
*/ | |
// Walk the DOM, depth-first (HTML order). Inside the callback, `this` is the | |
// element, and the only argument passed is the current depth. If the callback | |
// returns false, its children will be skipped. | |
// | |
// Based on https://gist.github.com/240274 | |
function walk(node, callback) { | |
var skip, tmp; | |
var depth = 0; | |
do { | |
if ( !skip ) { | |
skip = callback.call(node, depth) === false; | |
} | |
if ( !skip && (tmp = node.firstChild) ) { | |
depth++; | |
} else if ( tmp = node.nextSibling ) { | |
skip = false; | |
} else { | |
depth--; | |
skip = true; | |
tmp = node.parentNode; | |
} | |
node = tmp; | |
} while ( depth > 0 ); | |
} |
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
// Small Walker v0.1.1, 5/5/2011, http://benalman.com/ Copyright (c) 2011 "Cowboy" Ben Alman, dual licensed MIT/GPL. | |
;function walk(a,b){var c,d,e=0;do c||(c=b.call(a,e)===!1),!c&&(d=a.firstChild)?++e:(d=a.nextSibling)?c=0:(--e,c=1,d=a.parentNode),a=d;while(e>0)}; |
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
// Replace any "o" with "ø" throughout the DOM, retaining any existing element | |
// structure and references (silly example). | |
walk(document.documentElement, function() { | |
if ( this.nodeType == 3 ) { | |
this.nodeValue = this.nodeValue.replace(/o/g, 'ø'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment