Selection_removeRange.html (5993B)
1 <!doctype html> 2 <meta charset=utf-8> 3 <title>focus move tests caused by a call of Selection.removeRange()</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(staticBefore); 72 document.getSelection().removeRange(document.getSelection().getRangeAt(0)); 73 assert_equals(document.activeElement, document.body); 74 assert_equals(document.documentElement.scrollTop, 0); 75 }, "Active element should be the <body> after Selection.removeRange() to remove selected range at the start of the first text node of 'staticBefore' when active element is the <body>"); 76 test(function() { 77 resetFocusAndSelectionRange(editor); 78 document.getSelection().removeRange(document.getSelection().getRangeAt(0)); 79 assert_equals(document.activeElement, editor.element); 80 assert_equals(document.documentElement.scrollTop, 0); 81 }, "Active element should be 'editor' after Selection.removeRange() to remove selected range at the start of the first text node of 'editor' when active element is 'editor'"); 82 test(function() { 83 resetFocusAndSelectionRange(outerEditor); 84 document.getSelection().removeRange(document.getSelection().getRangeAt(0)); 85 assert_equals(document.activeElement, outerEditor.element); 86 assert_equals(document.documentElement.scrollTop, 0); 87 }, "Active element should be 'outerEditor' after Selection.removeRange() to remove selected range at the start of the first text node of 'outerEditor' when active element is 'outerEditor'"); 88 test(function() { 89 resetFocusAndSelectionRange(staticInEditor); 90 document.getSelection().removeRange(document.getSelection().getRangeAt(0)); 91 assert_equals(document.activeElement, document.body); 92 assert_equals(document.documentElement.scrollTop, 0); 93 }, "Active element should be the <body> after Selection.removeRange() to remove selected range at the start of the first text node of 'staticInEditor' when active element is the <body>"); 94 test(function() { 95 resetFocusAndSelectionRange(innerEditor); 96 document.getSelection().removeRange(document.getSelection().getRangeAt(0)); 97 assert_equals(document.activeElement, innerEditor.element); 98 assert_equals(document.documentElement.scrollTop, 0); 99 }, "Active element should be 'innerEditor' after Selection.removeRange() to remove selected range at the start of the first text node of 'innerEditor' when active element is 'innerEditor'"); 100 test(function() { 101 resetFocusAndSelectionRange(staticAfter); 102 document.getSelection().removeRange(document.getSelection().getRangeAt(0)); 103 assert_equals(document.activeElement, document.body); 104 assert_equals(document.documentElement.scrollTop, 0); 105 }, "Active element should be the <body> after Selection.removeRange() to remove selected range at the start of the first text node of 'staticAfter' when active element is the <body>"); 106 test(function() { 107 resetFocusAndSelectionRange(anchor); 108 document.getSelection().removeRange(document.getSelection().getRangeAt(0)); 109 assert_equals(document.activeElement, anchor.element); 110 assert_equals(document.documentElement.scrollTop, 0); 111 }, "Active element should be 'anchor' after Selection.removeRange() to remove selected range at the start of the first text node of 'anchor' when active element is 'anchor'"); 112 </script>