From fcc035bcaaf0ed52083ff2a2b86370d8cb26db38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Geelen=20=28sge=29?= Date: Thu, 14 Apr 2022 14:15:05 +0000 Subject: [PATCH] [FIX] web_editor : ensure break line consistency on delete When delete backward/forward OR deleteRange, we need to ensure the break lines stay consistent with the user action. Some cases where not handled correctly. Mainly when a

tags was removed and not directly adjacent to another block element. In those case we kept too much or too little break lines. @see tests added in `test/spec/editor.test.js` task-2604627 X-original-commit: 9f14422ffceadd48b76ff460e4080e0cf5bc6fa2 Part-of: odoo/odoo#89210 --- .../static/lib/odoo-editor/src/OdooEditor.js | 4 +++- .../odoo-editor/src/commands/deleteBackward.js | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) 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 d87a332ba4f..5134073f4d5 100644 --- a/addons/web_editor/static/lib/odoo-editor/src/OdooEditor.js +++ b/addons/web_editor/static/lib/odoo-editor/src/OdooEditor.js @@ -1312,7 +1312,9 @@ export class OdooEditor extends EventTarget { let start = range.startContainer; let end = range.endContainer; // Let the DOM split and delete the range. - const doJoin = closestBlock(start) !== closestBlock(range.commonAncestorContainer); + const doJoin = + closestBlock(start) !== closestBlock(range.commonAncestorContainer) || + closestBlock(end) !== closestBlock(range.commonAncestorContainer) ; let next = nextLeaf(end, this.editable); const splitEndTd = closestElement(end, 'td') && end.nextSibling; const contents = range.extractContents(); diff --git a/addons/web_editor/static/lib/odoo-editor/src/commands/deleteBackward.js b/addons/web_editor/static/lib/odoo-editor/src/commands/deleteBackward.js index b034de53c8b..0b8a01e2185 100644 --- a/addons/web_editor/static/lib/odoo-editor/src/commands/deleteBackward.js +++ b/addons/web_editor/static/lib/odoo-editor/src/commands/deleteBackward.js @@ -163,6 +163,7 @@ HTMLElement.prototype.oDeleteBackward = function (offset, alreadyMoved = false, } let node = this.childNodes[offset]; + const nextSibling = this.nextSibling; let currentNodeIndex = offset; // `offsetLimit` will ensure we never move nodes that were not initialy in the element @@ -194,6 +195,20 @@ HTMLElement.prototype.oDeleteBackward = function (offset, alreadyMoved = false, const { cType } = getState(cursorNode, cursorOffset, DIRECTIONS.LEFT); if (cType & CTGROUPS.BLOCK && (!alreadyMoved || cType === CTYPES.BLOCK_OUTSIDE)) { cursorNode.oDeleteBackward(cursorOffset, alreadyMoved, cursorOffset + currentNodeIndex - offset); + } else if (!alreadyMoved) { + // When removing a block node adjacent to a inline node, + // we need to ensure the block node induced line break are kept with a
. + // ex :

ab

[]c

d
=> deleteBakward + // =>
ab[]c
d
+ // In this case we cannot simply merge the

content into the div parent + // or we would loose the line break located after the

. + const cursorNodeNode = cursorNode.childNodes[cursorOffset]; + const cursorNodeRightNode = cursorNodeNode ? cursorNodeNode.nextSibling : undefined; + if (cursorNodeRightNode && + cursorNodeRightNode.nodeType === Node.TEXT_NODE && + nextSibling === cursorNodeRightNode) { + moveDest[0].insertBefore(document.createElement('br'), cursorNodeRightNode); + } } } };