NodeFilter

Uma interface NodeFilter representa um objeto usado para filtrar os nós (elementos), em uma iteração NodeIterator (en-US) ou TreeWalker (en-US). Essas interfaces não conhecem nada sobre o DOM ou sobre como percorrer ou atravessar, elementos; elas apenas sabem como avaliar se um único nó atende aos requisitos do filtro fornecido ou não.

Nota: O navegador não fornece nenhum objeto que implemente essa interface. É esperado que o desenvolvedor escreva tal objeto, utilizando o método acceptNode() conforme as suas necessidades, podendo mesclar com objetos do tipo TreeWalker (en-US) ou NodeIterator (en-US) em sua implementação..

Properties

Essa interface não implementa, nem herda, nenhuma propriedade.

Methods

Essa interface não herda nenhum método.

NodeFilter.acceptNode() (en-US)

Returns an unsigned short that will be used to tell if a given Node must be accepted or not by the NodeIterator (en-US) or TreeWalker (en-US) iteration algorithm. This method is expected to be written by the user of a NodeFilter. Possible return values are:

Constant Description
FILTER_ACCEPT Value returned by the NodeFilter.acceptNode() (en-US) method when a node should be accepted.
FILTER_REJECT Value to be returned by the NodeFilter.acceptNode() (en-US) method when a node should be rejected. For TreeWalker (en-US), child nodes are also rejected. For NodeIterator (en-US), this flag is synonymous with FILTER_SKIP.
FILTER_SKIP Value to be returned by NodeFilter.acceptNode() (en-US) for nodes to be skipped by the NodeIterator (en-US) or TreeWalker (en-US) object. The children of skipped nodes are still considered. This is treated as "skip this node but not its children".

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;
      }
    },
  },
  false,
);

// 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

Specification
DOM Standard
# dom-document-createnodeiterator

Compatibilidade com navegadores

BCD tables only load in the browser

See also