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.BrowserRunner;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.htmlunit.junit.annotation.HtmlUnitNYI;
21  import org.junit.Test;
22  import org.junit.runner.RunWith;
23  import org.openqa.selenium.By;
24  import org.openqa.selenium.WebDriver;
25  import org.openqa.selenium.WebElement;
26  import org.openqa.selenium.interactions.Actions;
27  
28  /**
29   * Tests for {@link HTMLElement}.
30   *
31   * @author Ahmed Ashour
32   * @author Marc Guillemot
33   * @author Ronald Brill
34   * @author Frank Danek
35   */
36  @RunWith(BrowserRunner.class)
37  public class HTMLElement2Test extends WebDriverTestCase {
38  
39      /**
40       * @throws Exception if the test fails
41       */
42      @Test
43      @Alerts({"undefined", "undefined"})
44      public void scopeName() throws Exception {
45          final String html = DOCTYPE_HTML
46              + "<html><head>\n"
47              + "<script>\n"
48              + LOG_TITLE_FUNCTION
49              + "  function test() {\n"
50              + "    log(document.body.scopeName);\n"
51              + "    log(document.body.tagUrn);\n"
52              + "  }\n"
53              + "</script>\n"
54              + "</head>\n"
55              + "<body onload='test()'>\n"
56              + "</body></html>";
57          loadPageVerifyTitle2(html);
58      }
59  
60      /**
61       * @throws Exception if the test fails
62       */
63      @Test
64      @Alerts({"undefined", "undefined", "undefined", "http://www.meh.com/meh"})
65      public void scopeName2() throws Exception {
66          final String html = "<html xmlns:blah='http://www.blah.com/blah'><head>\n"
67              + "<script>\n"
68              + LOG_TITLE_FUNCTION
69              + "  function test() {\n"
70              + "    var x = document.getElementById('x');\n"
71              + "    log(x.scopeName);\n"
72              + "    log(x.tagUrn);\n"
73              + "    try {\n"
74              + "      x.tagUrn = 'http://www.meh.com/meh';\n"
75              + "      log(x.scopeName);\n"
76              + "      log(x.tagUrn);\n"
77              + "    } catch(e) { logEx(e); }\n"
78              + "  }\n"
79              + "</script>\n"
80              + "</head>\n"
81              + "<body onload='test()'><blah:abc id='x'></blah:abc></body></html>";
82          loadPageVerifyTitle2(html);
83      }
84  
85      /**
86       * Test offsets (real values don't matter currently).
87       *
88       * @throws Exception if the test fails
89       */
90      @Test
91      @Alerts({"number", "number", "number", "number", "number", "number", "number", "number"})
92      public void offsets() throws Exception {
93          final String html = DOCTYPE_HTML
94                + "<html>\n"
95                + "<head></head>\n"
96                + "<body>\n"
97                + "</div></body>\n"
98                + "<div id='div1'>foo</div>\n"
99                + "<script>\n"
100               + LOG_TITLE_FUNCTION
101               + "function alertOffsets(_oElt) {\n"
102               + "  log(typeof _oElt.offsetHeight);\n"
103               + "  log(typeof _oElt.offsetWidth);\n"
104               + "  log(typeof _oElt.offsetLeft);\n"
105               + "  log(typeof _oElt.offsetTop);\n"
106               + "}\n"
107               + "alertOffsets(document.body);\n"
108               + "alertOffsets(document.getElementById('div1'));\n"
109               + "</script></body></html>";
110         loadPageVerifyTitle2(html);
111     }
112 
113     /**
114      * @throws Exception if an error occurs
115      */
116     @Test
117     @Alerts("attachEvent not available")
118     public void offsetWidth_withEvent() throws Exception {
119         final String html = DOCTYPE_HTML
120             + "<html>\n"
121             + "<head>\n"
122             + "<script>\n"
123             + LOG_TITLE_FUNCTION
124             + "  function test() {\n"
125             + "    var myDiv2 = document.getElementById('myDiv2');\n"
126             + "    if(!document.attachEvent) { log('attachEvent not available'); return }\n"
127 
128             + "    myDiv2.attachEvent('ondataavailable', handler);\n"
129             + "    document.attachEvent('ondataavailable', handler);\n"
130             + "    var m = document.createEventObject();\n"
131             + "    m.eventType = 'ondataavailable';\n"
132             + "    myDiv2.fireEvent(m.eventType, m);\n"
133             + "    document.fireEvent(m.eventType, m);\n"
134             + "  }\n"
135             + "  function handler() {\n"
136             + "    var e = document.getElementById('myDiv');\n"
137             + "    e.style.width = 30;\n"
138             + "    log(e.offsetWidth);\n"
139             + "  }\n"
140             + "</script>\n"
141             + "</head>\n"
142             + "<body onload='test()'>\n"
143             + "  <div id='myDiv'></div>\n"
144             + "  <div id='myDiv2'></div>\n"
145             + "</body></html>";
146         loadPageVerifyTitle2(html);
147     }
148 
149     /**
150      * @throws Exception if an error occurs
151      */
152     @Test
153     @Alerts({"true", "true"})
154     public void offsetWidth_spanWithDifferentFontSize() 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             + "    var myDefault = document.getElementById('myDefault');\n"
162             + "    var myLarge = document.getElementById('myLarge');\n"
163 
164             + "    log(myDefault.offsetWidth > 20);\n"
165             + "    log(myLarge.offsetWidth > myDefault.offsetWidth);\n"
166             + "  }\n"
167             + "</script>\n"
168             + "</head>\n"
169             + "<body onload='test()'>\n"
170             + "  <span id='myDefault'>1234567890</span>\n"
171             + "  <span id='myLarge' style='font-size: 10em'>1234567890</span>\n"
172             + "</body></html>";
173         loadPageVerifyTitle2(html);
174     }
175 
176     /**
177      * @throws Exception if an error occurs
178      */
179     @Test
180     @Alerts({"true", "true"})
181     @HtmlUnitNYI(CHROME = {"true", "false"},
182             EDGE = {"true", "false"},
183             FF = {"true", "false"},
184             FF_ESR = {"true", "false"})
185     public void offsetWidth_spanWithDifferentFonts() throws Exception {
186         final String html = DOCTYPE_HTML
187             + "<html>\n"
188             + "<head>\n"
189             + "<script>\n"
190             + LOG_TITLE_FUNCTION
191             + "  function test() {\n"
192             + "    var mySerif = document.getElementById('mySerif');\n"
193             + "    var mySans = document.getElementById('mySans');\n"
194 
195             + "    log(mySerif.offsetWidth > 20);\n"
196             + "    log(mySans.offsetWidth > mySerif.offsetWidth);\n"
197             + "  }\n"
198             + "</script>\n"
199             + "</head>\n"
200             + "<body onload='test()'>\n"
201             + "  <span id='mySerif' style='font-family: serif'>1234567890</span>\n"
202             + "  <span id='mySans' style='font-family: sans-serif'>1234567890</span>\n"
203             + "</body></html>";
204         loadPageVerifyTitle2(html);
205     }
206 
207     /**
208      * @throws Exception if an error occurs
209      */
210     @Test
211     @Alerts({"15", "15"})
212     public void offsetTopAndLeft_Padding() throws Exception {
213         final String html = DOCTYPE_HTML
214             + "<html>\n"
215             + "  <head>\n"
216             + "    <script>\n"
217             + LOG_TITLE_FUNCTION
218             + "      function test() {\n"
219             + "        var e = document.getElementById('d');\n"
220             + "        log(e.offsetTop);\n"
221             + "        log(e.offsetLeft);\n"
222             + "      }\n"
223             + "    </script>\n"
224             + "  </head>\n"
225             + "  <body onload='test()' style='padding: 3px; margin: 0px; border: 0px solid green;'>\n"
226             + "    <div style='padding: 5px; margin: 0px; border: 0px solid blue;'>\n"
227             + "      <div style='padding: 7px; margin: 0px; border: 0px solid red;'>\n"
228             + "        <div id='d' style='padding: 13px; margin: 0px; border: 0px solid black;'>d</div>\n"
229             + "      </div>\n"
230             + "    </div>\n"
231             + "  </body>\n"
232             + "</html>";
233         loadPageVerifyTitle2(html);
234     }
235 
236     /**
237      * @throws Exception if an error occurs
238      */
239     @Test
240     @Alerts({"13", "28"})
241     public void offsetTopAndLeft_Margins() throws Exception {
242         final String html = DOCTYPE_HTML
243             + "<html>\n"
244             + "  <head>\n"
245             + "    <script>\n"
246             + LOG_TITLE_FUNCTION
247             + "      function test() {\n"
248             + "        var e = document.getElementById('d');\n"
249             + "        log(e.offsetTop);\n"
250             + "        log(e.offsetLeft);\n"
251             + "      }\n"
252             + "    </script>\n"
253             + "  </head>\n"
254             + "  <body onload='test()' style='padding: 0px; margin: 3px; border: 0px solid green;'>\n"
255             + "    <div style='padding: 0px; margin: 5px; border: 0px solid blue;'>\n"
256             + "      <div style='padding: 0px; margin: 7px; border: 0px solid red;'>\n"
257             + "        <div id='d' style='padding: 0px; margin: 13px; border: 0px solid black;'>d</div>\n"
258             + "      </div>\n"
259             + "    </div>\n"
260             + "  </body>\n"
261             + "</html>";
262         loadPageVerifyTitle2(html);
263     }
264 
265     /**
266      * @throws Exception if an error occurs
267      */
268     @Test
269     @Alerts(DEFAULT = {"15", "15"},
270             FF = {"12", "12"},
271             FF_ESR = {"12", "12"})
272     @HtmlUnitNYI(CHROME = {"12", "12"},
273             EDGE = {"12", "12"})
274     public void offsetTopAndLeft_Borders() throws Exception {
275         final String html = DOCTYPE_HTML
276             + "<html>\n"
277             + "  <head>\n"
278             + "    <script>\n"
279             + LOG_TITLE_FUNCTION
280             + "      function test() {\n"
281             + "        var e = document.getElementById('d');\n"
282             + "        log(e.offsetTop);\n"
283             + "        log(e.offsetLeft);\n"
284             + "      }\n"
285             + "    </script>\n"
286             + "  </head>\n"
287             + "  <body onload='test()' style='padding: 0px; margin: 0px; border: 3px solid green;'>\n"
288             + "    <div style='padding: 0px; margin: 0px; border: 5px solid blue;'>\n"
289             + "      <div style='padding: 0px; margin: 0px; border: 7px solid red;'>\n"
290             + "        <div id='d' style='padding: 0px; margin: 0px; border: 13px solid black;'>d</div>\n"
291             + "      </div>\n"
292             + "    </div>\n"
293             + "  </body>\n"
294             + "</html>";
295         loadPageVerifyTitle2(html);
296     }
297 
298     /**
299      * @throws Exception if an error occurs
300      */
301     @Test
302     @Alerts({"0", "0"})
303     public void offsetTopAndLeft_Nothing() throws Exception {
304         final String html = DOCTYPE_HTML
305             + "<html>\n"
306             + "  <head>\n"
307             + "    <script>\n"
308             + LOG_TITLE_FUNCTION
309             + "      function test() {\n"
310             + "        var e = document.getElementById('d');\n"
311             + "        log(e.offsetTop);\n"
312             + "        log(e.offsetLeft);\n"
313             + "      }\n"
314             + "    </script>\n"
315             + "  </head>\n"
316             + "  <body onload='test()' style='padding: 0px; margin: 0px; border: 0px solid green;'>\n"
317             + "    <div style='padding: 0px; margin: 0px; border: 0px solid blue;'>\n"
318             + "      <div style='padding: 0px; margin: 0px; border: 0px solid red;'>\n"
319             + "        <div id='d' style='padding: 0px; margin: 0px; border: 0px solid black;'>d</div>\n"
320             + "      </div>\n"
321             + "    </div>\n"
322             + "  </body>\n"
323             + "</html>";
324         loadPageVerifyTitle2(html);
325     }
326 
327     /**
328      * @throws Exception if an error occurs
329      */
330     @Test
331     @Alerts({"50", "50"})
332     public void offsetTopAndLeft_AbsolutelyPositioned() throws Exception {
333         final String html = DOCTYPE_HTML
334             + "<html>\n"
335             + "  <head>\n"
336             + "    <script>\n"
337             + LOG_TITLE_FUNCTION
338             + "      function test() {\n"
339             + "        var e = document.getElementById('d');\n"
340             + "        log(e.offsetTop);\n"
341             + "        log(e.offsetLeft);\n"
342             + "      }\n"
343             + "    </script>\n"
344             + "  </head>\n"
345             + "  <body onload='test()'>\n"
346             + "    <div>\n"
347             + "      <div>\n"
348             + "        <div id='d' style='position:absolute; top:50px; left:50px;'>d</div>\n"
349             + "      </div>\n"
350             + "    </div>\n"
351             + "  </body>\n"
352             + "</html>";
353         loadPageVerifyTitle2(html);
354     }
355 
356     /**
357      * @throws Exception if an error occurs
358      */
359     @Test
360     @Alerts({"8", "8"})
361     // so far we still support the quirks behaviour
362     @HtmlUnitNYI(CHROME = {"50", "50"},
363             EDGE = {"50", "50"},
364             FF = {"50", "50"},
365             FF_ESR = {"50", "50"})
366     public void offsetTopAndLeft_AbsolutelyPositionedValueWithoutUnit() throws Exception {
367         final String html = DOCTYPE_HTML
368             + "<html>\n"
369             + "  <head>\n"
370             + "    <script>\n"
371             + LOG_TITLE_FUNCTION
372             + "      function test() {\n"
373             + "        var e = document.getElementById('d');\n"
374             + "        log(e.offsetTop);\n"
375             + "        log(e.offsetLeft);\n"
376             + "      }\n"
377             + "    </script>\n"
378             + "  </head>\n"
379             + "  <body onload='test()'>\n"
380             + "    <div>\n"
381             + "      <div>\n"
382             + "        <div id='d' style='position:absolute; top:50; left:50;'>d</div>\n"
383             + "      </div>\n"
384             + "    </div>\n"
385             + "  </body>\n"
386             + "</html>";
387         loadPageVerifyTitle2(html);
388     }
389 
390     /**
391      * @throws Exception if an error occurs
392      */
393     @Test
394     @Alerts({"1 absolute_auto 0", "2 absolute_length 50", "3 absolute_inherit 10", "4 fixed_auto 10",
395                 "5 fixed_length 50", "6 fixed_inherit 10", "7 relative_auto 0", "8 relative_length 50",
396                 "9 relative_inherit 10", "10 static_auto 0", "11 static_length 0", "12 static_inherit 0",
397                 "13 inherit_auto 0", "14 inherit_length 50", "15 inherit_inherit 10"})
398     public void offsetLeft_PositionLeft_DifferentCombinations() throws Exception {
399         final String html = DOCTYPE_HTML
400             + "<html><body onload='test()'><script language='javascript'>\n"
401             + LOG_TITLE_FUNCTION
402             + "String.prototype.trim = function() {\n"
403             + "  return this.replace(/^\\s+|\\s+$/g, '');\n"
404             + "}\n"
405             + "function test() {\n"
406             + "  var output = document.getElementById('output');\n"
407             + "  output.value = '';\n"
408             + "  var children = document.getElementById('container').childNodes;\n"
409             + "  for(var i = 0; i < children.length; i++) {\n"
410             + "    var c = children[i];\n"
411             + "    if(c.tagName) output.value += (c.innerHTML + ' ' + c.id + ' ' + c.offsetLeft + '\\n');\n"
412             + "  }\n"
413             + "  var alerts = output.value.split('\\n');\n"
414             + "  for(var i = 0; i < alerts.length; i++) {\n"
415             + "    var s = alerts[i].trim();\n"
416             + "    if(s) log(s);\n"
417             + "  }\n"
418             + "}\n"
419             + "</script>\n"
420             + "<textarea id='output' cols='40' rows='20'></textarea>\n"
421             + "<div id='container' style='position: absolute; left: 10px;'>\n"
422             + "  <div id='absolute_auto' style='position: absolute; left: auto;'>1</div>\n"
423             + "  <div id='absolute_length' style='position: absolute; left: 50px;'>2</div>\n"
424             + "  <div id='absolute_inherit' style='position: absolute; left: inherit;'>3</div>\n"
425             + "  <div id='fixed_auto' style='position: fixed; left: auto;'>4</div>\n"
426             + "  <div id='fixed_length' style='position: fixed; left: 50px;'>5</div>\n"
427             + "  <div id='fixed_inherit' style='position: fixed; left: inherit;'>6</div>\n"
428             + "  <div id='relative_auto' style='position: relative; left: auto;'>7</div>\n"
429             + "  <div id='relative_length' style='position: relative; left: 50px;'>8</div>\n"
430             + "  <div id='relative_inherit' style='position: relative; left: inherit;'>9</div>\n"
431             + "  <div id='static_auto' style='position: static; left: auto;'>10</div>\n"
432             + "  <div id='static_length' style='position: static; left: 50px;'>11</div>\n"
433             + "  <div id='static_inherit' style='position: static; left: inherit;'>12</div>\n"
434             + "  <div id='inherit_auto' style='position: inherit; left: auto;'>13</div>\n"
435             + "  <div id='inherit_length' style='position: inherit; left: 50px;'>14</div>v>\n"
436             + "  <div id='inherit_inherit' style='position: inherit; left: inherit;'>15</div>\n"
437             + "</div>\n"
438             + "</body></html>";
439         loadPageVerifyTitle2(html);
440     }
441 
442     /**
443      * @throws Exception if an error occurs
444      */
445     @Test
446     @Alerts({"40", "10"})
447     public void offsetTopAndLeft_parentAbsolute() throws Exception {
448         final String html = DOCTYPE_HTML
449             + "<html>\n"
450             + "<head>\n"
451             + "<script>\n"
452             + LOG_TITLE_FUNCTION
453             + "  function test() {\n"
454             + "    var e = document.getElementById('innerDiv');\n"
455             + "    log(e.offsetLeft);\n"
456             + "    log(e.offsetTop);\n"
457             + "  }\n"
458             + "</script>\n"
459             + "</head>\n"
460             + "<body onload='test()'>\n"
461             + "<div id='styleTest' style='position: absolute; left: 400px; top: 50px; padding: 10px 20px 30px 40px;'>\n"
462             + "<div id='innerDiv'></div>TEST</div>\n"
463             + "</body></html>";
464         loadPageVerifyTitle2(html);
465     }
466 
467     /**
468      * @throws Exception if an error occurs
469      */
470     @Test
471     @Alerts({"400", "50"})
472     public void offsetTopAndLeft_Fixed() throws Exception {
473         final String html = DOCTYPE_HTML
474             + "<html>\n"
475             + "<head>\n"
476             + "<script>\n"
477             + LOG_TITLE_FUNCTION
478             + "  function test() {\n"
479             + "    var e = document.getElementById('innerDiv');\n"
480             + "    log(e.offsetLeft);\n"
481             + "    log(e.offsetTop);\n"
482             + "  }\n"
483             + "</script>\n"
484             + "</head>\n"
485             + "<body onload='test()'>\n"
486             + "<div id='innerDiv' style='position: fixed; left: 400px; top: 50px;'></div>TEST</div>\n"
487             + "</body></html>";
488         loadPageVerifyTitle2(html);
489     }
490 
491     /**
492      * Minimal flow/layouting test: verifies that the <tt>offsetTop</tt> property changes depending
493      * on previous siblings. In the example below, the second div is below the first one, so its
494      * offsetTop must be greater than zero. This sort of test is part of the Dojo unit tests, so
495      * this needs to pass for the Dojo unit tests to pass.
496      *
497      * @throws Exception if an error occurs
498      */
499     @Test
500     @Alerts({"true", "true", "2", "3", "4", "5", "6", "7", "8", "9", "99", "199", "5999"})
501     public void offsetTopWithPreviousSiblings() throws Exception {
502         String html = DOCTYPE_HTML
503             + "<html><head>\n"
504             + "<script>\n"
505             + LOG_TITLE_FUNCTION
506             + "  function test() {\n"
507             + "    log(document.getElementById('d1').offsetTop == 0);\n"
508             + "    var d2OffsetTop = document.getElementById('d2').offsetTop;\n"
509             + "    log(d2OffsetTop > 0);\n"
510 
511             + "    log(document.getElementById('d3').offsetTop/d2OffsetTop);\n"
512             + "    log(document.getElementById('d4').offsetTop/d2OffsetTop);\n"
513             + "    log(document.getElementById('d5').offsetTop/d2OffsetTop);\n"
514             + "    log(document.getElementById('d6').offsetTop/d2OffsetTop);\n"
515             + "    log(document.getElementById('d7').offsetTop/d2OffsetTop);\n"
516             + "    log(document.getElementById('d8').offsetTop/d2OffsetTop);\n"
517             + "    log(document.getElementById('d9').offsetTop/d2OffsetTop);\n"
518             + "    log(document.getElementById('d10').offsetTop/d2OffsetTop);\n"
519 
520             + "    log(document.getElementById('d100').offsetTop/d2OffsetTop);\n"
521             + "    log(document.getElementById('d200').offsetTop/d2OffsetTop);\n"
522 
523             + "    log(document.getElementById('d6000').offsetTop/d2OffsetTop);\n"
524             + "  }\n"
525             + "</script>\n"
526             + "</head>\n"
527             + "<body style='padding: 0px; margin: 0px;' onload='test()'>\n";
528         for (int i = 1; i <= 6000; i++) {
529             html += "  <div id='d" + i + "'>bar</div>\n";
530         }
531         html = html
532             + "</body>\n"
533             + "</html>";
534         loadPageVerifyTitle2(html);
535     }
536 
537     /**
538      * Partial regression test for Bug #968.
539      * @throws Exception if an error occurs
540      */
541     @Test
542     @Alerts({"8", "8"})
543     public void offsetTopAndLeftWhenParentIsBody() throws Exception {
544         final String html = DOCTYPE_HTML
545             + "<html><head>\n"
546             + "<script>\n"
547             + LOG_TITLE_FUNCTION
548             + "  function test() {\n"
549             + "    var d = document.getElementById('d');\n"
550             + "    log(d.offsetLeft);\n"
551             + "    log(d.offsetTop);\n"
552             + "  }\n"
553             + "</script>\n"
554             + "</head>\n"
555             + "  <body onload='test()'>\n"
556             + "    <div id='d'>foo</div>\n"
557             + "  </body>\n"
558             + "</html>";
559         loadPageVerifyTitle2(html);
560     }
561 
562     /**
563      * Regression test for Bug #999.
564      * @throws Exception if an error occurs
565      */
566     @Test
567     @Alerts({"23", "19"})
568     public void offsetTopAndLeftWithRelativePosition() throws Exception {
569         final String html = DOCTYPE_HTML
570             + "<html><body onload='test()'><script language='javascript'>\n"
571             + LOG_TITLE_FUNCTION
572             + "  function test() {\n"
573             + "    var inner = document.createElement('div');\n"
574             + "    var outer = document.createElement('div');\n"
575             + "    \n"
576             + "    document.body.appendChild(outer);\n"
577             + "    outer.appendChild(inner);\n"
578             + "    \n"
579             + "    outer.style.position = 'absolute';\n"
580             + "    inner.style.position = 'relative';\n"
581             + "    inner.style.left = '19.0px';\n"
582             + "    inner.style.top = '23.0px';\n"
583             + "    \n"
584             + "    log(inner.offsetTop);\n"
585             + "    log(inner.offsetLeft);\n"
586             + "  }\n"
587             + "</script></body></html>";
588         loadPageVerifyTitle2(html);
589     }
590 
591     /**
592      * @throws Exception if an error occurs
593      */
594     @Test
595     @Alerts(DEFAULT = {"", "1240", "", "34", "34", "0", "0", "0", "0"},
596             EDGE = {"", "1232", "", "34", "34", "0", "0", "0", "0"},
597             FF_ESR = {"", "1244", "", "34", "34", "0", "0", "0", "0"})
598     @HtmlUnitNYI(EDGE = {"", "1240", "", "34", "34", "0", "0", "0", "0"},
599             FF_ESR = {"", "1240", "", "34", "34", "0", "0", "0", "0"})
600     public void offsetWidthAndHeight() throws Exception {
601         final String html = DOCTYPE_HTML
602             + "<html><head>\n"
603             + "<style>\n"
604             + ".dontDisplay { display: none }\n"
605             + ".hideMe { visibility: hidden }\n"
606             + "</style>\n"
607             + "<script>\n"
608             + LOG_TITLE_FUNCTION
609             + "  function test() {\n"
610             + "    var e = document.getElementById('myDiv');\n"
611             + "    e.style.width = 30;\n"
612             + "    log(e.style.width);\n"
613             + "    log(e.offsetWidth);\n"
614             + "    e.style.height = 55;\n"
615             + "    log(e.style.height);\n"
616             + "    log(e.offsetHeight);\n"
617             + "    e.className = 'hideMe';\n"
618             + "    log(e.offsetHeight);\n"
619             + "    e.className = 'dontDisplay';\n"
620             + "    log(e.offsetHeight);\n"
621             + "    log(e.offsetWidth);\n"
622             + "    var nested = document.getElementById('nested');\n"
623             + "    log(nested.offsetHeight);\n"
624             + "    log(nested.offsetWidth);\n"
625             + "  }\n"
626             + "</script>\n"
627             + "</head>\n"
628             + "<body onload='test()'>\n"
629             + "  <div id='myDiv' style='border: 3px solid #fff; padding: 5px;'><div id='nested'>hello</div></div>\n"
630             + "</body></html>";
631         loadPageVerifyTitle2(html);
632     }
633 
634     /**
635      * Regression test for Bug #1037.
636      * @throws Exception if an error occurs
637      */
638     @Test
639     @Alerts({"0", "0"})
640     public void offsetWidthAndHeight_displayNoneAndChildren() throws Exception {
641         final String html = DOCTYPE_HTML
642             + "<html><body>\n"
643             + "<div id='div' style='display: none;'><div style='width: 20px; height: 30px;'></div></div>\n"
644             + "<script>\n"
645             + LOG_TITLE_FUNCTION
646             + "log(document.getElementById('div').offsetWidth);</script>\n"
647             + "<script>log(document.getElementById('div').offsetHeight);</script>\n"
648             + "</body></html>";
649         loadPageVerifyTitle2(html);
650     }
651 
652     /**
653      * Regression test for Bug #1290.
654      * @throws Exception if an error occurs
655      */
656     @Test
657     @Alerts({"0", "18"})
658     public void offsetHeight_explicitHeightZero() throws Exception {
659         final String html = DOCTYPE_HTML
660             + "<html><body>\n"
661             + "<div id='d1' style='height: 0px;'><div id='d2'>x</div></div>\n"
662             + "<script>\n"
663             + LOG_TITLE_FUNCTION
664             + "log(document.getElementById('d1').offsetHeight);</script>\n"
665             + "<script>log(document.getElementById('d2').offsetHeight);</script>\n"
666             + "</body></html>";
667         loadPageVerifyTitle2(html);
668     }
669 
670     /**
671      * Partial regression test for Bug #968.
672      * @throws Exception if an error occurs
673      */
674     @Test
675     @Alerts({"75", "2", "5", "20", "50", "50", "18"})
676     public void offsetHeight_calculatedBasedOnChildren() throws Exception {
677         final String html = DOCTYPE_HTML
678             + "<html>\n"
679             + "  <body onload='h(\"d1\"); h(\"d2\"); h(\"d3\"); h(\"d4\"); h(\"d5\"); h(\"d6\"); h(\"d7\");'>\n"
680             + "    <div id='d1'>\n"
681             + "      <div id='d2' style='height:2px;'>x</div>\n"
682             + "      <div id='d3' style='height:5px;'><div id='d4' style='height:20px;'>x</div></div>\n"
683             + "      <div id='d5'><div id='d6' style='height:50px;'>x</div></div>\n"
684             + "      <div id='d7'>x</div>\n"
685             + "    </div>\n"
686             + "    <script>\n"
687             + LOG_TITLE_FUNCTION
688             + "function h(id) { log(document.getElementById(id).offsetHeight); }</script>\n"
689             + "  </body>\n"
690             + "</html>";
691         loadPageVerifyTitle2(html);
692     }
693 
694     /**
695      * @throws Exception if an error occurs
696      */
697     @Test
698     @Alerts({"true", "true"})
699     public void offsetHeight_takeFontSizeIntoAccount() throws Exception {
700         final String html = DOCTYPE_HTML
701                 + "<html><head>\n"
702                 + "<script>\n"
703                 + LOG_TITLE_FUNCTION
704                 + "  function test() {\n"
705                 + "    var elem = document.getElementById('myTestDiv');\n"
706                 + "    var initial = elem.offsetHeight;\n"
707                 + "    log(initial > 10);\n"
708                 + "    elem.style.fontSize = '42px';\n"
709                 + "    log(elem.offsetHeight > initial);\n"
710                 + "  }\n"
711                 + "</script>\n"
712                 + "</head>\n"
713                 + "<body onload='test()'>\n"
714                 + "  <div id='myTestDiv'>something</div>\n"
715                 + "</body></html>";
716 
717         loadPageVerifyTitle2(html);
718     }
719 
720     /**
721      * Value of offsetWidth is currently wrong when width is a % of the page.
722      * @throws Exception if an error occurs
723      */
724     @Test
725     @Alerts({"true", "true"})
726     public void offsetWidth_calculatedBasedOnPage() throws Exception {
727         final String html = DOCTYPE_HTML
728             + "<html><body>\n"
729             + "<div id='d1' style='width: 20%'>hello</div>\n"
730             + "<div><div id='d2' style='width: 20%'>hello</div></div>\n"
731             + "<script>\n"
732             + LOG_TITLE_FUNCTION
733             + "log(document.getElementById('d1').offsetWidth > 0);\n"
734             + "log(document.getElementById('d2').offsetWidth > 0);\n"
735             + "</script></body>\n"
736             + "</html>";
737         loadPageVerifyTitle2(html);
738     }
739 
740     /**
741      * @throws Exception if an error occurs
742      */
743     @Test
744     @Alerts("30")
745     public void offsetWidth_parentWidthConstrainsChildWidth() throws Exception {
746         final String html = DOCTYPE_HTML
747             + "<html>\n"
748             + "<head>\n"
749             + "  <style>#a { width: 30px; }</style>\n"
750             + "</head>\n"
751             + "<body>\n"
752             + "<div id='a'><div id='b'>foo</div></div>\n"
753             + "<script>\n"
754             + LOG_TITLE_FUNCTION
755             + "log(document.getElementById('b').offsetWidth);</script>\n"
756             + "</body>\n"
757             + "</html>";
758         loadPageVerifyTitle2(html);
759     }
760 
761     /**
762      * @throws Exception if an error occurs
763      */
764     @Test
765     @Alerts("30")
766     public void offsetWidth_parentWidthConstrainsChildWidth2() throws Exception {
767         final String html = DOCTYPE_HTML
768             + "<html>\n"
769             + "<head>\n"
770             + "  <style>#a{width:30px;} #b{border:2px;padding:3px;}</style>\n"
771             + "</head>\n"
772             + "<body>\n"
773             + "<div id='a'><div id='b'>foo</div></div>\n"
774             + "<script>\n"
775             + LOG_TITLE_FUNCTION
776             + "log(document.getElementById('b').offsetWidth);</script>\n"
777             + "</body>\n"
778             + "</html>";
779         loadPageVerifyTitle2(html);
780     }
781 
782     /**
783      * When CSS float is set to "right" or "left", the width of an element is related to
784      * its content and it doesn't takes the full available width.
785      * @throws Exception if an error occurs
786      */
787     @Test
788     @Alerts({"1", "0.5", "true"})
789     public void offsetWidth_cssFloat_rightOrLeft() throws Exception {
790         final String html = DOCTYPE_HTML
791             + "<html>\n"
792             + "<head></head>\n"
793             + "<body>\n"
794             + "<div id='withoutFloat1'>hello</div><div>hellohello</div>\n"
795             + "<div id='withFloat1' style='float: left'>hello</div><div style='float: left'>hellohello</div>\n"
796             + "<script>\n"
797             + LOG_TITLE_FUNCTION
798             + "var eltWithoutFloat1 = document.getElementById('withoutFloat1');\n"
799             + "log(eltWithoutFloat1.offsetWidth / eltWithoutFloat1.nextSibling.offsetWidth);\n"
800             + "var eltWithFloat1 = document.getElementById('withFloat1');\n"
801             + "log(eltWithFloat1.offsetWidth / eltWithFloat1.nextSibling.offsetWidth);\n"
802             // we don't make any strong assumption on the screen size here,
803             // but expect it to be big enough to show 10 times "hello" on one line
804             + "log(eltWithoutFloat1.offsetWidth > 10 * eltWithFloat1.offsetWidth);\n"
805             + "</script>\n"
806             + "</body>\n"
807             + "</html>";
808         loadPageVerifyTitle2(html);
809     }
810 
811     /**
812      * @throws Exception if an error occurs
813      */
814     @Test
815     @Alerts({"true", "true", "true"})
816     public void offsetWidth_takeContentIntoAccount() throws Exception {
817         final String html = DOCTYPE_HTML
818                 + "<html><head>\n"
819                 + "<script>\n"
820                 + LOG_TITLE_FUNCTION
821                 + "  function test() {\n"
822                 + "    var elem1 = document.getElementById('myTest1');\n"
823                 + "    var elem2 = document.getElementById('myTest2');\n"
824                 + "    var elem3 = document.getElementById('myTest3');\n"
825                 + "    log(elem1.offsetWidth == 0);\n"
826                 + "    log(elem1.offsetWidth < elem2.offsetWidth);\n"
827                 + "    log(elem2.offsetWidth < elem3.offsetWidth);\n"
828                 + "  }\n"
829                 + "</script>\n"
830                 + "</head>\n"
831                 + "<body onload='test()'>\n"
832                 + "  <span id='myTest1'></span>\n"
833                 + "  <span id='myTest2'>short</span>\n"
834                 + "  <span id='myTest3'>loooooooooong</span>\n"
835                 + "</body></html>";
836 
837         loadPageVerifyTitle2(html);
838     }
839 
840     /**
841      * @throws Exception if an error occurs
842      */
843     @Test
844     @Alerts({"true", "true"})
845     public void offsetWidth_takeFontSizeIntoAccount() throws Exception {
846         final String html = DOCTYPE_HTML
847                 + "<html><head>\n"
848                 + "<script>\n"
849                 + LOG_TITLE_FUNCTION
850                 + "  function test() {\n"
851                 + "    var elem = document.getElementById('myTestDiv');\n"
852                 + "    var initial = elem.offsetWidth;\n"
853                 + "    log(initial > 10);\n"
854                 + "    elem.style.fontSize = '42px';\n"
855                 + "    log(elem.offsetWidth > initial);\n"
856                 + "  }\n"
857                 + "</script>\n"
858                 + "</head>\n"
859                 + "<body onload='test()'>\n"
860                 + "  <span id='myTestDiv'>something</span>\n"
861                 + "</body></html>";
862 
863         loadPageVerifyTitle2(html);
864     }
865 
866     /**
867      * @throws Exception if the test fails
868      */
869     @Test
870     @Alerts({"something", "0"})
871     public void textContent_null() throws Exception {
872         final String html = DOCTYPE_HTML
873             + "<html><head>\n"
874             + "<script>\n"
875             + LOG_TITLE_FUNCTION
876             + "  function test() {\n"
877             + "    checkChildren();\n"
878             + "    myTestDiv.textContent = null;\n"
879             + "    checkChildren();\n"
880             + "  }\n"
881             + "  function checkChildren() {\n"
882             + "    if (myTestDiv.childNodes.length == 0)\n"
883             + "      log('0');\n"
884             + "    else\n"
885             + "      log(myTestDiv.childNodes.item(0).data);\n"
886             + "  }\n"
887             + "</script>\n"
888             + "</head>\n"
889             + "<body onload='test()'>\n"
890             + "  <div id='myTestDiv'>something</div>\n"
891             + "</body></html>";
892 
893         loadPageVerifyTitle2(html);
894     }
895 
896     /**
897      * @throws Exception if the test fails
898      */
899     @Test
900     @Alerts({"something", "0"})
901     public void textContent_emptyString() throws Exception {
902         final String html = DOCTYPE_HTML
903             + "<html><head>\n"
904             + "<script>\n"
905             + LOG_TITLE_FUNCTION
906             + "  function test() {\n"
907             + "    checkChildren();\n"
908             + "    myTestDiv.textContent = '';\n"
909             + "    checkChildren();\n"
910             + "  }\n"
911             + "  function checkChildren() {\n"
912             + "    if (myTestDiv.childNodes.length == 0)\n"
913             + "      log('0');\n"
914             + "    else\n"
915             + "      log(myTestDiv.childNodes.item(0).data);\n"
916             + "  }\n"
917             + "</script>\n"
918             + "</head>\n"
919             + "<body onload='test()'>\n"
920             + "  <div id='myTestDiv'>something</div>\n"
921             + "</body></html>";
922 
923         loadPageVerifyTitle2(html);
924     }
925 
926     /**
927      * @throws Exception if the test fails
928      */
929     @Test
930     @Alerts({"something", "Hello World"})
931     public void innerText() throws Exception {
932         final String html = DOCTYPE_HTML
933             + "<html><head>\n"
934             + "<script>\n"
935             + LOG_TITLE_FUNCTION
936             + "  function test() {\n"
937             + "    checkChildren();\n"
938             + "    myTestDiv.innerText = 'Hello World';\n"
939             + "    checkChildren();\n"
940             + "  }\n"
941             + "  function checkChildren() {\n"
942             + "    if (myTestDiv.childNodes.length == 0)\n"
943             + "      log('0');\n"
944             + "    else\n"
945             + "      log(myTestDiv.childNodes.item(0).data);\n"
946             + "  }\n"
947             + "</script>\n"
948             + "</head>\n"
949             + "<body onload='test()'>\n"
950             + "  <div id='myTestDiv'>something</div>\n"
951             + "</body></html>";
952 
953         loadPageVerifyTitle2(html);
954     }
955 
956     /**
957      * @throws Exception if the test fails
958      */
959     @Test
960     @Alerts({"something", "3", "Hello", "[object HTMLBRElement]", "World"})
961     public void innerText_LineBreak() throws Exception {
962         final String html = DOCTYPE_HTML
963             + "<html><head>\n"
964             + "<script>\n"
965             + LOG_TITLE_FUNCTION
966             + "  function test() {\n"
967             + "    log(myTestDiv.childNodes.item(0).data);\n"
968 
969             + "    myTestDiv.innerText = 'Hello\\nWorld';\n"
970             + "    log(myTestDiv.childNodes.length);\n"
971             + "    log(myTestDiv.childNodes.item(0).data);\n"
972             + "    log(myTestDiv.childNodes.item(1));\n"
973             + "    log(myTestDiv.childNodes.item(2).data);\n"
974             + "  }\n"
975             + "</script>\n"
976             + "</head>\n"
977             + "<body onload='test()'>\n"
978             + "  <div id='myTestDiv'>something</div>\n"
979             + "</body></html>";
980 
981         loadPageVerifyTitle2(html);
982     }
983 
984 
985     /**
986      * @throws Exception if the test fails
987      */
988     @Test
989     @Alerts({"0", "1", " ", "0", "1", "undefined", "1", "[object Object]"})
990     public void innerText_Empty() throws Exception {
991         final String html = DOCTYPE_HTML
992             + "<html><head>\n"
993             + "<script>\n"
994             + LOG_TITLE_FUNCTION
995             + "  function test() {\n"
996             + "    myTestDiv0.innerText = '';\n"
997             + "    log(myTestDiv0.childNodes.length);\n"
998 
999             + "    myTestDiv1.innerText = ' ';\n"
1000             + "    log(myTestDiv1.childNodes.length);\n"
1001             + "    log(myTestDiv1.childNodes.item(0).data);\n"
1002 
1003             + "    myTestDiv2.innerText = null;\n"
1004             + "    log(myTestDiv2.childNodes.length);\n"
1005 
1006             + "    myTestDiv3.innerText = undefined;\n"
1007             + "    log(myTestDiv3.childNodes.length);\n"
1008             + "    log(myTestDiv3.childNodes.item(0).data);\n"
1009 
1010             + "    myTestDiv4.innerText = { a: 'b'};\n"
1011             + "    log(myTestDiv4.childNodes.length);\n"
1012             + "    log(myTestDiv4.childNodes.item(0).data);\n"
1013             + "  }\n"
1014             + "</script>\n"
1015             + "</head>\n"
1016             + "<body onload='test()'>\n"
1017             + "  <div id='myTestDiv0'>something</div>\n"
1018             + "  <div id='myTestDiv1'>something</div>\n"
1019             + "  <div id='myTestDiv2'>something</div>\n"
1020             + "  <div id='myTestDiv3'>something</div>\n"
1021             + "  <div id='myTestDiv4'>something</div>\n"
1022             + "</body></html>";
1023 
1024         loadPageVerifyTitle2(html);
1025     }
1026 
1027     /**
1028      * @throws Exception if the test fails
1029      */
1030     @Test
1031     @Alerts({"something", "0"})
1032     public void innerText_null() throws Exception {
1033         final String html = DOCTYPE_HTML
1034             + "<html><head>\n"
1035             + "<script>\n"
1036             + LOG_TITLE_FUNCTION
1037             + "  function test() {\n"
1038             + "    checkChildren();\n"
1039             + "    myTestDiv.innerText = null;\n"
1040             + "    checkChildren();\n"
1041             + "  }\n"
1042             + "  function checkChildren() {\n"
1043             + "    if (myTestDiv.childNodes.length == 0)\n"
1044             + "      log('0');\n"
1045             + "    else\n"
1046             + "      log(myTestDiv.childNodes.item(0).data);\n"
1047             + "  }\n"
1048             + "</script>\n"
1049             + "</head>\n"
1050             + "<body onload='test()'>\n"
1051             + "  <div id='myTestDiv'>something</div>\n"
1052             + "</body></html>";
1053 
1054         loadPageVerifyTitle2(html);
1055     }
1056 
1057     /**
1058      * @throws Exception if the test fails
1059      */
1060     @Test
1061     @Alerts(DEFAULT = {"before\\nsvg-text\\nafter", "before\\nsvg-text\\nafter"},
1062             FF = {"beforesvg-textafter", "beforesvg-textafter"},
1063             FF_ESR = {"beforesvg-textafter", "beforesvg-textafter"})
1064     public void innerText_SVG() throws Exception {
1065         final String html = DOCTYPE_HTML
1066             + "<html><head>\n"
1067             + "<script>\n"
1068             + LOG_TITLE_FUNCTION_NORMALIZE
1069             + "  function test() {\n"
1070             + "    log(myTestDiv.innerText);\n"
1071             + "    log(myTestDiv.outerText);\n"
1072             + "  }\n"
1073             + "</script>\n"
1074             + "</head>\n"
1075             + "<body onload='test()'>abc"
1076             + "<div id='myTestDiv'>before<svg><title>svg-title</title><text>svg-text</text></svg>after</div>def"
1077             + "</body></html>";
1078 
1079         loadPageVerifyTitle2(html);
1080     }
1081 
1082     /**
1083      * @throws Exception if the test fails
1084      */
1085     @Test
1086     @Alerts({"MyTitlevar i;", "MyTitlevar i;"})
1087     public void innerText_Head() throws Exception {
1088         final String html = DOCTYPE_HTML
1089             + "<html><head>"
1090             + "<title>MyTitle</title>"
1091             + "<script>var i;</script>"
1092             + "</head>"
1093             + "<body onload='test()'>\n"
1094             + "<script>\n"
1095             + LOG_TEXTAREA_FUNCTION
1096             + "  function test() {\n"
1097             + "    log(document.head.innerText);\n"
1098             + "    log(document.head.outerText);\n"
1099             + "  }\n"
1100             + "</script>\n"
1101             + LOG_TEXTAREA
1102             + "</body></html>";
1103 
1104         loadPageVerifyTextArea2(html);
1105     }
1106 
1107     /**
1108      * @throws Exception if the test fails
1109      */
1110     @Test
1111     @Alerts({"something", "0"})
1112     public void innerText_emptyString() throws Exception {
1113         final String html = DOCTYPE_HTML
1114             + "<html><head>\n"
1115             + "<script>\n"
1116             + LOG_TITLE_FUNCTION
1117             + "  function test() {\n"
1118             + "    checkChildren();\n"
1119             + "    myTestDiv.innerText = '';\n"
1120             + "    checkChildren();\n"
1121             + "  }\n"
1122             + "  function checkChildren() {\n"
1123             + "    if (myTestDiv.childNodes.length == 0)\n"
1124             + "      log('0');\n"
1125             + "    else\n"
1126             + "      log(myTestDiv.childNodes.item(0).data);\n"
1127             + "  }\n"
1128             + "</script>\n"
1129             + "</head>\n"
1130             + "<body onload='test()'>\n"
1131             + "  <div id='myTestDiv'>something</div>\n"
1132             + "</body></html>";
1133 
1134         loadPageVerifyTitle2(html);
1135     }
1136 
1137     /**
1138      * @throws Exception if the test fails
1139      */
1140     @Test
1141     @Alerts({"something", " "})
1142     public void innerText_blankString() throws Exception {
1143         final String html = DOCTYPE_HTML
1144             + "<html><head>\n"
1145             + "<script>\n"
1146             + LOG_TITLE_FUNCTION
1147             + "  function test() {\n"
1148             + "    checkChildren();\n"
1149             + "    myTestDiv.innerText = '    \t';\n"
1150             + "    checkChildren();\n"
1151             + "  }\n"
1152             + "  function checkChildren() {\n"
1153             + "    if (myTestDiv.childNodes.length == 0)\n"
1154             + "      log('0');\n"
1155             + "    else\n"
1156             + "      log(myTestDiv.childNodes.item(0).data);\n"
1157             + "  }\n"
1158             + "</script>\n"
1159             + "</head>\n"
1160             + "<body onload='test()'>\n"
1161             + "  <div id='myTestDiv'>something</div>\n"
1162             + "</body></html>";
1163 
1164         loadPageVerifyTitle2(html);
1165     }
1166 
1167     /**
1168      * @throws Exception if the test fails
1169      */
1170     @Test
1171     @Alerts({"1", "undefined", "1", "Hello World", "Hello World"})
1172     public void outerText() throws Exception {
1173         final String html = DOCTYPE_HTML
1174             + "<html><head>\n"
1175             + "<script>\n"
1176             + LOG_TITLE_FUNCTION
1177             + "  function test() {\n"
1178             + "    checkChildren();\n"
1179             + "    myTestDiv.outerText = 'Hello World';\n"
1180             + "    checkChildren();\n"
1181             + "    log(myCheckDiv.innerHTML);\n"
1182             + "  }\n"
1183             + "  function checkChildren() {\n"
1184             + "    log(myCheckDiv.childNodes.length);\n"
1185             + "    if (myCheckDiv.childNodes.length > 0)\n"
1186             + "      log(myCheckDiv.childNodes.item(0).data);\n"
1187             + "  }\n"
1188             + "</script>\n"
1189             + "</head>\n"
1190             + "<body onload='test()'>\n"
1191             + "  <div id='myCheckDiv'><div id='myTestDiv'>something</div></div>\n"
1192             + "</body></html>";
1193 
1194         loadPageVerifyTitle2(html);
1195     }
1196 
1197 
1198     /**
1199      * @throws Exception if the test fails
1200      */
1201     @Test
1202     @Alerts({"3", " ", "1", " Hello World ", " Hello World "})
1203     @HtmlUnitNYI(CHROME = {"3", " ", "3", " ", " Hello World "},
1204             EDGE = {"3", " ", "3", " ", " Hello World "},
1205             FF = {"3", " ", "3", " ", " Hello World "},
1206             FF_ESR = {"3", " ", "3", " ", " Hello World "})
1207     public void outerText_removeSurroundings() throws Exception {
1208         final String html = DOCTYPE_HTML
1209             + "<html><head>\n"
1210             + "<script>\n"
1211             + LOG_TITLE_FUNCTION
1212             + "  function test() {\n"
1213             + "    checkChildren();\n"
1214             + "    myTestDiv.outerText = 'Hello World';\n"
1215             + "    checkChildren();\n"
1216             + "    log(myCheckDiv.innerHTML);\n"
1217             + "  }\n"
1218             + "  function checkChildren() {\n"
1219             + "    log(myCheckDiv.childNodes.length);\n"
1220             + "    if (myCheckDiv.childNodes.length > 0)\n"
1221             + "      log(myCheckDiv.childNodes.item(0).data);\n"
1222             + "  }\n"
1223             + "</script>\n"
1224             + "</head>\n"
1225             + "<body onload='test()'>\n"
1226             + "  <div id='myCheckDiv'>  <div id='myTestDiv'>something</div>\n</div>\n"
1227             + "</body></html>";
1228 
1229         loadPageVerifyTitle2(html);
1230     }
1231 
1232     /**
1233      * @throws Exception if the test fails
1234      */
1235     @Test
1236     @Alerts({"something", "3", "Hello", "[object HTMLBRElement]", "World",
1237              "Hello<br>World"})
1238     public void outerText_LineBreak() throws Exception {
1239         final String html = DOCTYPE_HTML
1240             + "<html><head>\n"
1241             + "<script>\n"
1242             + LOG_TITLE_FUNCTION
1243             + "  function test() {\n"
1244             + "    log(myTestDiv.childNodes.item(0).data);\n"
1245 
1246             + "    myTestDiv.outerText = 'Hello\\nWorld';\n"
1247             + "    log(myCheckDiv.childNodes.length);\n"
1248             + "    log(myCheckDiv.childNodes.item(0).data);\n"
1249             + "    log(myCheckDiv.childNodes.item(1));\n"
1250             + "    log(myCheckDiv.childNodes.item(2).data);\n"
1251 
1252             + "    log(myCheckDiv.innerHTML);\n"
1253             + "  }\n"
1254             + "</script>\n"
1255             + "</head>\n"
1256             + "<body onload='test()'>\n"
1257             + "  <div id='myCheckDiv'><div id='myTestDiv'>something</div></div>\n"
1258             + "</body></html>";
1259 
1260         loadPageVerifyTitle2(html);
1261     }
1262 
1263 
1264     /**
1265      * @throws Exception if the test fails
1266      */
1267     @Test
1268     @Alerts({"11", "3", " ", "[object HTMLDivElement]", " undefined [object Object] ",
1269              " <div id=\"myTestDiv0\"></div> undefined [object Object] "})
1270     @HtmlUnitNYI(CHROME = {"11", "11", " ", "[object HTMLDivElement]", " ",
1271                            " <div id=\"myTestDiv0\"></div> undefined [object Object] "},
1272             EDGE = {"11", "11", " ", "[object HTMLDivElement]", " ",
1273                     " <div id=\"myTestDiv0\"></div> undefined [object Object] "},
1274             FF = {"11", "11", " ", "[object HTMLDivElement]", " ",
1275                   " <div id=\"myTestDiv0\"></div> undefined [object Object] "},
1276             FF_ESR = {"11", "11", " ", "[object HTMLDivElement]", " ",
1277                       " <div id=\"myTestDiv0\"></div> undefined [object Object] "})
1278     public void outerText_Empty() throws Exception {
1279         final String html = DOCTYPE_HTML
1280             + "<html><head>\n"
1281             + "<script>\n"
1282             + LOG_TITLE_FUNCTION
1283             + "  function test() {\n"
1284             + "    myTestDiv0.innerText = '';\n"
1285             + "    log(myCheckDiv.childNodes.length);\n"
1286 
1287             + "    myTestDiv1.outerText = ' ';\n"
1288             + "    myTestDiv2.outerText = null;\n"
1289             + "    myTestDiv3.outerText = undefined;\n"
1290             + "    myTestDiv4.outerText = { a: 'b'};\n"
1291 
1292             + "    log(myCheckDiv.childNodes.length);\n"
1293             + "    log(myCheckDiv.childNodes.item(0).data);\n"
1294             + "    log(myCheckDiv.childNodes.item(1));\n"
1295             + "    log(myCheckDiv.childNodes.item(2).data);\n"
1296 
1297             + "    log(myCheckDiv.innerHTML);\n"
1298             + "  }\n"
1299             + "</script>\n"
1300             + "</head>\n"
1301             + "<body onload='test()'>\n"
1302             + "  <div id='myCheckDiv'>\n"
1303             + "    <div id='myTestDiv0'>something</div>\n"
1304             + "    <div id='myTestDiv1'>something</div>\n"
1305             + "    <div id='myTestDiv2'>something</div>\n"
1306             + "    <div id='myTestDiv3'>something</div>\n"
1307             + "    <div id='myTestDiv4'>something</div>\n"
1308             + "  </div>/n"
1309             + "</body></html>";
1310 
1311         loadPageVerifyTitle2(html);
1312     }
1313 
1314     /**
1315      * @throws Exception if the test fails
1316      */
1317     @Test
1318     @Alerts({"1", "[object HTMLDivElement]", "1", "[object Text]", ""})
1319     public void outerText_null() throws Exception {
1320         final String html = DOCTYPE_HTML
1321             + "<html><head>\n"
1322             + "<script>\n"
1323             + LOG_TITLE_FUNCTION
1324             + "  function test() {\n"
1325             + "    checkChildren();\n"
1326             + "    myTestDiv.outerText = null;\n"
1327             + "    checkChildren();\n"
1328             + "    log(myCheckDiv.innerHTML);\n"
1329             + "  }\n"
1330             + "  function checkChildren() {\n"
1331             + "    log(myCheckDiv.childNodes.length);\n"
1332             + "    if (myCheckDiv.childNodes.length > 0)\n"
1333             + "      log(myCheckDiv.childNodes.item(0));\n"
1334             + "  }\n"
1335             + "</script>\n"
1336             + "</head>\n"
1337             + "<body onload='test()'>\n"
1338             + "  <div id='myCheckDiv'><div id='myTestDiv'>something</div></div>\n"
1339             + "</body></html>";
1340 
1341         loadPageVerifyTitle2(html);
1342     }
1343 
1344     /**
1345      * @throws Exception if the test fails
1346      */
1347     @Test
1348     @Alerts({"1", "[object HTMLDivElement]", "1", "[object Text]", ""})
1349     public void outerText_emptyString() throws Exception {
1350         final String html = DOCTYPE_HTML
1351             + "<html><head>\n"
1352             + "<script>\n"
1353             + LOG_TITLE_FUNCTION
1354             + "  function test() {\n"
1355             + "    checkChildren();\n"
1356             + "    myTestDiv.outerText = '';\n"
1357             + "    checkChildren();\n"
1358             + "    log(myCheckDiv.innerHTML);\n"
1359             + "  }\n"
1360             + "  function checkChildren() {\n"
1361             + "    log(myCheckDiv.childNodes.length);\n"
1362             + "    if (myCheckDiv.childNodes.length > 0)\n"
1363             + "      log(myCheckDiv.childNodes.item(0));\n"
1364             + "  }\n"
1365             + "</script>\n"
1366             + "</head>\n"
1367             + "<body onload='test()'>\n"
1368             + "  <div id='myCheckDiv'><div id='myTestDiv'>something</div></div>\n"
1369             + "</body></html>";
1370 
1371         loadPageVerifyTitle2(html);
1372     }
1373 
1374     /**
1375      * @throws Exception if the test fails
1376      */
1377     @Test
1378     @Alerts({"1", "[object HTMLDivElement]", "1", "[object Text]", " "})
1379     public void outerText_blankString() throws Exception {
1380         final String html = DOCTYPE_HTML
1381             + "<html><head>\n"
1382             + "<script>\n"
1383             + LOG_TITLE_FUNCTION
1384             + "  function test() {\n"
1385             + "    checkChildren();\n"
1386             + "    myTestDiv.outerText = '   \t';\n"
1387             + "    checkChildren();\n"
1388             + "    log(myCheckDiv.innerHTML);\n"
1389             + "  }\n"
1390             + "  function checkChildren() {\n"
1391             + "    log(myCheckDiv.childNodes.length);\n"
1392             + "    if (myCheckDiv.childNodes.length > 0)\n"
1393             + "      log(myCheckDiv.childNodes.item(0));\n"
1394             + "  }\n"
1395             + "</script>\n"
1396             + "</head>\n"
1397             + "<body onload='test()'>\n"
1398             + "  <div id='myCheckDiv'><div id='myTestDiv'>something</div></div>\n"
1399             + "</body></html>";
1400 
1401         loadPageVerifyTitle2(html);
1402     }
1403 
1404     /**
1405      * Blur isn't fired on DIV elements for instance.
1406      * @throws Exception if the test fails
1407      */
1408     @Test
1409     @Alerts({"input handler", "blur input"})
1410     public void eventHandlerBubble_blur() throws Exception {
1411         events("blur");
1412     }
1413 
1414     /**
1415      * Focus isn't fired on DIV elements for instance.
1416      * @throws Exception if the test fails
1417      */
1418     @Test
1419     @Alerts({"input handler", "focus input"})
1420     public void eventHandlerBubble_focus() throws Exception {
1421         events("focus");
1422     }
1423 
1424     /**
1425      * @throws Exception if the test fails
1426      */
1427     @Test
1428     @Alerts({"input handler", "click input", "div handler", "click div"})
1429     public void eventHandlerBubble_click() throws Exception {
1430         events("click");
1431     }
1432 
1433     private void events(final String type) throws Exception {
1434         final String html = DOCTYPE_HTML
1435             + "<html><head>\n"
1436             + "</head>\n"
1437             + "<body>\n"
1438             + "<div id='div' on" + type + "='log(\"div handler\")'>\n"
1439             + "<input id='input' on" + type + "='log(\"input handler\")'>\n"
1440             + "</div>\n"
1441             + LOG_TEXTAREA
1442             + "<script>\n"
1443             + LOG_TEXTAREA_FUNCTION
1444             + "function addListener(id, event) {\n"
1445             + "  var handler = function(e) { log(event + ' ' + id) };\n"
1446             + "  var e = document.getElementById(id);\n"
1447             + "  e.addEventListener(event, handler, false);\n"
1448             + "}\n"
1449             + "var eventType = '" + type + "';\n"
1450             + "addListener('div', eventType);\n"
1451             + "addListener('input', eventType);\n"
1452             + "</script>\n"
1453             + "</body></html>";
1454 
1455         final WebDriver driver = loadPage2(html);
1456         driver.findElement(By.id("input")).click();
1457         final WebElement log = driver.findElement(By.id("myLog"));
1458         log.click();
1459         verifyTextArea2(driver, getExpectedAlerts());
1460     }
1461 
1462     /**
1463      * @throws Exception if the test fails
1464      */
1465     @Test
1466     @Alerts({"null", "klazz"})
1467     public void setAttributeNodeUnknown() throws Exception {
1468         final String html = DOCTYPE_HTML
1469             + "<html><head>\n"
1470             + "<script>\n"
1471             + LOG_TITLE_FUNCTION
1472             + "  function test() {\n"
1473             + "    var attribute = document.createAttribute('unknown');\n"
1474             + "    attribute.nodeValue = 'klazz';\n"
1475             + "    log(document.body.setAttributeNode(attribute));\n"
1476             + "    log(document.body.getAttributeNode('unknown').nodeValue);\n"
1477             + "  }\n"
1478             + "</script></head>\n"
1479             + "<body onload='test()'></body></html>";
1480 
1481         loadPageVerifyTitle2(html);
1482     }
1483 
1484     /**
1485      * @throws Exception if the test fails
1486      */
1487     @Test
1488     @Alerts({"null", "klazz"})
1489     public void setAttributeNodeUnknown2() throws Exception {
1490         final String html = DOCTYPE_HTML
1491             + "<html><head>\n"
1492             + "<script>\n"
1493             + LOG_TITLE_FUNCTION
1494             + "  function test() {\n"
1495             + "    var attribute = document.createAttribute('unknown');\n"
1496             + "    log(document.body.setAttributeNode(attribute));\n"
1497             + "    attribute.nodeValue = 'klazz';\n"
1498             + "    log(document.body.getAttributeNode('unknown').nodeValue);\n"
1499             + "  }\n"
1500             + "</script></head>\n"
1501             + "<body onload='test()'></body></html>";
1502 
1503         loadPageVerifyTitle2(html);
1504     }
1505 
1506     /**
1507      * @throws Exception if the test fails
1508      */
1509     @Test
1510     @Alerts({"null", "klazz"})
1511     public void setAttributeNodeClass() throws Exception {
1512         final String html = DOCTYPE_HTML
1513             + "<html><head>\n"
1514             + "<script>\n"
1515             + LOG_TITLE_FUNCTION
1516             + "  function test() {\n"
1517             + "    var attribute = document.createAttribute('class');\n"
1518             + "    attribute.nodeValue = 'klazz';\n"
1519             + "    log(document.body.setAttributeNode(attribute));\n"
1520             + "    log(document.body.getAttributeNode('class').nodeValue);\n"
1521             + "  }\n"
1522             + "</script></head>\n"
1523             + "<body onload='test()'></body></html>";
1524 
1525         loadPageVerifyTitle2(html);
1526     }
1527 
1528     /**
1529      * @throws Exception if the test fails
1530      */
1531     @Test
1532     @Alerts({"null", "klazz"})
1533     public void setAttributeNodeClass2() throws Exception {
1534         final String html = DOCTYPE_HTML
1535             + "<html><head>\n"
1536             + "<script>\n"
1537             + LOG_TITLE_FUNCTION
1538             + "  function test() {\n"
1539             + "    var attribute = document.createAttribute('class');\n"
1540             + "    log(document.body.setAttributeNode(attribute));\n"
1541             + "    attribute.nodeValue = 'klazz';\n"
1542             + "    log(document.body.getAttributeNode('class').nodeValue);\n"
1543             + "  }\n"
1544             + "</script></head>\n"
1545             + "<body onload='test()'></body></html>";
1546 
1547         loadPageVerifyTitle2(html);
1548     }
1549 
1550     /**
1551      * @throws Exception if an error occurs
1552      */
1553     @Test
1554     @Alerts({"true", "center", "true", "center", "false"})
1555     public void removeAttributeNode() throws Exception {
1556         final String html = DOCTYPE_HTML
1557             + "<html><head><script>\n"
1558             + LOG_TITLE_FUNCTION
1559             + "  function test() {\n"
1560             + "    var e = document.getElementById('foo');\n"
1561             + "    log(e.removeAttributeNode != null);\n"
1562             + "    log(e.getAttribute('align'));\n"
1563             + "    log(e.hasAttribute('align'));\n"
1564             + "    var attr = e.getAttributeNode('align');\n"
1565             + "    log(attr.value);\n"
1566             + "    e.removeAttributeNode(attr);\n"
1567             + "    log(e.hasAttribute('align'));\n"
1568             + "  }\n"
1569             + "</script></head>\n"
1570             + "<body onload='test()'>\n"
1571             + "  <div id='foo' align='center' />\n"
1572             + "</body></html>";
1573         loadPageVerifyTitle2(html);
1574     }
1575 
1576     /**
1577      * @throws Exception if the test fails
1578      */
1579     @Test
1580     @Alerts({"3", "div1"})
1581     public void querySelectorAll() throws Exception {
1582         final String html = DOCTYPE_HTML
1583             + "<html><head>\n"
1584             + "<style>\n"
1585             + "  .red   {color:#FF0000;}\n"
1586             + "  .green {color:#00FF00;}\n"
1587             + "  .blue  {color:#0000FF;}\n"
1588             + "</style>\n"
1589             + "<script>\n"
1590             + LOG_TITLE_FUNCTION
1591             + "function test() {\n"
1592             + "  var redTags = document.body.querySelectorAll('.green,.red');\n"
1593             + "  log(redTags.length);\n"
1594             + "  log(redTags.item(0).id);\n"
1595             + "}\n"
1596             + "</script></head><body onload='test()'>\n"
1597             + "  <div id='div1' class='red'>First</div>\n"
1598             + "  <div id='div2' class='red'>Second</div>\n"
1599             + "  <div id='div3' class='green'>Third</div>\n"
1600             + "  <div id='div4' class='blue'>Fourth</div>\n"
1601             + "</body></html>";
1602 
1603         loadPageVerifyTitle2(html);
1604     }
1605 
1606     /**
1607      * @throws Exception if the test fails
1608      */
1609     @Test
1610     @Alerts({"1", "p1"})
1611     public void querySelectorAllOnDisconnectedElement() throws Exception {
1612         final String html = DOCTYPE_HTML
1613             + "<html><head>\n"
1614             + "<script>\n"
1615             + LOG_TITLE_FUNCTION
1616             + "function test() {\n"
1617             + "  var myDiv = document.createElement('div');\n"
1618             + "  myDiv.innerHTML = '<p id=\"p1\" class=\"TEST\"></p>';\n"
1619             + "  var found = myDiv.querySelectorAll('.TEST');\n"
1620             + "  log(found.length);\n"
1621             + "  log(found.item(0).id);\n"
1622             + "}\n"
1623             + "</script></head>\n"
1624             + "<body onload='test()'>\n"
1625             + "</body></html>";
1626 
1627         loadPageVerifyTitle2(html);
1628     }
1629 
1630     /**
1631      * @throws Exception if the test fails
1632      */
1633     @Test
1634     @Alerts("SyntaxError/DOMException")
1635     public void querySelectorAll_badSelector() throws Exception {
1636         for (final String selector : HTMLDocumentTest.JQUERY_CUSTOM_SELECTORS) {
1637             doTestQuerySelectorAll_badSelector(selector);
1638         }
1639 
1640         // some other bad selectors tested in jQuery 1.8.2 tests
1641         final String[] otherBadSelectors = {":nth-child(2n+-0)", ":nth-child(2+0)",
1642             ":nth-child(- 1n)", ":nth-child(-1 n)"};
1643         for (final String selector : otherBadSelectors) {
1644             doTestQuerySelectorAll_badSelector(selector);
1645         }
1646     }
1647 
1648     private void doTestQuerySelectorAll_badSelector(final String selector) throws Exception {
1649         final String html = DOCTYPE_HTML
1650             + "<html><body><div id='it'></div><script>\n"
1651             + LOG_TITLE_FUNCTION
1652             + "try {\n"
1653             + "  document.getElementById('it').querySelectorAll('" + selector + "');\n"
1654             + "  log('working: " + selector + "');\n"
1655             + "} catch(e) { logEx(e); }\n"
1656             + "</script></body></html>";
1657 
1658         loadPageVerifyTitle2(html);
1659     }
1660 
1661     /**
1662      * @throws Exception if the test fails
1663      */
1664     @Test
1665     @Alerts("SyntaxError/DOMException")
1666     public void querySelector_badSelector() throws Exception {
1667         for (final String selector : HTMLDocumentTest.JQUERY_CUSTOM_SELECTORS) {
1668             doTestQuerySelector_badSelector(selector);
1669         }
1670     }
1671 
1672     private void doTestQuerySelector_badSelector(final String selector) throws Exception {
1673         final String html = DOCTYPE_HTML
1674             + "<html><body><div id='it'></div><script>\n"
1675             + LOG_TITLE_FUNCTION
1676             + "try {\n"
1677             + "  document.getElementById('it').querySelector('" + selector + "');\n"
1678             + "  log('working " + selector + "');\n"
1679             + "} catch(e) { logEx(e); }\n"
1680             + "</script></body></html>";
1681 
1682         loadPageVerifyTitle2(html);
1683     }
1684 
1685     /**
1686      * Function querySelectorAll should return nodes matched by many rules only once.
1687      * @throws Exception if the test fails
1688      */
1689     @Test
1690     @Alerts("1")
1691     public void querySelectorAll_noDuplication() throws Exception {
1692         final String html = DOCTYPE_HTML
1693             + "<html><body>\n"
1694             + "<div><span>First</span></div>\n"
1695             + "<script>\n"
1696             + LOG_TITLE_FUNCTION
1697             + "  var tags = document.body.querySelectorAll('span, div > span');\n"
1698             + "  log(tags.length);\n"
1699             + "</script></body></html>";
1700 
1701         loadPageVerifyTitle2(html);
1702     }
1703 
1704     /**
1705      * Test the use of innerHTML to set new HTML code.
1706      * @throws Exception if the test fails
1707      */
1708     @Test
1709     @Alerts({"Old = <b>Old innerHTML</b><!-- old comment -->",
1710                 "New =  <b><i id=\"newElt\">New cell value</i></b>",
1711                 "I"})
1712     public void getSetInnerHTMLComplex() throws Exception {
1713         final String html = DOCTYPE_HTML
1714             + "<html>\n"
1715             + "<head>\n"
1716             + "  <script>\n"
1717             + LOG_TEXTAREA_FUNCTION
1718             + "  function doTest() {\n"
1719             + "    var myNode = document.getElementById('myNode');\n"
1720             + "    log('Old = ' + myNode.innerHTML);\n"
1721             + "    myNode.innerHTML = ' <b><i id=\"newElt\">New cell value</i></b>';\n"
1722             + "    log('New = ' + myNode.innerHTML);\n"
1723             + "    log(document.getElementById('newElt').tagName);\n"
1724             + "  }\n"
1725             + "  </script>\n"
1726             + "</head>\n"
1727             + "<body onload='doTest()'>\n"
1728             + "  <p id='myNode'><b>Old innerHTML</b><!-- old comment --></p>\n"
1729             + LOG_TEXTAREA
1730             + "</body>\n"
1731             + "</html>";
1732 
1733         final WebDriver driver = loadPageVerifyTextArea2(html);
1734 
1735         final WebElement pElt = driver.findElement(By.id("myNode"));
1736         assertEquals("p", pElt.getTagName());
1737 
1738         final WebElement elt = driver.findElement(By.id("newElt"));
1739         assertEquals("New cell value", elt.getText());
1740         assertEquals(1, driver.getWindowHandles().size());
1741     }
1742 
1743     /**
1744      * Test the use of outerHTML to set new HTML code.
1745      * @throws Exception if the test fails
1746      */
1747     @Test
1748     @Alerts({"Old\\s=\\s<b\\sid=\"innerNode\">Old\\souterHTML</b>",
1749                 "New\\s=\\s\\s<b><i\\sid=\"newElt\">New\\scell\\svalue</i></b>",
1750                 "I"})
1751     public void getSetOuterHTMLComplex() throws Exception {
1752         final String html = DOCTYPE_HTML
1753             + "<html>\n"
1754             + "<head>\n"
1755             + "  <script>\n"
1756             + LOG_TITLE_FUNCTION_NORMALIZE
1757             + "  function doTest() {\n"
1758             + "    var myNode = document.getElementById('myNode');\n"
1759             + "    var innerNode = document.getElementById('innerNode');\n"
1760             + "    log('Old = ' + innerNode.outerHTML);\n"
1761             + "    innerNode.outerHTML = ' <b><i id=\"newElt\">New cell value</i></b>';\n"
1762             + "    log('New = ' + myNode.innerHTML);\n"
1763             + "    log(document.getElementById('newElt').tagName);\n"
1764             + "  }\n"
1765             + "  </script>\n"
1766             + "</head>\n"
1767             + "<body onload='doTest()'>\n"
1768             + "<p id='myNode'><b id='innerNode'>Old outerHTML</b></p>\n"
1769             + "</body>\n"
1770             + "</html>";
1771 
1772         final WebDriver driver = loadPageVerifyTitle2(html);
1773 
1774         final WebElement pElt = driver.findElement(By.id("myNode"));
1775         assertEquals("p", pElt.getTagName());
1776 
1777         final WebElement elt = driver.findElement(By.id("newElt"));
1778         assertEquals("New cell value", elt.getText());
1779         assertEquals(1, driver.getWindowHandles().size());
1780     }
1781 
1782     /**
1783      * @throws Exception if an error occurs
1784      */
1785     @Test
1786     @Alerts({"false", "true"})
1787     public void dispatchEvent2() throws Exception {
1788         final String html = DOCTYPE_HTML
1789             + "<html>\n"
1790             + "<head>\n"
1791             + "<script>\n"
1792             + LOG_TITLE_FUNCTION
1793             + "  function simulateClick() {\n"
1794             + "    var evt = document.createEvent('MouseEvents');\n"
1795             + "    evt.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0,"
1796                         + " false, false, false, false, 0, null);\n"
1797             + "    var cb = document.getElementById('checkbox');\n"
1798             + "    cb.dispatchEvent(evt);\n"
1799             + "  }\n"
1800             + "  function test() {\n"
1801             + "    log(document.getElementById('checkbox').checked);\n"
1802             + "    simulateClick();\n"
1803             + "    log(document.getElementById('checkbox').checked);\n"
1804             + "  }\n"
1805             + "</script>\n"
1806             + "<body onload='test()'>\n"
1807             + "  <input type='checkbox' id='checkbox'/><label for='checkbox'>Checkbox</label>\n"
1808             + "</body>\n"
1809             + "</html>";
1810 
1811         loadPageVerifyTitle2(html);
1812     }
1813 
1814     /**
1815      * Test case for issue #1626.
1816      * @throws Exception if an error occurs
1817      */
1818     @Test
1819     @Alerts("true")
1820     public void offsetLeft_PositionFixed() throws Exception {
1821         final String html = DOCTYPE_HTML
1822                 + "<html>\n"
1823                 + "<head>\n"
1824                 + "  <script>\n"
1825                 + LOG_TITLE_FUNCTION
1826                 + "  </script>\n"
1827                 + "  <style>\n"
1828                 + "    body {\n"
1829                 + "      padding: 0; margin:0;\n"
1830                 + "    }\n"
1831                 + "    #container {\n"
1832                 + "      width: 200px; position: fixed; right: 0px;\n"
1833                 + "    }\n"
1834                 + "  </style>\n"
1835                 + "</head>\n"
1836                 + "<body onload=\"log(document.getElementById('container').offsetLeft > 0)\">\n"
1837                 + "  <div id=\"container\">\n"
1838                 + "    <ul>\n"
1839                 + "      <li><span>1st</span> List Item.</li>\n"
1840                 + "      <li><span>Another</span> List Item.</li>\n"
1841                 + "    </ul>\n"
1842                 + "  </div>\n"
1843                 + "</body>\n"
1844                 + "</html>";
1845 
1846         loadPageVerifyTitle2(html);
1847     }
1848 
1849     /**
1850      * @throws Exception if an error occurs
1851      */
1852     @Test
1853     @Alerts({"clicked", "fireEvent not available"})
1854     public void fireEvent_WithoutTemplate() throws Exception {
1855         final String html = DOCTYPE_HTML
1856             + "<html>\n"
1857             + "  <head>\n"
1858             + "    <script>\n"
1859             + LOG_TITLE_FUNCTION
1860             + "    function doTest() {\n"
1861             + "      var elem = document.getElementById('a');\n"
1862             + "      if (!elem.fireEvent) { log('fireEvent not available'); return }\n"
1863             + "      elem.fireEvent('onclick');\n"
1864             + "    }\n"
1865             + "    </script>\n"
1866             + "  </head>\n"
1867             + "<body>\n"
1868             + "  <div id='a' onclick='log(\"clicked\")'>foo</div>\n"
1869             + "  <div id='b' onmouseover='doTest()'>bar</div>\n"
1870             + "</body></html>";
1871 
1872         final WebDriver driver = loadPage2(html);
1873         driver.findElement(By.id("a")).click();
1874         verifyTitle2(driver, getExpectedAlerts()[0]);
1875 
1876         final Actions actions = new Actions(driver);
1877         actions.moveToElement(driver.findElement(By.id("b")));
1878         actions.perform();
1879         verifyTitle2(driver, getExpectedAlerts());
1880     }
1881 
1882     /**
1883      * @throws Exception if an error occurs
1884      */
1885     @Test
1886     @Alerts({"click", "fireEvent not available", "fireEvent not available"})
1887     public void fireEvent_WithTemplate() throws Exception {
1888         final String html = DOCTYPE_HTML
1889             + "<html>\n"
1890             + "  <head>\n"
1891             + "    <script>\n"
1892             + LOG_TITLE_FUNCTION
1893             + "    function dolog(e) {\n"
1894             + "      log(e.type);\n"
1895             + "    }\n"
1896 
1897             + "    function doTest() {\n"
1898             + "      var elem = document.getElementById('a');\n"
1899             + "      if (!elem.fireEvent) { log('fireEvent not available'); return }\n"
1900             + "      elem.fireEvent('onclick');\n"
1901             + "    }\n"
1902 
1903             + "    function doTest2() {\n"
1904             + "      var elem = document.getElementById('a');\n"
1905             + "      if (!elem.fireEvent) { log('fireEvent not available'); return }\n"
1906             + "      var template = document.createEventObject();\n"
1907             + "      elem.fireEvent('onclick', template);\n"
1908             + "    }\n"
1909 
1910             + "    </script>\n"
1911             + "  </head>\n"
1912             + "<body>\n"
1913             + "  <div id='a' onclick='dolog(event)'>foo</div>\n"
1914             + "  <div id='b' onclick='doTest()'>bar</div>\n"
1915             + "  <div id='c' onclick='doTest2()'>baz</div>\n"
1916             + "</body></html>";
1917 
1918         final WebDriver driver = loadPage2(html);
1919         driver.findElement(By.id("a")).click();
1920         verifyTitle2(driver, getExpectedAlerts()[0]);
1921 
1922         driver.findElement(By.id("b")).click();
1923         verifyTitle2(driver, getExpectedAlerts()[0], getExpectedAlerts()[1]);
1924 
1925         driver.findElement(By.id("c")).click();
1926         verifyTitle2(driver, getExpectedAlerts());
1927     }
1928 
1929     /**
1930      * Document.write after setting innerHTML.
1931      * @throws Exception if the test fails
1932      */
1933     @Test
1934     @Alerts("hello")
1935     public void setInnerHTMLDocumentWrite() throws Exception {
1936         final String html = DOCTYPE_HTML
1937             + "<html>\n"
1938             + "<head><title>test</title></head>\n"
1939             + "<body>\n"
1940             + "<script>\n"
1941             + "     var a = document.createElement('a');\n"
1942             + "     a.innerHTML = 'break';\n"
1943             + "     document.write('hello');\n"
1944             + "</script></body></html>";
1945         final WebDriver driver = loadPage2(html);
1946         assertEquals(getExpectedAlerts()[0], driver.findElement(By.tagName("body")).getText());
1947     }
1948 
1949     /**
1950      * @throws Exception if the test fails
1951      */
1952     @Test
1953     @Alerts({"First: body1", "Second:", "Second: body1 setActive not available"})
1954     // alert conflicts with focus/blur
1955     public void setActiveAndFocus() throws Exception {
1956         final String firstHtml = DOCTYPE_HTML
1957             + "<html>\n"
1958             + "<head>\n"
1959             + "  <title>First: </title>\n"
1960             + "  <script>var win2;</script>\n"
1961             + "</head>\n"
1962             + "<body id='body1' onload='document.title += \" \" + document.activeElement.id'>\n"
1963             + "<form name='form1'>\n"
1964             + "  <input id='text1' onfocus='document.title += \" onfocus text1\"; win2.focus();'>\n"
1965             + "  <button id='button1' onClick='win2=window.open(\"" + URL_SECOND + "\", \"second\");'>Click me</a>\n"
1966             + "</form>\n"
1967             + "</body></html>";
1968 
1969         final String secondHtml = DOCTYPE_HTML
1970             + "<html>\n"
1971             + "<head>\n"
1972             + "  <title>Second: </title>\n"
1973             + "</head>\n"
1974             + "<body id='body2'>\n"
1975             + "  <input id='text2' onfocus='document.title += \" onfocus text2\"'>\n"
1976             + "  <button id='button2' onClick='doTest();'>Click me</a>\n"
1977             + "  <script>\n"
1978             + "    function doTest() {\n"
1979             + "      var elem = opener.document.getElementById('text1');\n"
1980             + "      document.title += ' ' + opener.document.activeElement.id;\n"
1981             + "      if (!elem.setActive) { document.title += ' setActive not available'; return; }\n"
1982             + "      elem.setActive();\n"
1983             + "      document.title += ' ' + opener.document.activeElement.id;\n"
1984             + "      document.title += ' ' + document.activeElement;\n"
1985             + "      document.getElementById('text2').setActive();\n"
1986             + "      document.title += ' ' + document.activeElement.id;\n"
1987             + "      document.title += ' ' + opener;\n"
1988             + "      opener.focus();\n"
1989             + "    }\n"
1990             + "  </script>\n"
1991             + "</body></html>";
1992         getMockWebConnection().setResponse(URL_SECOND, secondHtml);
1993 
1994         final WebDriver driver = loadPage2(firstHtml);
1995         assertTitle(driver, getExpectedAlerts()[0]);
1996 
1997         driver.findElement(By.id("button1")).click();
1998 
1999         driver.switchTo().window("second");
2000         assertTitle(driver, getExpectedAlerts()[1]);
2001 
2002         driver.findElement(By.id("button2")).click();
2003         assertTitle(driver,  getExpectedAlerts()[2]);
2004     }
2005 
2006     /**
2007      * @throws Exception failure
2008      */
2009     @Test
2010     @Alerts({"DIV,DIV,http://www.w3.org/1999/xhtml,null,div", "svg,svg,http://www.w3.org/2000/svg,null,svg",
2011              "g,g,http://www.w3.org/2000/svg,null,g", "svg,svg,http://www.w3.org/2000/svg,null,svg"})
2012     public void variousNames() throws Exception {
2013         final String html =
2014             "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" "
2015                             + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
2016             + "<html>\n"
2017             + "<head>\n"
2018             + "<script>\n"
2019             + LOG_TITLE_FUNCTION
2020             + "  function test() {\n"
2021             + "    debug(document.getElementById('myDiv'));\n"
2022             + "    debug(document.getElementById('mySVG'));\n"
2023             + "    debug(document.getElementById('myG'));\n"
2024             + "    debug(document.getElementById('mySVGWithNS'));\n"
2025             + "  }\n"
2026             + "  function debug(e) {\n"
2027             + "    log(e.nodeName + ',' + e.tagName + ',' + e.namespaceURI + ',' + e.prefix + ',' + e.localName);\n"
2028             + "  }\n"
2029             + "</script>\n"
2030             + "</head>\n"
2031             + "<body onload='test()'>\n"
2032             + "  <div id=myDiv>test</div>\n"
2033             + "  <svg id='mySVG'>\n"
2034             + "    <G id='myG'></G>\n"
2035             + "  </svg>\n"
2036             + "  <svg id='mySVGWithNS' xmlns='http://www.w3.org/2017/svg'>\n"
2037             + "  </svg>\n"
2038             + "</body></html>\n";
2039 
2040         loadPageVerifyTitle2(html);
2041     }
2042 
2043     /**
2044      * @throws Exception if the test fails
2045      */
2046     @Test
2047     @Alerts("success")
2048     public void setOnclick() throws Exception {
2049         eventHandlerSetterGetterTest("onclick");
2050     }
2051 
2052     /**
2053      * @throws Exception if the test fails
2054      */
2055     @Test
2056     @Alerts("success")
2057     public void setOndblclick() throws Exception {
2058         eventHandlerSetterGetterTest("ondblclick");
2059     }
2060 
2061     /**
2062      * @throws Exception if the test fails
2063      */
2064     @Test
2065     @Alerts("success")
2066     public void setOnblur() throws Exception {
2067         eventHandlerSetterGetterTest("onblur");
2068     }
2069 
2070     /**
2071      * @throws Exception if the test fails
2072      */
2073     @Test
2074     @Alerts("success")
2075     public void setOnfocus() throws Exception {
2076         eventHandlerSetterGetterTest("onfocus");
2077     }
2078 
2079     /**
2080      * @throws Exception if the test fails
2081      */
2082     @Test
2083     @Alerts("success")
2084     public void setOnkeydown() throws Exception {
2085         eventHandlerSetterGetterTest("onkeydown");
2086     }
2087 
2088     /**
2089      * @throws Exception if the test fails
2090      */
2091     @Test
2092     @Alerts("success")
2093     public void setOnkeypress() throws Exception {
2094         eventHandlerSetterGetterTest("onkeypress");
2095     }
2096 
2097     /**
2098      * @throws Exception if the test fails
2099      */
2100     @Test
2101     @Alerts("success")
2102     public void setOnkeyup() throws Exception {
2103         eventHandlerSetterGetterTest("onkeyup");
2104     }
2105 
2106     /**
2107      * @throws Exception if the test fails
2108      */
2109     @Test
2110     @Alerts("success")
2111     public void setOnmousedown() throws Exception {
2112         eventHandlerSetterGetterTest("onmousedown");
2113     }
2114 
2115     /**
2116      * @throws Exception if the test fails
2117      */
2118     @Test
2119     @Alerts("success")
2120     public void setOnmouseup() throws Exception {
2121         eventHandlerSetterGetterTest("onmouseup");
2122     }
2123 
2124     /**
2125      * @throws Exception if the test fails
2126      */
2127     @Test
2128     @Alerts("success")
2129     public void setOnmouseover() throws Exception {
2130         eventHandlerSetterGetterTest("onmouseover");
2131     }
2132 
2133     /**
2134      * @throws Exception if the test fails
2135      */
2136     @Test
2137     @Alerts("success")
2138     public void setOnmouseout() throws Exception {
2139         eventHandlerSetterGetterTest("onmouseout");
2140     }
2141 
2142     /**
2143      * @throws Exception if the test fails
2144      */
2145     @Test
2146     @Alerts("success")
2147     public void setOnmousemove() throws Exception {
2148         eventHandlerSetterGetterTest("onmousemove");
2149     }
2150 
2151     /**
2152      * @throws Exception if the test fails
2153      */
2154     @Test
2155     @Alerts("success")
2156     public void setOnresize() throws Exception {
2157         eventHandlerSetterGetterTest("onresize");
2158     }
2159 
2160     /**
2161      * @throws Exception if the test fails
2162      */
2163     @Test
2164     @Alerts("success")
2165     public void setOnerror() throws Exception {
2166         eventHandlerSetterGetterTest("onerror");
2167     }
2168 
2169     /**
2170      * @param eventName the name of the event
2171      * @throws Exception if the test fails
2172      */
2173     private void eventHandlerSetterGetterTest(final String eventName) throws Exception {
2174         final String html = DOCTYPE_HTML
2175             + "<html>\n"
2176             + "<head>\n"
2177             + "<script>\n"
2178             + LOG_TITLE_FUNCTION
2179             + "function handler(event) {}\n"
2180             + "function test() {\n"
2181             + "  var oDiv = document.getElementById('myDiv');\n"
2182             + "  oDiv." + eventName + " = handler;\n"
2183             + "  if (oDiv." + eventName + " == handler) {\n"
2184             + "    log('success');\n"
2185             + "  } else {\n"
2186             + "    log('fail');\n"
2187             + "  }\n"
2188             + "}\n"
2189             + "</script>\n"
2190             + "</head>\n"
2191             + "<body onload='test()'>\n"
2192             + "<div id='myDiv'><br/><div><span>test</span></div></div>\n"
2193             + "</body>\n"
2194             + "</html>";
2195 
2196         loadPageVerifyTitle2(html);
2197     }
2198 
2199 }