1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
---
title: NodeFilter
slug: Web/API/NodeFilter
translation_of: Web/API/NodeFilter
---
<div>{{APIRef("DOM")}}</div>
<div>Uma interface <strong><code>NodeFilter</code></strong> representa um objeto usado para filtrar os nós (elementos), em uma iteração {{ domxref("NodeIterator") }} ou {{ domxref("TreeWalker") }}. 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.</div>
<div> </div>
<div class="note">
<p>O navegador não fornece nenhum objeto que implemente essa interface. É esperado que o desenvolvedor escreva tal objeto, utilizando o método <code>acceptNode()</code> conforme as suas necessidades, podendo mesclar com objetos do tipo {{domxref("TreeWalker")}} ou {{domxref("NodeIterator")}} em sua implementação.. </p>
</div>
<h2 id="Properties">Properties</h2>
<p><em>Essa interface não implementa, nem herda, nenhuma propriedade.</em></p>
<h2 id="Methods">Methods</h2>
<p><em>Essa interface não herda nenhum método.</em></p>
<dl>
<dt>{{domxref("NodeFilter.acceptNode()")}}</dt>
<dd>Returns an <code>unsigned short</code> 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 <code>NodeFilter</code>. Possible return values are:
<table class="standard-table">
<tbody>
<tr>
<td class="header">Constant</td>
<td class="header">Description</td>
</tr>
<tr>
<td><code>FILTER_ACCEPT</code></td>
<td>Value returned by the {{ domxref("NodeFilter.acceptNode()") }} method when a node should be accepted.</td>
</tr>
<tr>
<td><code>FILTER_REJECT</code></td>
<td>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.</td>
</tr>
<tr>
<td><code>FILTER_SKIP</code></td>
<td>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".</td>
</tr>
</tbody>
</table>
</dd>
</dl>
<h2 id="Example">Example</h2>
<pre class="brush: 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);
}
</pre>
<h2 id="Specification" name="Specification">Specifications</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('DOM WHATWG', '#interface-nodefilter', 'NodeFilter')}}</td>
<td>{{Spec2('DOM WHATWG')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('DOM2 Traversal_Range', 'traversal.html#Traversal-NodeFilter', 'NodeFilter')}}</td>
<td>{{Spec2('DOM2 Traversal_Range')}}</td>
<td>Initial definition.</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Compatibilidade com navegadores</h2>
<p>{{CompatibilityTable}}</p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Edge</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatChrome(1)}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatGeckoDesktop("1.8.1")}}</td>
<td>9.0</td>
<td>9.0</td>
<td>3.0</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android Webview</th>
<th>Chrome for Android</th>
<th>Edge</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatGeckoMobile("1.8.1")}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>9.0</td>
<td>3.0</td>
</tr>
</tbody>
</table>
</div>
<h2 id="See_also">See also</h2>
<ul>
<li>Related interfaces: {{domxref("TreeWalker")}}, {{domxref("NodeIterator")}}.</li>
</ul>
|