[FIX] web_editor: transform space to nbsp when deleterange

Before this commit
When removing the last word of an html container, the last space was
not transformed into an `nbsp`. This caused google chrome to not
render the space.

Task-2809312

closes odoo/odoo#95124

X-original-commit: cd4f1c4bce66817640eea20f0b297095d48ea647
Signed-off-by: David Monjoie (dmo) <dmo@odoo.com>
This commit is contained in:
Nicolas Bayet
2022-07-02 11:41:21 +02:00
parent 6bef82db98
commit ac0cb67f36
2 changed files with 14 additions and 5 deletions
@@ -54,6 +54,7 @@ import {
rightPos,
getAdjacentPreviousSiblings,
getAdjacentNextSiblings,
rightLeafOnlyNotBlockPath,
} from './utils/utils.js';
import { editorCommands } from './commands/commands.js';
import { Powerbox } from './powerbox/Powerbox.js';
@@ -1421,9 +1422,9 @@ export class OdooEditor extends EventTarget {
fillEmpty(closestBlock(range.endContainer));
// Ensure trailing space remains visible.
const joinWith = range.endContainer;
const joinSibling = joinWith && joinWith.nextSibling;
const oldText = joinWith.textContent;
const hasSpaceAfter = joinSibling && joinSibling.textContent.startsWith(' ');
const rightLeaf = rightLeafOnlyNotBlockPath(range.endContainer).next().value;
const hasSpaceAfter = !rightLeaf || rightLeaf.textContent.startsWith(' ');
const shouldPreserveSpace = (doJoin || hasSpaceAfter) && joinWith && oldText.endsWith(' ');
if (shouldPreserveSpace) {
joinWith.textContent = oldText.replace(/ $/, '\u00A0');
@@ -1451,10 +1452,9 @@ export class OdooEditor extends EventTarget {
break;
}
}
next = joinWith && joinWith.nextSibling;
next = range.endContainer && rightLeafOnlyNotBlockPath(range.endContainer).next().value;
if (
shouldPreserveSpace &&
!(next && next.nodeType === Node.TEXT_NODE && next.textContent.startsWith(' '))
shouldPreserveSpace && next && !(next && next.nodeType === Node.TEXT_NODE && next.textContent.startsWith(' '))
) {
// Restore the text we modified in order to preserve trailing space.
joinWith.textContent = oldText;
@@ -2497,6 +2497,15 @@ X[]
</div></div>`),
});
});
it('should transform the last space of a container to an &nbsp; after removing the last word through deleteRange', async () => {
await testEditor(BasicEditor, {
contentBefore: `<p>a [b]</p>`,
stepFunction: async editor => {
await deleteBackward(editor);
},
contentAfter: `<p>a&nbsp;[]</p>`,
});
});
});
describe('insertParagraphBreak', () => {