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.dom;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for {@link CharacterData}.
23   *
24   * @author David K. Taylor
25   * @author Ronald Brill
26   */
27  public class CharacterDataTest extends WebDriverTestCase {
28  
29      /**
30       * Regression test for inline text nodes.
31       * @throws Exception if the test fails
32       */
33      @Test
34      @Alerts({"Some Text", "9", "3", "Some Text", "#text"})
35      public void textNode() throws Exception {
36          final String html = DOCTYPE_HTML
37              + "<html><head><script>\n"
38              + LOG_TITLE_FUNCTION
39              + "function doTest() {\n"
40              + "  var div1=document.getElementById('div1');\n"
41              + "  var text1=div1.firstChild;\n"
42              + "  log(text1.data);\n"
43              + "  log(text1.length);\n"
44              + "  log(text1.nodeType);\n"
45              + "  log(text1.nodeValue);\n"
46              + "  log(text1.nodeName);\n"
47              + "}\n"
48              + "</script></head><body onload='doTest()'>\n"
49              + "<div id='div1'>Some Text</div></body></html>";
50  
51          loadPageVerifyTitle2(html);
52      }
53  
54      /**
55       * Regression test for setting the data property of a text node.
56       * @throws Exception if the test fails
57       */
58      @Test
59      @Alerts({"Some New Text", "Some New Text"})
60      public void setData() throws Exception {
61          final String html = DOCTYPE_HTML
62              + "<html><head>\n"
63              + "<script>\n"
64              + LOG_TITLE_FUNCTION
65              + "function doTest() {\n"
66              + "  var div1=document.getElementById('div1');\n"
67              + "  var text1=div1.firstChild;\n"
68              + "  text1.data = 'Some New Text';\n"
69              + "  log(text1.data);\n"
70              + "  log(text1.nodeValue);\n"
71              + "}\n"
72              + "</script></head><body onload='doTest()'>\n"
73              + "<div id='div1'>Some Text</div></body></html>";
74  
75          loadPageVerifyTitle2(html);
76      }
77  
78      /**
79       * Regression test for setting the nodeValue property of a text node.
80       * @throws Exception if the test fails
81       */
82      @Test
83      @Alerts({"Some New Text", "Some New Text"})
84      public void setNodeValue() throws Exception {
85          final String html = DOCTYPE_HTML
86              + "<html><head>\n"
87              + "<script>\n"
88              + LOG_TITLE_FUNCTION
89              + "function doTest() {\n"
90              + "  var div1=document.getElementById('div1');\n"
91              + "  var text1=div1.firstChild;\n"
92              + "  text1.nodeValue = 'Some New Text';\n"
93              + "  log(text1.data);\n"
94              + "  log(text1.nodeValue);\n"
95              + "}\n"
96              + "</script></head><body onload='doTest()'>\n"
97              + "<div id='div1'>Some Text</div></body></html>";
98  
99          loadPageVerifyTitle2(html);
100     }
101 
102     /**
103      * Regression test for appendData of a text node.
104      * @throws Exception if the test fails
105      */
106     @Test
107     @Alerts("Some Text Appended")
108     public void appendData() throws Exception {
109         final String html = DOCTYPE_HTML
110             + "<html><head>\n"
111             + "<script>\n"
112             + LOG_TITLE_FUNCTION
113             + "function doTest() {\n"
114             + "  var div1=document.getElementById('div1');\n"
115             + "  var text1=div1.firstChild;\n"
116             + "  text1.appendData(' Appended');\n"
117             + "  log(text1.data);\n"
118             + "}\n"
119             + "</script></head><body onload='doTest()'>\n"
120             + "<div id='div1'>Some Text</div></body></html>";
121 
122         loadPageVerifyTitle2(html);
123     }
124 
125     /**
126      * Regression test for deleteData of a text node.
127      * @throws Exception if the test fails
128      */
129     @Test
130     @Alerts({"Some Text", "Some", "Some", "me", ""})
131     public void deleteData() throws Exception {
132         final String html = DOCTYPE_HTML
133             + "<html><head>\n"
134             + "<script>\n"
135             + LOG_TITLE_FUNCTION
136             + "function doTest() {\n"
137             + "  var div1=document.getElementById('div1');\n"
138             + "  var text1=div1.firstChild;\n"
139 
140             + "  try {\n"
141             + "    text1.deleteData(5, 11);\n"
142             + "    log(text1.data);\n"
143             + "  } catch(e) { logEx(e) }\n"
144 
145             + "  try {\n"
146             + "    text1.deleteData(4, 5);\n"
147             + "    log(text1.data);\n"
148             + "  } catch(e) { logEx(e) }\n"
149 
150             + "  try {\n"
151             + "    text1.deleteData(1, 0);\n"
152             + "    log(text1.data);\n"
153             + "  } catch(e) { logEx(e) }\n"
154 
155             + "  try {\n"
156             + "    text1.deleteData(0, 2);\n"
157             + "    log(text1.data);\n"
158             + "  } catch(e) { logEx(e) }\n"
159 
160             + "  try {\n"
161             + "    text1.deleteData(0, 2);\n"
162             + "    log(text1.data);\n"
163             + "  } catch(e) { logEx(e) }\n"
164             + "}\n"
165             + "</script></head><body onload='doTest()'>\n"
166             + "<div id='div1'>Some Not So New Text</div></body></html>";
167 
168         loadPageVerifyTitle2(html);
169     }
170 
171     /**
172      * Regression test for deleteData of a text node.
173      * @throws Exception if the test fails
174      */
175     @Test
176     @Alerts({"", "", "", ""})
177     public void deleteDataEmptyImput() throws Exception {
178         final String html = DOCTYPE_HTML
179             + "<html><head>\n"
180             + "<script>\n"
181             + LOG_TITLE_FUNCTION
182             + "function doTest() {\n"
183             + "  var div1=document.getElementById('div1');\n"
184             + "  var text1=div1.firstChild;\n"
185 
186             + "  try {\n"
187             + "    text1.deleteData(0, 1);\n"
188             + "    log(text1.data);\n"
189             + "  } catch(e) { logEx(e) }\n"
190 
191             + "  try {\n"
192             + "    text1.deleteData(0, 0);\n"
193             + "    log(text1.data);\n"
194             + "  } catch(e) { logEx(e) }\n"
195 
196             + "  try {\n"
197             + "    text1.deleteData(0, 1);\n"
198             + "    log(text1.data);\n"
199             + "  } catch(e) { logEx(e) }\n"
200 
201             + "  try {\n"
202             + "    text1.deleteData(0, -1);\n"
203             + "    log(text1.data);\n"
204             + "  } catch(e) { logEx(e) }\n"
205             + "}\n"
206             + "</script></head><body onload='doTest()'>\n"
207             + "<div id='div1'>-</div></body></html>";
208 
209         loadPageVerifyTitle2(html);
210     }
211 
212     /**
213      * @throws Exception if the test fails
214      */
215     @Test
216     @Alerts({"IndexSizeError/DOMException", "IndexSizeError/DOMException",
217              "IndexSizeError/DOMException", "IndexSizeError/DOMException"})
218     public void deleteDataInvalidStart() throws Exception {
219         final String html = DOCTYPE_HTML
220             + "<html><head>\n"
221             + "<script>\n"
222             + LOG_TITLE_FUNCTION
223             + "function doTest() {\n"
224             + "  var div1=document.getElementById('div1');\n"
225             + "  var text1=div1.firstChild;\n"
226             + "  try {\n"
227             + "    text1.deleteData(-1, 4);\n"
228             + "    log(text1.data);\n"
229             + "  } catch(e) { logEx(e) }\n"
230 
231             + "  try {\n"
232             + "    text1.deleteData(20, 4);\n"
233             + "    log(text1.data);\n"
234             + "  } catch(e) { logEx(e) }\n"
235 
236             + "  try {\n"
237             + "    text1.deleteData(20, 0);\n"
238             + "    log(text1.data);\n"
239             + "  } catch(e) { logEx(e) }\n"
240 
241             + "  try {\n"
242             + "    text1.deleteData(20, -18);\n"
243             + "    log(text1.data);\n"
244             + "  } catch(e) { logEx(e) }\n"
245             + "}\n"
246             + "</script></head><body onload='doTest()'>\n"
247             + "<div id='div1'>abcde</div></body></html>";
248 
249         loadPageVerifyTitle2(html);
250     }
251 
252     /**
253      * @throws Exception if the test fails
254      */
255     @Test
256     @Alerts({"Some Not So New Te", "Some ", "So"})
257     public void deleteDataNegativeCount() throws Exception {
258         final String html = DOCTYPE_HTML
259             + "<html><head>\n"
260             + "<script>\n"
261             + LOG_TITLE_FUNCTION
262             + "function doTest() {\n"
263             + "  var div1=document.getElementById('div1');\n"
264             + "  var text1=div1.firstChild;\n"
265             + "  try {\n"
266             + "    text1.deleteData(18, -15);\n"
267             + "    log(text1.data);\n"
268             + "  } catch(e) { logEx(e) }\n"
269 
270             + "  try {\n"
271             + "    text1.deleteData(5, -4);\n"
272             + "    log(text1.data);\n"
273             + "  } catch(e) { logEx(e) }\n"
274 
275             + "  try {\n"
276             + "    text1.deleteData(2, -4);\n"
277             + "    log(text1.data);\n"
278             + "  } catch(e) { logEx(e) }\n"
279             + "}\n"
280             + "</script></head><body onload='doTest()'>\n"
281             + "<div id='div1'>Some Not So New Text</div></body></html>";
282 
283         loadPageVerifyTitle2(html);
284     }
285 
286     /**
287      * Regression test for insertData of a text node.
288      * @throws Exception if the test fails
289      */
290     @Test
291     @Alerts("Some New Text")
292     public void insertData() throws Exception {
293         final String html = DOCTYPE_HTML
294             + "<html><head><script>\n"
295             + LOG_TITLE_FUNCTION
296             + "function doTest() {\n"
297             + "  var div1=document.getElementById('div1');\n"
298             + "  var text1=div1.firstChild;\n"
299             + "  text1.insertData(5, 'New ');\n"
300             + "  log(text1.data);\n"
301             + "}\n"
302             + "</script></head><body onload='doTest()'>\n"
303             + "<div id='div1'>Some Text</div></body></html>";
304 
305         loadPageVerifyTitle2(html);
306     }
307 
308     /**
309      * Regression test for replaceData of a text node.
310      * @throws Exception if the test fails
311      */
312     @Test
313     @Alerts("Some New Text")
314     public void replaceData() throws Exception {
315         final String html = DOCTYPE_HTML
316             + "<html><head>\n"
317             + "<script>\n"
318             + LOG_TITLE_FUNCTION
319             + "function doTest() {\n"
320             + "  var div1=document.getElementById('div1');\n"
321             + "  var text1=div1.firstChild;\n"
322             + "  text1.replaceData(5, 3, 'New');\n"
323             + "  log(text1.data);\n"
324             + "}\n"
325             + "</script></head><body onload='doTest()'>\n"
326             + "<div id='div1'>Some Old Text</div></body></html>";
327 
328         loadPageVerifyTitle2(html);
329     }
330 
331     /**
332      * Regression test for substringData of a text node.
333      * @throws Exception if the test fails
334      */
335     @Test
336     @Alerts({"New", "Some New Text"})
337     public void substringData() throws Exception {
338         final String html = DOCTYPE_HTML
339             + "<html><head>\n"
340             + "<script>\n"
341             + LOG_TITLE_FUNCTION
342             + "function doTest() {\n"
343             + "  var div1=document.getElementById('div1');\n"
344             + "  var text1=div1.firstChild;\n"
345             + "  log(text1.substringData(5, 3));\n"
346             + "  log(text1.data);\n"
347             + "}\n"
348             + "</script></head><body onload='doTest()'>\n"
349             + "<div id='div1'>Some New Text</div></body></html>";
350 
351         loadPageVerifyTitle2(html);
352     }
353 
354     /**
355      * Regression test for substringData of a text node.
356      * @throws Exception if the test fails
357      */
358     @Test
359     @Alerts({"Some ", "Text", "true"})
360     public void textImpl_splitText() throws Exception {
361         final String html = DOCTYPE_HTML
362             + "<html><head>\n"
363             + "<script>\n"
364             + LOG_TITLE_FUNCTION
365             + "function doTest() {\n"
366             + "  var div1=document.getElementById('div1');\n"
367             + "  var text1=div1.firstChild;\n"
368             + "  var text2=text1.splitText(5);\n"
369             + "  log(text1.data);\n"
370             + "  log(text2.data);\n"
371             + "  log(text1.nextSibling == text2);\n"
372             + "}\n"
373             + "</script></head><body onload='doTest()'>\n"
374             + "<div id='div1'>Some Text</div></body></html>";
375 
376         loadPageVerifyTitle2(html);
377     }
378 }