Find all text nodes in HTML page
Based on @kennebec's answer, a slightly tighter implementation of the same logic:
function textNodesUnder(node){
var all = [];
for (node=node.firstChild;node;node=node.nextSibling){
if (node.nodeType==3) all.push(node);
else all = all.concat(textNodesUnder(node));
}
return all;
}
However, far faster, tighter, and more elegant is using createTreeWalker
so that the browser filters out everything but the text nodes for you:
function textNodesUnder(el){
var n, a=[], walk=document.createTreeWalker(el,NodeFilter.SHOW_TEXT,null,false);
while(n=walk.nextNode()) a.push(n);
return a;
}
function deepText(node){
var A= [];
if(node){
node= node.firstChild;
while(node!= null){
if(node.nodeType== 3) A[A.length]=node;
else A= A.concat(deepText(node));
node= node.nextSibling;
}
}
return A;
}