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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
<!DOCTYPE html>
<title>Test Windows conventional caret movement behavior</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css" />
<style>
[contenteditable] {
font-size: 14px;
font-family: monospace;
overflow-wrap: normal;
outline: none;
width: 35ch;
}
textarea {
font-size: 14px;
font-family: monospace;
}
.break-word {
overflow-wrap: break-word;
}
</style>
<div id="testDiv" contenteditable></div>
<textarea id="testTextarea" cols=35></textarea>
<script>
// Call testEach(cases[i]) on console when debugging
/**
* @type {TestCase[]}
*
* @typedef {object} TestCase
* @property {string} title
* @property {string} text Text on which the test runs.
* The newlines and spaces will be auto-converted if needed.
* @property {boolean} [reverse] Test in both forward and backward directions
* @property {boolean} [backward] Move backward by cmd_wordPrevious
* @property {ElementSpecific} [div] Can be omitted when there is a bug
* @property {ElementSpecific} textarea
*
* @typedef {object} ElementSpecific
* @property {number} expectedOffset Expected offset after a caret move
* @property {number} [expectedNodeIndex] The index of child node with focus after a caret move.
* -1 means the parent node.
* @property {number} [initialOffset=0] initialOffset offset before a caret move
* @property {number} [initialNodeIndex]
*/
const kFirstWordLength = "Supercalifragilisticexpialidocious".length; // 34
const kParentNodeIndex = -1; // special value
const cases = [
{
title: "Eats inline whitespaces after word",
text: "Supercalifragilisticexpialidocious foo bar fussball",
reverse: true,
div: {
expectedOffset: kFirstWordLength + 1
},
textarea: {
expectedOffset: kFirstWordLength + 1
}
},
{
title: "Eats inline whitespaces across a wrapped line after word",
text: "Supercalifragilisticexpialidocious foo bar fussball",
reverse: true,
div: {
expectedOffset: kFirstWordLength + 7
},
textarea: {
expectedOffset: kFirstWordLength + 7
}
},
{
title: "Eats inline whitespaces across multiple wrapped lines after word",
text: `Supercalifragilisticexpialidocious foo bar fussball`,
reverse: true,
div: {
expectedOffset: kFirstWordLength + 65
},
textarea: {
expectedOffset: kFirstWordLength + 65
}
},
{
title: "Eats inline whitespaces after a whole word and stop before a newline",
text: "Supercalifragilisticexpialidocious \nfoo bar fussball",
reverse: true,
div: {
expectedOffset: 1,
expectedNodeIndex: kParentNodeIndex
},
textarea: {
expectedOffset: kFirstWordLength + 1
}
},
{
title: "Eats inline whitespaces after a partial word and stop before a newline",
text: "Supercalifragilisticexpialidocious \nfoo bar fussball",
div: {
initialOffset: 5,
expectedOffset: 1,
expectedNodeIndex: kParentNodeIndex
},
textarea: {
initialOffset: 5,
expectedOffset: kFirstWordLength + 1
}
},
{
title: "Eats inline whitespaces without a word and stop before a newline",
text: "Supercalifragilisticexpialidocious \n foo bar fussball",
// TODO(krosylight): Currently ClusterIterator internally skips trailing whitespace
// div: {
// initialOffset: kFirstWordLength,
// expectedOffset: 1,
// expectedNodeIndex: kParentNodeIndex
// },
textarea: {
initialOffset: kFirstWordLength,
expectedOffset: kFirstWordLength + 1
}
},
{
title: "Jumps to the next line and eats inline whitespaces",
text: "Supercalifragilisticexpialidocious \n foo bar fussball",
reverse: true,
div: {
initialOffset: kFirstWordLength + 1,
expectedOffset: 6,
expectedNodeIndex: 2
},
textarea: {
initialOffset: kFirstWordLength + 1,
expectedOffset: kFirstWordLength + 8
}
},
{
title: "Stops on whitespaces after word",
text: "Supercalifragilisticexpialidocious \n foo bar fussball",
backward: true,
div: {
initialOffset: 8,
initialNodeIndex: 2,
expectedOffset: 6,
expectedNodeIndex: 2
},
textarea: {
initialOffset: 44, // middle of "foo"
expectedOffset: 42
}
},
// TODO(krosylight): Currently no way to tell a word break is from line wrapping
// {
// title: "Ignore a word break introduced by line wrapping",
// text: "Supercalifragilisticexpialidociouspostfix",
// className: "narrow break-word",
// div: {
// expectedOffset: kFirstWordLength + 7
// },
// textarea: {
// expectedOffset: kFirstWordLength + 7
// }
// }
{
title: "Jump only one line at an empty line end",
text: "Supercalifragilisticexpialidocious \n\nfoo bar fussball",
reverse: true,
div: {
initialOffset: 2,
initialNodeIndex: kParentNodeIndex,
expectedOffset: 0,
expectedNodeIndex: 3
},
textarea: {
initialOffset: kFirstWordLength + 2,
expectedOffset: kFirstWordLength + 3
}
},
{
title: "Jump only one line at a non-empty line end",
text: "Supercalifragilisticexpialidocious \n\nfoo bar fussball",
reverse: true,
div: {
initialOffset: kFirstWordLength + 1,
expectedOffset: 2,
expectedNodeIndex: -1
},
textarea: {
initialOffset: kFirstWordLength + 1,
expectedOffset: kFirstWordLength + 2
}
},
];
SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(async () => {
await SpecialPowers.pushPrefEnv({
set: [["layout.word_select.eat_space_to_next_word", true]]
});
// Test on div first to prevent Bug 1623413
for (const testCase of cases) {
if (testCase.div) {
testOnDiv(testCase);
}
}
for (const testCase of cases) {
testOnTextarea(testCase);
}
SimpleTest.finish();
});
/** @param {TestCase} testCase */
function testOnDiv(testCase) {
const {
title,
backward = false,
reverse = false,
} = testCase;
const {
initialOffset = 0,
expectedOffset,
} = testCase.div;
if (expectedOffset === undefined) {
throw new Error("`expectedOffset` must be defined.")
}
testDiv.innerHTML = testCase.text.replaceAll(/ {2}/g, " ").replaceAll(/\n/g, "<br>");
const initialNode = childNode(testDiv, testCase.div.initialNodeIndex);
const expectedNode = childNode(testDiv, testCase.div.expectedNodeIndex);
const selection = getSelection();
selection.collapse(initialNode, initialOffset);
moveByWord(backward);
is(selection.focusNode, expectedNode, `focusNode in "${title}"`);
is(selection.focusOffset, expectedOffset, `focusOffset in "${title}"`);
if (reverse) {
selection.collapse(expectedNode, expectedOffset);
moveByWord(!backward);
is(selection.focusNode, initialNode, `focusNode with reversed selection in "${title}"`);
is(selection.focusOffset, initialOffset, `focusOffset with reversed selection in "${title}"`);
}
}
function testOnTextarea(testCase) {
const {
title,
backward = false,
reverse = false,
} = testCase;
const {
initialOffset = 0,
expectedOffset,
} = testCase.textarea;
if (expectedOffset === undefined) {
throw new Error("`expectedOffset` must be defined.")
}
testTextarea.value = testCase.text;
testTextarea.selectionStart = testTextarea.selectionEnd = initialOffset;
testTextarea.focus();
moveByWord(backward);
is(testTextarea.selectionStart, expectedOffset, `selectionStart in "${title}"`);
if (reverse) {
testTextarea.selectionStart = testTextarea.selectionEnd = expectedOffset;
moveByWord(!backward);
is(testTextarea.selectionStart, initialOffset, `selectionStart with reversed selection in "${title}"`);
}
}
function childNode(parent, index = 0) {
if (index === kParentNodeIndex) {
return parent;
}
return parent.childNodes[index];
}
/** @param {boolean} backward */
function moveByWord(backward) {
const dir = backward ? "Previous" : "Next";
SpecialPowers.doCommand(window, `cmd_word${dir}`);
}
</script>
|