Selection_collapseToStart.html (8087B)
1 <!doctype html> 2 <meta charset=utf-8> 3 <title>focus move tests caused by a call of Selection.collapseToStart()</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().setBaseAndExtent(anchor.textNode, 0, 73 anchor.textNode, anchor.textLength); 74 document.getSelection().collapseToStart(); 75 assert_equals(document.activeElement, document.body); 76 assert_equals(document.documentElement.scrollTop, 0); 77 }, "Active element should be the <body> after Selection.collapseToStart() with selection between start of the first text node of 'anchor' and end of the first text node of 'anchor'"); 78 test(function() { 79 resetFocusAndSelectionRange(editor); 80 document.getSelection().setBaseAndExtent(editor.textNode, 0, 81 editor.textNode, editor.textLength); 82 document.getSelection().collapseToStart(); 83 assert_equals(document.activeElement, editor.element); 84 assert_equals(document.documentElement.scrollTop, 0); 85 }, "Active element should be 'editor' after Selection.collapseToStart() with selection between start of the first text node of 'editor' and end of the first text node of 'editor'"); 86 test(function() { 87 resetFocusAndSelectionRange(innerEditor); 88 document.getSelection().setBaseAndExtent(innerEditor.textNode, 0, 89 innerEditor.textNode, innerEditor.textLength); 90 document.getSelection().collapseToStart(); 91 assert_equals(document.activeElement, innerEditor.element); 92 assert_equals(document.documentElement.scrollTop, 0); 93 }, "Active element should be 'innerEditor' after Selection.collapseToStart() with selection between start of the first text node of 'innerEditor' and end of the first text node of 'innerEditor'"); 94 test(function() { 95 resetFocusAndSelectionRange(anchor); 96 document.getSelection().setBaseAndExtent(anchor.textNode, 0, 97 anchor.textNode, anchor.textLength); 98 document.getSelection().collapseToStart(); 99 assert_equals(document.activeElement, anchor.element); 100 assert_equals(document.documentElement.scrollTop, 0); 101 }, "Active element should be 'anchor' after Selection.collapseToStart() with selection between start of the first text node of 'anchor' and end of the first text node of 'anchor' and 'anchor' has focus before the call"); 102 test(function() { 103 resetFocusAndSelectionRange(); 104 document.getSelection().setBaseAndExtent(staticBefore.textNode, 0, 105 editor.textNode, editor.textLength); 106 document.getSelection().collapseToStart(); 107 assert_equals(document.activeElement, document.body); 108 assert_equals(document.documentElement.scrollTop, 0); 109 }, "Active element should be the <body> after Selection.collapseToStart() with selection between start of the first text node of 'staticBefore' and end of the first text node of 'editor'"); 110 test(function() { 111 resetFocusAndSelectionRange(); 112 document.getSelection().setBaseAndExtent(editor.textNode, 0, 113 outerEditor.textNode, outerEditor.textLength); 114 document.getSelection().collapseToStart(); 115 assert_equals(document.activeElement, editor.element); 116 assert_equals(document.documentElement.scrollTop, 0); 117 }, "Active element should be 'outerEditor' after Selection.collapseToStart() with selection between start of the first text node of 'editor' and end of the first text node of 'outerEditor'"); 118 test(function() { 119 resetFocusAndSelectionRange(outerEditor); 120 document.getSelection().setBaseAndExtent(outerEditor.textNode, 0, 121 staticInEditor.textNode, staticInEditor.textLength); 122 document.getSelection().collapseToStart(); 123 assert_equals(document.activeElement, outerEditor.element); 124 assert_equals(document.documentElement.scrollTop, 0); 125 }, "Active element should be 'outerEditor' after Selection.collapseToStart() with selection between start of the first text node of 'outerEditor' and end of the first text node of 'staticInEditor'"); 126 test(function() { 127 resetFocusAndSelectionRange(outerEditor); 128 document.getSelection().setBaseAndExtent(outerEditor.textNode, 0, 129 innerEditor.textNode, innerEditor.textLength); 130 document.getSelection().collapseToStart(); 131 assert_equals(document.activeElement, outerEditor.element); 132 assert_equals(document.documentElement.scrollTop, 0); 133 }, "Active element should be 'outerEditor' after Selection.collapseToStart() with selection between start of the first text node of 'outerEditor' and end of the first text node of 'innerEditor'"); 134 test(function() { 135 resetFocusAndSelectionRange(); 136 document.getSelection().setBaseAndExtent(innerEditor.textNode, 0, 137 staticAfter.textNode, staticAfter.textLength); 138 document.getSelection().collapseToStart(); 139 assert_equals(document.activeElement, innerEditor.element); 140 assert_equals(document.documentElement.scrollTop, 0); 141 }, "Active element should be 'innerEditor' after Selection.collapseToStart() with selection between start of the first text node of 'innerEditor' and end of the first text node of 'staticAfter'"); 142 </script>