[FIX] web_editor: prevent error with arrow keys whithout selection

Protect left/right arrow key handling against an empty selection (no
previous/next character to find without a selection).

Example of steps to reproduce the issue (= lose the selection while keeping the
focus):
- create such a configuration in the editable:
```html
<p [p1]><br></p>
<table>...</table>
<p [p2]><br></p>
```

- Place the cursor inside [p2] and delete the node with `CTRL + BACKSPACE`
- The cursor should have moved in the last cell of the table
- Place the cursor inside [p1]
- Start the table deletion process with `CTRL + DELETE`
- The table should be selected, as well as [p1]
- Type `DELETE` (this time without CTRL) a second time.
- The table and the paragraph are replaced by a single `<br>` node and the
  selection is lost.
- Type `LEFT/RIGHT ARROW KEY`
=> Traceback
- A paragraph is inserted again when clicking in the editable afterwards
- The isolated `<br>` node stays until it is deleted by pressing on `DELETE` in
  the last (reinserted) paragraph.

Typically, any other case where the selection is manipulated programatically and
not put back in the editable (be it through a bug, or because a target
selection has become obsolete and can not be found) would produce such a
taceback.

task-3425395

closes odoo/odoo#128631

X-original-commit: 2ee6e08f42f913e3102ca7340bb40b0203432b67
Signed-off-by: David Monjoie (dmo) <dmo@odoo.com>
This commit is contained in:
abd-msyukyu-odoo
2023-07-17 09:21:10 +02:00
parent 70b0731154
commit ffa36f5b20
@@ -3729,6 +3729,9 @@ export class OdooEditor extends EventTarget {
const selection = this.document.getSelection();
// Find previous character.
let { focusNode, focusOffset } = selection;
if (!focusNode) {
return;
}
let previousCharacter = focusOffset > 0 && focusNode.textContent[focusOffset - 1];
if (!previousCharacter) {
focusNode = previousLeaf(focusNode);
@@ -3751,6 +3754,9 @@ export class OdooEditor extends EventTarget {
const selection = this.document.getSelection();
// Find next character.
let { focusNode, focusOffset } = selection;
if (!focusNode) {
return;
}
let nextCharacter = focusNode.textContent[focusOffset];
if (!nextCharacter) {
focusNode = nextLeaf(focusNode);