Selection_extend.html (11066B)
1 <!doctype html> 2 <meta charset=utf-8> 3 <title>focus move tests caused by a call of Selection.extend()</title> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <body> 7 <div style="height: 3000px;">Spacer to check whether or not page was scrolled down to focused editor</div> 8 <p id="staticBefore">static text</p> 9 <div id="editor" contenteditable><p>content of editor</p></div> 10 <div id="outerEditor" contenteditable 11 ><p>content of outer editor</p><div id="staticInEditor" contenteditable="false" 12 ><p>static content of outer editor</p><div id="innerEditor" contenteditable 13 ><p>content of inner editor</p></div></div></div> 14 <p id="staticAfter">static text</p> 15 <p><a id="anchor" href="about:blank">anchor</a></p> 16 <script> 17 "use strict"; 18 19 var staticBefore = { 20 element: document.getElementById("staticBefore"), 21 textNode: document.getElementById("staticBefore").firstChild, 22 textLength: document.getElementById("staticBefore").firstChild.length 23 }; 24 var editor = { 25 element: document.getElementById("editor"), 26 textNode: document.getElementById("editor").firstChild.firstChild, 27 textLength: document.getElementById("editor").firstChild.firstChild.length 28 }; 29 var outerEditor = { 30 element: document.getElementById("outerEditor"), 31 textNode: document.getElementById("outerEditor").firstChild.firstChild, 32 textLength: document.getElementById("outerEditor").firstChild.firstChild.length 33 }; 34 var staticInEditor = { 35 element: document.getElementById("staticInEditor"), 36 textNode: document.getElementById("staticInEditor").firstChild, 37 textLength: document.getElementById("staticInEditor").firstChild.length 38 }; 39 var innerEditor = { 40 element: document.getElementById("innerEditor"), 41 textNode: document.getElementById("innerEditor").firstChild.firstChild, 42 textLength: document.getElementById("innerEditor").firstChild.firstChild.length 43 }; 44 var staticAfter = { 45 element: document.getElementById("staticAfter"), 46 textNode: document.getElementById("staticAfter").firstChild, 47 textLength: document.getElementById("staticAfter").firstChild.length 48 }; 49 var anchor = { 50 element: document.getElementById("anchor"), 51 textNode: document.getElementById("anchor").firstChild, 52 textLength: document.getElementById("anchor").firstChild.length 53 }; 54 55 function resetFocusAndSelectionRange(aFocus) 56 { 57 document.getSelection().removeAllRanges(); 58 if (document.activeElement) { 59 document.activeElement.blur(); 60 } 61 if (aFocus) { 62 aFocus.element.focus(); 63 document.getSelection().collapse(aFocus.textNode, 0); 64 } else { 65 document.getSelection().collapse(staticBefore.textNode, 0); 66 } 67 document.documentElement.scrollTop = 0; 68 } 69 70 test(function() { 71 resetFocusAndSelectionRange(); 72 document.getSelection().extend(editor.textNode, 0); 73 assert_equals(document.activeElement, document.body); 74 assert_equals(document.documentElement.scrollTop, 0); 75 }, "Active element should be the <body> after Selection.extend() from selection at start of the first text node of 'staticBefore' to start of the first text node of 'editor'"); 76 test(function() { 77 resetFocusAndSelectionRange(); 78 document.getSelection().extend(outerEditor.textNode, 0); 79 assert_equals(document.activeElement, document.body); 80 assert_equals(document.documentElement.scrollTop, 0); 81 }, "Active element should be the <body> after Selection.extend() from selection at start of the first text node of 'staticBefore' to start of the first text node of 'outerEditor'"); 82 test(function() { 83 resetFocusAndSelectionRange(); 84 document.getSelection().extend(staticInEditor.textNode, 0); 85 assert_equals(document.activeElement, document.body); 86 assert_equals(document.documentElement.scrollTop, 0); 87 }, "Active element should be the <body> after Selection.extend() from selection at start of the first text node of 'staticBefore' to start of the first text node of 'staticInEditor'"); 88 test(function() { 89 resetFocusAndSelectionRange(); 90 document.getSelection().extend(innerEditor.textNode, 0); 91 assert_equals(document.activeElement, document.body); 92 assert_equals(document.documentElement.scrollTop, 0); 93 }, "Active element should be the <body> after Selection.extend() from selection at start of the first text node of 'staticBefore' to start of the first text node of 'innerEditor'"); 94 test(function() { 95 resetFocusAndSelectionRange(); 96 document.getSelection().extend(staticAfter.textNode, 0); 97 assert_equals(document.activeElement, document.body); 98 assert_equals(document.documentElement.scrollTop, 0); 99 }, "Active element should be the <body> after Selection.extend() from selection at start of the first text node of 'staticBefore' to start of the first text node of 'staticAfter'"); 100 test(function() { 101 resetFocusAndSelectionRange(); 102 document.getSelection().extend(anchor.textNode, 0); 103 assert_equals(document.activeElement, document.body); 104 assert_equals(document.documentElement.scrollTop, 0); 105 }, "Active element should be the <body> after Selection.extend() from selection at start of the first text node of 'staticBefore' to start of the first text node of 'anchor'"); 106 107 test(function() { 108 resetFocusAndSelectionRange(editor); 109 document.getSelection().extend(editor.textNode, editor.textLength); 110 assert_equals(document.activeElement, editor.element); 111 assert_equals(document.documentElement.scrollTop, 0); 112 }, "Active element should be 'editor' after Selection.extend() from selection at start of the first text node of 'editor' to end of the first text node of 'editor'"); 113 test(function() { 114 resetFocusAndSelectionRange(editor); 115 document.getSelection().extend(outerEditor.textNode, 0); 116 assert_equals(document.activeElement, editor.element); 117 assert_equals(document.documentElement.scrollTop, 0); 118 }, "Active element should be 'editor' after Selection.extend() from selection at start of the first text node of 'editor' to start of the first text node of 'outerEditor'"); 119 test(function() { 120 resetFocusAndSelectionRange(editor); 121 document.getSelection().extend(innerEditor.textNode, 0); 122 assert_equals(document.activeElement, editor.element); 123 assert_equals(document.documentElement.scrollTop, 0); 124 }, "Active element should be 'editor' after Selection.extend() from selection at start of the first text node of 'editor' to start of the first text node of 'innerEditor'"); 125 126 test(function() { 127 resetFocusAndSelectionRange(outerEditor); 128 document.getSelection().extend(outerEditor.textNode, outerEditor.textLength); 129 assert_equals(document.activeElement, outerEditor.element); 130 assert_equals(document.documentElement.scrollTop, 0); 131 }, "Active element should be 'outerEditor' after Selection.extend() from selection at start of the first text node of 'outerEditor' to end of the first text node of 'outerEditor'"); 132 test(function() { 133 resetFocusAndSelectionRange(outerEditor); 134 document.getSelection().extend(staticInEditor.textNode, 0); 135 assert_equals(document.activeElement, outerEditor.element); 136 assert_equals(document.documentElement.scrollTop, 0); 137 }, "Active element should be 'outerEditor' after Selection.extend() from selection at start of the first text node of 'outerEditor' to start of the first text node of 'staticInEditor'"); 138 test(function() { 139 resetFocusAndSelectionRange(outerEditor); 140 document.getSelection().extend(innerEditor.textNode, 0); 141 assert_equals(document.activeElement, outerEditor.element); 142 assert_equals(document.documentElement.scrollTop, 0); 143 }, "Active element should be 'outerEditor' after Selection.extend() from selection at start of the first text node of 'outerEditor' to start of the first text node of 'innerEditor'"); 144 test(function() { 145 resetFocusAndSelectionRange(outerEditor); 146 document.getSelection().extend(editor.textNode, 0); 147 assert_equals(document.activeElement, outerEditor.element); 148 assert_equals(document.documentElement.scrollTop, 0); 149 }, "Active element should be 'outerEditor' after Selection.extend() from selection at start of the first text node of 'outerEditor' to start of the first text node of 'editor'"); 150 151 test(function() { 152 resetFocusAndSelectionRange(innerEditor); 153 document.getSelection().extend(innerEditor.textNode, innerEditor.textLength); 154 assert_equals(document.activeElement, innerEditor.element); 155 assert_equals(document.documentElement.scrollTop, 0); 156 }, "Active element should be 'innerEditor' after Selection.extend() from selection at start of the first text node of 'innerEditor' to end of the first text node of 'innerEditor'"); 157 test(function() { 158 resetFocusAndSelectionRange(innerEditor); 159 document.getSelection().extend(outerEditor.textNode, 0); 160 assert_equals(document.activeElement, outerEditor.element); 161 assert_equals(document.documentElement.scrollTop, 0); 162 }, "Active element should be 'outerEditor' after Selection.extend() from selection at start of the first text node of 'innerEditor' to start of the first text node of 'outerEditor'"); 163 test(function() { 164 resetFocusAndSelectionRange(innerEditor); 165 document.getSelection().extend(staticInEditor.textNode, 0); 166 assert_equals(document.activeElement, outerEditor.element); 167 assert_equals(document.documentElement.scrollTop, 0); 168 }, "Active element should be 'outerEditor' after Selection.extend() from selection at start of the first text node of 'innerEditor' to start of the first text node of 'staticInEditor'"); 169 test(function() { 170 resetFocusAndSelectionRange(innerEditor); 171 document.getSelection().extend(anchor.textNode, 0); 172 assert_equals(document.activeElement, innerEditor.element); 173 assert_equals(document.documentElement.scrollTop, 0); 174 }, "Active element should be 'innerEditor' after Selection.extend() from selection at start of the first text node of 'innerEditor' to start of the first text node of 'anchor'"); 175 176 test(function() { 177 resetFocusAndSelectionRange(anchor); 178 document.getSelection().extend(anchor.textNode, anchor.textLength); 179 assert_equals(document.activeElement, anchor.element); 180 assert_equals(document.documentElement.scrollTop, 0); 181 }, "Active element should be 'anchor' after Selection.extend() from selection at start of the first text node of 'anchor' to end of the first text node of 'anchor' and 'anchor' has focus before the call"); 182 test(function() { 183 resetFocusAndSelectionRange(anchor); 184 anchor.element.blur(); 185 document.getSelection().extend(anchor.textNode, anchor.textLength); 186 assert_equals(document.activeElement, document.body); 187 assert_equals(document.documentElement.scrollTop, 0); 188 }, "Active element should be the <body> after Selection.extend() from selection at start of the first text node of 'anchor' to end of the first text node of 'anchor' and 'anchor' doesn't have focus before the call"); 189 </script>