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.html;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Unit tests for {@link HTMLTitleElement}.
23   * @author Sudhan Moghe
24   * @author Ronald Brill
25   */
26  public class HTMLTitleElementTest extends WebDriverTestCase {
27  
28      /**
29       * @throws Exception if the test fails
30       */
31      @Test
32      @Alerts({"Page Title", "New Title"})
33      public void text() throws Exception {
34          final String html = DOCTYPE_HTML
35              + "<html>\n"
36              + "  <head>\n"
37              + "    <title>Page Title</title>\n"
38              + "    <script>\n"
39              + LOG_TEXTAREA_FUNCTION
40              + "      function test() {\n"
41              + "        var title = document.getElementsByTagName('title')[0];\n"
42              + "        log(title.text);\n"
43              + "        title.text = 'New Title';\n"
44              + "        log(title.text);\n"
45              + "      }\n"
46              + "    </script>\n"
47              + "  </head>\n"
48              + "  <body onload='test()'>\n"
49              + LOG_TEXTAREA
50              + "  </body>\n"
51              + "</html>";
52  
53          loadPageVerifyTextArea2(html);
54      }
55  
56      /**
57       * @throws Exception if the test fails
58       */
59      @Test
60      @Alerts({"", "New Title"})
61      public void textCreateElement() throws Exception {
62          final String html = DOCTYPE_HTML
63              + "<html>\n"
64              + "  <head>\n"
65              + "    <script>\n"
66              + LOG_TEXTAREA_FUNCTION
67              + "      function test() {\n"
68              + "        var title = document.createElement('title');\n"
69              + "        log(title.text);\n"
70              + "        title.text = 'New Title';\n"
71              + "        log(title.text);\n"
72              + "      }\n"
73              + "    </script>\n"
74              + "  </head>\n"
75              + "  <body onload='test()'>\n"
76              + LOG_TEXTAREA
77              + "  </body>\n"
78              + "</html>";
79  
80          loadPageVerifyTextArea2(html);
81      }
82  
83      /**
84       * @throws Exception if the test fails
85       */
86      @Test
87      @Alerts({"Page Title", "</> htmx rocks!", "</> htmx rocks!"})
88      public void innerHtml() throws Exception {
89          final String html = DOCTYPE_HTML
90              + "<html>\n"
91              + "  <head>\n"
92              + "    <title>Page Title</title>\n"
93              + "    <script>\n"
94              + LOG_TEXTAREA_FUNCTION
95              + "      function test() {\n"
96              + "        var title = document.getElementsByTagName('title')[0];\n"
97              + "        log(title.text);\n"
98              + "        title.innerHTML = '</> htmx rocks!';\n"
99              + "        log(title.text);\n"
100             + "        log(window.document.title);\n"
101             + "      }\n"
102             + "    </script>\n"
103             + "  </head>\n"
104             + "  <body onload='test()'>\n"
105             + LOG_TEXTAREA
106             + "  </body>\n"
107             + "</html>";
108 
109         loadPageVerifyTextArea2(html);
110     }
111 
112     /**
113      * @throws Exception if the test fails
114      */
115     @Test
116     @Alerts({"Page Title", "<div>htmx rocks</div>", "<div>htmx rocks</div>"})
117     public void innerHtmlTag() throws Exception {
118         final String html = DOCTYPE_HTML
119             + "<html>\n"
120             + "  <head>\n"
121             + "    <title>Page Title</title>\n"
122             + "    <script>\n"
123             + LOG_TEXTAREA_FUNCTION
124             + "      function test() {\n"
125             + "        var title = document.getElementsByTagName('title')[0];\n"
126             + "        log(title.text);\n"
127             + "        title.innerHTML = '<div>htmx rocks</div>';\n"
128             + "        log(title.text);\n"
129             + "        log(window.document.title);\n"
130             + "      }\n"
131             + "    </script>\n"
132             + "  </head>\n"
133             + "  <body onload='test()'>\n"
134             + LOG_TEXTAREA
135             + "  </body>\n"
136             + "</html>";
137 
138         loadPageVerifyTextArea2(html);
139     }
140 
141     /**
142      * @throws Exception if the test fails
143      */
144     @Test
145     @Alerts({"", "</> htmx rocks!", "</> htmx rocks!"})
146     public void innerHtmlEscaping() throws Exception {
147         final String html = DOCTYPE_HTML
148             + "<html>\n"
149             + "  <head>\n"
150             + "    <title></title>\n"
151             + "    <script>\n"
152             + LOG_TEXTAREA_FUNCTION
153             + "      function test() {\n"
154             + "        var title = document.getElementsByTagName('title')[0];\n"
155             + "        log(title.text);\n"
156             + "        title.innerHTML = '&lt;/> htmx rocks!';\n"
157             + "        log(title.text);\n"
158             + "        log(window.document.title);\n"
159             + "      }\n"
160             + "    </script>\n"
161             + "  </head>\n"
162             + "  <body onload='test()'>\n"
163             + LOG_TEXTAREA
164             + "  </body>\n"
165             + "</html>";
166 
167         loadPageVerifyTextArea2(html);
168     }
169 }