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 () => {