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