[FIX] web_editor: fix infinite loop on empty oe-structure click

Steps to reproduce the bug:

- Install e-commerce.
- Open a product page in edit mode.
- Click on the "product.description_ecommerce" field.
- Bug: an infinite loop starts. The "selectionchange" event is triggered
in an endless loop, causing various issues such as the page freezing
after a while.

This issue was introduced by this commit [1].

Since this other commit [2], when a selection itself is the main
container of the editable area, we replace this selection to only
include the content of it. With commit [1], this has been modified to
take into account "oe_structure" and "[contenteditable]" elements. As
these elements can be empty, clicking on them would reselect them,
creating an endless loop since we'd never leave the condition that
replaces the selection.

[1]: https://github.com/odoo/odoo/commit/e93fa23c29421ffe8917d9650330585d6dad210f
[2]: https://github.com/odoo/odoo/commit/10c5a16cd44b8d8e54082df90166f2309dfa788d

task-3830033

closes odoo/odoo#160535

Signed-off-by: David Monjoie (dmo) <dmo@odoo.com>
This commit is contained in:
Benjamin Vray
2024-04-10 11:45:55 +00:00
parent 7b4624b078
commit fb7246f3dd
@@ -4294,7 +4294,11 @@ export class OdooEditor extends EventTarget {
selection.anchorNode === container &&
selection.focusNode === container &&
selection.anchorOffset === 0 &&
selection.focusOffset === [...container.childNodes].length
selection.focusOffset === [...container.childNodes].length &&
// Checks that the container is not an empty editable structure to
// avoid calling "getDeepRange" if it is, otherwise it will be
// selected again, creating an infine loop.
container.childNodes.length
) {
getDeepRange(container, {select: true});
// The selection is changed in `getDeepRange` and will therefore