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