diff --git a/addons/web_editor/static/lib/odoo-editor/src/commands/commands.js b/addons/web_editor/static/lib/odoo-editor/src/commands/commands.js index 0f4b8010c56..ed072c66108 100644 --- a/addons/web_editor/static/lib/odoo-editor/src/commands/commands.js +++ b/addons/web_editor/static/lib/odoo-editor/src/commands/commands.js @@ -33,6 +33,9 @@ import { splitTextNode, startPos, nodeSize, + allowsParagraphRelatedElements, + isUnbreakable, + makeContentsInline, } from '../utils/utils.js'; const TEXT_CLASSES_REGEX = /\btext-[^\s]*\b/g; @@ -71,13 +74,18 @@ function insert(editor, data, isText = true) { let nodeToInsert; const insertedNodes = [...fakeEl.childNodes]; while ((nodeToInsert = fakeEl.childNodes[0])) { - if (isBlock(nodeToInsert) && !isBlock(startNode)) { + if (isBlock(nodeToInsert) && !allowsParagraphRelatedElements(startNode)) { // Split blocks at the edges if inserting new blocks (preventing //
text
scenarios). while ( startNode.parentElement !== editor.editable && - !isBlock(startNode.parentElement) + !allowsParagraphRelatedElements(startNode.parentElement) ) { + if (isUnbreakable(startNode.parentElement)) { + makeContentsInline(fakeEl); + nodeToInsert = fakeEl.childNodes[0]; + break; + } let offset = childNodeIndex(startNode); if (!insertBefore) { offset += 1; diff --git a/addons/web_editor/static/lib/odoo-editor/src/utils/utils.js b/addons/web_editor/static/lib/odoo-editor/src/utils/utils.js index 55ee0a0cf8c..c6ecc28572a 100644 --- a/addons/web_editor/static/lib/odoo-editor/src/utils/utils.js +++ b/addons/web_editor/static/lib/odoo-editor/src/utils/utils.js @@ -800,7 +800,7 @@ export function preserveCursor(document) { * Source: * https://developer.mozilla.org/en-US/docs/Web/HTML/Block-level_elements * - * */ + **/ const blockTagNames = [ 'ADDRESS', 'ARTICLE', @@ -985,6 +985,53 @@ export function getInSelection(document, selector) { ); } +// This is a list of "paragraph-related elements", defined as elements that +// behave like paragraphs. +const paragraphRelatedElements = [ + 'P', + 'H1', + 'H2', + 'H3', + 'H4', + 'H5', + 'H6', +]; + +/** + * Return true if the given node allows "paragraph-related elements". + * + * @see paragraphRelatedElements + * @param {Node} node + * @returns {boolean} + */ +export function allowsParagraphRelatedElements(node) { + return isBlock(node) && !paragraphRelatedElements.includes(node.nodeName); +} + +/** + * Take a node and unwrap all of its block contents recursively. All blocks + * (except for firstChilds) are preceded by a