View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript.host.dom;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for {@link NodeIterator}.
23   *
24   * @author Ahmed Ashour
25   */
26  public class NodeIteratorTest extends WebDriverTestCase {
27  
28      /**
29       * @throws Exception if the test fails
30       */
31      @Test
32      @Alerts({"[object HTMLDivElement]", "[object HTMLSpanElement]", "[object HTMLSpanElement]",
33               "[object HTMLSpanElement]"})
34      public void filterNull() throws Exception {
35          final String html = DOCTYPE_HTML
36              + "<html>\n"
37              + "<head>\n"
38              + "  <script>\n"
39              + LOG_TITLE_FUNCTION
40              + "    function test() {\n"
41              + "      if (document.createNodeIterator) {\n"
42              + "        var nodeIterator = document.createNodeIterator(\n"
43              + "          document.getElementById('myId'),\n"
44              + "          NodeFilter.SHOW_ELEMENT,\n"
45              + "          null\n"
46              + "        );\n"
47  
48              + "        var currentNode;\n"
49              + "        while (currentNode = nodeIterator.nextNode()) {\n"
50              + "          log(currentNode);\n"
51              + "        }\n"
52              + "      }\n"
53              + "    }\n"
54              + "  </script>\n"
55              + "</head>\n"
56              + "<body onload='test()'>\n"
57              + "<div id='myId'><span>a</span><span>b</span><span>c</span></div>\n"
58              + "</body></html>";
59  
60          loadPageVerifyTitle2(html);
61      }
62  
63      /**
64       * @throws Exception if the test fails
65       */
66      @Test
67      @Alerts("[object HTMLParagraphElement]")
68      public void filterFunction() throws Exception {
69          final String html = DOCTYPE_HTML
70              + "<html>\n"
71              + "<head>\n"
72              + "  <script>\n"
73              + LOG_TITLE_FUNCTION
74              + "    function test() {\n"
75              + "      if (document.createNodeIterator) {\n"
76              + "        var nodeIterator = document.createNodeIterator(\n"
77              + "          document.getElementById('myId'),\n"
78              + "          NodeFilter.SHOW_ELEMENT,\n"
79              + "          function(node) {\n"
80              + "            return node.nodeName.toLowerCase() === 'p' ? NodeFilter.FILTER_ACCEPT"
81              + " : NodeFilter.FILTER_REJECT;\n"
82              + "          }\n"
83              + "        );\n"
84  
85              + "        var currentNode;\n"
86              + "        while (currentNode = nodeIterator.nextNode()) {\n"
87              + "          log(currentNode);\n"
88              + "        }\n"
89              + "      }\n"
90              + "    }\n"
91              + "  </script>\n"
92              + "</head>\n"
93              + "<body onload='test()'>\n"
94              + "<div id='myId'><span>a</span><p>b</p><span>c</span></div>\n"
95              + "</body></html>";
96  
97          loadPageVerifyTitle2(html);
98      }
99  
100     /**
101      * @throws Exception if the test fails
102      */
103     @Test
104     @Alerts("def")
105     public void filterObject() throws Exception {
106         final String html = DOCTYPE_HTML
107             + "<html>\n"
108             + "<head>\n"
109             + "  <script>\n"
110             + LOG_TITLE_FUNCTION
111             + "    function test() {\n"
112             + "      if (document.createNodeIterator) {\n"
113             + "        var nodeIterator = document.createNodeIterator(\n"
114             + "          document.getElementById('myId'),\n"
115             + "          NodeFilter.SHOW_TEXT,\n"
116             + "          { acceptNode: function(node) {\n"
117             + "            if (node.data.indexOf('e') != -1) {\n"
118             + "              return NodeFilter.FILTER_ACCEPT;\n"
119             + "            }\n"
120             + "          } }\n"
121             + "        );\n"
122 
123             + "        var currentNode;\n"
124             + "        while (currentNode = nodeIterator.nextNode()) {\n"
125             + "          log(currentNode.data);\n"
126             + "        }\n"
127             + "      }\n"
128             + "    }\n"
129             + "  </script>\n"
130             + "</head>\n"
131             + "<body onload='test()'>\n"
132             + "<div id='myId'><span>abc</span><p>def</p><span>ghi</span></div>\n"
133             + "</body></html>";
134 
135         loadPageVerifyTitle2(html);
136     }
137 
138     /**
139      * Test case for issue 1982.
140      * @throws Exception if the test fails
141      */
142     @Test
143     @Alerts({"1", "11", "12"})
144     public void subroot() throws Exception {
145         final String html = DOCTYPE_HTML
146             + "<html>\n"
147             + "<head>\n"
148             + "  <script>\n"
149             + LOG_TITLE_FUNCTION
150             + "    function test() {\n"
151             + "      if (document.createNodeIterator) {\n"
152             + "        var nodeIterator = document.createNodeIterator(\n"
153             + "          document.getElementById('1'),\n"
154             + "          NodeFilter.SHOW_ELEMENT );\n"
155 
156             + "        var currentNode;\n"
157             + "        while (currentNode = nodeIterator.nextNode()) {\n"
158             + "          log(currentNode.id);\n"
159             + "        }\n"
160             + "      }\n"
161             + "    }\n"
162             + "  </script>\n"
163             + "</head>\n"
164             + "<body onload='test()'>\n"
165             + "<div id='before'>before</div>\n"
166             + "<table>\n"
167             + "  <tr id='1'>\n"
168             + "    <td id='11'>11</td>\n"
169             + "    <td id='12'>12</td>\n"
170             + "  </tr>\n"
171             + "  <tr id='2'>\n"
172             + "    <td id='21'>21</td>\n"
173             + "    <td id='22'>22</td>\n"
174             + "  </tr>\n"
175             + "</table>\n"
176             + "<div id='after'>after</div>\n"
177             + "</body></html>";
178 
179         loadPageVerifyTitle2(html);
180     }
181 }