Home | History | Annotate | Download | only in js
      1 <!DOCTYPE HTML>
      2 <html>
      3 <script src='test.js'></script>
      4 <script src='focus.js'></script>
      5 <script>
      6 
      7 function testInactiveElement() {
      8   var a = document.getElementById('a');
      9   a.value = 'abc';
     10   a.setSelectionRange(0, 2);
     11   assertEquals(0, a.selectionStart);
     12   assertEquals(2, a.selectionEnd);
     13   var c = document.getElementById('c');
     14   c.focus();
     15   assertEquals(c, document.activeElement);
     16   focus(a);
     17   assertEquals(3, a.selectionStart);
     18   assertEquals(3, a.selectionEnd);
     19 }
     20 
     21 function testActiveElement() {
     22   var a = document.getElementById('a');
     23   a.focus();
     24   var is_blurred = false;
     25   var onBlur = function() {
     26     is_blurred = true;
     27   }
     28   a.addEventListener('blur', onBlur, false);
     29   try {
     30     focus(a);
     31     assert(!is_blurred);
     32   } catch (error) {
     33     a.removeEventListener('blur', onBlur, false);
     34     throw error;
     35   }
     36 }
     37 
     38 function testNonTextInput() {
     39   var a = document.getElementById('a');
     40   a.focus();
     41   assertEquals(a, document.activeElement);
     42   var c = document.getElementById('c');
     43   focus(c);
     44   assertEquals(c, document.activeElement);
     45 }
     46 
     47 function testEditableDiv() {
     48   var a = document.getElementById('a');
     49   a.focus();
     50   assertEquals(a, document.activeElement);
     51   var d = document.getElementById('d');
     52   focus(d);
     53   assertEquals(d, document.activeElement);
     54 }
     55 
     56 function testNonFocusableChildOfEditableDiv() {
     57   var a = document.getElementById('a');
     58   a.focus();
     59   assertEquals(a, document.activeElement);
     60   try {
     61     focus(document.getElementById('e'));
     62     assert(false);
     63   } catch (error) {
     64   }
     65 }
     66 
     67 function testFocusableChildOfEditableDiv() {
     68   var a = document.getElementById('a');
     69   a.focus();
     70   assertEquals(a, document.activeElement);
     71   var f = document.getElementById('f');
     72   focus(f);
     73   assertEquals(f, document.activeElement);
     74 }
     75 
     76 function testTransparentElement() {
     77   var a = document.getElementById('a');
     78   a.focus();
     79   assertEquals(a, document.activeElement);
     80   var g = document.getElementById('g');
     81   focus(g);
     82   assertEquals(g, document.activeElement);
     83 }
     84 
     85 </script>
     86 <body>
     87   <input type="text" id="a">
     88   <br>
     89   <textarea id="b">
     90   </textarea>
     91   <br>
     92   <input type="button" value="c" id="c">
     93   <br>
     94   <div id="d" contentEditable="true">
     95   </div>
     96   <div contentEditable="true">
     97     <div id="e">editable</div>
     98   </div>
     99   <div contentEditable="true">
    100     <input id="f" type="text">
    101   </div>
    102   <div>
    103     <input id="g" style='opacity: 0' type='text'>
    104   </div>
    105 </body>
    106 </html>
    107