Skip to content

Truly independent web browser

LibWeb: Keep spaces that follow a preserved space when editing

Andreas Kling665afb495e2d
diff --git a/Libraries/LibWeb/Editing/Internal/Algorithms.cpp b/Libraries/LibWeb/Editing/Internal/Algorithms.cpp
index ebcb69e4bf9..4fb223d7a89 100644
--- a/Libraries/LibWeb/Editing/Internal/Algorithms.cpp
+++ b/Libraries/LibWeb/Editing/Internal/Algorithms.cpp
@@ -770,7 +770,7 @@ Vector<size_t> remove_collapsed_spaces_in_text_node(DOM::Text& text, size_t offs
if (auto* previous_text = as_if<DOM::Text>(*node)) {
if (previous_text->length() == 0)
continue;
- return previous_text->data().code_unit_at(previous_text->length() - 1) == ' ';
+ return !text_preserves_spaces(*previous_text) && previous_text->data().code_unit_at(previous_text->length() - 1) == ' ';
}
if (!is<DOM::Element>(*node) || !is_inline_node(*node) || editing_ignores_content(*node))
return false;
diff --git a/Tests/LibWeb/Text/expected/Editing/execCommand-delete-after-preserved-space.txt b/Tests/LibWeb/Text/expected/Editing/execCommand-delete-after-preserved-space.txt
new file mode 100644
index 00000000000..9c22434ca7c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Editing/execCommand-delete-after-preserved-space.txt
@@ -0,0 +1,6 @@
+Before: "<p><span style=\"white-space: break-spaces\">a </span> b</p>"
+After: "<p><span style=\"white-space: break-spaces\">a </span>b</p>"
+Before: "<p><span style=\"white-space: pre-wrap\">a </span> b</p>"
+After: "<p><span style=\"white-space: pre-wrap\">a </span>b</p>"
+Before: "<p><span>a </span> b</p>"
+After: "<p><span>a</span>b</p>"
diff --git a/Tests/LibWeb/Text/input/Editing/execCommand-delete-after-preserved-space.html b/Tests/LibWeb/Text/input/Editing/execCommand-delete-after-preserved-space.html
new file mode 100644
index 00000000000..449d509927b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Editing/execCommand-delete-after-preserved-space.html
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<div id="a" contenteditable><p><span style="white-space: break-spaces">a </span> b</p></div>
+<div id="b" contenteditable><p><span style="white-space: pre-wrap">a </span> b</p></div>
+<div id="c" contenteditable><p><span>a </span> b</p></div>
+<script>
+ test(() => {
+ for (const id of ["a", "b", "c"]) {
+ const editingHost = document.getElementById(id);
+ println(`Before: ${JSON.stringify(editingHost.innerHTML)}`);
+
+ // Put the cursor after the leading space of " b", which follows a space at the end of the span, and press
+ // backspace. The leading space is rendered only if the span preserves its trailing space.
+ const text = editingHost.querySelector("span").nextSibling;
+ getSelection().collapse(text, 1);
+ document.execCommand("delete");
+
+ println(`After: ${JSON.stringify(editingHost.innerHTML)}`);
+ }
+ });
+</script>