Skip to content

Truly independent web browser

LibWeb: Insert a newline for line breaks in break-spaces text

Andreas Kling716e0b6dd0ca
diff --git a/Libraries/LibWeb/Editing/Commands.cpp b/Libraries/LibWeb/Editing/Commands.cpp
index 4f6196a8e01..d523ab91798 100644
--- a/Libraries/LibWeb/Editing/Commands.cpp
+++ b/Libraries/LibWeb/Editing/Commands.cpp
@@ -1763,14 +1763,14 @@ bool command_insert_linebreak_action(DOM::Document& document, Utf16View)
MUST(selection.collapse(start_node->parent(), start_node->index() + 1));
// AD-HOC: In preformatted white-space contexts, use newlines instead of <br> for line breaks. This matches
- // Chrome behavior and WPT expectations. The white-space: pre, pre-wrap, and pre-line styles all preserve
- // newlines, so inserting a literal newline character is more appropriate than a <br> element.
+ // Chrome behavior and WPT expectations. The white-space: pre, pre-wrap, pre-line, and break-spaces styles
+ // all preserve newlines, so inserting a literal newline character is more appropriate than a <br> element.
// However, padding line breaks (needed for empty last lines) should still be <br> because they won't
// appear in .textContent, making the content easier to work with programmatically.
// INTEROP: Browsers do insert a <br> if the white-space style comes from the default style of an element such as
// pre.
auto resolved_white_space_collapse = resolved_keyword(*start_node, CSS::PropertyID::WhiteSpaceCollapse);
- if (resolved_white_space_collapse.has_value() && first_is_one_of(resolved_white_space_collapse.value(), CSS::Keyword::Preserve, CSS::Keyword::PreserveBreaks)
+ if (resolved_white_space_collapse.has_value() && first_is_one_of(resolved_white_space_collapse.value(), CSS::Keyword::Preserve, CSS::Keyword::PreserveBreaks, CSS::Keyword::BreakSpaces)
&& !white_space_is_from_default_style(*start_node)) {
if (auto* text_node = as_if<DOM::Text>(*start_node)) {
MUST(insert_data(*text_node, start_offset, "\n"_utf16));
diff --git a/Tests/LibWeb/Text/expected/Editing/insertLinebreak-break-spaces.txt b/Tests/LibWeb/Text/expected/Editing/insertLinebreak-break-spaces.txt
new file mode 100644
index 00000000000..f301da07768
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Editing/insertLinebreak-break-spaces.txt
@@ -0,0 +1,4 @@
+Before: "foobar"
+After: "foo\nbar"
+Before: "foobar"
+After: "foo\nbar"
diff --git a/Tests/LibWeb/Text/input/Editing/insertLinebreak-break-spaces.html b/Tests/LibWeb/Text/input/Editing/insertLinebreak-break-spaces.html
new file mode 100644
index 00000000000..7e082b8fd41
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Editing/insertLinebreak-break-spaces.html
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<div id="a" contenteditable style="white-space: break-spaces">foobar</div>
+<div id="b" contenteditable style="white-space: pre-wrap">foobar</div>
+<script>
+ test(() => {
+ for (const id of ["a", "b"]) {
+ const editingHost = document.getElementById(id);
+ println(`Before: ${JSON.stringify(editingHost.innerHTML)}`);
+ getSelection().collapse(editingHost.firstChild, 3);
+ document.execCommand("insertLinebreak");
+ println(`After: ${JSON.stringify(editingHost.innerHTML)}`);
+ }
+ });
+</script>