Skip to content

Truly independent web browser

LibWeb: Insert a br for insertLineBreak in pre elements

Andreas Kling5bc78c4569e1
diff --git a/Libraries/LibWeb/Editing/Commands.cpp b/Libraries/LibWeb/Editing/Commands.cpp
index 2a49ced74c2..07a203a7fab 100644
--- a/Libraries/LibWeb/Editing/Commands.cpp
+++ b/Libraries/LibWeb/Editing/Commands.cpp
@@ -1442,8 +1442,11 @@ bool command_insert_linebreak_action(DOM::Document& document, Utf16View)
// 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)
+ && !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));
MUST(selection.collapse(start_node, start_offset + 1));
diff --git a/Libraries/LibWeb/Editing/Internal/Algorithms.cpp b/Libraries/LibWeb/Editing/Internal/Algorithms.cpp
index 6c23cbed850..c9ce0ffd961 100644
--- a/Libraries/LibWeb/Editing/Internal/Algorithms.cpp
+++ b/Libraries/LibWeb/Editing/Internal/Algorithms.cpp
@@ -990,7 +990,7 @@ static bool author_style_sheet_sets_white_space(DOM::Element& element)
// INTEROP: Browsers do not keep white-space styles that come from the default style of elements such as pre, and do not
// add white-space styles to content that is joined into such an element. They do keep white-space styles that
// the style attribute or author style sheets set on such elements.
-static bool white_space_is_from_default_style(GC::Ref<DOM::Node> node)
+bool white_space_is_from_default_style(GC::Ref<DOM::Node> node)
{
auto element = element_establishing_white_space(node);
return element
diff --git a/Libraries/LibWeb/Editing/Internal/Algorithms.h b/Libraries/LibWeb/Editing/Internal/Algorithms.h
index 67750bb9c24..75330cb0623 100644
--- a/Libraries/LibWeb/Editing/Internal/Algorithms.h
+++ b/Libraries/LibWeb/Editing/Internal/Algorithms.h
@@ -64,6 +64,7 @@ void canonicalize_collapsed_selection_for_editing(Selection&);
void canonicalize_whitespace(DOM::BoundaryPoint, bool fix_collapsed_space = true);
void canonicalize_whitespace_after_selection_replacement(DOM::BoundaryPoint);
void canonicalize_whitespace_after_node_insertion(DOM::BoundaryPoint);
+bool white_space_is_from_default_style(GC::Ref<DOM::Node>);
GC::RootVector<GC::Ref<DOM::Node>> clear_the_value(Utf16FlyString const&, GC::Ref<DOM::Element>);
void delete_the_selection(Selection&, bool block_merging = true, bool strip_wrappers = true, Selection::Direction = Selection::Direction::Forwards);
Optional<Utf16String> effective_command_value(GC::Ptr<DOM::Node>, Utf16FlyString const& command);
diff --git a/Tests/LibWeb/Text/expected/wpt-import/editing/run/[email protected] b/Tests/LibWeb/Text/expected/wpt-import/editing/run/[email protected]
index 2cbdf90b6c3..ef44a95351f 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/editing/run/[email protected]
+++ b/Tests/LibWeb/Text/expected/wpt-import/editing/run/[email protected]
@@ -2,8 +2,8 @@ Harness status: OK
Found 1000 tests
-989 Pass
-11 Fail
+999 Pass
+1 Fail
Pass [["insertlinebreak",""]] "foo[bar]baz": execCommand("insertlinebreak", false, "") return value
Pass [["insertlinebreak",""]] "foo[bar]baz" checks for modifications to non-editable content
Pass [["insertlinebreak",""]] "foo[bar]baz" compare innerHTML
@@ -393,7 +393,7 @@ Pass [["insertlinebreak",""]] "<p>foo[]bar</p>" queryCommandState("insertlinebre
Pass [["insertlinebreak",""]] "<p>foo[]bar</p>" queryCommandValue("insertlinebreak") after
Pass [["insertlinebreak",""]] "<pre>[]foo</pre>": execCommand("insertlinebreak", false, "") return value
Pass [["insertlinebreak",""]] "<pre>[]foo</pre>" checks for modifications to non-editable content
-Fail [["insertlinebreak",""]] "<pre>[]foo</pre>" compare innerHTML
+Pass [["insertlinebreak",""]] "<pre>[]foo</pre>" compare innerHTML
Pass [["insertlinebreak",""]] "<pre>[]foo</pre>" queryCommandIndeterm("insertlinebreak") before
Pass [["insertlinebreak",""]] "<pre>[]foo</pre>" queryCommandState("insertlinebreak") before
Pass [["insertlinebreak",""]] "<pre>[]foo</pre>" queryCommandValue("insertlinebreak") before
@@ -402,7 +402,7 @@ Pass [["insertlinebreak",""]] "<pre>[]foo</pre>" queryCommandState("insertlinebr
Pass [["insertlinebreak",""]] "<pre>[]foo</pre>" queryCommandValue("insertlinebreak") after
Pass [["insertlinebreak",""]] "<pre>foo[]</pre>": execCommand("insertlinebreak", false, "") return value
Pass [["insertlinebreak",""]] "<pre>foo[]</pre>" checks for modifications to non-editable content
-Fail [["insertlinebreak",""]] "<pre>foo[]</pre>" compare innerHTML
+Pass [["insertlinebreak",""]] "<pre>foo[]</pre>" compare innerHTML
Pass [["insertlinebreak",""]] "<pre>foo[]</pre>" queryCommandIndeterm("insertlinebreak") before
Pass [["insertlinebreak",""]] "<pre>foo[]</pre>" queryCommandState("insertlinebreak") before
Pass [["insertlinebreak",""]] "<pre>foo[]</pre>" queryCommandValue("insertlinebreak") before
@@ -411,7 +411,7 @@ Pass [["insertlinebreak",""]] "<pre>foo[]</pre>" queryCommandState("insertlinebr
Pass [["insertlinebreak",""]] "<pre>foo[]</pre>" queryCommandValue("insertlinebreak") after
Pass [["insertlinebreak",""]] "<pre>foo[]<br></pre>": execCommand("insertlinebreak", false, "") return value
Pass [["insertlinebreak",""]] "<pre>foo[]<br></pre>" checks for modifications to non-editable content
-Fail [["insertlinebreak",""]] "<pre>foo[]<br></pre>" compare innerHTML
+Pass [["insertlinebreak",""]] "<pre>foo[]<br></pre>" compare innerHTML
Pass [["insertlinebreak",""]] "<pre>foo[]<br></pre>" queryCommandIndeterm("insertlinebreak") before
Pass [["insertlinebreak",""]] "<pre>foo[]<br></pre>" queryCommandState("insertlinebreak") before
Pass [["insertlinebreak",""]] "<pre>foo[]<br></pre>" queryCommandValue("insertlinebreak") before
@@ -420,7 +420,7 @@ Pass [["insertlinebreak",""]] "<pre>foo[]<br></pre>" queryCommandState("insertli
Pass [["insertlinebreak",""]] "<pre>foo[]<br></pre>" queryCommandValue("insertlinebreak") after
Pass [["insertlinebreak",""]] "<pre>foo[]bar</pre>": execCommand("insertlinebreak", false, "") return value
Pass [["insertlinebreak",""]] "<pre>foo[]bar</pre>" checks for modifications to non-editable content
-Fail [["insertlinebreak",""]] "<pre>foo[]bar</pre>" compare innerHTML
+Pass [["insertlinebreak",""]] "<pre>foo[]bar</pre>" compare innerHTML
Pass [["insertlinebreak",""]] "<pre>foo[]bar</pre>" queryCommandIndeterm("insertlinebreak") before
Pass [["insertlinebreak",""]] "<pre>foo[]bar</pre>" queryCommandState("insertlinebreak") before
Pass [["insertlinebreak",""]] "<pre>foo[]bar</pre>" queryCommandValue("insertlinebreak") before
@@ -429,7 +429,7 @@ Pass [["insertlinebreak",""]] "<pre>foo[]bar</pre>" queryCommandState("insertlin
Pass [["insertlinebreak",""]] "<pre>foo[]bar</pre>" queryCommandValue("insertlinebreak") after
Pass [["insertlinebreak",""]] "<pre>foo[]<br><br></pre>": execCommand("insertlinebreak", false, "") return value
Pass [["insertlinebreak",""]] "<pre>foo[]<br><br></pre>" checks for modifications to non-editable content
-Fail [["insertlinebreak",""]] "<pre>foo[]<br><br></pre>" compare innerHTML
+Pass [["insertlinebreak",""]] "<pre>foo[]<br><br></pre>" compare innerHTML
Pass [["insertlinebreak",""]] "<pre>foo[]<br><br></pre>" queryCommandIndeterm("insertlinebreak") before
Pass [["insertlinebreak",""]] "<pre>foo[]<br><br></pre>" queryCommandState("insertlinebreak") before
Pass [["insertlinebreak",""]] "<pre>foo[]<br><br></pre>" queryCommandValue("insertlinebreak") before
@@ -438,7 +438,7 @@ Pass [["insertlinebreak",""]] "<pre>foo[]<br><br></pre>" queryCommandState("inse
Pass [["insertlinebreak",""]] "<pre>foo[]<br><br></pre>" queryCommandValue("insertlinebreak") after
Pass [["insertlinebreak",""]] "<pre>foo<br>{}<br></pre>": execCommand("insertlinebreak", false, "") return value
Pass [["insertlinebreak",""]] "<pre>foo<br>{}<br></pre>" checks for modifications to non-editable content
-Fail [["insertlinebreak",""]] "<pre>foo<br>{}<br></pre>" compare innerHTML
+Pass [["insertlinebreak",""]] "<pre>foo<br>{}<br></pre>" compare innerHTML
Pass [["insertlinebreak",""]] "<pre>foo<br>{}<br></pre>" queryCommandIndeterm("insertlinebreak") before
Pass [["insertlinebreak",""]] "<pre>foo<br>{}<br></pre>" queryCommandState("insertlinebreak") before
Pass [["insertlinebreak",""]] "<pre>foo<br>{}<br></pre>" queryCommandValue("insertlinebreak") before
@@ -447,7 +447,7 @@ Pass [["insertlinebreak",""]] "<pre>foo<br>{}<br></pre>" queryCommandState("inse
Pass [["insertlinebreak",""]] "<pre>foo<br>{}<br></pre>" queryCommandValue("insertlinebreak") after
Pass [["insertlinebreak",""]] "<pre>foo&#10;[]</pre>": execCommand("insertlinebreak", false, "") return value
Pass [["insertlinebreak",""]] "<pre>foo&#10;[]</pre>" checks for modifications to non-editable content
-Fail [["insertlinebreak",""]] "<pre>foo&#10;[]</pre>" compare innerHTML
+Pass [["insertlinebreak",""]] "<pre>foo&#10;[]</pre>" compare innerHTML
Pass [["insertlinebreak",""]] "<pre>foo&#10;[]</pre>" queryCommandIndeterm("insertlinebreak") before
Pass [["insertlinebreak",""]] "<pre>foo&#10;[]</pre>" queryCommandState("insertlinebreak") before
Pass [["insertlinebreak",""]] "<pre>foo&#10;[]</pre>" queryCommandValue("insertlinebreak") before
@@ -456,7 +456,7 @@ Pass [["insertlinebreak",""]] "<pre>foo&#10;[]</pre>" queryCommandState("insertl
Pass [["insertlinebreak",""]] "<pre>foo&#10;[]</pre>" queryCommandValue("insertlinebreak") after
Pass [["insertlinebreak",""]] "<pre>foo[]&#10;</pre>": execCommand("insertlinebreak", false, "") return value
Pass [["insertlinebreak",""]] "<pre>foo[]&#10;</pre>" checks for modifications to non-editable content
-Fail [["insertlinebreak",""]] "<pre>foo[]&#10;</pre>" compare innerHTML
+Pass [["insertlinebreak",""]] "<pre>foo[]&#10;</pre>" compare innerHTML
Pass [["insertlinebreak",""]] "<pre>foo[]&#10;</pre>" queryCommandIndeterm("insertlinebreak") before
Pass [["insertlinebreak",""]] "<pre>foo[]&#10;</pre>" queryCommandState("insertlinebreak") before
Pass [["insertlinebreak",""]] "<pre>foo[]&#10;</pre>" queryCommandValue("insertlinebreak") before
@@ -465,7 +465,7 @@ Pass [["insertlinebreak",""]] "<pre>foo[]&#10;</pre>" queryCommandState("insertl
Pass [["insertlinebreak",""]] "<pre>foo[]&#10;</pre>" queryCommandValue("insertlinebreak") after
Pass [["insertlinebreak",""]] "<pre>foo&#10;[]&#10;</pre>": execCommand("insertlinebreak", false, "") return value
Pass [["insertlinebreak",""]] "<pre>foo&#10;[]&#10;</pre>" checks for modifications to non-editable content
-Fail [["insertlinebreak",""]] "<pre>foo&#10;[]&#10;</pre>" compare innerHTML
+Pass [["insertlinebreak",""]] "<pre>foo&#10;[]&#10;</pre>" compare innerHTML
Pass [["insertlinebreak",""]] "<pre>foo&#10;[]&#10;</pre>" queryCommandIndeterm("insertlinebreak") before
Pass [["insertlinebreak",""]] "<pre>foo&#10;[]&#10;</pre>" queryCommandState("insertlinebreak") before
Pass [["insertlinebreak",""]] "<pre>foo&#10;[]&#10;</pre>" queryCommandValue("insertlinebreak") before
@@ -492,7 +492,7 @@ Pass [["insertlinebreak",""]] "<script>foo[]bar</script>baz" queryCommandState("
Pass [["insertlinebreak",""]] "<script>foo[]bar</script>baz" queryCommandValue("insertlinebreak") after
Pass [["insertlinebreak",""]] "<listing>foo[]bar</listing>": execCommand("insertlinebreak", false, "") return value
Pass [["insertlinebreak",""]] "<listing>foo[]bar</listing>" checks for modifications to non-editable content
-Fail [["insertlinebreak",""]] "<listing>foo[]bar</listing>" compare innerHTML
+Pass [["insertlinebreak",""]] "<listing>foo[]bar</listing>" compare innerHTML
Pass [["insertlinebreak",""]] "<listing>foo[]bar</listing>" queryCommandIndeterm("insertlinebreak") before
Pass [["insertlinebreak",""]] "<listing>foo[]bar</listing>" queryCommandState("insertlinebreak") before
Pass [["insertlinebreak",""]] "<listing>foo[]bar</listing>" queryCommandValue("insertlinebreak") before