-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
selection-start-end.html
146 lines (132 loc) · 5.15 KB
/
selection-start-end.html
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
<!doctype html>
<meta charset=utf-8>
<title></title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<div id=log></div>
<script>
function createInputElement(value, append, suffix) {
var el = document.createElement("input");
el.type = "text";
el.value = value;
el.id = "input" + (append ? "-appended" : "-not-appended") + (suffix ? suffix : "");
if (append) {
document.body.appendChild(el);
}
return el;
};
function createTextareaElement(value, append, suffix) {
var el = document.createElement("textarea");
el.value = value;
el.id = "textarea" + (append ? "-appended" : "-not-appended") + (suffix ? suffix : "");
if (append) {
document.body.appendChild(el);
}
return el;
};
function createPrefocusedInputElement(value, append) {
var el = createInputElement(value, append, "-prefocused");
el.focus();
el.blur();
return el;
}
function createPrefocusedTextareaElement(value, append) {
var el = createTextareaElement(value, append, "-prefocused");
el.focus();
el.blur();
return el;
}
function createTestElements(value) {
return [ createInputElement(value, true),
createInputElement(value, false),
createPrefocusedInputElement(value, true),
createPrefocusedInputElement(value, false),
createTextareaElement(value, true),
createTextareaElement(value, false),
createPrefocusedTextareaElement(value, true),
createPrefocusedTextareaElement(value, false),
];
}
var testValue = "abcdefghij";
test(function() {
assert_equals(testValue.length, 10);
}, "Sanity check for testValue length; if this fails, variou absolute offsets in the test below need to be adjusted to be less than testValue.length");
for (let prop of ["selectionStart", "selectionEnd"]) {
for (let el of createTestElements(testValue)) {
test(function() {
assert_equals(el.selectionStart, testValue.length);
}, `Initial .value set on ${el.id} should set ${prop} to end of value`);
}
}
test(function() {
for (let el of createTestElements(testValue)) {
var t = async_test(`onselect should fire when selectionStart is changed on ${el.id}`);
el.onselect = t.step_func_done(function(e) {
assert_equals(e.type, "select");
el.remove();
});
el.selectionStart = 2;
}
}, "onselect should fire when selectionStart is changed");
test(function() {
for (let el of createTestElements(testValue)) {
var t = async_test(`onselect should fire when selectionEnd is changed on ${el.id}`);
el.onselect = t.step_func_done(function(e) {
assert_equals(e.type, "select");
el.remove();
});
el.selectionEnd = 2;
}
}, "onselect should fire when selectionEnd is changed");
test(function() {
for (let el of createTestElements(testValue)) {
el.selectionStart = 0;
el.selectionEnd = 5;
el.selectionStart = 8;
assert_equals(el.selectionStart, 8, `selectionStart on ${el.id}`);
assert_equals(el.selectionEnd, 8, `selectionEnd on ${el.id}`);
el.remove();
}
}, "Setting selectionStart to a value larger than selectionEnd should increase selectionEnd");
test(function() {
for (let el of createTestElements(testValue)) {
el.selectionStart = 8;
el.selectionEnd = 5;
assert_equals(el.selectionStart, 5, `selectionStart on ${el.id}`);
assert_equals(el.selectionEnd, 5, `selectionEnd on ${el.id}`);
el.remove();
}
}, "Setting selectionEnd to a value smaller than selectionStart should decrease selectionStart");
test(function() {
for (let el of createTestElements(testValue)) {
el.selectionStart = 0;
assert_equals(el.selectionStart, 0, `We just set it on ${el.id}`);
el.selectionStart = -1;
assert_equals(el.selectionStart, testValue.length,
`selectionStart setter on ${el.id} should convert -1 to 2^32-1`);
el.selectionStart = Math.pow(2, 32);
assert_equals(el.selectionStart, 0,
`selectionStart setter on ${el.id} should convert 2^32 to 0`);
el.selectionStart = Math.pow(2, 32) - 1;
assert_equals(el.selectionStart, testValue.length,
`selectionStart setter on ${el.id} should leave 2^32-1 as-is`);
el.remove();
}
}, "selectionStart edge-case values");
test(function() {
for (let el of createTestElements(testValue)) {
el.selectionEnd = 0;
assert_equals(el.selectionEnd, 0, `We just set it on ${el.id}`);
el.selectionEnd = -1;
assert_equals(el.selectionEnd, testValue.length,
`selectionEnd setter on ${el.id} should convert -1 to 2^32-1`);
el.selectionEnd = Math.pow(2, 32);
assert_equals(el.selectionEnd, 0,
`selectionEnd setter on ${el.id} should convert 2^32 to 0`);
el.selectionEnd = Math.pow(2, 32) - 1;
assert_equals(el.selectionEnd, testValue.length,
`selectionEnd setter on ${el.id} should leave 2^32-1 as-is`);
el.remove();
}
}, "selectionEnd edge-case values");
</script>