[FIX] web_editor: prevent selection in root after insert

The insert command ends with setting the selection to the "right" of the
last inserted node. However, it did this without checking if that
position was valid for the selection, so we could end up with the
selection directly within the root of the editor. This corrects it if
such is the case.

X-original-commit: 722cf3bf11fcc9839677837018c1389d65b3f4fa
Part-of: odoo/odoo#109208
This commit is contained in:
Antoine Guenet
2023-01-05 16:52:10 +01:00
parent 2efece9884
commit 759d6972c2
2 changed files with 7 additions and 3 deletions
@@ -291,7 +291,11 @@ export const editorCommands = {
currentNode = lastChildNode || currentNode;
selection.removeAllRanges();
const newRange = new Range();
const lastPosition = rightPos(currentNode);
let lastPosition = rightPos(currentNode);
if (lastPosition[0] === editor.editable) {
// Correct the position if it happens to be in the editable root.
lastPosition = getDeepestPosition(...lastPosition);
}
newRange.setStart(lastPosition[0], lastPosition[1]);
newRange.setEnd(lastPosition[0], lastPosition[1]);
selection.addRange(newRange);
@@ -1182,7 +1182,7 @@ describe('Copy and paste', () => {
stepFunction: async editor => {
await pasteHtml(editor, '<ul><li>abc</li><li>def</li><li>ghi</li></ul>');
},
contentAfter: '<p>12</p><ul><li>abc</li><li>def</li><li>ghi</li></ul>[]<p>34</p>',
contentAfter: '<p>12</p><ul><li>abc</li><li>def</li><li>ghi</li></ul><p>[]34</p>',
});
});
it('should paste the text of an li into another li', async () => {
@@ -1496,7 +1496,7 @@ describe('Copy and paste', () => {
stepFunction: async editor => {
await pasteOdooEditorHtml(editor, '<div class="custom-paste">b</div>');
},
contentAfter: '<p>a</p><div class="custom-paste">b</div>[]<p>b</p>',
contentAfter: '<p>a</p><div class="custom-paste">b</div><p>[]b</p>',
});
});
it('should not paste unsafe content', async () => {