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