From 4477af4041ec7b23e0fb395aa774bf4ba4b54c71 Mon Sep 17 00:00:00 2001 From: "Mahdi Cheikh Rouhou (macr)" Date: Tue, 30 Jan 2024 13:08:55 +0100 Subject: [PATCH] [FIX] web_editor: delete forward invisible white spaces at start MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue: ====== Invisible whitespaces at the start gets deleted by one by one with delete forward. Steps to reproduce the issue: ============================= - Go to knowledge - Add any text - Make sure you have an empty line before it - Change the html of the added text and add some spaces at the start (this is the easiest way to reproduce it) - Go to the line before and keep deleting forward - The invisible spaces will be deleted one by one. Solution: ========= When the selection is at the start of the node and it contains whitespaces, `parentState` whill have undefined node when deleting forward since we are at the first leaf. We need to keep deleting forward with the text node instead of parentElement because calling delete forward with the parent will do a 1 delete backward call from the specified offset and it will not propagate forward anymore. task-3629743 closes odoo/odoo#155435 X-original-commit: 6cfe30b1f194f7c53e8f91dde3251d7ba327ec56 Signed-off-by: Geelen Sébastien (sge) Signed-off-by: Mahdi Cheikh Rouhou (macr) --- .../odoo-editor/src/commands/deleteForward.js | 12 ++++++--- .../odoo-editor/test/spec/editor.test.js | 25 +++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/src/commands/deleteForward.js b/addons/web_editor/static/src/js/editor/odoo-editor/src/commands/deleteForward.js index 2dfe628cfdf..eef6eb267e7 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/src/commands/deleteForward.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/src/commands/deleteForward.js @@ -54,15 +54,21 @@ export function deleteText(charSize, offset, direction, alreadyMoved) { restore(); // If the removed element was not visible content, propagate the deletion. + const parentState = getState(parentElement, firstSplitOffset, direction); if ( isZWS || (isSpace && - getState(parentElement, firstSplitOffset, direction).cType !== CTYPES.CONTENT) + (parentState.cType !== CTYPES.CONTENT || parentState.node === undefined)) ) { - if(direction === DIRECTIONS.LEFT) { + if (direction === DIRECTIONS.LEFT) { parentElement.oDeleteBackward(firstSplitOffset, alreadyMoved); } else { - parentElement.oDeleteForward(firstSplitOffset, alreadyMoved); + if (isSpace && parentState.node == undefined) { + // multiple invisible space at the start of the node + this.oDeleteForward(offset, alreadyMoved); + } else { + parentElement.oDeleteForward(firstSplitOffset, alreadyMoved); + } } if (isZWS) { fillEmpty(parentElement); 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 e30b54016fc..3f26718ddb7 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 @@ -433,6 +433,31 @@ X[] contentAfter: `

[] def

`, }); }); + it('should remove invisible empty space at the start', async () => { + await testEditor(BasicEditor, { + contentBefore: `

[] def

`, + stepFunction: async editor => { + await deleteForward(editor); + }, + contentAfter: `

[]ef

`, + }); + }); + it('should remove invisible empty space at the start (2)', async () => { + await testEditor(BasicEditor, { + // The first 3 spaces are invisible : considered + // formating by the browser. + // The   is visible (space 1). + // The last 3 spaces are consider as 1 visble space and + // two formating spaces (space 2). + contentBefore: `

[]   def

`, + stepFunction: async editor => { + await deleteForward(editor); + }, + // Space 1 is deleted and space 2 should be transformed + // to a   to stay visible. + contentAfter: `

[] def

`, + }); + }); }); describe('white spaces', () => { describe('no intefering spaces', () => {