Skip to content

Commit 417ab26

Browse files
ltenmozmoz-wptsync-bot
authored andcommitted
Allow text input into focused EditContext even if selection is elsewhere.
Differential Revision: https://phabricator.services.mozilla.com/D314038 bugzilla-url: https://bugzilla.mozilla.org/show_bug.cgi?id=2055955 gecko-commit: 051c443862b38b7db7ffc9ab7656fb4de08452dd gecko-commit-git: ca1b52a68258f316da3ec85658ce38827d3fa7dc gecko-reviewers: masayuki
1 parent fefb5fc commit 417ab26

1 file changed

Lines changed: 95 additions & 0 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset=utf-8>
5+
<title>Canvas EditContext should still accept text input when selection is outside of its associated element.</title>
6+
<script src="/resources/testharness.js"></script>
7+
<script src="/resources/testharnessreport.js"></script>
8+
<script src="/resources/testdriver.js"></script>
9+
<script src="/resources/testdriver-vendor.js"></script>
10+
<script src="/resources/testdriver-actions.js"></script>
11+
<script src="../include/editor-test-utils.js"></script>
12+
</head>
13+
<body>
14+
<canvas></canvas>
15+
<div></div>
16+
<span>Selection is here</span>
17+
<script>
18+
'use strict';
19+
const canvasHost = document.querySelector('canvas');
20+
const domHost = document.querySelector('div');
21+
canvasHost.editContext = new EditContext();
22+
domHost.editContext = new EditContext();
23+
const selectionSpan = document.querySelector('span');
24+
const originalText = selectionSpan.textContent;
25+
26+
for (const host of [domHost, canvasHost]) {
27+
const type = host === domHost ? 'DOM' : 'canvas';
28+
let firedTextUpdate = false;
29+
host.editContext.addEventListener('textupdate', () => {
30+
firedTextUpdate = true;
31+
});
32+
function reset() {
33+
firedTextUpdate = false;
34+
host.editContext.updateText(0, host.editContext.text.length, '');
35+
}
36+
function selectionAsStr() {
37+
return `${host.editContext.selectionStart}-${host.editContext.selectionEnd}`;
38+
}
39+
40+
promise_test(async t => {
41+
t.add_cleanup(reset);
42+
host.focus();
43+
getSelection().collapse(selectionSpan);
44+
await test_driver.send_keys(host, 'a');
45+
assert_true(firedTextUpdate, 'Should fire textupdate.');
46+
assert_equals(document.activeElement, host, 'Focused element should not change.');
47+
assert_equals(host.editContext.text, 'a', 'EditContext.text should be updated.');
48+
assert_equals(selectionAsStr(), '1-1', 'EditContext.selectionStart/End should be updated.');
49+
assert_equals(selectionSpan.textContent, originalText, 'DOM should be unchanged.');
50+
}, `Inserting into ${type} EditContext when selection is in non-editable content.`);
51+
52+
promise_test(async t => {
53+
t.add_cleanup(reset);
54+
host.focus();
55+
getSelection().collapse(selectionSpan);
56+
host.editContext.updateText(0, 0, 'hi');
57+
host.editContext.updateSelection(1, 1);
58+
await test_driver.send_keys(host, '\uE003'); // backspace
59+
assert_true(firedTextUpdate, 'Should fire textupdate.');
60+
assert_equals(document.activeElement, host, 'Focused element should not change.');
61+
assert_equals(host.editContext.text, 'i', 'EditContext.text should be updated.');
62+
assert_equals(selectionAsStr(), '0-0', 'EditContext.selectionStart/End should be updated.');
63+
assert_equals(selectionSpan.textContent, originalText, 'DOM should be unchanged.');
64+
}, `Deleting from ${type} EditContext when selection is in non-editable content.`);
65+
66+
const utils = new EditorTestUtils(host);
67+
async function testSelectionMovement(movement) {
68+
// Collapse somewhere in the middle of the text
69+
getSelection().collapse(selectionSpan.firstChild, 5);
70+
await utils[movement]();
71+
assert_equals(getSelection().rangeCount, 1, 'DOM selection should still have 1 range.');
72+
const range = getSelection().getRangeAt(0);
73+
assert_equals(range.startContainer, selectionSpan.firstChild,
74+
'DOM selection start container should be unchanged.');
75+
assert_equals(range.endContainer, selectionSpan.firstChild,
76+
'DOM selection end container should be unchanged.');
77+
assert_equals(`${range.startOffset}-${range.endOffset}`, '5-5',
78+
'DOM selection offsets should be unchanged.');
79+
}
80+
81+
if (host === canvasHost) {
82+
for (let movement of ['sendArrowLeftKey', 'sendArrowRightKey',
83+
'sendMoveWordLeftKey', 'sendMoveWordRightKey',
84+
'sendHomeKey', 'sendEndKey']) {
85+
promise_test(t => {
86+
t.add_cleanup(reset);
87+
return testSelectionMovement(movement);
88+
}, `Test that DOM selection is not changed when keyboard shortcut ${movement} is used with canvas EditContext focused.`);
89+
}
90+
}
91+
}
92+
93+
</script>
94+
</body>
95+
</html>

0 commit comments

Comments
 (0)