diff --git a/files/en-us/_redirects.txt b/files/en-us/_redirects.txt index a7736ed8d6c2e3d..f01bf46c517eb5c 100644 --- a/files/en-us/_redirects.txt +++ b/files/en-us/_redirects.txt @@ -1571,7 +1571,7 @@ /en-US/docs/DOM/Node.removeChild /en-US/docs/Web/API/Node/removeChild /en-US/docs/DOM/Node.replaceChild /en-US/docs/Web/API/Node/replaceChild /en-US/docs/DOM/Node.textContent /en-US/docs/Web/API/Node/textContent -/en-US/docs/DOM/NodeFilter /en-US/docs/Web/API/NodeFilter +/en-US/docs/DOM/NodeFilter /en-US/docs/Web/API/Document/createNodeIterator /en-US/docs/DOM/NodeIterator /en-US/docs/Web/API/NodeIterator /en-US/docs/DOM/NodeIterator.detach /en-US/docs/Web/API/NodeIterator/detach /en-US/docs/DOM/NodeIterator.filter /en-US/docs/Web/API/NodeIterator/filter @@ -2904,7 +2904,7 @@ /en-US/docs/Document_Object_Model_(DOM)/Node.removeChild /en-US/docs/Web/API/Node/removeChild /en-US/docs/Document_Object_Model_(DOM)/Node.replaceChild /en-US/docs/Web/API/Node/replaceChild /en-US/docs/Document_Object_Model_(DOM)/Node.textContent /en-US/docs/Web/API/Node/textContent -/en-US/docs/Document_Object_Model_(DOM)/NodeFilter /en-US/docs/Web/API/NodeFilter +/en-US/docs/Document_Object_Model_(DOM)/NodeFilter /en-US/docs/Web/API/Document/createNodeIterator /en-US/docs/Document_Object_Model_(DOM)/NodeIterator /en-US/docs/Web/API/NodeIterator /en-US/docs/Document_Object_Model_(DOM)/NodeIterator.detach /en-US/docs/Web/API/NodeIterator/detach /en-US/docs/Document_Object_Model_(DOM)/NodeIterator.filter /en-US/docs/Web/API/NodeIterator/filter @@ -8817,7 +8817,9 @@ /en-US/docs/Web/API/Node/outerText /en-US/docs/Web/API/HTMLElement/outerText /en-US/docs/Web/API/Node/prefix /en-US/docs/Web/API/Element/prefix /en-US/docs/Web/API/Node/rootNode /en-US/docs/Web/API/Node/getRootNode -/en-US/docs/Web/API/NodeFilter.acceptNode /en-US/docs/Web/API/NodeFilter/acceptNode +/en-US/docs/Web/API/NodeFilter /en-US/docs/Web/API/Document/createNodeIterator +/en-US/docs/Web/API/NodeFilter.acceptNode /en-US/docs/Web/API/Document/createNodeIterator +/en-US/docs/Web/API/NodeFilter/acceptNode /en-US/docs/Web/API/Document/createNodeIterator /en-US/docs/Web/API/NodeIterator.detach /en-US/docs/Web/API/NodeIterator/detach /en-US/docs/Web/API/NodeIterator.filter /en-US/docs/Web/API/NodeIterator/filter /en-US/docs/Web/API/NodeIterator.nextNode /en-US/docs/Web/API/NodeIterator/nextNode diff --git a/files/en-us/_wikihistory.json b/files/en-us/_wikihistory.json index 4e87713b6c1ebbb..cb39583698b42aa 100644 --- a/files/en-us/_wikihistory.json +++ b/files/en-us/_wikihistory.json @@ -60850,39 +60850,6 @@ "Fguisset" ] }, - "Web/API/NodeFilter": { - "modified": "2020-10-15T21:11:18.491Z", - "contributors": [ - "lionel-rowe", - "Zearin_Galaurum", - "ExE-Boss", - "jpmedley", - "krosylight", - "JonMarkPerry", - "abbycar", - "a0preetham", - "fscholz", - "kollishivagangadhar", - "kennethcachia", - "Jesse", - "teoli", - "Sheppy", - "Brettz9", - "RobG" - ] - }, - "Web/API/NodeFilter/acceptNode": { - "modified": "2020-10-15T21:24:48.176Z", - "contributors": [ - "sideshowbarker", - "ExE-Boss", - "abbycar", - "fscholz", - "sayanchowdhury", - "teoli", - "Khodaidad_Basharmand" - ] - }, "Web/API/NodeIterator": { "modified": "2020-10-15T21:14:44.038Z", "contributors": [ diff --git a/files/en-us/web/api/document/createnodeiterator/index.md b/files/en-us/web/api/document/createnodeiterator/index.md index 3efe26e9b4bdafa..4010a847a2a5ee6 100644 --- a/files/en-us/web/api/document/createnodeiterator/index.md +++ b/files/en-us/web/api/document/createnodeiterator/index.md @@ -48,15 +48,25 @@ const nodeIterator = document.createNodeIterator(root[, whatToShow[, filter]]); | `NodeFilter.SHOW_TEXT` | `4` | Shows {{ domxref("Text") }} nodes. | - `filter` {{ optional_inline() }} - - : An object implementing the {{ domxref("NodeFilter") }} interface. Its - `acceptNode()` method will be called for each node in the subtree based at - root which is accepted as included by the whatToShow flag to determine whether or not - to include it in the list of iterable nodes (a simple callback function may also be - used instead). The method should return one of `NodeFilter.FILTER_ACCEPT`, - `NodeFilter.FILTER_REJECT`, or `NodeFilter.FILTER_SKIP`. See the - [Example](#example). - -## Example + - : A callback function or an object with an `acceptNode()` method. The function or method will be called for each node in the subtree based at root which is accepted as included by the whatToShow flag to determine whether or not to include it in the list of iterable nodes. The method should return one of `NodeFilter.FILTER_ACCEPT`, `NodeFilter.FILTER_REJECT`, or `NodeFilter.FILTER_SKIP`. See the [Example](#example). + +## Examples + +```js +const nodeIterator = document.createNodeIterator( + document.body, + NodeFilter.SHOW_ELEMENT, + (node) => node.nodeName.toLowerCase() === 'p' ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT +); +const pars = []; +let currentNode; + +while (currentNode = nodeIterator.nextNode()) { + pars.push(currentNode); +} +``` + +The same, but using an object with an `acceptNode()` method: ```js const nodeIterator = document.createNodeIterator( diff --git a/files/en-us/web/api/nodefilter/acceptnode/index.md b/files/en-us/web/api/nodefilter/acceptnode/index.md deleted file mode 100644 index e25384462076b0c..000000000000000 --- a/files/en-us/web/api/nodefilter/acceptnode/index.md +++ /dev/null @@ -1,130 +0,0 @@ ---- -title: NodeFilter.acceptNode() -slug: Web/API/NodeFilter/acceptNode -tags: - - API - - DOM - - DOM Reference - - Method - - NodeFilter -browser-compat: api.NodeFilter.acceptNode ---- -{{APIRef("DOM")}} - -The **`NodeFilter.acceptNode()`** method returns an -`unsigned short` that will be used to tell if a given {{domxref("Node")}} -must be accepted or not by the {{ domxref("NodeIterator") }} or {{ domxref("TreeWalker") - }} iteration algorithm. This method is expected to be written by the user of a -`NodeFilter`. Possible return values are: - - - - - - - - - - - - - - - - - - - - - - -
ConstantDescription
NodeFilter.FILTER_ACCEPT - Value returned by the - {{ domxref("NodeFilter.acceptNode()") }} method when a node - should be accepted. -
NodeFilter.FILTER_REJECT -

- Value to be returned by the - {{domxref("NodeFilter.acceptNode()")}} method when a - node should be rejected. For {{domxref("TreeWalker")}}, child - nodes are also rejected. -

-

- For {{ domxref("NodeIterator") }}, this flag is synonymous - with FILTER_SKIP. -

-
NodeFilter.FILTER_SKIP -

- Value to be returned by - {{domxref("NodeFilter.acceptNode()")}} for nodes to be - skipped by the {{domxref("NodeIterator")}} or - {{domxref("TreeWalker")}} object. -

-

- The children of skipped nodes are still considered. This is treated as - "skip this node but not its children". -

-
- -The function should return `NodeFilter.FILTER_ACCEPT`, which causes the -TreeWalker to return the node, `NodeFilter.FILTER_REJECT`, which causes the -TreeWalker to ignore the entire subtree, or `NodeFilter.FILTER_SKIP`. - -The browser doesn't provide any object implementing this method. It is the user who is -expected to write an object implementing the {{domxref("NodeFilter")}} interface, -tailoring the `acceptNode()` method to its needs, and using it with some -{{domxref("TreeWalker")}} or {{domxref("NodeIterator")}} objects. - -## Syntax - -```js -nodeFilter.acceptNode(node) -``` - -### Parameters - -- _node_ - - : Is a {{domxref("Node")}} being the object to check against the filter. - -## Example - -```js -var nodeIterator = document.createNodeIterator( - // Node to use as root - document.getElementById('someId'), - - // Only consider nodes that are text nodes (nodeType 3) - NodeFilter.SHOW_TEXT, - - // Object containing the function to use for the acceptNode method - // of the NodeFilter - { acceptNode: function(node) { - // Logic to determine whether to accept, reject or skip node - // In this case, only accept nodes that have content - // other than whitespace - if ( ! /^\s*$/.test(node.data) ) { - return NodeFilter.FILTER_ACCEPT; - } - } - } -); - -// Show the content of every non-empty text node that is a child of root -var node; - -while ((node = nodeIterator.nextNode())) { - alert(node.data); -} -``` - -## Specifications - -{{Specifications}} - -## Browser compatibility - -{{Compat}} - -## See also - -- The interface it belongs to: {{domxref("NodeFilter")}}. diff --git a/files/en-us/web/api/nodefilter/index.md b/files/en-us/web/api/nodefilter/index.md deleted file mode 100644 index 951c463a5288814..000000000000000 --- a/files/en-us/web/api/nodefilter/index.md +++ /dev/null @@ -1,124 +0,0 @@ ---- -title: NodeFilter -slug: Web/API/NodeFilter -tags: - - API - - DOM - - DOM Reference -browser-compat: api.NodeFilter ---- -{{APIRef("DOM")}} - -A **`NodeFilter`** interface represents an object used to filter the nodes in a {{domxref("NodeIterator")}} or {{domxref("TreeWalker")}}. A `NodeFilter` knows nothing about the document or traversing nodes; it only knows how to evaluate a single node against the provided filter. - -> **Note:** The browser doesn't provide any object implementing this interface. It is the user who is expected to write one, tailoring the `acceptNode()` method to its needs, and using it with some {{domxref("TreeWalker")}} or {{domxref("NodeIterator")}} objects. - -## Properties - -_This interface neither implements nor inherits any properties._ - -## Methods - -_This interface doesn't inherit any methods._ - -- {{domxref("NodeFilter.acceptNode()")}} - - - : Returns an `unsigned short` that will be used to tell if a given {{domxref("Node")}} must be accepted or not by the {{ domxref("NodeIterator") }} or {{ domxref("TreeWalker") }} iteration algorithm. - - This method is expected to be written by the user of a `NodeFilter`. Possible return values are: - - - - - - - - - - - - - - - - - - - - - - -
ConstantDescription
FILTER_ACCEPT - Value returned by the - {{domxref("NodeFilter.acceptNode()")}} method when a node - should be accepted. -
FILTER_REJECT -

- Value to be returned by the - {{domxref("NodeFilter.acceptNode()")}} method when a - node should be rejected. For {{domxref("TreeWalker")}}, child - nodes are also rejected. -

-

- For {{ domxref("NodeIterator") }}, this flag is synonymous - with FILTER_SKIP. -

-
FILTER_SKIP -

- Value to be returned by - {{domxref("NodeFilter.acceptNode()")}} for nodes to be - skipped by the {{domxref("NodeIterator")}} or - {{domxref("TreeWalker")}} object. -

-

- The children of skipped nodes are still considered. This is treated as - "skip this node but not its children". -

-
- -## Example - -```js -const nodeIterator = document.createNodeIterator( - // Node to use as root - document.getElementById('someId'), - - // Only consider nodes that are text nodes (nodeType 3) - NodeFilter.SHOW_TEXT, - - // Object containing the function to use for the acceptNode method - // of the NodeFilter - { - acceptNode: function(node) { - // Logic to determine whether to accept, reject or skip node - // In this case, only accept nodes that have content - // other than whitespace - if (/\S/.test(node.data)) { - return NodeFilter.FILTER_ACCEPT - } - } - } -); - -// Show the content of every non-empty text node that is a child of root -let node; - -while ((node = nodeIterator.nextNode())) { - alert(node.data) -} -``` - -## Specifications - -{{Specifications}} - -## Browser compatibility - -{{Compat}} - -## See also - -- **Related interfaces** - - - {{domxref("TreeWalker")}} - - {{domxref("NodeIterator")}}.