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