Skip to content

Instantly share code, notes, and snippets.

@mhgbrown
Created July 9, 2015 18:30
Show Gist options
  • Save mhgbrown/f9a692b3a6135c11f775 to your computer and use it in GitHub Desktop.
Save mhgbrown/f9a692b3a6135c11f775 to your computer and use it in GitHub Desktop.
retrieve all text nodes which are not made of space characters using TreeWalker API
/**
* Retrieve all text nodes which are not
* made of space characters.
* @return {Array} A collection of text nodes
**/
var getTextNodes = function() {
var walker = document.createTreeWalker(
this.rootElement,
NodeFilter.SHOW_TEXT,
null,
false
),
node = walker.nextNode(),
textNodes = [];
while(node) {
if(!this.REGEXP_SPACES.test(node.nodeValue)) {
textNodes.push(node);
}
node = walker.nextNode();
}
return textNodes;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment