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.xml;
16  
17  import static java.nio.charset.StandardCharsets.ISO_8859_1;
18  import static java.nio.charset.StandardCharsets.UTF_8;
19  import static org.htmlunit.javascript.host.xml.XMLDocumentTest.LOAD_XML_DOCUMENT_FROM_FILE_FUNCTION;
20  import static org.htmlunit.javascript.host.xml.XMLDocumentTest.LOAD_XML_DOCUMENT_FROM_STRING_FUNCTION;
21  import static org.htmlunit.javascript.host.xml.XMLDocumentTest.callLoadXMLDocumentFromFile;
22  import static org.htmlunit.javascript.host.xml.XMLDocumentTest.callLoadXMLDocumentFromString;
23  
24  import java.util.Collections;
25  
26  import org.htmlunit.WebDriverTestCase;
27  import org.htmlunit.junit.BrowserRunner;
28  import org.htmlunit.junit.annotation.Alerts;
29  import org.htmlunit.util.MimeType;
30  import org.junit.Test;
31  import org.junit.runner.RunWith;
32  import org.openqa.selenium.WebDriver;
33  
34  /**
35   * Tests for {@link XMLDocument}.
36   *
37   * @author Ahmed Ashour
38   * @author Marc Guillemot
39   * @author Frank Danek
40   * @author Ronald Brill
41   */
42  @RunWith(BrowserRunner.class)
43  public class XMLDocument3Test extends WebDriverTestCase {
44  
45      /**
46       * @throws Exception if the test fails
47       */
48      @Test
49      @Alerts({"1610", "1575", "32", "1604", "1610", "1610", "1610", "1610", "1610", "1610", "1604"})
50      public void load_Encoding() throws Exception {
51          final String html = DOCTYPE_HTML
52              + "<html><head>\n"
53              + "<script>\n"
54              + LOG_TITLE_FUNCTION
55              + "  function test() {\n"
56              + "    var doc = " + callLoadXMLDocumentFromFile("'" + URL_SECOND + "'") + ";\n"
57              + "    var value = doc.documentElement.firstChild.nodeValue;\n"
58              + "    for (var i = 0; i < value.length; i++) {\n"
59              + "      log(value.charCodeAt(i));\n"
60              + "    }\n"
61              + "  }\n"
62              + LOAD_XML_DOCUMENT_FROM_FILE_FUNCTION
63              + "</script></head>\n"
64              + "<body onload='test()'>\n"
65              + "</body></html>";
66  
67          final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
68              + "<something>\u064A\u0627 \u0644\u064A\u064A\u064A\u064A\u064A\u064A\u0644</something>";
69  
70          getMockWebConnection().setResponse(URL_SECOND, xml.getBytes("UTF-8"), 200, "OK",
71                  MimeType.TEXT_XML, Collections.emptyList());
72  
73          loadPageVerifyTitle2(html);
74      }
75  
76      /**
77       * @throws Exception if the test fails
78       */
79      @Test
80      @Alerts({"230", "230"})
81      public void parseIso88591Encoding() throws Exception {
82          final String html = DOCTYPE_HTML
83              + "<html>\n"
84              + "  <head>\n"
85              + "<script>\n"
86              + LOG_TITLE_FUNCTION
87              + "  function test(encoding) {\n"
88              + "    var text=\"<?xml version='1.0' encoding='\" + encoding + \"'?><body>\u00e6</body>\";\n"
89              + "    var doc=" + callLoadXMLDocumentFromString("text") + ";\n"
90              + "    var value = doc.documentElement.firstChild.nodeValue;\n"
91              + "    for (var i = 0; i < value.length; i++) {\n"
92              + "      log(value.charCodeAt(i));\n"
93              + "    }\n"
94              + "  }\n"
95              + LOAD_XML_DOCUMENT_FROM_STRING_FUNCTION
96              + "</script></head>\n"
97              + "<body onload='test(\"ISO-8859-1\");test(\"UTF8\");'>\n"
98              + "</body></html>";
99  
100         // javascript ignores the encoding defined in the xml, the xml is parsed as string
101         final WebDriver driver = loadPage2(html, URL_FIRST, "text/html; charset=ISO-8859-1", ISO_8859_1);
102         verifyTitle2(driver, getExpectedAlerts());
103     }
104 
105     /**
106      * @throws Exception if the test fails
107      */
108     @Test
109     @Alerts({"1044", "1044"})
110     public void parseUtf8Encoding() throws Exception {
111         final String html = DOCTYPE_HTML
112             + "<html>\n"
113             + "  <head>\n"
114             + "<script>\n"
115             + LOG_TITLE_FUNCTION
116             + "  function test(encoding) {\n"
117             + "    var text=\"<?xml version='1.0' encoding='\" + encoding + \"'?><body>\u0414</body>\";\n"
118             + "    var doc=" + callLoadXMLDocumentFromString("text") + ";\n"
119             + "    var value = doc.documentElement.firstChild.nodeValue;\n"
120             + "    for (var i = 0; i < value.length; i++) {\n"
121             + "      log(value.charCodeAt(i));\n"
122             + "    }\n"
123             + "  }\n"
124             + LOAD_XML_DOCUMENT_FROM_STRING_FUNCTION
125             + "</script></head>\n"
126             + "<body onload='test(\"UTF-8\");test(\"ISO-8859-1\");'>\n"
127             + "</body></html>";
128 
129         // javascript ignores the encoding defined in the xml, the xml is parsed as string
130         final WebDriver driver = loadPage2(html, URL_FIRST, "text/html; charset=UTF-8", UTF_8);
131         verifyTitle2(driver, getExpectedAlerts());
132     }
133 }