From ffa36f5b20d1ed5b6f0bfd8ed24fbcfa119b3b00 Mon Sep 17 00:00:00 2001 From: abd-msyukyu-odoo Date: Thu, 25 May 2023 12:14:57 +0000 Subject: [PATCH] [FIX] web_editor: prevent error with arrow keys whithout selection Protect left/right arrow key handling against an empty selection (no previous/next character to find without a selection). Example of steps to reproduce the issue (= lose the selection while keeping the focus): - create such a configuration in the editable: ```html


...


``` - Place the cursor inside [p2] and delete the node with `CTRL + BACKSPACE` - The cursor should have moved in the last cell of the table - Place the cursor inside [p1] - Start the table deletion process with `CTRL + DELETE` - The table should be selected, as well as [p1] - Type `DELETE` (this time without CTRL) a second time. - The table and the paragraph are replaced by a single `
` node and the selection is lost. - Type `LEFT/RIGHT ARROW KEY` => Traceback - A paragraph is inserted again when clicking in the editable afterwards - The isolated `
` node stays until it is deleted by pressing on `DELETE` in the last (reinserted) paragraph. Typically, any other case where the selection is manipulated programatically and not put back in the editable (be it through a bug, or because a target selection has become obsolete and can not be found) would produce such a taceback. task-3425395 closes odoo/odoo#128631 X-original-commit: 2ee6e08f42f913e3102ca7340bb40b0203432b67 Signed-off-by: David Monjoie (dmo) --- .../static/src/js/editor/odoo-editor/src/OdooEditor.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js b/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js index d62d45f3e89..7c23a15ad98 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js @@ -3729,6 +3729,9 @@ export class OdooEditor extends EventTarget { const selection = this.document.getSelection(); // Find previous character. let { focusNode, focusOffset } = selection; + if (!focusNode) { + return; + } let previousCharacter = focusOffset > 0 && focusNode.textContent[focusOffset - 1]; if (!previousCharacter) { focusNode = previousLeaf(focusNode); @@ -3751,6 +3754,9 @@ export class OdooEditor extends EventTarget { const selection = this.document.getSelection(); // Find next character. let { focusNode, focusOffset } = selection; + if (!focusNode) { + return; + } let nextCharacter = focusNode.textContent[focusOffset]; if (!nextCharacter) { focusNode = nextLeaf(focusNode);