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.html;
16  
17  import static java.nio.charset.StandardCharsets.ISO_8859_1;
18  
19  import org.htmlunit.WebDriverTestCase;
20  import org.htmlunit.junit.annotation.Alerts;
21  import org.htmlunit.junit.annotation.HtmlUnitNYI;
22  import org.junit.jupiter.api.Test;
23  import org.openqa.selenium.By;
24  import org.openqa.selenium.WebDriver;
25  
26  /**
27   * Tests for {@link XHtmlPage}.
28   *
29   * @author Ronald Brill
30   */
31  public class XHtmlPage2Test extends WebDriverTestCase {
32  
33      /**
34       * Self closing tags are valid in XHtml.
35       * @throws Exception if an error occurs
36       */
37      @Test
38      @Alerts("false")
39      public void selfClosingDiv() throws Exception {
40          final String html
41              = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
42              + "<!DOCTYPE html PUBLIC \n"
43              + "  \"-//W3C//DTD XHTML 1.0 Strict//EN\" \n"
44              + "  \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
45              + "<html xmlns='http://www.w3.org/1999/xhtml' xmlns:xhtml='http://www.w3.org/1999/xhtml'>\n"
46              + "<body>\n"
47              + "  <div id='div1'/>\n"
48              + "  <div id='div2'>not empty</div>\n"
49              + "</body>\n"
50              + "</html>";
51  
52          final WebDriver driver = loadPage2(html, URL_FIRST, "application/xhtml+xml", ISO_8859_1);
53  
54          assertEquals("", driver.findElement(By.id("div1")).getText());
55          assertEquals("not empty", driver.findElement(By.id("div2")).getText());
56      }
57  
58      /**
59       * Regression test for Bug #1219.
60       *
61       * @throws Exception if an error occurs
62       */
63      @Test
64      @Alerts("false")
65      public void selfClosingTextarea() throws Exception {
66          final String html
67              = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
68              + "<!DOCTYPE html PUBLIC \n"
69              + "  \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \n"
70              + "  \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
71              + "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
72              + "<body>\n"
73              + "  <form action='javascript:void(0)' enctype='application/x-www-form-urlencoded' id='j_id27'>\n"
74              + "    <textarea id='myText'/>\n"
75              + "    <div id='div2'>not empty</div>\n"
76              + "  </form>\n"
77              + "</body>\n"
78              + "</html>";
79  
80          final WebDriver driver = loadPage2(html, URL_FIRST, "application/xhtml+xml", ISO_8859_1);
81  
82          assertEquals("", driver.findElement(By.id("myText")).getText());
83      }
84  
85      /**
86       * Regression test for Bug #1219.
87       *
88       * @throws Exception if an error occurs
89       */
90      @Test
91      @Alerts({"before<![CDATA[inside]]>after",
92               "<div xmlns=\"http://www.w3.org/1999/xhtml\" id=\"tester\">before<![CDATA[inside]]>after</div>",
93               "beforeinsideafter"})
94      @HtmlUnitNYI(CHROME = {"before<!--[CDATA[inside]]-->after",
95                             "<div id=\"tester\">before<!--[CDATA[inside]]-->after</div>",
96                             "beforeafter"},
97              EDGE = {"before<!--[CDATA[inside]]-->after",
98                      "<div id=\"tester\">before<!--[CDATA[inside]]-->after</div>",
99                      "beforeafter"},
100             FF = {"before<!--[CDATA[inside]]-->after",
101                   "<div id=\"tester\">before<!--[CDATA[inside]]-->after</div>",
102                   "beforeafter"},
103             FF_ESR = {"before<!--[CDATA[inside]]-->after",
104                       "<div id=\"tester\">before<!--[CDATA[inside]]-->after</div>",
105                       "beforeafter"})
106     public void cdata() throws Exception {
107         final String html
108             = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
109             + "<!DOCTYPE html PUBLIC \n"
110             + "  \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \n"
111             + "  \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
112             + "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
113             + "<head>"
114             + "  <script>\n"
115 
116             // do not use LOG_TITLE_FUNCTION here
117             + "    function log(msg) { window.document.title += msg + '\\u00a7'; }\n"
118             + "    function test() {"
119             + "      log(document.getElementById('tester').innerHTML);\n"
120             + "      log(document.getElementById('tester').outerHTML);\n"
121             + "      log(document.getElementById('tester').textContent);\n"
122             + "    }\n"
123             + "  </script>\n"
124             + "</head>"
125             + "<body onload='test()'>\n"
126             + "  <div id='tester'>before<![CDATA[inside]]>after</div>\n"
127             + "</body>\n"
128             + "</html>";
129 
130         final WebDriver driver = loadPage2(html, URL_FIRST, "application/xhtml+xml", ISO_8859_1);
131         assertEquals("beforeafter", driver.findElement(By.id("tester")).getText());
132         verifyTitle2(driver, getExpectedAlerts());
133     }
134 
135     /**
136      * Regression test for Bug #1219.
137      *
138      * @throws Exception if an error occurs
139      */
140     @Test
141     @Alerts({"before<![CDATA[<div></div>]]>after",
142              "<div xmlns=\"http://www.w3.org/1999/xhtml\" id=\"tester\">before<![CDATA[<div></div>]]>after</div>",
143              "before<div></div>after"})
144     @HtmlUnitNYI(CHROME = {"before<!--[CDATA[<div></div>]]-->after",
145                            "<div id=\"tester\">before<!--[CDATA[<div></div>]]-->after</div>",
146                            "beforeafter"},
147             EDGE = {"before<!--[CDATA[<div></div>]]-->after",
148                     "<div id=\"tester\">before<!--[CDATA[<div></div>]]-->after</div>",
149                     "beforeafter"},
150             FF = {"before<!--[CDATA[<div></div>]]-->after",
151                   "<div id=\"tester\">before<!--[CDATA[<div></div>]]-->after</div>",
152                   "beforeafter"},
153             FF_ESR = {"before<!--[CDATA[<div></div>]]-->after",
154                       "<div id=\"tester\">before<!--[CDATA[<div></div>]]-->after</div>",
155                       "beforeafter"})
156     public void earlyClosingCdata() throws Exception {
157         final String html
158             = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
159             + "<!DOCTYPE html PUBLIC \n"
160             + "  \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \n"
161             + "  \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
162             + "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
163             + "<head>"
164             + "  <script>\n"
165 
166             // do not use LOG_TITLE_FUNCTION here
167             + "    function log(msg) { window.document.title += msg + '\\u00a7'; }\n"
168             + "    function test() {"
169             + "      log(document.getElementById('tester').innerHTML);\n"
170             + "      log(document.getElementById('tester').outerHTML);\n"
171             + "      log(document.getElementById('tester').textContent);\n"
172             + "    }\n"
173             + "  </script>\n"
174             + "</head>"
175             + "<body onload='test()'>\n"
176             + "  <div id='tester'>before<![CDATA[<div></div>]]>after</div>\n"
177             + "</body>\n"
178             + "</html>";
179 
180         final WebDriver driver = loadPage2(html, URL_FIRST, "application/xhtml+xml", ISO_8859_1);
181         assertEquals("beforeafter", driver.findElement(By.id("tester")).getText());
182         verifyTitle2(driver, getExpectedAlerts());
183     }
184 }