[FIX] web_editor: fix traceback when the main element is clicked

In the website editor, a traceback error occurs when clicking on the
main element since the commit [1]. This is caused by the function
'_handleSelectionInTable' calling 'selection.getRangeAt(0)' without
checking if there is at least one range in the selection.

Steps to reproduce the bug:

    - Open the website editor and drop a text snippet onto the page.
    - Click on the snippet to activate it.
    - Click on the empty area below the snippet, which corresponds to
    the <main> element.
    - A traceback error will appear (if it does not appear, repeat the
    first three steps multiple times).

[1]: https://github.com/odoo/odoo/commit/d977bd64fe88c059c915f9eb0f8029cde65b216a

task-3196722

closes odoo/odoo#113383

X-original-commit: 6e79728c7e812458709d3a60ba37dc4c578905b2
Signed-off-by: David Monjoie (dmo) <dmo@odoo.com>
This commit is contained in:
Benjamin Vray
2023-02-22 15:39:12 +01:00
parent 8fad8e6a26
commit 4c453c1a8a
@@ -2231,7 +2231,7 @@ export class OdooEditor extends EventTarget {
_handleSelectionInTable(ev=undefined) {
const selection = this.document.getSelection();
// Selection could be gone if the document comes from an iframe that has been removed.
const anchorNode = selection && selection.getRangeAt(0) && selection.anchorNode;
const anchorNode = selection && selection.rangeCount && selection.getRangeAt(0) && selection.anchorNode;
if (anchorNode && (closestElement(anchorNode, '[data-oe-protected="true"]') || !ancestors(anchorNode).includes(this.editable))) {
return false;
}