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 DocumentFragment}.
25   *
26   * @author Marc Guillemot
27   * @author Ahmed Ashour
28   * @author Ronald Brill
29   * @author Frank Danek
30   */
31  @RunWith(BrowserRunner.class)
32  public class DocumentFragmentTest extends WebDriverTestCase {
33  
34      /**
35       * Regression test for bug 3191431 on computation from child selector
36       * in a document fragment.
37       * @throws Exception if the test fails
38       */
39      @Test
40      @Alerts(DEFAULT = "[object CSSStyleDeclaration]",
41              FF = "[object CSS2Properties]",
42              FF_ESR = "[object CSS2Properties]")
43      public void getComputedStyleOnChild() throws Exception {
44          final String html = DOCTYPE_HTML
45              + "<html><head><style>\n"
46              + "  body > div { background-color: green#FF0000; }\n"
47              + "</style></head>\n"
48              + "<body>\n"
49              + "<script>\n"
50              + LOG_TITLE_FUNCTION
51              + "  try {\n"
52              + "    var frag = document.createDocumentFragment();\n"
53              + "    var d = document.createElement('div');\n"
54              + "    frag.appendChild(d);\n"
55              + "    log(window.getComputedStyle(d, null));\n"
56              + "  } catch(e) { logEx(e); }\n"
57              + "</script>\n"
58              + "</body>\n"
59              + "</html>";
60  
61          loadPageVerifyTitle2(html);
62      }
63  
64      /**
65       * @throws Exception if the test fails
66       */
67      @Test
68      public void createElement() throws Exception {
69          final String html = DOCTYPE_HTML
70              + "<html>\n"
71              + "  <head>\n"
72              + "    <script>\n"
73              + LOG_TITLE_FUNCTION
74              + "      function test() {\n"
75              + "        var frag = document.createDocumentFragment();\n"
76              + "        if (frag.createElement) {\n"
77              + "          var d = frag.createElement('div');\n"
78              + "          log(d.tagName);\n"
79              + "        }\n"
80              + "      }\n"
81              + "    </script>\n"
82              + "  </head>\n"
83              + "  <body onload='test()'>\n"
84              + "  </body>\n"
85              + "</html>";
86  
87          loadPageVerifyTitle2(html);
88      }
89  
90      /**
91       * @throws Exception if an error occurs
92       */
93      @Test
94      @Alerts({"1", "DIV", "DIV"})
95      public void querySelector() throws Exception {
96          final String html = DOCTYPE_HTML
97              + "<html><head>\n"
98              + "<meta http-equiv='X-UA-Compatible' content='IE=edge'>\n"
99              + "<script>\n"
100             + LOG_TITLE_FUNCTION
101             + "function test() {\n"
102             + "  var frag = document.createDocumentFragment();\n"
103             + "  var d = document.createElement('div');\n"
104             + "  frag.appendChild(d);\n"
105 
106             + "  log(frag.querySelectorAll('div').length);\n"
107             + "  log(frag.querySelectorAll('div')[0].tagName);\n"
108             + "  log(frag.querySelector('div').tagName);\n"
109             + "}\n"
110             + "</script></head>\n"
111             + "<body onload='test()'>\n"
112             + "<div id='root'>\n"
113             + "</div>\n"
114             + "</body></html>";
115 
116         loadPageVerifyTitle2(html);
117     }
118 
119     /**
120      * @throws Exception if an error occurs
121      */
122     @Test
123     @Alerts("0")
124     public void children() throws Exception {
125         final String html = DOCTYPE_HTML
126             + "<html><head>\n"
127             + "<script>\n"
128             + LOG_TITLE_FUNCTION
129             + "function test() {\n"
130             + "  var fragment = document.createDocumentFragment();\n"
131             + "  fragment.textContent = '';\n"
132             + "  log(fragment.childNodes.length);\n"
133             + "}\n"
134             + "</script></head>\n"
135             + "<body onload='test()'>\n"
136             + "</body></html>";
137 
138         loadPageVerifyTitle2(html);
139     }
140 
141     /**
142      * @throws Exception if an error occurs
143      */
144     @Test
145     @Alerts({"[object DocumentFragment]", "undefined"})
146     public void url() throws Exception {
147         final String html = "<!DOCTYPE><html><head>\n"
148             + "<script>\n"
149             + LOG_TITLE_FUNCTION
150             + "function test() {\n"
151             + "  var fragment = document.createDocumentFragment();\n"
152             + "  log(fragment);\n"
153             + "  log(fragment.URL);\n"
154             + "}\n"
155             + "</script></head>\n"
156             + "<body onload='test()'>\n"
157             + "</body></html>";
158 
159         loadPageVerifyTitle2(html);
160     }
161 
162     /**
163      * @throws Exception if the test fails
164      */
165     @Test
166     @Alerts({"0", "null", "null", "1", "myDiv", "myDiv"})
167     public void firstElementChild() throws Exception {
168         final String html = DOCTYPE_HTML
169             + "<html><head>\n"
170             + "<script>\n"
171             + LOG_TITLE_FUNCTION
172             + "  function test() {\n"
173             + "    var fragment = document.createDocumentFragment();\n"
174 
175             + "    log(fragment.childElementCount);\n"
176             + "    log(fragment.firstElementChild);\n"
177             + "    log(fragment.lastElementChild);\n"
178 
179             + "    if (fragment.childElementCount === undefined) { return; };\n"
180 
181             + "    var d = document.createElement('div');\n"
182             + "    d.id = 'myDiv';\n"
183             + "    fragment.appendChild(d);\n"
184             + "    var e = document.createElement('input');\n"
185             + "    e.id = 'first';\n"
186             + "    d.appendChild(e);\n"
187 
188             + "    log(fragment.childElementCount);\n"
189             + "    log(fragment.firstElementChild.id);\n"
190             + "    log(fragment.lastElementChild.id);\n"
191             + "  }\n"
192             + "</script>\n"
193             + "</head>\n"
194             + "<body onload='test()'>\n"
195             + "</body></html>";
196 
197         loadPageVerifyTitle2(html);
198     }
199 
200     /**
201      * @throws Exception if the test fails
202      */
203     @Test
204     @Alerts({"0", "null", "null", "0", "null", "null", "1", "myDiv", "myDiv"})
205     public void firstElementChildTextNode() throws Exception {
206         final String html = DOCTYPE_HTML
207             + "<html><head>\n"
208             + "<script>\n"
209             + LOG_TITLE_FUNCTION
210             + "  function test() {\n"
211             + "    var fragment = document.createDocumentFragment();\n"
212 
213             + "    log(fragment.childElementCount);\n"
214             + "    log(fragment.firstElementChild);\n"
215             + "    log(fragment.lastElementChild);\n"
216 
217             + "    if (fragment.childElementCount === undefined) { return; };\n"
218 
219             + "    var txt = document.createTextNode('HtmlUnit');\n"
220             + "    fragment.appendChild(txt);\n"
221 
222             + "    log(fragment.childElementCount);\n"
223             + "    log(fragment.firstElementChild);\n"
224             + "    log(fragment.lastElementChild);\n"
225 
226             + "    var d = document.createElement('div');\n"
227             + "    d.id = 'myDiv';\n"
228             + "    fragment.appendChild(d);\n"
229 
230             + "    log(fragment.childElementCount);\n"
231             + "    log(fragment.firstElementChild.id);\n"
232             + "    log(fragment.lastElementChild.id);\n"
233             + "  }\n"
234             + "</script>\n"
235             + "</head>\n"
236             + "<body onload='test()'>\n"
237             + "</body></html>";
238 
239         loadPageVerifyTitle2(html);
240     }
241 
242     /**
243      * @throws Exception if the test fails
244      */
245     @Test
246     @Alerts({"null", "null", "null", "null", "[object HTMLDivElement]", "null", "null"})
247     public void getElementById() throws Exception {
248         final String html = DOCTYPE_HTML
249             + "<html>\n"
250             + "<head>\n"
251             + "  <script>\n"
252             + LOG_TITLE_FUNCTION
253             + "  function test() {\n"
254             + "    var fragment = document.createDocumentFragment();\n"
255             + "    var d = document.createElement('div');\n"
256             + "    d.id = 'myDiv';\n"
257             + "    fragment.appendChild(d);\n"
258             + "    var e = document.createElement('input');\n"
259             + "    e.id = 'first';\n"
260             + "    d.appendChild(e);\n"
261 
262             + "    log(document.getElementById(''));\n"
263             + "    log(document.getElementById(undefined));\n"
264             + "    log(document.getElementById(null));\n"
265             + "    log(document.getElementById('unknown'));\n"
266             + "    log(document.getElementById('myDiv'));\n"
267             + "    log(document.getElementById('mydiv'));\n"
268             + "    log(document.getElementById('first'));\n"
269             + "  }\n"
270             + "  </script>\n"
271             + "</head>\n"
272             + "<body onload='test()'>\n"
273             + "<div id='myDiv'>\n"
274             + "  <div></div>\n"
275             + "</div>\n"
276             + "</body>\n"
277             + "</html>";
278 
279         loadPageVerifyTitle2(html);
280     }
281 
282     /**
283      * @throws Exception on test failure
284      */
285     @Test
286     @Alerts({"true", "true"})
287     public void ownerDocument() throws Exception {
288         final String content = DOCTYPE_HTML
289             + "<html>\n"
290             + "<head>\n"
291             + "  <script>\n"
292             + LOG_TITLE_FUNCTION
293             + "    function test() {\n"
294             + "      var fragment = document.createDocumentFragment();\n"
295             + "      log(document === fragment.ownerDocument);\n"
296 
297             + "      var div = document.createElement('div');\n"
298             + "      fragment.appendChild(div);\n"
299             + "      log(div.ownerDocument === document);\n"
300             + "    }\n"
301             + "  </script>\n"
302             + "</head>\n"
303             + "<body onload='test()'>bla\n"
304             + "</body>\n"
305             + "</html>";
306 
307         loadPageVerifyTitle2(content);
308     }
309 
310     /**
311      * @throws Exception on test failure
312      */
313     @Test
314     @Alerts({"false", "true", "false", "true", "false", "true", "true", "false"})
315     public void getRootNode() throws Exception {
316         final String content = DOCTYPE_HTML
317             + "<html>\n"
318             + "<head>\n"
319             + "  <script>\n"
320             + LOG_TITLE_FUNCTION
321             + "    function test() {\n"
322             + "      if (!document.body.getRootNode) {\n"
323             + "        log('-'); return;\n"
324             + "      }\n"
325             + "      var fragment = document.createDocumentFragment();\n"
326             + "      log(document === fragment.getRootNode());\n"
327             + "      log(fragment === fragment.getRootNode());\n"
328 
329             + "      var div = document.createElement('div');\n"
330             + "      fragment.appendChild(div);\n"
331             + "      log(document === div.getRootNode());\n"
332             + "      log(fragment === div.getRootNode());\n"
333 
334             + "      document.body.appendChild(fragment);\n"
335             + "      log(document === fragment.getRootNode());\n"
336             + "      log(fragment === fragment.getRootNode());\n"
337             + "      log(document === div.getRootNode());\n"
338             + "      log(fragment === div.getRootNode());\n"
339             + "    }\n"
340             + "  </script>\n"
341             + "</head>\n"
342             + "<body onload='test()'>bla\n"
343             + "</body>\n"
344             + "</html>";
345 
346         loadPageVerifyTitle2(content);
347     }
348 
349     /**
350      * @throws Exception on test failure
351      */
352     @Test
353     @Alerts("0")
354     public void ctor() throws Exception {
355         final String content = DOCTYPE_HTML
356             + "<html>\n"
357             + "<head>\n"
358             + "  <script>\n"
359             + LOG_TITLE_FUNCTION
360             + "    function test() {\n"
361             + "      var fragment = new DocumentFragment();\n"
362             + "      log(fragment.querySelectorAll('p').length);\n"
363             + "    }\n"
364             + "  </script>\n"
365             + "</head>\n"
366             + "<body onload='test()'>bla\n"
367             + "</body>\n"
368             + "</html>";
369 
370         loadPageVerifyTitle2(content);
371     }
372 
373     /**
374      * @throws Exception on test failure
375      */
376     @Test
377     @Alerts({"1", "[object HTMLDivElement]"})
378     public void append() throws Exception {
379         final String content = DOCTYPE_HTML
380             + "<html>\n"
381             + "<head>\n"
382             + "  <script>\n"
383             + LOG_TITLE_FUNCTION
384             + "    function test() {\n"
385             + "      let fragment = new DocumentFragment();\n"
386             + "      let div = document.createElement('div');\n"
387             + "      fragment.append(div);"
388             + "      log(fragment.children.length);\n"
389             + "      log(fragment.children[0]);\n"
390             + "    }\n"
391             + "  </script>\n"
392             + "</head>\n"
393             + "<body onload='test()'>bla\n"
394             + "</body>\n"
395             + "</html>";
396 
397         loadPageVerifyTitle2(content);
398     }
399 
400     /**
401      * @throws Exception on test failure
402      */
403     @Test
404     @Alerts("0")
405     public void appendNoParam() throws Exception {
406         final String content = DOCTYPE_HTML
407             + "<html>\n"
408             + "<head>\n"
409             + "  <script>\n"
410             + LOG_TITLE_FUNCTION
411             + "    function test() {\n"
412             + "      let fragment = new DocumentFragment();\n"
413             + "      fragment.append();"
414             + "      log(fragment.children.length);\n"
415             + "    }\n"
416             + "  </script>\n"
417             + "</head>\n"
418             + "<body onload='test()'>bla\n"
419             + "</body>\n"
420             + "</html>";
421 
422         loadPageVerifyTitle2(content);
423     }
424 
425     /**
426      * @throws Exception on test failure
427      */
428     @Test
429     @Alerts({"1", "[object Text]", "abcd",
430              "2", "[object Text]", "1234"})
431     public void appendText() throws Exception {
432         final String content = DOCTYPE_HTML
433             + "<html>\n"
434             + "<head>\n"
435             + "  <script>\n"
436             + LOG_TITLE_FUNCTION
437             + "    function test() {\n"
438             + "      let fragment = new DocumentFragment();\n"
439             + "      fragment.append('abcd');"
440             + "      log(fragment.childNodes.length);\n"
441             + "      log(fragment.childNodes[0]);\n"
442             + "      log(fragment.childNodes[0].textContent);\n"
443 
444             + "      let txt = document.createTextNode('1234');\n"
445             + "      fragment.append(txt);"
446             + "      log(fragment.childNodes.length);\n"
447             + "      log(fragment.childNodes[1]);\n"
448             + "      log(fragment.childNodes[1].textContent);\n"
449             + "    }\n"
450             + "  </script>\n"
451             + "</head>\n"
452             + "<body onload='test()'>bla\n"
453             + "</body>\n"
454             + "</html>";
455 
456         loadPageVerifyTitle2(content);
457     }
458 
459     /**
460      * @throws Exception on test failure
461      */
462     @Test
463     @Alerts({"1", "[object HTMLDivElement]",
464              "2", "[object Text]", "abcd",
465              "3", "[object HTMLDivElement]",
466              "4", "[object Text]", "1234",
467              "5", "[object HTMLDivElement]"})
468     public void appendMixed() throws Exception {
469         final String content = DOCTYPE_HTML
470             + "<html>\n"
471             + "<head>\n"
472             + "  <script>\n"
473             + LOG_TITLE_FUNCTION
474             + "    function test() {\n"
475             + "      let fragment = new DocumentFragment();\n"
476 
477             + "      let div = document.createElement('div');\n"
478             + "      fragment.append(div);"
479             + "      log(fragment.childNodes.length);\n"
480             + "      log(fragment.childNodes[0]);\n"
481 
482             + "      fragment.append('abcd');"
483             + "      log(fragment.childNodes.length);\n"
484             + "      log(fragment.childNodes[1]);\n"
485             + "      log(fragment.childNodes[1].textContent);\n"
486 
487             + "      div = document.createElement('div');\n"
488             + "      fragment.append(div);"
489             + "      log(fragment.childNodes.length);\n"
490             + "      log(fragment.childNodes[2]);\n"
491 
492             + "      let txt = document.createTextNode('1234');\n"
493             + "      fragment.append(txt);"
494             + "      log(fragment.childNodes.length);\n"
495             + "      log(fragment.childNodes[3]);\n"
496             + "      log(fragment.childNodes[3].textContent);\n"
497 
498             + "      div = document.createElement('div');\n"
499             + "      fragment.append(div);"
500             + "      log(fragment.childNodes.length);\n"
501             + "      log(fragment.childNodes[4]);\n"
502             + "    }\n"
503             + "  </script>\n"
504             + "</head>\n"
505             + "<body onload='test()'>bla\n"
506             + "</body>\n"
507             + "</html>";
508 
509         loadPageVerifyTitle2(content);
510     }
511 
512     /**
513      * @throws Exception on test failure
514      */
515     @Test
516     @Alerts({"1", "[object HTMLDivElement]"})
517     public void prepend() throws Exception {
518         final String content = DOCTYPE_HTML
519             + "<html>\n"
520             + "<head>\n"
521             + "  <script>\n"
522             + LOG_TITLE_FUNCTION
523             + "    function test() {\n"
524             + "      let fragment = new DocumentFragment();\n"
525             + "      let div = document.createElement('div');\n"
526             + "      fragment.prepend(div);"
527             + "      log(fragment.children.length);\n"
528             + "      log(fragment.children[0]);\n"
529             + "    }\n"
530             + "  </script>\n"
531             + "</head>\n"
532             + "<body onload='test()'>bla\n"
533             + "</body>\n"
534             + "</html>";
535 
536         loadPageVerifyTitle2(content);
537     }
538 
539     /**
540      * @throws Exception on test failure
541      */
542     @Test
543     @Alerts("0")
544     public void prependNoParam() throws Exception {
545         final String content = DOCTYPE_HTML
546             + "<html>\n"
547             + "<head>\n"
548             + "  <script>\n"
549             + LOG_TITLE_FUNCTION
550             + "    function test() {\n"
551             + "      let fragment = new DocumentFragment();\n"
552             + "      fragment.prepend();"
553             + "      log(fragment.children.length);\n"
554             + "    }\n"
555             + "  </script>\n"
556             + "</head>\n"
557             + "<body onload='test()'>bla\n"
558             + "</body>\n"
559             + "</html>";
560 
561         loadPageVerifyTitle2(content);
562     }
563 
564     /**
565      * @throws Exception on test failure
566      */
567     @Test
568     @Alerts({"1", "[object Text]", "abcd",
569              "2", "[object Text]", "1234"})
570     public void prependText() throws Exception {
571         final String content = DOCTYPE_HTML
572             + "<html>\n"
573             + "<head>\n"
574             + "  <script>\n"
575             + LOG_TITLE_FUNCTION
576             + "    function test() {\n"
577             + "      let fragment = new DocumentFragment();\n"
578             + "      fragment.prepend('abcd');"
579             + "      log(fragment.childNodes.length);\n"
580             + "      log(fragment.childNodes[0]);\n"
581             + "      log(fragment.childNodes[0].textContent);\n"
582 
583             + "      let txt = document.createTextNode('1234');\n"
584             + "      fragment.prepend(txt);"
585             + "      log(fragment.childNodes.length);\n"
586             + "      log(fragment.childNodes[0]);\n"
587             + "      log(fragment.childNodes[0].textContent);\n"
588             + "    }\n"
589             + "  </script>\n"
590             + "</head>\n"
591             + "<body onload='test()'>bla\n"
592             + "</body>\n"
593             + "</html>";
594 
595         loadPageVerifyTitle2(content);
596     }
597 
598     /**
599      * @throws Exception on test failure
600      */
601     @Test
602     @Alerts({"1", "[object HTMLDivElement]",
603              "2", "[object Text]", "abcd",
604              "3", "[object HTMLDivElement]",
605              "4", "[object Text]", "1234",
606              "5", "[object HTMLDivElement]"})
607     public void prependMixed() throws Exception {
608         final String content = DOCTYPE_HTML
609             + "<html>\n"
610             + "<head>\n"
611             + "  <script>\n"
612             + LOG_TITLE_FUNCTION
613             + "    function test() {\n"
614             + "      let fragment = new DocumentFragment();\n"
615 
616             + "      let div = document.createElement('div');\n"
617             + "      fragment.prepend(div);"
618             + "      log(fragment.childNodes.length);\n"
619             + "      log(fragment.childNodes[0]);\n"
620 
621             + "      fragment.prepend('abcd');"
622             + "      log(fragment.childNodes.length);\n"
623             + "      log(fragment.childNodes[0]);\n"
624             + "      log(fragment.childNodes[0].textContent);\n"
625 
626             + "      div = document.createElement('div');\n"
627             + "      fragment.prepend(div);"
628             + "      log(fragment.childNodes.length);\n"
629             + "      log(fragment.childNodes[0]);\n"
630 
631             + "      let txt = document.createTextNode('1234');\n"
632             + "      fragment.prepend(txt);"
633             + "      log(fragment.childNodes.length);\n"
634             + "      log(fragment.childNodes[0]);\n"
635             + "      log(fragment.childNodes[0].textContent);\n"
636 
637             + "      div = document.createElement('div');\n"
638             + "      fragment.prepend(div);"
639             + "      log(fragment.childNodes.length);\n"
640             + "      log(fragment.childNodes[0]);\n"
641             + "    }\n"
642             + "  </script>\n"
643             + "</head>\n"
644             + "<body onload='test()'>bla\n"
645             + "</body>\n"
646             + "</html>";
647 
648         loadPageVerifyTitle2(content);
649     }
650 
651     /**
652      * @throws Exception on test failure
653      */
654     @Test
655     @Alerts({"1", "[object HTMLDivElement]"})
656     public void replaceChildren() throws Exception {
657         final String content = DOCTYPE_HTML
658             + "<html>\n"
659             + "<head>\n"
660             + "  <script>\n"
661             + LOG_TITLE_FUNCTION
662             + "    function test() {\n"
663             + "      let fragment = new DocumentFragment();\n"
664             + "      let div = document.createElement('div');\n"
665             + "      fragment.replaceChildren(div);"
666             + "      log(fragment.children.length);\n"
667             + "      log(fragment.children[0]);\n"
668             + "    }\n"
669             + "  </script>\n"
670             + "</head>\n"
671             + "<body onload='test()'>bla\n"
672             + "</body>\n"
673             + "</html>";
674 
675         loadPageVerifyTitle2(content);
676     }
677 
678     /**
679      * @throws Exception on test failure
680      */
681     @Test
682     @Alerts("0")
683     public void replaceChildrenNoParam() throws Exception {
684         final String content = DOCTYPE_HTML
685             + "<html>\n"
686             + "<head>\n"
687             + "  <script>\n"
688             + LOG_TITLE_FUNCTION
689             + "    function test() {\n"
690             + "      let fragment = new DocumentFragment();\n"
691             + "      fragment.replaceChildren();"
692             + "      log(fragment.children.length);\n"
693             + "    }\n"
694             + "  </script>\n"
695             + "</head>\n"
696             + "<body onload='test()'>bla\n"
697             + "</body>\n"
698             + "</html>";
699 
700         loadPageVerifyTitle2(content);
701     }
702 
703     /**
704      * @throws Exception on test failure
705      */
706     @Test
707     @Alerts({"1", "[object Text]", "abcd",
708              "1", "[object Text]", "1234"})
709     public void replaceChildrenText() throws Exception {
710         final String content = DOCTYPE_HTML
711             + "<html>\n"
712             + "<head>\n"
713             + "  <script>\n"
714             + LOG_TITLE_FUNCTION
715             + "    function test() {\n"
716             + "      let fragment = new DocumentFragment();\n"
717             + "      fragment.replaceChildren('abcd');"
718             + "      log(fragment.childNodes.length);\n"
719             + "      log(fragment.childNodes[0]);\n"
720             + "      log(fragment.childNodes[0].textContent);\n"
721 
722             + "      let txt = document.createTextNode('1234');\n"
723             + "      fragment.replaceChildren(txt);"
724             + "      log(fragment.childNodes.length);\n"
725             + "      log(fragment.childNodes[0]);\n"
726             + "      log(fragment.childNodes[0].textContent);\n"
727             + "    }\n"
728             + "  </script>\n"
729             + "</head>\n"
730             + "<body onload='test()'>bla\n"
731             + "</body>\n"
732             + "</html>";
733 
734         loadPageVerifyTitle2(content);
735     }
736 
737     /**
738      * @throws Exception on test failure
739      */
740     @Test
741     @Alerts({"1", "[object HTMLDivElement]",
742              "1", "[object Text]", "abcd",
743              "1", "[object HTMLDivElement]",
744              "1", "[object Text]", "1234",
745              "1", "[object HTMLDivElement]"})
746     public void replaceChildrenMixed() throws Exception {
747         final String content = DOCTYPE_HTML
748             + "<html>\n"
749             + "<head>\n"
750             + "  <script>\n"
751             + LOG_TITLE_FUNCTION
752             + "    function test() {\n"
753             + "      let fragment = new DocumentFragment();\n"
754 
755             + "      let div = document.createElement('div');\n"
756             + "      fragment.replaceChildren(div);"
757             + "      log(fragment.childNodes.length);\n"
758             + "      log(fragment.childNodes[0]);\n"
759 
760             + "      fragment.replaceChildren('abcd');"
761             + "      log(fragment.childNodes.length);\n"
762             + "      log(fragment.childNodes[0]);\n"
763             + "      log(fragment.childNodes[0].textContent);\n"
764 
765             + "      div = document.createElement('div');\n"
766             + "      fragment.replaceChildren(div);"
767             + "      log(fragment.childNodes.length);\n"
768             + "      log(fragment.childNodes[0]);\n"
769 
770             + "      let txt = document.createTextNode('1234');\n"
771             + "      fragment.replaceChildren(txt);"
772             + "      log(fragment.childNodes.length);\n"
773             + "      log(fragment.childNodes[0]);\n"
774             + "      log(fragment.childNodes[0].textContent);\n"
775 
776             + "      div = document.createElement('div');\n"
777             + "      fragment.replaceChildren(div);"
778             + "      log(fragment.childNodes.length);\n"
779             + "      log(fragment.childNodes[0]);\n"
780             + "    }\n"
781             + "  </script>\n"
782             + "</head>\n"
783             + "<body onload='test()'>bla\n"
784             + "</body>\n"
785             + "</html>";
786 
787         loadPageVerifyTitle2(content);
788     }
789 }