Skip to content

Commit

Permalink
[FIX] ContentEditableHelper: do not throw on missing node
Browse files Browse the repository at this point in the history
Currently,the method `findSizeBeforeElement` will throw if called when
the selection is not targetting a node inside the ContentEditableHelper
instance.

Unfortunately, this can happen since commit d466315. If a menu is
opened and we decide to click on the composer, the click event if first
caught by the menu global listener which will close the menu and invoke
a callback that focuses the grid; this focus will alter the selection of
`document`. The event is then handled by the composer component but at
that point, the selection is not targetting it anymore.

With this revision, we change the behavior of `findSizeBeforeElement` to
return a default value of 0 in those situations as we simply don't know
how to determine the selection outside of the scope of the
ContentEditableHelper.

There is a functional drawback to this solution:
When clicking on a specific character of the composer when a menu is
opened, the selection will be set to `{ start: 0, end: 0 }`.

closes #2462

Signed-off-by: Lucas Lefèvre (lul) <lul@odoo.com>
  • Loading branch information
rrahir committed May 19, 2023
1 parent fe4fd4d commit 371666f
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/components/composer/content_editable_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,14 @@ export class ContentEditableHelper {
current = it.next();
}
if (current.value !== nodeToFind) {
throw new Error("Cannot find the node in the children of the element");
/** This situation can happen if the code is called while the selection is not currently on the ContentEditableHelper.
* In this case, we return 0 because we don't know the size of the text before the selection.
*
* A known occurence is triggered since the introduction of commit d4663158 (PR #2038).
*
* FIXME: find a way to test eventhough the selection API is not available in jsDOM.
*/
return 0;
} else {
if (!current.value.hasChildNodes()) {
usedCharacters += nodeOffset;
Expand Down

0 comments on commit 371666f

Please sign in to comment.