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