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