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.html.parser;
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  import org.openqa.selenium.JavascriptExecutor;
23  
24  /**
25   * Test class for {@link HTMLParser}.
26   *
27   * @author Atsushi Nakagawa
28   * @author Ronald Brill
29   */
30  @RunWith(BrowserRunner.class)
31  public class HTMLParser5Test extends WebDriverTestCase {
32  
33      private static final String LOG_INPUT_FORMS =
34              "inputs = document.getElementsByTagName('input');"
35              + "for(i=0; i<inputs.length; i++) {"
36              + "  var inp = inputs[i];"
37              + "  var f = inp.form;"
38              + "  log(inp.id + '-' + (f?f.name:null))"
39              + "}";
40  
41      /**
42       * @throws Exception if the test fails
43       */
44      @Test
45      @Alerts({"i1-f1", "i2-f2"})
46      public void formEnclosure_table() throws Exception {
47          final String html =
48              "<html><body>\n"
49              + "<div>\n"
50              + "<form name='f1'>\n"
51              + "  <table>\n"
52              + "    <input id='i1'>\n"
53              + "</form>\n"
54              + "<form name='f2'>\n"
55              + "  </table>\n"
56              + "</div>\n"
57              + "<input id='i2'>\n"
58              + "</form>\n"
59              + "<script>\n"
60              + LOG_TITLE_FUNCTION
61              + LOG_INPUT_FORMS
62              + "</script>\n"
63              + "</body></html>";
64  
65          loadPageVerifyTitle2(html);
66      }
67  
68      /**
69       * @throws Exception if the test fails
70       */
71      @Test
72      @Alerts({"i1-f1", "i2-f2"})
73      public void formEnclosure_div() throws Exception {
74          final String html =
75              "<html><body>\n"
76              + "<div>\n"
77              + "<form name='f1'>\n"
78              + "  <div>\n"
79              + "    <input id='i1'>\n"
80              + "</form>\n"
81              + "<form name='f2'>\n"
82              + "  </div>\n"
83              + "</div>\n"
84              + "<input id='i2'>\n"
85              + "</form>\n"
86              + "<script>\n"
87              + LOG_TITLE_FUNCTION
88              + LOG_INPUT_FORMS
89              + "</script>\n"
90              + "</body></html>";
91  
92          loadPageVerifyTitle2(html);
93      }
94  
95      /**
96       * @throws Exception if the test fails
97       */
98      @Test
99      @Alerts({"i1-f1", "i2-f1"})
100     public void formEnclosure_table_nestedForms() throws Exception {
101         final String html =
102             "<html><body>\n"
103             + "<div>\n"
104             + "<form name='f1'>\n"
105             + "  <table>\n"
106             + "    <input id='i1'>\n"
107             + "<form name='f2'>\n"
108             + "  </table>\n"
109             + "</div>\n"
110             + "<input id='i2'>\n"
111             + "</form>\n"
112             + "</form>\n"
113             + "<script>\n"
114             + LOG_TITLE_FUNCTION
115             + LOG_INPUT_FORMS
116             + "</script>\n"
117             + "</body></html>";
118 
119         loadPageVerifyTitle2(html);
120     }
121 
122     /**
123      * @throws Exception if the test fails
124      */
125     @Test
126     @Alerts({"i1-f1", "i2-f1"})
127     public void formEnclosure_div_nestedForms() throws Exception {
128         final String html =
129             "<html><body>\n"
130             + "<div>\n"
131             + "<form name='f1'>\n"
132             + "  <div>\n"
133             + "    <input id='i1'>\n"
134             + "<form name='f2'>\n"
135             + "  </div>\n"
136             + "</div>\n"
137             + "<input id='i2'>\n"
138             + "</form>\n"
139             + "</form>\n"
140             + "<script>\n"
141             + LOG_TITLE_FUNCTION
142             + LOG_INPUT_FORMS
143             + "</script>\n"
144             + "</body></html>";
145 
146         loadPageVerifyTitle2(html);
147     }
148 
149     /**
150      * @throws Exception if the test fails
151      */
152     @Test
153     @Alerts({"i1-f1", "i2-f1", "i3-null", "i4-null"})
154     public void formEnclosure_nestedForms() throws Exception {
155         final String html =
156             "<html><body>\n"
157             + "<form name='f1'>\n"
158             + "<input id='i1'>\n"
159             + "<form name='f2'>\n"
160             + "<input id='i2'>\n"
161             + "</form>\n"
162             + "<input id='i3'>\n"
163             + "</form>\n"
164             + "<input id='i4'>\n"
165             + "<script>\n"
166             + LOG_TITLE_FUNCTION
167             + LOG_INPUT_FORMS
168             + "</script>\n"
169             + "</body></html>";
170 
171         loadPageVerifyTitle2(html);
172     }
173 
174     /**
175      * @throws Exception if the test fails
176      */
177     @Test
178     @Alerts({"i1-f1", "i2-f1", "i3-f1", "i4-f1", "i5-f1", "i6-f1", "i7-null"})
179     public void formEnclosure_tree1() throws Exception {
180         final String html =
181             "<html><body>\n"
182             + "<div>\n"
183 
184             + "<form name='f1'>\n"
185             + "  <input id='i1'>\n"
186             + "  <div>\n"
187             + "    <input id='i2'>\n"
188 
189             + "    <table>\n"
190             + "      <input id='i3'>\n"
191             + "</form>\n"
192 
193             + "      <input id='i4'>\n"
194             + "    </table>\n"
195 
196             + "    <input id='i5'>\n"
197             + "  </div>\n"
198             + "  <input id='i6'>\n"
199             + "</div>\n"
200             + "<input id='i7'>\n"
201             + "<script>\n"
202             + LOG_TITLE_FUNCTION
203             + LOG_INPUT_FORMS
204             + "</script>\n"
205             + "</body></html>";
206 
207         loadPageVerifyTitle2(html);
208     }
209 
210     /**
211      * @throws Exception if the test fails
212      */
213     @Test
214     @Alerts({"i1-f1", "i2-f1", "i3-f1", "i4-f2", "i5-f2", "i6-f2", "i7-f1", "i8-null"})
215     public void formEnclosure_tree2() throws Exception {
216         final String html =
217             "<html><body>\n"
218             + "<form name='f1'>\n"
219             + "  <input id='i1'>\n"
220             + "  <div>\n"
221             + "    <input id='i2'>\n"
222             + "</form>\n"
223 
224             + "    <input id='i3'>\n"
225             + "<form name='f2'>\n"
226 
227             + "    <input id='i4'>\n"
228             + "    <div>\n"
229             + "      <input id='i5'>\n"
230 
231             + "</form>\n"
232 
233             + "      <input id='i6'>\n"
234             + "    </div>\n"
235             + "    <input id='i7'>\n"
236             + "  </div>\n"
237             + "  <input id='i8'>\n"
238             + "<script>\n"
239             + LOG_TITLE_FUNCTION
240             + LOG_INPUT_FORMS
241             + "</script>\n"
242             + "</body></html>";
243 
244         loadPageVerifyTitle2(html);
245     }
246 
247     /**
248      * @throws Exception if the test fails
249      */
250     @Test
251     @Alerts({"i1-f1", "i2-null"})
252     public void formEnclosure_tree3() throws Exception {
253         final String html =
254             "<html><body>\n"
255             + "<form name='f1'>\n"
256             + "  <div>\n"
257             + "    <input id='i1'>\n"
258             + "</form>\n"
259             + "  </div>\n"
260             + "  <input id='i2'>\n"
261             + "<script>\n"
262             + LOG_TITLE_FUNCTION
263             + LOG_INPUT_FORMS
264             + "</script>\n"
265             + "</body></html>";
266 
267         loadPageVerifyTitle2(html);
268     }
269 
270     /**
271      * @throws Exception if the test fails
272      */
273     @Test
274     @Alerts("x<form name=\"f1\">y</form>")
275     public void unclosedForm() throws Exception {
276         final String html =
277             "<html><body>"
278             + "x<form name=\"f1\">y"
279             + "</body></html>";
280 
281         loadPage2(html);
282 
283         final String script = "return document.body.innerHTML";
284         final String result = (String) ((JavascriptExecutor) getWebDriver()).executeScript(script);
285         assertEquals(getExpectedAlerts()[0], result);
286     }
287 }