From ac0cb67f36eb97849f0a112d37f860db29b9143a Mon Sep 17 00:00:00 2001 From: Nicolas Bayet Date: Thu, 16 Jun 2022 13:02:31 +0000 Subject: [PATCH] [FIX] web_editor: transform space to nbsp when deleterange Before this commit When removing the last word of an html container, the last space was not transformed into an `nbsp`. This caused google chrome to not render the space. Task-2809312 closes odoo/odoo#95124 X-original-commit: cd4f1c4bce66817640eea20f0b297095d48ea647 Signed-off-by: David Monjoie (dmo) --- .../static/lib/odoo-editor/src/OdooEditor.js | 10 +++++----- .../static/lib/odoo-editor/test/spec/editor.test.js | 9 +++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/addons/web_editor/static/lib/odoo-editor/src/OdooEditor.js b/addons/web_editor/static/lib/odoo-editor/src/OdooEditor.js index 87c5e560038..584251b90bb 100644 --- a/addons/web_editor/static/lib/odoo-editor/src/OdooEditor.js +++ b/addons/web_editor/static/lib/odoo-editor/src/OdooEditor.js @@ -54,6 +54,7 @@ import { rightPos, getAdjacentPreviousSiblings, getAdjacentNextSiblings, + rightLeafOnlyNotBlockPath, } from './utils/utils.js'; import { editorCommands } from './commands/commands.js'; import { Powerbox } from './powerbox/Powerbox.js'; @@ -1421,9 +1422,9 @@ export class OdooEditor extends EventTarget { fillEmpty(closestBlock(range.endContainer)); // Ensure trailing space remains visible. const joinWith = range.endContainer; - const joinSibling = joinWith && joinWith.nextSibling; const oldText = joinWith.textContent; - const hasSpaceAfter = joinSibling && joinSibling.textContent.startsWith(' '); + const rightLeaf = rightLeafOnlyNotBlockPath(range.endContainer).next().value; + const hasSpaceAfter = !rightLeaf || rightLeaf.textContent.startsWith(' '); const shouldPreserveSpace = (doJoin || hasSpaceAfter) && joinWith && oldText.endsWith(' '); if (shouldPreserveSpace) { joinWith.textContent = oldText.replace(/ $/, '\u00A0'); @@ -1451,10 +1452,9 @@ export class OdooEditor extends EventTarget { break; } } - next = joinWith && joinWith.nextSibling; + next = range.endContainer && rightLeafOnlyNotBlockPath(range.endContainer).next().value; if ( - shouldPreserveSpace && - !(next && next.nodeType === Node.TEXT_NODE && next.textContent.startsWith(' ')) + shouldPreserveSpace && next && !(next && next.nodeType === Node.TEXT_NODE && next.textContent.startsWith(' ')) ) { // Restore the text we modified in order to preserve trailing space. joinWith.textContent = oldText; diff --git a/addons/web_editor/static/lib/odoo-editor/test/spec/editor.test.js b/addons/web_editor/static/lib/odoo-editor/test/spec/editor.test.js index 29f57227e59..26ce1c8ef4f 100644 --- a/addons/web_editor/static/lib/odoo-editor/test/spec/editor.test.js +++ b/addons/web_editor/static/lib/odoo-editor/test/spec/editor.test.js @@ -2497,6 +2497,15 @@ X[] `), }); }); + it('should transform the last space of a container to an   after removing the last word through deleteRange', async () => { + await testEditor(BasicEditor, { + contentBefore: `

a [b]

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

a []

`, + }); + }); }); describe('insertParagraphBreak', () => {