[FIX] web_editor: backspace only char with composition menu open

When typing in Japanese with Romaji input, a traceback occurred when
hitting backspace to remove the only character in the editable while the
Japanese character selection menu was open. This is because that case
triggers an `insertCompositionText` input event with an empty string,
and tries to delete the editable, which is unremovable. Since we called
`_applyRawCommand` without wrapping it in `_protect`, the unremovable
error caused a traceback instead of a rollback. When fixing that, an
other bug appeared, which was the insertion of a `#` character. This was
due to the fact that `insertText` was called with empty content, and
`insertText` defaulted to the character `#` for no particular reason
since the early proof of concept of the editor.

task-3134566

closes odoo/odoo#159005

X-original-commit: cb75212d4fe302b54097fbc90da6d6ef8b38205b
Signed-off-by: David Monjoie (dmo) <dmo@odoo.com>
Signed-off-by: Antoine Guenet (age) <age@odoo.com>
This commit is contained in:
Antoine Guenet
2024-03-25 08:33:17 +00:00
parent 429a37b395
commit 6615bc76b4
2 changed files with 4 additions and 1 deletions
@@ -3705,7 +3705,7 @@ export class OdooEditor extends EventTarget {
// Since the unit test Event is not trusted by the browser, we don't
// need to undo the char during the unit tests.
// @see https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted
this._applyRawCommand('oDeleteBackward');
this._protect(() => this._applyRawCommand('oDeleteBackward'));
}
if (latestSelectionInsideEmptyTag) {
// Restore the selection inside the empty Element.
@@ -2053,6 +2053,9 @@ export function splitAroundUntil(elements, limitAncestor) {
}
export function insertText(sel, content) {
if (!content) {
return;
}
if (sel.anchorNode.nodeType === Node.TEXT_NODE) {
const pos = [sel.anchorNode.parentElement, splitTextNode(sel.anchorNode, sel.anchorOffset)];
setSelection(...pos, ...pos, false);