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 TextRange}.
23   *
24   * @author Marc Guillemot
25   * @author Ahmed Ashour
26   * @author David Gileadi
27   * @author Frank Danek
28   */
29  public class TextRangeTest extends WebDriverTestCase {
30  
31      /**
32       * @throws Exception if the test fails
33       */
34      @Test
35      @Alerts("TypeError")
36      public void text() throws Exception {
37          final String html = DOCTYPE_HTML
38              + "<html>\n"
39              + "<head>\n"
40              + "  <script>\n"
41              + LOG_TITLE_FUNCTION
42              + "    function test() {\n"
43              + "      try {\n"
44              + "        var f = document.getElementById('foo');\n"
45              + "        f.focus();\n"
46              + "        var r = document.selection.createRange();\n"
47              + "        log(f.value);\n"
48              + "        r.text = 'bla bla';\n"
49              + "        log(f.value);\n"
50              + "        r.duplicate().text = 'bli bli';\n"
51              + "        log(f.value);\n"
52              + "      } catch(e) { logEx(e); }\n"
53              + "    }\n"
54              + "  </script>\n"
55              + "</head>\n"
56              + "<body onload='test()'>\n"
57              + "<textarea id='foo'></textarea>\n"
58              + "</body>\n"
59              + "</html>";
60  
61          loadPageVerifyTitle2(html);
62      }
63  
64      /**
65       * @throws Exception if the test fails
66       */
67      @Test
68      @Alerts("TypeError")
69      public void parentElement() throws Exception {
70          final String html = DOCTYPE_HTML
71              + "<html>\n"
72              + "<head>\n"
73              + "  <script>\n"
74              + LOG_TITLE_FUNCTION
75              + "    function test() {\n"
76              + "      try {\n"
77              + "        log(document.body.createTextRange().parentElement().tagName);\n"
78              + "      } catch(e) { logEx(e); }\n"
79              + "    }\n"
80              + "  </script>\n"
81              + "</head>\n"
82              + "<body onload='test()'>\n"
83              + "</body>\n"
84              + "</html>";
85  
86          loadPageVerifyTitle2(html);
87      }
88  
89      /**
90       * @throws Exception if the test fails
91       */
92      @Test
93      @Alerts("TypeError")
94      public void collapse() throws Exception {
95          final String html = DOCTYPE_HTML
96              + "<html>\n"
97              + "<head>\n"
98              + "  <script>\n"
99              + LOG_TITLE_FUNCTION
100             + "    function test() {\n"
101             + "      try {\n"
102             + "        var f = document.getElementById('foo');\n"
103             + "        f.focus();\n"
104             + "        f.select();\n"
105             + "        var r = document.selection.createRange();\n"
106             + "        log(r.text);\n"
107             + "        r.collapse();\n"
108             + "        log(r.text);\n"
109             + "      } catch(e) { logEx(e); }\n"
110             + "    }\n"
111             + "  </script>\n"
112             + "</head>\n"
113             + "<body onload='test()'>\n"
114             + "<textarea id='foo'>hello</textarea>\n"
115             + "</body>\n"
116             + "</html>";
117 
118         loadPageVerifyTitle2(html);
119     }
120 
121     /**
122      * Minimal test: just test that function is available.
123      * @throws Exception if the test fails
124      */
125     @Test
126     @Alerts("TypeError")
127     public void select() throws Exception {
128         final String html = DOCTYPE_HTML
129             + "<html>\n"
130             + "<head>\n"
131             + "  <script>\n"
132             + LOG_TITLE_FUNCTION
133             + "    function test() {\n"
134             + "      try {\n"
135             + "        var r = document.selection.createRange();\n"
136             + "        r.select();\n"
137             + "      } catch(e) { logEx(e); }\n"
138             + "    }\n"
139             + "  </script>\n"
140             + "</head>\n"
141             + "<body onload='test()'>\n"
142             + "<textarea id='foo'>hello</textarea>\n"
143             + "</body>\n"
144             + "</html>";
145 
146         loadPageVerifyTitle2(html);
147     }
148 
149     /**
150      * @throws Exception if the test fails
151      */
152     @Test
153     @Alerts("TypeError")
154     public void moveEnd() throws Exception {
155         final String html = DOCTYPE_HTML
156             + "<html>\n"
157             + "<head>\n"
158             + "  <script>\n"
159             + LOG_TITLE_FUNCTION
160             + "    function test() {\n"
161             + "      try {\n"
162             + "        var f = document.getElementById('foo');\n"
163             + "        f.focus();\n"
164             + "        f.select();\n"
165             + "        var r = document.selection.createRange();\n"
166             + "        log(r.text);\n"
167             + "        r.moveEnd('character', -1);\n"
168             + "        log(r.text);\n"
169             + "        r.moveStart('character');\n"
170             + "        log(r.text);\n"
171             + "      } catch(e) { logEx(e); }\n"
172             + "    }\n"
173             + "  </script>\n"
174             + "</head>\n"
175             + "<body onload='test()'>\n"
176             + "<textarea id='foo'>hello</textarea>\n"
177             + "</body>\n"
178             + "</html>";
179 
180         loadPageVerifyTitle2(html);
181     }
182 
183     /**
184      * @throws Exception if the test fails
185      */
186     @Test
187     @Alerts("TypeError")
188     public void moveOutOfBounds_input() throws Exception {
189         final String html = DOCTYPE_HTML
190             + "<html>\n"
191             + "<head>\n"
192             + "  <script>\n"
193             + LOG_TITLE_FUNCTION
194             + "    function test() {\n"
195             + "      try {\n"
196             + "        var f = document.getElementById('foo');\n"
197             + "        f.focus();\n"
198             + "        f.select();\n"
199             + "        var r = document.selection.createRange();\n"
200             + "        log(r.text);\n"
201             + "        r.moveEnd('character', -1);\n"
202             + "        log(r.text);\n"
203             + "        r.moveStart('character');\n"
204             + "        log(r.text);\n"
205             + "        log(r.moveEnd('character', 100));\n"
206             + "        log(r.moveStart('character', -100));\n"
207             + "        log(r.text);\n"
208             + "      } catch(e) { logEx(e); }\n"
209             + "    }\n"
210             + "  </script>\n"
211             + "</head>\n"
212             + "<body onload='test()'>\n"
213             + "<input id='foo' value='hello'/>\n"
214             + "</body>\n"
215             + "</html>";
216 
217         loadPageVerifyTitle2(html);
218     }
219 
220     /**
221      * @throws Exception if the test fails
222      */
223     @Test
224     @Alerts("TypeError")
225     public void inRange() throws Exception {
226         final String html = DOCTYPE_HTML
227             + "<html>\n"
228             + "<head>\n"
229             + "  <script>\n"
230             + LOG_TITLE_FUNCTION
231             + "    function test() {\n"
232             + "      try {\n"
233             + "        var r1 = document.body.createTextRange();\n"
234             + "        var r2 = r1.duplicate();\n"
235             + "        log(r1.inRange(r2));\n"
236             + "        log(r2.inRange(r1));\n"
237             + "        r1.collapse();\n"
238             + "        log(r1.inRange(r2));\n"
239             + "        log(r2.inRange(r1));\n"
240             + "      } catch(e) { logEx(e); }\n"
241             + "    }\n"
242             + "  </script>\n"
243             + "</head>\n"
244             + "<body onload='test()'>\n"
245             + "<textarea id='foo'>hello</textarea>\n"
246             + "</body>\n"
247             + "</html>";
248 
249         loadPageVerifyTitle2(html);
250     }
251 
252     /**
253      * Regression test for
254      * <a href="http://sourceforge.net/support/tracker.php?aid=2836591">Bug 2836591</a>.
255      * @throws Exception if the test fails
256      */
257     @Test
258     @Alerts("TypeError")
259     public void inRange2() throws Exception {
260         final String html = DOCTYPE_HTML
261             + "<html><body>\n"
262             + "<form name='f'><input name='q' value=''></form>\n"
263             + "<script>\n"
264             + LOG_TITLE_FUNCTION
265             + "  try {\n"
266             + "    var range = document.f.q.createTextRange();\n"
267             + "    var selectionRange = document.selection.createRange();\n"
268             + "    log(range.inRange(selectionRange));\n"
269             + "  } catch(e) { logEx(e); }\n"
270             + "</script>\n"
271             + "</body></html>";
272 
273         loadPageVerifyTitle2(html);
274     }
275 
276     /**
277      * @throws Exception if an error occurs
278      */
279     @Test
280     @Alerts("TypeError")
281     public void moveToElementText() throws Exception {
282         final String html = DOCTYPE_HTML
283             + "<html><body onload='test()'>\n"
284             + "<span id='s1'>abc</span><span id='s2'>xyz</span><span id='s3'>foo</span>\n"
285             + "<script>\n"
286             + LOG_TITLE_FUNCTION
287             + "  function test() {\n"
288             + "    try {\n"
289             + "      var r = document.selection.createRange();\n"
290             + "      r.moveToElementText(document.getElementById('s3'));\n"
291             + "      log(r.parentElement().id + ' ' + r.text + ' ' + r.htmlText);\n"
292             + "    } catch(e) { logEx(e); }\n"
293             + "  }\n"
294             + "</script>\n"
295             + "</body></html>";
296 
297         loadPageVerifyTitle2(html);
298     }
299 
300     /**
301      * @throws Exception if an error occurs
302      */
303     @Test
304     @Alerts("TypeError")
305     public void setEndRange() throws Exception {
306         final String html = DOCTYPE_HTML
307             + "<html><body>\n"
308             + "<form name='f'><input name='q' value='hello world'></form>\n"
309             + "<script>\n"
310             + LOG_TITLE_FUNCTION
311             + "  try {\n"
312             + "    var range1 = document.f.q.createTextRange();\n"
313             + "    var range2 = range1.duplicate();\n"
314             + "    range1.moveEnd('character', -6);\n"
315             + "    log(range1.text);\n"
316             + "    range2.moveStart('character', 6);\n"
317             + "    log(range2.text);\n"
318             + "    var r3 = range1.duplicate();\n"
319             + "    r3.setEndPoint('EndToEnd', range2);\n"
320             + "    log(r3.text);\n"
321             + "  } catch(e) { logEx(e); }\n"
322             + "</script>\n"
323             + "</body></html>";
324         loadPageVerifyTitle2(html);
325     }
326 
327     /**
328      * @throws Exception if an error occurs
329      */
330     @Test
331     @Alerts("TypeError")
332     public void createRangeParentElement() throws Exception {
333         final String html = DOCTYPE_HTML
334             + "<html><body>\n"
335             + "<script>\n"
336             + LOG_TITLE_FUNCTION
337             + "  try {\n"
338             + "    s = document.selection.createRange();\n"
339             + "    p = s.parentElement();\n"
340             + "    log(p.tagName);\n"
341             + "  } catch(e) { logEx(e); }\n"
342             + "</script>\n"
343             + "</body></html>";
344 
345         loadPageVerifyTitle2(html);
346     }
347 
348     /**
349      * @throws Exception if an error occurs
350      */
351     @Test
352     @Alerts("TypeError")
353     public void createRangeHtmlText() throws Exception {
354         final String html = DOCTYPE_HTML
355             + "<html><body>\n"
356             + "<script>\n"
357             + LOG_TITLE_FUNCTION
358             + "  try {\n"
359             + "    s = document.selection.createRange();\n"
360             + "    t = s.htmlText;\n"
361             + "    log(t);\n"
362             + "  } catch(e) { logEx(e); }\n"
363             + "</script>\n"
364             + "</body></html>";
365 
366         loadPageVerifyTitle2(html);
367     }
368 
369     /**
370      * @throws Exception if an error occurs
371      */
372     @Test
373     @Alerts("TypeError")
374     public void moveToBookmark() throws Exception {
375         final String html = DOCTYPE_HTML
376             + "<html><body>\n"
377             + "<script>\n"
378             + LOG_TITLE_FUNCTION
379             + "  try {\n"
380             + "    var rng = document.body.createTextRange();\n"
381             + "    rng.moveToBookmark(rng.getBookmark());\n"
382             + "    log('ok');\n"
383             + "  } catch(e) { logEx(e); }\n"
384             + "</script>\n"
385             + "</body></html>";
386 
387         loadPageVerifyTitle2(html);
388     }
389 
390     /**
391      * @throws Exception if the test fails
392      */
393     @Test
394     public void compareEndPoints() throws Exception {
395         final String html = DOCTYPE_HTML
396             + "<html>\n"
397             + "<head>\n"
398             + "  <script>\n"
399             + LOG_TITLE_FUNCTION
400             + "    function test() {\n"
401             + "      if (document.selection) {\n"
402             + "        var r1 = document.selection.createRange();\n"
403             + "        var r2 = document.selection.createRange();\n"
404             + "        log(r1.compareEndPoints('StartToStart', r2));\n"
405             + "      }\n"
406             + "    }\n"
407             + "  </script>\n"
408             + "</head>\n"
409             + "<body onload='test()'>\n"
410             + "</body>\n"
411             + "</html>";
412 
413         loadPageVerifyTitle2(html);
414     }
415 
416 }