From 8f971c045c084d3d6a668240c25ebe03d59a32f9 Mon Sep 17 00:00:00 2001 From: Stanislas Gueniffey Date: Mon, 27 Mar 2023 16:23:01 +0000 Subject: [PATCH] [FIX] web_editor: fix newline bug in code sections This commit fixes a bug that was occurring in the code sections when using Shift+Enter (soft return) for new lines. Steps to reproduce: - in a code section (a `
` node)
- write two or more lines separated with Shift+Enter
- make the last line at least two characters long
- place the cursor at the end of the section and press backspace, or
before the last character of the section and press delete

Example (in a code section):
ABC
DEF
  ^ cursor before or after "F" (for delete or backspace respectively)

Unexpected behaviors:
- an extra line is inserted before the current line
- the cursor is teleported at the beginning of the current line

The bug originated from method `enforceWhitespace` which iterates over
the dom from the cursor's position. There, the conditions unintendedly
allowed the iteration to pursue until a `BR` node was found, in which
case a rule which should not have been enforced would add another `BR`
node, which also had the side effect of teleporting the cursor.

This commit adds a break statement such that if none of the "main"
conditions are met in the first iteration, it is stopped right away
instead. Meeting those conditions still allows the iteration to proceed
as before. Not meeting any of those conditions only happened in code
sections as far as we can tell, as they are explicitly excluded because
of their lower-level management of spaces. Should other cases occur,
they should have their own conditions added and not rely on a top-level
"else-by-default" to continue the iteration indefinitely.

task-3239097

closes odoo/odoo#119390

X-original-commit: e47c2eddb3b675324c181593a3243175a1095732
Signed-off-by: David Monjoie (dmo) 
---
 .../static/src/js/editor/odoo-editor/src/utils/utils.js         | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/utils.js b/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/utils.js
index 28e9283d900..6fabbba3fcb 100644
--- a/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/utils.js
+++ b/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/utils.js
@@ -2374,6 +2374,8 @@ export function enforceWhitespace(el, offset, direction, rule) {
             } else if (!isWhitespace(node)) {
                 break;
             }
+        } else {
+            break;
         }
     }