Debian Codegraph @

testing/web-platform/mozilla/tests/focus/Selection_collapse.html

raw
1 <!doctype html>
2 <meta charset=utf-8>
3 <title>focus move tests caused by a call of Selection.collapse()</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().collapse(staticBefore.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.collapse() not moving selection from first text node in the <body>");
76 test(function() {
77 resetFocusAndSelectionRange();
78 document.getSelection().collapse(editor.textNode, 0);
79 assert_equals(document.activeElement, editor.element);
80 assert_equals(document.documentElement.scrollTop, 0);
81 }, "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'");
82 test(function() {
83 resetFocusAndSelectionRange();
84 document.getSelection().collapse(outerEditor.textNode, 0);
85 assert_equals(document.activeElement, outerEditor.element);
86 assert_equals(document.documentElement.scrollTop, 0);
87 }, "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'");
88 test(function() {
89 resetFocusAndSelectionRange();
90 document.getSelection().collapse(staticInEditor.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.collapse() moving selection from first text node in the <body> to start of the first text node of 'staticInEditor'");
94 test(function() {
95 resetFocusAndSelectionRange();
96 document.getSelection().collapse(innerEditor.textNode, 0);
97 assert_equals(document.activeElement, innerEditor.element);
98 assert_equals(document.documentElement.scrollTop, 0);
99 }, "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'");
100 test(function() {
101 resetFocusAndSelectionRange();
102 document.getSelection().collapse(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.collapse() moving selection from first text node in the <body> to start of the first text node of 'anchor'");
106 test(function() {
107 resetFocusAndSelectionRange(outerEditor);
108 document.getSelection().collapse(innerEditor.textNode, 0);
109 assert_equals(document.activeElement, innerEditor.element);
110 assert_equals(document.documentElement.scrollTop, 0);
111 }, "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'");
112 test(function() {
113 resetFocusAndSelectionRange(outerEditor);
114 document.getSelection().collapse(staticInEditor.textNode, 0);
115 assert_equals(document.activeElement, outerEditor.element);
116 assert_equals(document.documentElement.scrollTop, 0);
117 }, "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'");
118 test(function() {
119 resetFocusAndSelectionRange(innerEditor);
120 document.getSelection().collapse(staticBefore.textNode, 0);
121 assert_equals(document.activeElement, innerEditor.element);
122 assert_equals(document.documentElement.scrollTop, 0);
123 }, "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>");
124 test(function() {
125 resetFocusAndSelectionRange(innerEditor);
126 document.getSelection().collapse(editor.textNode, 0);
127 assert_equals(document.activeElement, editor.element);
128 assert_equals(document.documentElement.scrollTop, 0);
129 }, "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'");
130 test(function() {
131 resetFocusAndSelectionRange(innerEditor);
132 document.getSelection().collapse(outerEditor.textNode, 0);
133 assert_equals(document.activeElement, outerEditor.element);
134 assert_equals(document.documentElement.scrollTop, 0);
135 }, "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'");
136 test(function() {
137 resetFocusAndSelectionRange(innerEditor);
138 document.getSelection().collapse(staticInEditor.textNode, 0);
139 assert_equals(document.activeElement, innerEditor.element);
140 assert_equals(document.documentElement.scrollTop, 0);
141 }, "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'");
142 test(function() {
143 resetFocusAndSelectionRange(innerEditor);
144 document.getSelection().collapse(anchor.textNode, 0);
145 assert_equals(document.activeElement, innerEditor.element);
146 assert_equals(document.documentElement.scrollTop, 0);
147 }, "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'");
148 </script>