Document: createTreeWalker() method
The Document.createTreeWalker() creator method returns a newly created TreeWalker object.
Syntax
js
createTreeWalker(root)
createTreeWalker(root, whatToShow)
createTreeWalker(root, whatToShow, filter)
Parameters
root-
A root
Nodeof thisTreeWalkertraversal. Typically this will be an element owned by the document. whatToShowOptional-
An
unsigned longrepresenting a bitmask created by combining the constant properties ofNodeFilter. It is a convenient way of filtering for certain types of node. It defaults to0xFFFFFFFF, representing theSHOW_ALLconstant.Constant Numerical value Description NodeFilter.SHOW_ALL4294967295(that is the max value ofunsigned long)Shows all nodes. NodeFilter.SHOW_ATTRIBUTEDeprecated2Shows attribute Attrnodes. This is meaningful only when creating aTreeWalkerwith anAttrnode as its root; in this case, it means that the attribute node will appear in the first position of the iteration or traversal. Since attributes are never children of other nodes, they do not appear when traversing over the document tree.NodeFilter.SHOW_CDATA_SECTIONDeprecated8Shows CDATASectionnodes.NodeFilter.SHOW_COMMENT128Shows Commentnodes.NodeFilter.SHOW_DOCUMENT256Shows Documentnodes.NodeFilter.SHOW_DOCUMENT_FRAGMENT1024Shows DocumentFragmentnodes.NodeFilter.SHOW_DOCUMENT_TYPE512Shows DocumentTypenodes.NodeFilter.SHOW_ELEMENT1Shows Elementnodes.NodeFilter.SHOW_ENTITYDeprecated32Legacy, no longer usable. NodeFilter.SHOW_ENTITY_REFERENCEDeprecated16Legacy, no longer usable. NodeFilter.SHOW_NOTATIONDeprecated2048Legacy, no longer usable. NodeFilter.SHOW_PROCESSING_INSTRUCTION64Shows ProcessingInstructionnodes.NodeFilter.SHOW_TEXT4Shows Textnodes. filterOptional-
A callback function or an object with an
acceptNode()method, which returnsNodeFilter.FILTER_ACCEPT,NodeFilter.FILTER_REJECT, orNodeFilter.FILTER_SKIP. The function or method will be called for each node in the subtree based atrootwhich is accepted as included by thewhatToShowflag to determine whether or not to include it in the list of iterable nodes:- If the return value is
NodeFilter.FILTER_ACCEPT, this node is included. - If the return value is
NodeFilter.FILTER_REJECT, any node in the subtree based at this node is not included. - If the return value is
NodeFilter.FILTER_SKIP, this node is not included.
- If the return value is
Return value
A new TreeWalker object.
Examples
Using whatToShow
This example uses whatToShow to transform text contents into upper case. Note that the text nodes of the descendants of the #root element are also traversed despite of the fact that they are not child nodes of the #root element.
HTML
html
<div id="root">
This is a text node.
<span>And this is a <code>span</code> element.</span>
</div>
JavaScript
js
const treeWalker = document.createTreeWalker(
document.querySelector("#root"),
NodeFilter.SHOW_TEXT
);
let currentNode;
while ((currentNode = treeWalker.nextNode())) {
currentNode.data = currentNode.data.toUpperCase();
}
Result
Using filter
This example uses filter to escape text contents. For any .escape element, the text contents of all its descendants will be escaped using encodeURI(), unless a descendant is also a descendant of a .no-escape element.
HTML
html
<div>
<div>
This is not escaped. <span class="escape">But this is escaped.</span>
</div>
<div class="escape">This is escaped.</div>
<div class="no-escape">This is not escaped.</div>
</div>
<hr />
<div class="escape">
<div>
This is escaped. <span class="no-escape">But this is not escaped.</span>
</div>
<div class="no-escape">This is not escaped.</div>
</div>
<hr />
<div class="no-escape">
<div>This is not escaped.</div>
<div class="escape">This is not escaped.</div>
</div>
CSS
css
.escape {
border: dashed;
}
.no-escape {
border: solid;
}
JavaScript
js
const treeWalker = document.createTreeWalker(
document.body,
NodeFilter.SHOW_ELEMENT,
(node) =>
node.classList.contains("no-escape")
? NodeFilter.FILTER_REJECT
: node.closest(".escape")
? NodeFilter.FILTER_ACCEPT
: NodeFilter.FILTER_SKIP
);
let currentNode;
while ((currentNode = treeWalker.nextNode())) {
const textTreeWalker = document.createTreeWalker(
currentNode,
NodeFilter.SHOW_ALL,
(node) =>
node.nodeName === "#text" && !/^\s*$/.test(node.data)
? NodeFilter.FILTER_ACCEPT
: NodeFilter.FILTER_REJECT
);
let currentTextNode;
while ((currentTextNode = textTreeWalker.nextNode())) {
currentTextNode.data = encodeURI(currentTextNode.data.replace(/\s+/g, " "));
}
}
Result
Specifications
| Specification |
|---|
| DOM Standard # dom-document-createtreewalker |
Browser compatibility
BCD tables only load in the browser
See also
TreeWalker: Related interface