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 dbefdc357c7..ab65506a1bb 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 @@ -752,7 +752,7 @@ export function getDeepestPosition(node, offset) { while (!isVisible(node) && (node.previousSibling || (!reversed && node.nextSibling))) { reversed = reversed || !node.nextSibling; node = reversed ? node.previousSibling : node.nextSibling; - offset = 0; + offset = reversed ? nodeSize(node) : 0; didMove = true; } return didMove && isVisible(node) ? getDeepestPosition(node, offset) : [node, offset]; diff --git a/addons/web_editor/static/lib/odoo-editor/test/spec/utils.test.js b/addons/web_editor/static/lib/odoo-editor/test/spec/utils.test.js index 370b90d5d24..0c4b7c594eb 100644 --- a/addons/web_editor/static/lib/odoo-editor/test/spec/utils.test.js +++ b/addons/web_editor/static/lib/odoo-editor/test/spec/utils.test.js @@ -1208,6 +1208,52 @@ describe('Utils', () => { .expect([anchorNode, anchorOffset, focusNode, focusOffset]) .to.eql([p1.firstChild, 0, p1.firstChild, 11]); }); + it('should limit the selection to the title text (nested)', () => { + const [p] = insertTestHtml( + `
+ + title + +
`, + ); + const span = p.childNodes[1]; + const whiteBeforeFont = span.childNodes[0]; + const title = span.childNodes[1].firstChild; + const whiteAfterFont = span.childNodes[2]; + const range = document.createRange(); + range.setStart(whiteBeforeFont, 0); + range.setEnd(whiteAfterFont, 10); + const result = getDeepRange(p.parentElement, { range, select: true }); + const { startContainer, startOffset, endContainer, endOffset } = result; + window.chai + .expect([startContainer, startOffset, endContainer, endOffset]) + .to.eql([title, 0, title, 5]); + const { anchorNode, anchorOffset, focusNode, focusOffset } = + document.getSelection(); + window.chai + .expect([anchorNode, anchorOffset, focusNode, focusOffset]) + .to.eql([title, 0, title, 5]); + }); + it('should not limit the selection to the title text within p siblings', () => { + const [p0, p1, p2] = insertTestHtml( + `+ title +