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