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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
<!DOCTYPE html>
<meta charset="utf-8">
<title>Test selectionchange events from text controls</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel="stylesheet" href="/fonts/ahem.css">
<style>
input,
textarea {
font: 16px/1 Ahem;
}
</style>
<input id="input" width="200"><br>
<textarea id="textarea" width="200"></textarea>
<script>
class SelectionChangeCollector {
/**
* @param {HTMLElement} target
*/
constructor(target) {
this.target = target;
this.events = [];
target.addEventListener("selectionchange", ev => {
this.events.push(ev);
});
}
clear() {
this.events.length = 0;
}
}
const data = {
collectors: [
new SelectionChangeCollector(input),
new SelectionChangeCollector(input.cloneNode()),
new SelectionChangeCollector(textarea),
new SelectionChangeCollector(textarea.cloneNode(true)),
],
async initialize() {
for (const collector of this.collectors) {
collector.target.value = "XXXXXXXXXXXXXXXXXXX";
collector.target.blur();
collector.target.setSelectionRange(0, 0);
}
await this.spin();
for (const collector of this.collectors) {
collector.clear();
}
},
spin() {
return new Promise(setTimeout);
},
async assert_empty_spin() {
// firing selectionchange must be asynchronous
for (const collector of this.collectors) {
assert_equals(collector.events.length, 0);
}
await this.spin();
}
};
for (const collector of data.collectors) {
const target = collector.target;
const name = `the ${!target.parentNode ? "disconnected " : ""}${target.localName} element`;
promise_test(async () => {
await data.initialize();
target.selectionStart = 1;
await data.assert_empty_spin();
assert_equals(collector.events.length, 1);
}, `Modifying selectionStart value of ${name}`);
promise_test(async () => {
await data.initialize();
target.selectionEnd = 1;
await data.assert_empty_spin();
assert_equals(collector.events.length, 1);
}, `Modifying selectionEnd value of ${name}`);
promise_test(async () => {
await data.initialize();
target.setSelectionRange(0, 4);
await data.assert_empty_spin();
assert_equals(collector.events.length, 1);
}, `Calling setSelectionRange() on ${name}`);
promise_test(async () => {
await data.initialize();
target.select();
await data.assert_empty_spin();
assert_equals(collector.events.length, 1);
}, `Calling select() on ${name}`);
promise_test(async () => {
await data.initialize();
target.setRangeText("newmiddle", 2, 3, "select");
await data.assert_empty_spin();
assert_equals(collector.events.length, 1);
}, `Calling setRangeText() on ${name}`);
promise_test(async () => {
await data.initialize();
target.selectionStart = 0;
await data.assert_empty_spin();
assert_equals(collector.events.length, 0);
}, `Setting initial zero selectionStart value on ${name}`);
promise_test(async () => {
await data.initialize();
target.selectionStart = 2;
target.selectionStart = 2;
await data.assert_empty_spin();
assert_equals(collector.events.length, 1);
}, `Setting the same selectionStart value twice on ${name}`);
promise_test(async () => {
await data.initialize();
target.selectionEnd = 0;
await data.assert_empty_spin();
assert_equals(collector.events.length, 0);
}, `Setting initial zero selectionEnd value on ${name}`);
promise_test(async () => {
await data.initialize();
target.selectionEnd = 2;
target.selectionEnd = 2;
await data.assert_empty_spin();
assert_equals(collector.events.length, 1);
}, `Setting the same selectionEnd value twice on ${name}`);
promise_test(async () => {
await data.initialize();
target.setSelectionRange(0, 0);
await data.assert_empty_spin();
assert_equals(collector.events.length, 0);
}, `Setting initial zero selection range on ${name}`);
promise_test(async () => {
await data.initialize();
target.setSelectionRange(3, 3);
target.setSelectionRange(3, 3);
await data.assert_empty_spin();
assert_equals(collector.events.length, 1);
}, `Setting the same selection range twice on ${name}`);
promise_test(async () => {
await data.initialize();
target.select();
target.select();
await data.assert_empty_spin();
assert_equals(collector.events.length, 1);
}, `Calling select() twice on ${name}`);
promise_test(async () => {
await data.initialize();
target.select();
target.setRangeText("foo", 2, 6);
await data.assert_empty_spin();
assert_equals(collector.events.length, 1);
}, `Calling setRangeText() after select() on ${name}`);
promise_test(async () => {
await data.initialize();
target.select();
target.setRangeText("", 10, 12);
target.setRangeText("", 10, 12);
target.setRangeText("", 10, 12);
await data.assert_empty_spin();
assert_equals(collector.events.length, 1);
}, `Calling setRangeText() repeatedly on ${name}`);
promise_test(async () => {
await data.initialize();
target.value = "";
target.setRangeText("foo");
await data.assert_empty_spin();
assert_equals(collector.events.length, 0);
}, `Calling setRangeText() on empty ${name}`);
}
</script>
|