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 213 214 215 216 217
|
<!doctype html>
<meta charset=utf-8>
<title></title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<div id=log></div>
<div id=target></div>
<script>
var target = document.getElementById("target");
var sometext = "something";
var shorttext = "abc";
var elemData = [
{
desc: "textarea not in body",
factory: () => document.createElement("textarea"),
},
{
desc: "input not in body",
factory: () => document.createElement("input"),
},
{
desc: "textarea in body",
factory: () => document.body.appendChild(document.createElement("textarea")),
},
{
desc: "input in body",
factory: () => document.body.appendChild(document.createElement("input")),
},
{
desc: "textarea in body with parsed default value",
factory: () => {
target.innerHTML = "<textarea>abcdefghij</textarea>"
return target.querySelector("textarea");
},
},
{
desc: "input in body with parsed default value",
factory: () => {
target.innerHTML = "<input value='abcdefghij'>"
return target.querySelector("input");
},
},
{
desc: "focused textarea",
factory: () => {
var t = document.body.appendChild(document.createElement("textarea"));
t.focus();
return t;
},
},
{
desc: "focused input",
factory: () => {
var i = document.body.appendChild(document.createElement("input"));
i.focus();
return i;
},
},
{
desc: "focused then blurred textarea",
factory: () => {
var t = document.body.appendChild(document.createElement("textarea"));
t.focus();
t.blur();
return t;
},
},
{
desc: "focused then blurred input",
factory: () => {
var i = document.body.appendChild(document.createElement("input"));
i.focus();
i.blur()
return i;
},
},
];
for (var data of elemData) {
test(function() {
var el = data.factory();
this.add_cleanup(() => el.remove());
assert_equals(el.selectionStart, 0,
`Cursor start should be at beginning of ${data.desc}`);
assert_equals(el.selectionEnd, 0,
`Cursor end should be at beginning of ${data.desc}`);
}, `cursor location for initial value of ${data.desc}`);
}
for (var data of elemData) {
test(function() {
var el = data.factory();
this.add_cleanup(() => el.remove());
el.defaultValue = sometext;
// The "focused or has been focused" case behaves differently.
if (data.desc.includes("focused")) {
assert_equals(el.selectionStart, el.value.length,
`Cursor start should be at end of ${data.desc}`);
assert_equals(el.selectionEnd, el.value.length,
`Cursor end should be at end of ${data.desc}`);
} else {
assert_equals(el.selectionStart, 0,
`Cursor start should be at beginning of ${data.desc}`);
assert_equals(el.selectionEnd, 0,
`Cursor end should be at beginning of ${data.desc}`);
}
}, `cursor location after defaultValue set of ${data.desc}`);
}
for (var data of elemData) {
test(function() {
var el = data.factory();
this.add_cleanup(() => el.remove());
el.selectionStart = el.selectionStart;
el.defaultValue = sometext;
// The focused case behaves differently.
if (data.desc.includes("focused")) {
assert_equals(el.selectionStart, el.value.length,
`Cursor start should be at end of ${data.desc}`);
assert_equals(el.selectionEnd, el.value.length,
`Cursor end should be at end of ${data.desc}`);
} else {
assert_equals(el.selectionStart, 0,
`Cursor start should be at beginning of ${data.desc}`);
assert_equals(el.selectionEnd, 0,
`Cursor end should be at beginning of ${data.desc}`);
}
}, `cursor location after defaultValue set after no-op selectionStart set of ${data.desc}`);
}
for (var data of elemData) {
test(function() {
var el = data.factory();
this.add_cleanup(() => el.remove());
el.value = sometext;
assert_equals(el.selectionStart, sometext.length,
`Cursor start should be at end of ${data.desc}`);
assert_equals(el.selectionEnd, sometext.length,
`Cursor end should be at end of ${data.desc}`);
}, `cursor location after value set of ${data.desc}`);
}
for (var data of elemData) {
test(function() {
var el = data.factory();
this.add_cleanup(() => el.remove());
assert_true(sometext.length > 8,
"sometext too short, test won't work right");
el.defaultValue = sometext;
el.selectionStart = 1;
el.selectionEnd = 8;
assert_equals(el.selectionStart, 1, "We just set selectionStart!");
assert_equals(el.selectionEnd, 8, "We just set selectionEnd!");
assert_true(shorttext.length > 1,
"shorttext too short, test won't work right");
assert_true(shorttext.length < 8,
"shorttext too long, test won't work right");
el.defaultValue = shorttext;
// The "focused or has been focused" case behaves differently.
if (data.desc.includes("focused")) {
assert_equals(el.selectionStart, el.value.length,
`Cursor start should be at end of ${data.desc}`);
assert_equals(el.selectionEnd, el.value.length,
`Cursor end should be at end of ${data.desc}`);
} else {
if (el.tagName.toLowerCase() === "textarea") {
assert_equals(el.selectionStart, 0,
"Selection should be collapsed to the beginning");
assert_equals(el.selectionEnd, 0,
"Selection should be collapsed to the beginning");
} else {
assert_equals(el.selectionStart, 1,
"Shouldn't have moved selection start");
assert_equals(el.selectionEnd, shorttext.length,
"Should have adjusted selection end");
}
}
}, `selection location after defaultValue set to shorter than selectionEnd of ${data.desc}`);
}
for (var data of elemData) {
test(function() {
var el = data.factory();
this.add_cleanup(() => el.remove());
assert_true(sometext.length > 8,
"sometext too short, test won't work right");
el.defaultValue = sometext;
el.selectionStart = 5;
el.selectionEnd = 8;
assert_equals(el.selectionStart, 5, "We just set selectionStart!");
assert_equals(el.selectionEnd, 8, "We just set selectionEnd!");
assert_true(shorttext.length < 5,
"shorttext too long, test won't work right");
el.defaultValue = shorttext;
// The "focused or has been focused" case behaves differently.
if (data.desc.includes("focused")) {
assert_equals(el.selectionStart, el.value.length,
`Cursor start should be at end of ${data.desc}`);
assert_equals(el.selectionEnd, el.value.length,
`Cursor end should be at end of ${data.desc}`);
} else {
if (el.tagName.toLowerCase() === "textarea") {
assert_equals(el.selectionStart,0,
"Selection should be collapsed to the beginning");
assert_equals(el.selectionEnd, 0,
"Selection should be collapsed to the beginning");
} else {
assert_equals(el.selectionStart, shorttext.length,
"Should have adjusted selection start");
assert_equals(el.selectionEnd, shorttext.length,
"Should have adjusted selection end");
}
}
}, `selection location after defaultValue set to shorter than selectionStart of ${data.desc}`);
}
</script>
|