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.javascript.host.dom;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.BrowserRunner;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.htmlunit.util.MimeType;
21  import org.junit.Test;
22  import org.junit.runner.RunWith;
23  
24  /**
25   * Tests for {@link DocumentType}.
26   *
27   * @author Ahmed Ashour
28   * @author Marc Guillemot
29   * @author Frank Danek
30   */
31  @RunWith(BrowserRunner.class)
32  public class DocumentTypeTest extends WebDriverTestCase {
33  
34      /**
35       * @throws Exception if the test fails
36       */
37      @Test
38      @Alerts({"[object DocumentType]", "true", "html,10,null,undefined,undefined,undefined",
39               "html,-//W3C//DTD XHTML 1.0 Strict//EN,http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd,"
40                          + "undefined,undefined,undefined"})
41      public void doctype() throws Exception {
42          final String html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n"
43              + "    \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
44              + "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n"
45              + "<head>\n"
46              + "  <script>\n"
47              + LOG_TITLE_FUNCTION
48              + "    function test() {\n"
49              + "      var t = document.doctype;\n"
50              + "      log(t);\n"
51              + "      if (t != null) {\n"
52              + "        log(t == document.firstChild);\n"
53              + "        log(t.nodeName + ',' + t.nodeType + ',' + t.nodeValue + ',' + t.prefix "
54              + "+ ',' + t.localName + ',' + t.namespaceURI);\n"
55              + "        log(t.name + ',' + t.publicId + ',' + t.systemId + ',' + t.internalSubset"
56              + " + ',' + t.entities + ',' + t.notations);\n"
57              + "      }\n"
58              + "    }\n"
59              + "  </script>\n"
60              + "</head>\n"
61              + "<body onload='test()'>\n"
62              + "</body>\n"
63              + "</html>";
64  
65          loadPageVerifyTitle2(html);
66      }
67  
68      /**
69       * @throws Exception if the test fails
70       */
71      @Test
72      @Alerts({"[object DocumentType]", "greeting,10,null,undefined,undefined,undefined",
73               "greeting,MyIdentifier,hello.dtd,undefined,undefined,undefined"})
74      public void doctype_xml() throws Exception {
75          final String html = DOCTYPE_HTML
76              + "<html>\n"
77              + "  <head>\n"
78              + "    <script>\n"
79              + "      function test() {\n"
80              + LOG_TITLE_FUNCTION
81              + "        var request = new XMLHttpRequest();\n"
82              + "        request.open('GET', 'foo.xml', false);\n"
83              + "        request.send('');\n"
84              + "        var doc = request.responseXML;\n"
85              + "        var t = doc.doctype;\n"
86              + "        log(t);\n"
87              + "        if (t != null) {\n"
88              + "          log(t.nodeName + ',' + t.nodeType + ',' + t.nodeValue + ',' + t.prefix "
89              + "+ ',' + t.localName + ',' + t.namespaceURI);\n"
90              + "          log(t.name + ',' + t.publicId + ',' + t.systemId + ',' + t.internalSubset"
91              + " + ',' + t.entities + ',' + t.notations);\n"
92              + "        }\n"
93              + "      }\n"
94              + "    </script>\n"
95              + "  </head>\n"
96              + "  <body onload='test()'>\n"
97              + "  </body>\n"
98              + "</html>";
99  
100         final String xml = "<!DOCTYPE greeting PUBLIC 'MyIdentifier' 'hello.dtd'>\n"
101               + "<greeting/>";
102 
103         getMockWebConnection().setDefaultResponse(xml, MimeType.TEXT_XML);
104         loadPageVerifyTitle2(html);
105     }
106 
107     /**
108      * @throws Exception if the test fails
109      */
110     @Test
111     @Alerts("undefined")
112     public void html_previousSibling() throws Exception {
113         final String html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n"
114             + "    \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
115             + "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n"
116             + "<head>\n"
117             + "  <title>Test</title>\n"
118             + "  <script>\n"
119             + LOG_WINDOW_NAME_FUNCTION
120             + "    function test() {\n"
121             + "      if (document.body.parentElement) {\n"
122             + "        //.text is defined for Comment in IE\n"
123             + "        log(typeof document.body.parentElement.previousSibling.text);\n"
124             + "        }\n"
125             + "    }\n"
126             + "  </script>\n"
127             + "</head>\n"
128             + "<body onload='test()'>\n"
129             + "</body>\n"
130             + "</html>";
131         loadPage2(html);
132         verifyWindowName2(getWebDriver(), getExpectedAlerts());
133     }
134 
135     /**
136      * @throws Exception if the test fails
137      */
138     @Test
139     @Alerts({"[object DocumentType]", "[object HTMLHtmlElement]"})
140     public void document_children() throws Exception {
141         final String html = DOCTYPE_HTML
142             + "<html>\n"
143             + "<head>\n"
144             + "  <title>Test</title>\n"
145             + "  <script>\n"
146             + LOG_WINDOW_NAME_FUNCTION
147             + "    function test() {\n"
148             + "      for (var elem = document.firstChild; elem; elem = elem.nextSibling) {\n"
149             + "        log(elem);\n"
150             + "      }\n"
151             + "    }\n"
152             + "  </script>\n"
153             + "</head>\n"
154             + "<body onload='test()'>\n"
155             + "</body>\n"
156             + "</html>";
157         loadPage2(html);
158         verifyWindowName2(getWebDriver(), getExpectedAlerts());
159     }
160 }