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  
23  /**
24   * Test class for {@link HTMLParser}.
25   *
26   * @author Ronald Brill
27   */
28  @RunWith(BrowserRunner.class)
29  public class HTMLParser6Test extends WebDriverTestCase {
30  
31      /**
32       * @throws Exception if the test fails
33       */
34      @Test
35      @Alerts("<div id=\"myDiv2\"></div><div>harhar</div><div id=\"myDiv3\"></div>")
36      public void fragmentParser() throws Exception {
37          final String html = "<html><head>\n"
38              + "<script>\n"
39              + LOG_TITLE_FUNCTION
40              + "  function test() {\n"
41              + "    var element = document.getElementById('myDiv2');\n"
42              + "    var range = element.ownerDocument.createRange();\n"
43              + "    range.setStartAfter(element);\n"
44              + "    var fragment = range.createContextualFragment('<div>harhar</div>');\n"
45              + "    element.parentNode.insertBefore(fragment, element.nextSibling);\n"
46              + "    log(element.parentNode.innerHTML);\n"
47              + "  }\n"
48              + "</script></head><body onload='test()'>\n"
49              + "  <div id='myDiv'><div id='myDiv2'></div><div id='myDiv3'></div></div>\n"
50              + "</body></html>";
51  
52          loadPageVerifyTitle2(html);
53      }
54  
55      /**
56       * @throws Exception if the test fails
57       */
58      @Test
59      @Alerts("<div id=\"myDiv2\"></div><select><option value=\"da\">Dansk</option></select><div id=\"myDiv3\"></div>")
60      public void fragmentParserHtmlHeadMissingBody() throws Exception {
61          final String fragment = "<html><head></head><select><option value=\"da\">Dansk</option></select></body></html>";
62          final String html = "<html><head>\n"
63              + "<script>\n"
64              + LOG_TITLE_FUNCTION
65              + "  function test() {\n"
66              + "    var element = document.getElementById('myDiv2');\n"
67              + "    var range = element.ownerDocument.createRange();\n"
68              + "    range.setStartAfter(element);\n"
69              + "    var fragment = range.createContextualFragment('" + fragment + "');\n"
70              + "    element.parentNode.insertBefore(fragment, element.nextSibling);\n"
71              + "    log(element.parentNode.innerHTML);\n"
72              + "  }\n"
73              + "</script></head><body onload='test()'>\n"
74              + "  <div id='myDiv'><div id='myDiv2'></div><div id='myDiv3'></div></div>\n"
75              + "</body></html>";
76  
77          loadPageVerifyTitle2(html);
78      }
79  
80      /**
81       * @throws Exception if the test fails
82       */
83      @Test
84      @Alerts("<select id=\"mySelect\"><option>op1</option></select>"
85              + "<select><option value=\"da\">Dansk</option></select> ")
86      public void fragmentParserHtmlInsideSelect() throws Exception {
87          final String fragment = "<select><option value=\"da\">Dansk</option></select>";
88          final String html = "<html><head>\n"
89              + "<script>\n"
90              + LOG_TITLE_FUNCTION
91              + "  function test() {\n"
92              + "    var element = document.getElementById('mySelect');\n"
93              + "    var range = element.ownerDocument.createRange();\n"
94              + "    range.setStartAfter(element);\n"
95              + "    var fragment = range.createContextualFragment('" + fragment + "');\n"
96              + "    element.parentNode.insertBefore(fragment, element.nextSibling);\n"
97              + "    log(element.parentNode.innerHTML);\n"
98              + "  }\n"
99              + "</script></head><body onload='test()'>\n"
100             + "  <select id='mySelect'><option>op1</option></select>\n"
101             + "</body></html>";
102 
103         loadPageVerifyTitle2(html);
104     }
105 
106     /**
107      * @throws Exception if the test fails
108      */
109     @Test
110     @Alerts("<option id=\"myOption\">op1</option><option value=\"da\">Dansk</option>")
111     public void fragmentParserHtmlInsideOption() throws Exception {
112         final String fragment = "<select><option value=\"da\">Dansk</option></select>";
113         final String html = "<html><head>\n"
114             + "<script>\n"
115             + LOG_TITLE_FUNCTION
116             + "  function test() {\n"
117             + "    var element = document.getElementById('myOption');\n"
118             + "    var range = element.ownerDocument.createRange();\n"
119             + "    range.setStartAfter(element);\n"
120             + "    var fragment = range.createContextualFragment('" + fragment + "');\n"
121             + "    element.parentNode.insertBefore(fragment, element.nextSibling);\n"
122             + "    log(element.parentNode.innerHTML);\n"
123             + "  }\n"
124             + "</script></head><body onload='test()'>\n"
125             + "  <select><option id='myOption'>op1</option></select>\n"
126             + "</body></html>";
127 
128         loadPageVerifyTitle2(html);
129     }
130 
131     /**
132      * @throws Exception if the test fails
133      */
134     @Test
135     @Alerts("<div id=\"myTest\"></div><div a<b=\"7\">xy</div> ")
136     public void fragmentParserLtInAttributeName() throws Exception {
137         final String fragment = "<div a<b=7>xy</div>";
138         final String html = "<html><head>\n"
139             + "<script>\n"
140             + LOG_TITLE_FUNCTION
141             + "  function test() {\n"
142             + "    var element = document.getElementById('myTest');\n"
143             + "    var range = element.ownerDocument.createRange();\n"
144             + "    range.setStartAfter(element);\n"
145             + "    var fragment = range.createContextualFragment('" + fragment + "');\n"
146             + "    element.parentNode.insertBefore(fragment, element.nextSibling);\n"
147             + "    log(element.parentNode.innerHTML);\n"
148             + "  }\n"
149             + "</script></head><body onload='test()'>\n"
150             + "  <div id='myTest'></div>\n"
151             + "</body></html>";
152 
153         loadPageVerifyTitle2(html);
154     }
155 
156     /**
157      * @throws Exception if the test fails
158      */
159     @Test
160     @Alerts("<div id=\"myTest\"></div><div <ab=\"7\">xy</div> ")
161     public void fragmentParserLtFirstInAttributeName() throws Exception {
162         final String fragment = "<div <ab=7>xy</div>";
163         final String html = "<html><head>\n"
164             + "<script>\n"
165             + LOG_TITLE_FUNCTION
166             + "  function test() {\n"
167             + "    var element = document.getElementById('myTest');\n"
168             + "    var range = element.ownerDocument.createRange();\n"
169             + "    range.setStartAfter(element);\n"
170             + "    var fragment = range.createContextualFragment('" + fragment + "');\n"
171             + "    element.parentNode.insertBefore(fragment, element.nextSibling);\n"
172             + "    log(element.parentNode.innerHTML);\n"
173             + "  }\n"
174             + "</script></head><body onload='test()'>\n"
175             + "  <div id='myTest'></div>\n"
176             + "</body></html>";
177 
178         loadPageVerifyTitle2(html);
179     }
180 
181     /**
182      * @throws Exception if the test fails
183      */
184     @Test
185     @Alerts("<div id=\"myTest\"></div><div 7=\"ab\">xy</div> ")
186     public void fragmentParserNumericAttributeName() throws Exception {
187         final String fragment = "<div 7=\"ab\">xy</div>";
188         final String html = "<html><head>\n"
189             + "<script>\n"
190             + LOG_TITLE_FUNCTION
191             + "  function test() {\n"
192             + "    var element = document.getElementById('myTest');\n"
193             + "    var range = element.ownerDocument.createRange();\n"
194             + "    range.setStartAfter(element);\n"
195             + "    var fragment = range.createContextualFragment('" + fragment + "');\n"
196             + "    element.parentNode.insertBefore(fragment, element.nextSibling);\n"
197             + "    log(element.parentNode.innerHTML);\n"
198             + "  }\n"
199             + "</script></head><body onload='test()'>\n"
200             + "  <div id='myTest'></div>\n"
201             + "</body></html>";
202 
203         loadPageVerifyTitle2(html);
204     }
205 }