From 68e09ca92d0a1e20849c63edb71a9d18772a5192 Mon Sep 17 00:00:00 2001 From: Antoine Guenet Date: Tue, 10 Jan 2023 12:10:49 +0000 Subject: [PATCH] [FIX] web_editor: prevent changing blocks on correct arrow nav near zws If a block ends with a zero-width space, the mechanism that skips these characters when using the arrow keys should not skip all the way to the next block. This is because this mechanism happens before the browser applies its own behavior for the arrow key so we want to let it skip to the next block instead (or we'll end up navigating too far). X-original-commit: acae237410e43e1cf97fc023568f49cc2e974db4 Part-of: odoo/odoo#116558 --- .../js/editor/odoo-editor/src/OdooEditor.js | 13 +++++++++---- .../odoo-editor/test/spec/editor.test.js | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) 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 d452fff29be..aa5926321dc 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 @@ -3607,13 +3607,18 @@ export class OdooEditor extends EventTarget { // Move selection if next character is zero-width space if (nextCharacter === '\u200B') { focusOffset += 1; - while (focusNode && (!focusNode.textContent[focusOffset] || !closestElement(focusNode).isContentEditable)) { - focusNode = nextLeaf(focusNode); + let newFocusNode = focusNode; + while (newFocusNode && (!newFocusNode.textContent[focusOffset] || !closestElement(newFocusNode).isContentEditable)) { + newFocusNode = nextLeaf(newFocusNode); focusOffset = 0; } - const startContainer = ev.shiftKey ? selection.anchorNode : focusNode; + if (!focusOffset && closestBlock(focusNode) !== closestBlock(newFocusNode)) { + newFocusNode = focusNode; // Do not move selection to next block. + focusOffset = nodeSize(focusNode); + } + const startContainer = ev.shiftKey ? selection.anchorNode : newFocusNode; const startOffset = ev.shiftKey ? selection.anchorOffset : focusOffset; - setSelection(startContainer, startOffset, focusNode, focusOffset); + setSelection(startContainer, startOffset, newFocusNode, focusOffset); } } } diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/editor.test.js b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/editor.test.js index 8041867dfa7..42259836a9b 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/editor.test.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/editor.test.js @@ -5478,6 +5478,24 @@ X[] // Final state: '

ab\u200Bc[]d

' }); }); + it('should move past a zws (collapsed at the end of a block)', async () => { + await testEditor(BasicEditor, { + contentBefore: '

ab[]\u200B

cd

', + stepFunction: async editor => { + triggerEvent(editor.editable, 'keydown', { key: 'ArrowRight'}); + }, + contentAfter: '

ab\u200B[]

cd

', + // Final state: '

ab\u200B

[]cd

' + }); + await testEditor(BasicEditor, { + contentBefore: '

ab[]\u200B

cd

', + stepFunction: async editor => { + triggerEvent(editor.editable, 'keydown', { key: 'ArrowRight'}); + }, + contentAfter: '

ab\u200B[]

cd

', + // Final state: '

ab\u200B

[]cd

' + }); + }); it('should select a zws', async () => { await testEditor(BasicEditor, { contentBefore: '

[ab]\u200Bcd

',