From 759d6972c2ca15aaea3f00af91eabfc9d304a76c Mon Sep 17 00:00:00 2001 From: Antoine Guenet Date: Fri, 16 Dec 2022 13:50:43 +0000 Subject: [PATCH] [FIX] web_editor: prevent selection in root after insert The insert command ends with setting the selection to the "right" of the last inserted node. However, it did this without checking if that position was valid for the selection, so we could end up with the selection directly within the root of the editor. This corrects it if such is the case. X-original-commit: 722cf3bf11fcc9839677837018c1389d65b3f4fa Part-of: odoo/odoo#109208 --- .../src/js/editor/odoo-editor/src/commands/commands.js | 6 +++++- .../src/js/editor/odoo-editor/test/spec/copyPaste.test.js | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/src/commands/commands.js b/addons/web_editor/static/src/js/editor/odoo-editor/src/commands/commands.js index 1439d579283..092e3f32676 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/src/commands/commands.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/src/commands/commands.js @@ -291,7 +291,11 @@ export const editorCommands = { currentNode = lastChildNode || currentNode; selection.removeAllRanges(); const newRange = new Range(); - const lastPosition = rightPos(currentNode); + let lastPosition = rightPos(currentNode); + if (lastPosition[0] === editor.editable) { + // Correct the position if it happens to be in the editable root. + lastPosition = getDeepestPosition(...lastPosition); + } newRange.setStart(lastPosition[0], lastPosition[1]); newRange.setEnd(lastPosition[0], lastPosition[1]); selection.addRange(newRange); diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/copyPaste.test.js b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/copyPaste.test.js index 569e4ac4512..f701d4eda83 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/copyPaste.test.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/copyPaste.test.js @@ -1182,7 +1182,7 @@ describe('Copy and paste', () => { stepFunction: async editor => { await pasteHtml(editor, ''); }, - contentAfter: '

12

[]

34

', + contentAfter: '

12

[]34

', }); }); it('should paste the text of an li into another li', async () => { @@ -1496,7 +1496,7 @@ describe('Copy and paste', () => { stepFunction: async editor => { await pasteOdooEditorHtml(editor, '
b
'); }, - contentAfter: '

a

b
[]

b

', + contentAfter: '

a

b

[]b

', }); }); it('should not paste unsafe content', async () => {