A general-purpose DOM tree walker based on https://stackoverflow.com/questions/10730309/find-all-text-nodes-in-html-page (Phrogz' answer and its comments).
The textNodesUnder()
function would then look like this:
function textNodesUnder(el) {
return walkNodeTree(el, {
inspect: n => !['STYLE', 'SCRIPT'].includes(n.nodeName),
collect: n => (n.nodeType === Node.TEXT_NODE),
//callback: n => console.log(n.nodeName, n),
});
}
For reference, this is probably how I'd write it (with some unnecessary modern syntax thrown in):
It seems to be faster from my amateur benchmarks (at least in Chrome). If I use
NodeFilter.SHOW_ALL
with my version it shows similar results to the original, so usingNodeFilter.SHOW_TEXT
is what seems to make the biggest difference (rather than the other little changes).Of course if any of the elements you wanted to filter out were able to contain nested elements, the original would handle those cases by just adding the tags to the array, while this version would need extra work.
Wrong again, they can have elements inserted into them programmatically.style/script/title
can only contain text nodes though, as far I know.