[FIX] web_editor: insert empty paragraph on enter at end of heading

When pressing enter at the end of a heading, we want to insert an empty
paragraph instead of a new heading. This failed when the paragraph had
a zero-width space in it because we didn't recognize it as empty.

closes odoo/odoo#116558

X-original-commit: 228e7937f818f5606601e575b1d9a2bb87cda99d
Signed-off-by: David Monjoie (dmo) <dmo@odoo.com>
This commit is contained in:
Antoine Guenet
2023-03-25 02:46:58 +01:00
parent 68e09ca92d
commit 7af191db78
2 changed files with 10 additions and 2 deletions
@@ -91,8 +91,9 @@ HTMLElement.prototype.oEnter = function (offset, firstSplit = true) {
*/
HTMLHeadingElement.prototype.oEnter = function () {
const newEl = HTMLElement.prototype.oEnter.call(this, ...arguments);
if (!newEl.textContent) {
if ([...newEl.textContent].every(char => char === '\u200B')) { // empty or all invisible
const node = setTagName(newEl, 'P');
node.replaceChildren(document.createElement('br'));
setCursorStart(node);
}
};
@@ -3389,13 +3389,20 @@ X[]
});
});
describe('POC extra tests', () => {
it('should duplicate an empty h1', async () => {
it('should insert a paragraph after an empty h1', async () => {
await testEditor(BasicEditor, {
contentBefore: '<h1>[]<br></h1>',
stepFunction: insertParagraphBreak,
contentAfter: '<h1><br></h1><p>[]<br></p>',
});
});
it('should insert a paragraph after an empty h1 with styles and a zero-width space', async () => {
await testEditor(BasicEditor, {
contentBefore: '<h1><font style="color: red;" data-oe-zws-empty-inline="">[]\u200B</font><br></h1>',
stepFunction: insertParagraphBreak,
contentAfter: '<h1><br></h1><p>[]<br></p>',
});
});
});
});
describe('Selection not collapsed', () => {