1
2
3
4
5
6
7
8
9
10
11
12
13
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
26
27
28
29
30
31 @RunWith(BrowserRunner.class)
32 public class DocumentTypeTest extends WebDriverTestCase {
33
34
35
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
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
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
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 }