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.BrowserRunner;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.junit.Test;
21  import org.junit.runner.RunWith;
22  
23  /**
24   * Tests for {@link NodeIterator}.
25   *
26   * @author Ahmed Ashour
27   */
28  @RunWith(BrowserRunner.class)
29  public class NodeIteratorTest extends WebDriverTestCase {
30  
31      /**
32       * @throws Exception if the test fails
33       */
34      @Test
35      @Alerts({"[object HTMLDivElement]", "[object HTMLSpanElement]", "[object HTMLSpanElement]",
36               "[object HTMLSpanElement]"})
37      public void filterNull() throws Exception {
38          final String html = DOCTYPE_HTML
39              + "<html>\n"
40              + "<head>\n"
41              + "  <script>\n"
42              + LOG_TITLE_FUNCTION
43              + "    function test() {\n"
44              + "      if (document.createNodeIterator) {\n"
45              + "        var nodeIterator = document.createNodeIterator(\n"
46              + "          document.getElementById('myId'),\n"
47              + "          NodeFilter.SHOW_ELEMENT,\n"
48              + "          null\n"
49              + "        );\n"
50  
51              + "        var currentNode;\n"
52              + "        while (currentNode = nodeIterator.nextNode()) {\n"
53              + "          log(currentNode);\n"
54              + "        }\n"
55              + "      }\n"
56              + "    }\n"
57              + "  </script>\n"
58              + "</head>\n"
59              + "<body onload='test()'>\n"
60              + "<div id='myId'><span>a</span><span>b</span><span>c</span></div>\n"
61              + "</body></html>";
62  
63          loadPageVerifyTitle2(html);
64      }
65  
66      /**
67       * @throws Exception if the test fails
68       */
69      @Test
70      @Alerts("[object HTMLParagraphElement]")
71      public void filterFunction() throws Exception {
72          final String html = DOCTYPE_HTML
73              + "<html>\n"
74              + "<head>\n"
75              + "  <script>\n"
76              + LOG_TITLE_FUNCTION
77              + "    function test() {\n"
78              + "      if (document.createNodeIterator) {\n"
79              + "        var nodeIterator = document.createNodeIterator(\n"
80              + "          document.getElementById('myId'),\n"
81              + "          NodeFilter.SHOW_ELEMENT,\n"
82              + "          function(node) {\n"
83              + "            return node.nodeName.toLowerCase() === 'p' ? NodeFilter.FILTER_ACCEPT"
84              + " : NodeFilter.FILTER_REJECT;\n"
85              + "          }\n"
86              + "        );\n"
87  
88              + "        var currentNode;\n"
89              + "        while (currentNode = nodeIterator.nextNode()) {\n"
90              + "          log(currentNode);\n"
91              + "        }\n"
92              + "      }\n"
93              + "    }\n"
94              + "  </script>\n"
95              + "</head>\n"
96              + "<body onload='test()'>\n"
97              + "<div id='myId'><span>a</span><p>b</p><span>c</span></div>\n"
98              + "</body></html>";
99  
100         loadPageVerifyTitle2(html);
101     }
102 
103     /**
104      * @throws Exception if the test fails
105      */
106     @Test
107     @Alerts("def")
108     public void filterObject() throws Exception {
109         final String html = DOCTYPE_HTML
110             + "<html>\n"
111             + "<head>\n"
112             + "  <script>\n"
113             + LOG_TITLE_FUNCTION
114             + "    function test() {\n"
115             + "      if (document.createNodeIterator) {\n"
116             + "        var nodeIterator = document.createNodeIterator(\n"
117             + "          document.getElementById('myId'),\n"
118             + "          NodeFilter.SHOW_TEXT,\n"
119             + "          { acceptNode: function(node) {\n"
120             + "            if (node.data.indexOf('e') != -1) {\n"
121             + "              return NodeFilter.FILTER_ACCEPT;\n"
122             + "            }\n"
123             + "          } }\n"
124             + "        );\n"
125 
126             + "        var currentNode;\n"
127             + "        while (currentNode = nodeIterator.nextNode()) {\n"
128             + "          log(currentNode.data);\n"
129             + "        }\n"
130             + "      }\n"
131             + "    }\n"
132             + "  </script>\n"
133             + "</head>\n"
134             + "<body onload='test()'>\n"
135             + "<div id='myId'><span>abc</span><p>def</p><span>ghi</span></div>\n"
136             + "</body></html>";
137 
138         loadPageVerifyTitle2(html);
139     }
140 
141     /**
142      * Test case for issue 1982.
143      * @throws Exception if the test fails
144      */
145     @Test
146     @Alerts({"1", "11", "12"})
147     public void subroot() throws Exception {
148         final String html = DOCTYPE_HTML
149             + "<html>\n"
150             + "<head>\n"
151             + "  <script>\n"
152             + LOG_TITLE_FUNCTION
153             + "    function test() {\n"
154             + "      if (document.createNodeIterator) {\n"
155             + "        var nodeIterator = document.createNodeIterator(\n"
156             + "          document.getElementById('1'),\n"
157             + "          NodeFilter.SHOW_ELEMENT );\n"
158 
159             + "        var currentNode;\n"
160             + "        while (currentNode = nodeIterator.nextNode()) {\n"
161             + "          log(currentNode.id);\n"
162             + "        }\n"
163             + "      }\n"
164             + "    }\n"
165             + "  </script>\n"
166             + "</head>\n"
167             + "<body onload='test()'>\n"
168             + "<div id='before'>before</div>\n"
169             + "<table>\n"
170             + "  <tr id='1'>\n"
171             + "    <td id='11'>11</td>\n"
172             + "    <td id='12'>12</td>\n"
173             + "  </tr>\n"
174             + "  <tr id='2'>\n"
175             + "    <td id='21'>21</td>\n"
176             + "    <td id='22'>22</td>\n"
177             + "  </tr>\n"
178             + "</table>\n"
179             + "<div id='after'>after</div>\n"
180             + "</body></html>";
181 
182         loadPageVerifyTitle2(html);
183     }
184 }