Selection_selectAllChildren.html (15012B)
1 <!doctype html> 2 <meta charset=utf-8> 3 <title>focus move tests caused by a call of Selection.selectAllChildren()</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().selectAllChildren(staticBefore.element); 73 assert_equals(document.activeElement, document.body); 74 assert_equals(document.documentElement.scrollTop, 0); 75 }, "Active element should be the <body> after Selection.selectAllChildren() to select the children of 'staticBefore' when active element is the <body>"); 76 test(function() { 77 resetFocusAndSelectionRange(); 78 document.getSelection().selectAllChildren(editor.element); 79 assert_equals(document.activeElement, editor.element); 80 assert_equals(document.documentElement.scrollTop, 0); 81 }, "Active element should be 'editor' after Selection.selectAllChildren() to select the children of 'editor' when active element is the <body>"); 82 test(function() { 83 resetFocusAndSelectionRange(); 84 document.getSelection().selectAllChildren(outerEditor.element); 85 assert_equals(document.activeElement, outerEditor.element); 86 assert_equals(document.documentElement.scrollTop, 0); 87 }, "Active element should be 'outerEditor' after Selection.selectAllChildren() to select the children of 'outerEditor' when active element is the <body>"); 88 test(function() { 89 resetFocusAndSelectionRange(); 90 document.getSelection().selectAllChildren(staticInEditor.element); 91 assert_equals(document.activeElement, document.body); 92 assert_equals(document.documentElement.scrollTop, 0); 93 }, "Active element should be the <body> after Selection.selectAllChildren() to select the children of 'staticInEditor' when active element is the <body>"); 94 test(function() { 95 resetFocusAndSelectionRange(); 96 document.getSelection().selectAllChildren(innerEditor.element); 97 assert_equals(document.activeElement, innerEditor.element); 98 assert_equals(document.documentElement.scrollTop, 0); 99 }, "Active element should be 'innerEditor' after Selection.selectAllChildren() to select the children of 'innerEditor' when active element is the <body>"); 100 test(function() { 101 resetFocusAndSelectionRange(); 102 document.getSelection().selectAllChildren(anchor.element); 103 assert_equals(document.activeElement, document.body); 104 assert_equals(document.documentElement.scrollTop, 0); 105 }, "Active element should be the <body> after Selection.selectAllChildren() to select the children of 'anchor' when active element is the <body>"); 106 107 test(function() { 108 resetFocusAndSelectionRange(editor); 109 document.getSelection().selectAllChildren(staticBefore.element); 110 assert_equals(document.activeElement, editor.element); 111 assert_equals(document.documentElement.scrollTop, 0); 112 }, "Active element should be 'editor' after Selection.selectAllChildren() to select the children of 'staticBefore' when active element is 'editor'"); 113 test(function() { 114 resetFocusAndSelectionRange(editor); 115 document.getSelection().selectAllChildren(editor.element); 116 assert_equals(document.activeElement, editor.element); 117 assert_equals(document.documentElement.scrollTop, 0); 118 }, "Active element should be 'editor' after Selection.selectAllChildren() to select the children of 'editor' when active element is 'editor'"); 119 test(function() { 120 resetFocusAndSelectionRange(editor); 121 document.getSelection().selectAllChildren(outerEditor.element); 122 assert_equals(document.activeElement, outerEditor.element); 123 assert_equals(document.documentElement.scrollTop, 0); 124 }, "Active element should be 'outerEditor' after Selection.selectAllChildren() to select the children of 'outerEditor' when active element is 'editor'"); 125 test(function() { 126 resetFocusAndSelectionRange(editor); 127 document.getSelection().selectAllChildren(staticInEditor.element); 128 assert_equals(document.activeElement, editor.element); 129 assert_equals(document.documentElement.scrollTop, 0); 130 }, "Active element should be 'editor' after Selection.selectAllChildren() to select the children of 'staticInEditor' when active element is 'editor'"); 131 test(function() { 132 resetFocusAndSelectionRange(editor); 133 document.getSelection().selectAllChildren(innerEditor.element); 134 assert_equals(document.activeElement, innerEditor.element); 135 assert_equals(document.documentElement.scrollTop, 0); 136 }, "Active element should be 'innerEditor' after Selection.selectAllChildren() to select the children of 'innerEditor' when active element is 'editor'"); 137 test(function() { 138 resetFocusAndSelectionRange(editor); 139 document.getSelection().selectAllChildren(anchor.element); 140 assert_equals(document.activeElement, editor.element); 141 assert_equals(document.documentElement.scrollTop, 0); 142 }, "Active element should be 'editor' after Selection.selectAllChildren() to select the children of 'anchor' when active element is 'editor'"); 143 144 test(function() { 145 resetFocusAndSelectionRange(outerEditor); 146 document.getSelection().selectAllChildren(staticBefore.element); 147 assert_equals(document.activeElement, outerEditor.element); 148 assert_equals(document.documentElement.scrollTop, 0); 149 }, "Active element should be 'outerEditor' after Selection.selectAllChildren() to select the children of 'staticBefore' when active element is 'outerEditor'"); 150 test(function() { 151 resetFocusAndSelectionRange(outerEditor); 152 document.getSelection().selectAllChildren(editor.element); 153 assert_equals(document.activeElement, editor.element); 154 assert_equals(document.documentElement.scrollTop, 0); 155 }, "Active element should be 'editor' after Selection.selectAllChildren() to select the children of 'editor' when active element is 'outerEditor'"); 156 test(function() { 157 resetFocusAndSelectionRange(outerEditor); 158 document.getSelection().selectAllChildren(outerEditor.element); 159 assert_equals(document.activeElement, outerEditor.element); 160 assert_equals(document.documentElement.scrollTop, 0); 161 }, "Active element should be 'outerEditor' after Selection.selectAllChildren() to select the children of 'outerEditor' when active element is 'outerEditor'"); 162 test(function() { 163 resetFocusAndSelectionRange(outerEditor); 164 document.getSelection().selectAllChildren(staticInEditor.element); 165 assert_equals(document.activeElement, outerEditor.element); 166 assert_equals(document.documentElement.scrollTop, 0); 167 }, "Active element should be 'ouerEditor' after Selection.selectAllChildren() to select the children of 'staticInEditor' when active element is 'outerEditor'"); 168 test(function() { 169 resetFocusAndSelectionRange(outerEditor); 170 document.getSelection().selectAllChildren(innerEditor.element); 171 assert_equals(document.activeElement, innerEditor.element); 172 assert_equals(document.documentElement.scrollTop, 0); 173 }, "Active element should be 'innerEditor' after Selection.selectAllChildren() to select the children of 'innerEditor' when active element is 'outerEditor'"); 174 test(function() { 175 resetFocusAndSelectionRange(outerEditor); 176 document.getSelection().selectAllChildren(anchor.element); 177 assert_equals(document.activeElement, outerEditor.element); 178 assert_equals(document.documentElement.scrollTop, 0); 179 }, "Active element should be 'outerEditor' after Selection.selectAllChildren() to select the children of 'anchor' when active element is 'outerEditor'"); 180 181 test(function() { 182 resetFocusAndSelectionRange(innerEditor); 183 document.getSelection().selectAllChildren(staticBefore.element); 184 assert_equals(document.activeElement, innerEditor.element); 185 assert_equals(document.documentElement.scrollTop, 0); 186 }, "Active element should be 'innerEditor' after Selection.selectAllChildren() to select the children of 'staticBefore' when active element is 'innerEditor'"); 187 test(function() { 188 resetFocusAndSelectionRange(innerEditor); 189 document.getSelection().selectAllChildren(editor.element); 190 assert_equals(document.activeElement, editor.element); 191 assert_equals(document.documentElement.scrollTop, 0); 192 }, "Active element should be 'editor' after Selection.selectAllChildren() to select the children of 'editor' when active element is 'innerEditor'"); 193 test(function() { 194 resetFocusAndSelectionRange(innerEditor); 195 document.getSelection().selectAllChildren(outerEditor.element); 196 assert_equals(document.activeElement, outerEditor.element); 197 assert_equals(document.documentElement.scrollTop, 0); 198 }, "Active element should be 'outerEditor' after Selection.selectAllChildren() to select the children of 'outerEditor' when active element is 'innerEditor'"); 199 test(function() { 200 resetFocusAndSelectionRange(innerEditor); 201 document.getSelection().selectAllChildren(staticInEditor.element); 202 assert_equals(document.activeElement, innerEditor.element); 203 assert_equals(document.documentElement.scrollTop, 0); 204 }, "Active element should be 'innerEditor' after Selection.selectAllChildren() to select the children of 'staticInEditor' when active element is 'innerEditor'"); 205 test(function() { 206 resetFocusAndSelectionRange(innerEditor); 207 document.getSelection().selectAllChildren(innerEditor.element); 208 assert_equals(document.activeElement, innerEditor.element); 209 assert_equals(document.documentElement.scrollTop, 0); 210 }, "Active element should be 'innerEditor' after Selection.selectAllChildren() to select the children of 'innerEditor' when active element is 'innerEditor'"); 211 test(function() { 212 resetFocusAndSelectionRange(innerEditor); 213 document.getSelection().selectAllChildren(anchor.element); 214 assert_equals(document.activeElement, innerEditor.element); 215 assert_equals(document.documentElement.scrollTop, 0); 216 }, "Active element should be 'innerEditor' after Selection.selectAllChildren() to select the children of 'anchor' when active element is 'innerEditor'"); 217 218 test(function() { 219 resetFocusAndSelectionRange(anchor); 220 document.getSelection().selectAllChildren(staticBefore.element); 221 assert_equals(document.activeElement, anchor.element); 222 assert_equals(document.documentElement.scrollTop, 0); 223 }, "Active element should be 'anchor' after Selection.selectAllChildren() to select the children of 'staticBefore' when active element is 'anchor'"); 224 test(function() { 225 resetFocusAndSelectionRange(anchor); 226 document.getSelection().selectAllChildren(editor.element); 227 assert_equals(document.activeElement, editor.element); 228 assert_equals(document.documentElement.scrollTop, 0); 229 }, "Active element should be 'editor' after Selection.selectAllChildren() to select the children of 'editor' when active element is 'anchor'"); 230 test(function() { 231 resetFocusAndSelectionRange(anchor); 232 document.getSelection().selectAllChildren(outerEditor.element); 233 assert_equals(document.activeElement, outerEditor.element); 234 assert_equals(document.documentElement.scrollTop, 0); 235 }, "Active element should be 'outerEditor' after Selection.selectAllChildren() to select the children of 'outerEditor' when active element is 'anchor'"); 236 test(function() { 237 resetFocusAndSelectionRange(anchor); 238 document.getSelection().selectAllChildren(staticInEditor.element); 239 assert_equals(document.activeElement, anchor.element); 240 assert_equals(document.documentElement.scrollTop, 0); 241 }, "Active element should be 'anchor' after Selection.selectAllChildren() to select the children of 'staticInEditor' when active element is 'anchor'"); 242 test(function() { 243 resetFocusAndSelectionRange(anchor); 244 document.getSelection().selectAllChildren(innerEditor.element); 245 assert_equals(document.activeElement, innerEditor.element); 246 assert_equals(document.documentElement.scrollTop, 0); 247 }, "Active element should be 'innerEditor' after Selection.selectAllChildren() to select the children of 'innerEditor' when active element is 'anchor'"); 248 test(function() { 249 resetFocusAndSelectionRange(anchor); 250 document.getSelection().selectAllChildren(anchor.element); 251 assert_equals(document.activeElement, anchor.element); 252 assert_equals(document.documentElement.scrollTop, 0); 253 }, "Active element should be 'anchor' after Selection.selectAllChildren() to select the children of 'anchor' when active element is 'anchor'"); 254 </script>