OK, I have a little function walking the tree like this:
function walkTree(node, func, args) {
func(node, args);
node = node.firstChild;
while (node) {
walkTree(node, func, args);
node = node.nextSibling;
}
}
And another function that would pick up only the text nodes like so:
function selectTextNodes(node, nodes) {
if (node instanceof Text) {
nodes.push(node);
}
}
And finally, using both:
texts = [];
walkTree(body, selectTextNodes, texts);
However, it doesn’t fill the list at all!
If I were to modify the test as to use Node.nodeType it would work:
function selectTextNodes(node, nodes) {
if (node.nodeType == Node.TEXT_NODE) {
nodes.push(node);
}
}
On the other hand, in the console it works both ways:
t = window.document.createTextNode("test");
r = (t.nodeType == Node.TEXT_NODE) && (t instanceof Text);
That is, r is true.
Note that, all functions are nested inside another function that receives the body variable. In my case, this is the contentDocument.body of an iframe. There is no x-domain restriction being applied.
Any idea what’s going on?
There are different
Textinterfaces in the different windows. So, if you have a DOM node from your iframe document, it is not aninstanceof window.Text, but aninstanceof iframe.contentWindow.Text. Afaik, also the availability of theTextinterface as a Javascript object is non-standard.That is why you just should check the
nodeTypeof the elements. But notice that (older?) IE does not support theTEXT_NODEconstant onNode, so you will either need to compare with3or assign that value as a polyfill toNode.TEXT_NODE.