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