From 2f196d1eb18346b798a129fa435cddd9f7c6871b Mon Sep 17 00:00:00 2001 From: Antoine Guenet Date: Fri, 16 Dec 2022 13:57:19 +0000 Subject: [PATCH] [FIX] web_editor: properly paste pre in pre Commit [1] attempted to fix unexpected behavior when copy-pasting from a
 to a 
, where the pasted text was smaller the intended, and
preceded and followed by new lines. The chosen approach had uninteded
side effects though as it removed any empty paragraph after the 
.
The source of the issue it tried to fix was that the pasted 
 was
inserted as a new 
, splitting the existing element in two in the
process, instead of simply inserting the text content.

[1] 449cacf

task-3086603

closes odoo/odoo#109208

X-original-commit: dfcc3abe3396eaf764c3c3c8510b654649ed82f8
Signed-off-by: David Monjoie (dmo) 
---
 .../odoo-editor/src/commands/commands.js       | 16 +++++++++-------
 .../odoo-editor/test/spec/insertHTML.test.js   | 18 ++++++++++++++++++
 2 files changed, 27 insertions(+), 7 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 092e3f32676..34d39edd511 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
@@ -20,7 +20,6 @@ import {
     isBlock,
     isColorGradient,
     isContentTextNode,
-    isEmptyBlock,
     isSelectionFormat,
     isShrunkBlock,
     isVisible,
@@ -46,6 +45,7 @@ import {
     getRowIndex,
     parseHTML,
     formatSelection,
+    getDeepestPosition,
 } from '../utils/utils.js';
 
 const TEXT_CLASSES_REGEX = /\btext-[^\s]*\b/g;
@@ -175,10 +175,16 @@ export const editorCommands = {
             container.replaceChildren(...container.firstChild.childNodes);
         }
 
+        startNode = startNode || editor.document.getSelection().anchorNode;
+
         // In case the html inserted is all contained in a single root 

or

  • // tag, we take the all content of the

    or

  • and avoid inserting the - //

    or

  • . - if (container.childElementCount === 1 && (container.firstChild.nodeName === 'P' || container.firstChild.nodeName === 'LI')) { + //

    or

  • . The same is true for a
     inside a 
    .
    +        if (container.childElementCount === 1 && (
    +            container.firstChild.nodeName === 'P' ||
    +            container.firstChild.nodeName === 'LI' ||
    +            container.firstChild.nodeName === 'PRE' && closestElement(startNode, 'pre')
    +        )) {
                 const p = container.firstElementChild;
                 container.replaceChildren(...p.childNodes);
             } else if (container.childElementCount > 1) {
    @@ -194,7 +200,6 @@ export const editorCommands = {
                 }
             }
     
    -        startNode = startNode || editor.document.getSelection().anchorNode;
             if (startNode.nodeType === Node.ELEMENT_NODE) {
                 if (selection.anchorOffset === 0) {
                     const textNode = editor.document.createTextNode('');
    @@ -268,9 +273,6 @@ export const editorCommands = {
                         if (offset) {
                             const [left, right] = splitElement(currentNode.parentElement, offset);
                             currentNode = insertBefore ? right : left;
    -                        if (isEmptyBlock(right)) {
    -                            right.remove();
    -                        }
                         } else {
                             currentNode = currentNode.parentElement;
                         }
    diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/insertHTML.test.js b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/insertHTML.test.js
    index feb23dc7ec3..f0d0cb7a4a5 100644
    --- a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/insertHTML.test.js
    +++ b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/insertHTML.test.js
    @@ -72,6 +72,24 @@ describe('insert HTML', () => {
                     contentAfter: '

    ab

    c

    d[]e

    ', }); }); + it('should keep a paragraph after a div block', async () => { + await testEditor(BasicEditor, { + contentBefore: '

    []

    ', + stepFunction: async editor => { + await editor.execCommand('insert', parseHTML('

    content

    ')); + }, + contentAfter: '

    content

    []

    ', + }); + }); + it('should not split a pre to insert another pre but just insert the text', async () => { + await testEditor(BasicEditor, { + contentBefore: '
    abc[]
    ghi
    ', + stepFunction: async editor => { + await editor.execCommand('insert', parseHTML('
    def
    ')); + }, + contentAfter: '
    abcdef[]
    ghi
    ', + }); + }); }); describe('not collapsed selection', () => { it('should delete selection and insert html in its place', async () => {