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;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for {@link DomNode}.
23   *
24   * @author Chris Erskine
25   * @author Ahmed Ashour
26   * @author Ronald Brill
27   */
28  public class DomNode2Test extends WebDriverTestCase {
29  
30      /**
31       * Test for Bug #1253.
32       *
33       * @throws Exception on test failure
34       */
35      @Test
36      @Alerts({"HierarchyRequestError/DOMException", "0"})
37      public void appendChild_recursive() throws Exception {
38          final String html = DOCTYPE_HTML
39                  + "<html><head>\n"
40                  + "<script>\n"
41                  + LOG_TITLE_FUNCTION
42                  + "function test() {\n"
43                  + "  var e = document.createElement('div');\n"
44                  + "  try {\n"
45                  + "    log(e.appendChild(e) === e);\n"
46                  + "  } catch(e) {logEx(e);}\n"
47                  + "  log(e.childNodes.length);\n"
48                  + "}\n"
49                  + "</script>\n"
50                  + "</head><body onload='test()'>\n"
51                  + "</body></html>";
52  
53          loadPageVerifyTitle2(html);
54      }
55  
56      /**
57       * Test for Bug #1253.
58       *
59       * @throws Exception on test failure
60       */
61      @Test
62      @Alerts({"true", "HierarchyRequestError/DOMException", "1", "0"})
63      public void appendChild_recursive_parent() throws Exception {
64          final String html = DOCTYPE_HTML
65                  + "<html><head>\n"
66                  + "<script>\n"
67                  + LOG_TITLE_FUNCTION
68                  + "function test() {\n"
69                  + "  var e1 = document.createElement('div');\n"
70                  + "  var e2 = document.createElement('div');\n"
71                  + "  try {\n"
72                  + "    log(e1.appendChild(e2) === e2);\n"
73                  + "    log(e2.appendChild(e1) === e1);\n"
74                  + "  } catch(e) {logEx(e);}\n"
75                  + "  log(e1.childNodes.length);\n"
76                  + "  log(e2.childNodes.length);\n"
77                  + "}\n"
78                  + "</script>\n"
79                  + "</head><body onload='test()'>\n"
80                  + "</body></html>";
81  
82          loadPageVerifyTitle2(html);
83      }
84  
85      /**
86       * @throws Exception on test failure
87       */
88      @Test
89      @Alerts({"true", "true", "true", "true"})
90      public void ownerDocument() throws Exception {
91          final String content = DOCTYPE_HTML
92              + "<html>\n"
93              + "<head>\n"
94              + "  <script>\n"
95              + LOG_TITLE_FUNCTION
96              + "    function test() {\n"
97              + "      log(document === document.body.ownerDocument);\n"
98              + "      log(document === document.getElementById('foo').ownerDocument);\n"
99              + "      log(document === document.body.firstChild.ownerDocument);\n"
100 
101             + "      var div = document.createElement('div');"
102             + "      log(document === div.ownerDocument);\n"
103             + "    }\n"
104             + "  </script>\n"
105             + "</head>\n"
106             + "<body onload='test()'>bla\n"
107             + "<div id='foo'>bla</div>\n"
108             + "</body>\n"
109             + "</html>";
110 
111         loadPageVerifyTitle2(content);
112     }
113 
114     /**
115      * @throws Exception on test failure
116      */
117     @Test
118     @Alerts({"true", "true", "true", "true"})
119     public void getRootNode() throws Exception {
120         final String content = DOCTYPE_HTML
121             + "<html>\n"
122             + "<head>\n"
123             + "  <script>\n"
124             + LOG_TITLE_FUNCTION
125             + "    function test() {\n"
126             + "      if (!document.body.getRootNode) {\n"
127             + "        log('-'); return;\n"
128             + "      }\n"
129             + "      log(document === document.body.getRootNode());\n"
130             + "      log(document === document.getElementById('foo').getRootNode());\n"
131             + "      log(document === document.body.firstChild.getRootNode());\n"
132 
133             + "      var div = document.createElement('div');"
134             + "      log(div.getRootNode() === div);\n"
135             + "    }\n"
136             + "  </script>\n"
137             + "</head>\n"
138             + "<body onload='test()'>bla\n"
139             + "<div id='foo'>bla</div>\n"
140             + "</body>\n"
141             + "</html>";
142 
143         loadPageVerifyTitle2(content);
144     }
145 
146     /**
147      * @throws Exception on test failure
148      */
149     @Test
150     @Alerts("beforeafter")
151     public void textContentCdata() throws Exception {
152         final String content = DOCTYPE_HTML
153             + "<html>\n"
154             + "<head>\n"
155             + "  <script>\n"
156             + LOG_TITLE_FUNCTION
157             + "    function test() {\n"
158             + "      log(document.getElementById('tester').textContent);\n"
159             + "    }\n"
160             + "  </script>\n"
161             + "</head>\n"
162             + "<body onload='test()'>\n"
163             + "<div id='tester'>before<![CDATA[inside]]>after</div>\n"
164             + "</body>\n"
165             + "</html>";
166 
167         loadPageVerifyTitle2(content);
168     }
169 
170 }