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