Skip to content

Truly independent web browser

LibWeb: Keep line feeds that pre-line preserves after collapsible spaces

sideshowbarker720e9e7b6899
diff --git a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs b/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
index ca5ff532c31..66e42f30cd0 100644
--- a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
+++ b/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
@@ -1211,7 +1211,11 @@ impl<'text> TextChunker<'text> {
}
fn is_collapsible(&self, code_point: u32) -> bool {
- self.should_collapse_whitespace && code_point_is_ascii_space(code_point)
+ // NB: A line feed that white-space-collapse: preserve-breaks keeps is a forced line break, not white space that
+ // collapses with the white space around it.
+ self.should_collapse_whitespace
+ && code_point_is_ascii_space(code_point)
+ && !(self.should_respect_linebreaks && code_point == '\n' as u32)
}
fn is_at_line_break_opportunity(&self) -> bool {
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-text/white-space/pre-line-051.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/white-space/pre-line-051.html
new file mode 100644
index 00000000000..d85a25c7f25
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/white-space/pre-line-051.html
@@ -0,0 +1,37 @@
+<!DOCTYPE html>
+
+ <meta charset="UTF-8">
+
+ <title>CSS Text: inline starting with a collapsible white space</title>
+
+ <link rel="author" title="Gérard Talbot" href="http://www.gtalbot.org/BrowserBugsSection/css21testsuite/">
+ <link rel="help" href="https://www.w3.org/TR/css-text-3/#white-space-phase-2">
+ <link rel="match" href="../../../../../expected/wpt-import/css/css-text/white-space/../../reference/ref-filled-green-100px-square.xht">
+ <link rel="stylesheet" type="text/css" href="../../../fonts/ahem.css">
+
+ <style>
+ div
+ {
+ background-color: red;
+ color: green;
+ float: left;
+ font-family: Ahem;
+ font-size: 50px;
+ line-height: 1;
+ }
+
+ span#pre-line
+ {
+ white-space: pre-line;
+ }
+ </style>
+
+ <p>Test passes if there is a filled green square and <strong>no red</strong>.
+
+ <div><span id="pre-line">AB&NewLine;</span><span> CD</span></div>
+
+<!--
+
+ &NewLine; == Line feed == &#x000A; == &#0010;
+
+-->
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-text/white-space/pre-line-052.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/white-space/pre-line-052.html
new file mode 100644
index 00000000000..153fc5a3e0b
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/white-space/pre-line-052.html
@@ -0,0 +1,37 @@
+<!DOCTYPE html>
+
+ <meta charset="UTF-8">
+
+ <title>CSS Text: inline starting with a collapsible white space</title>
+
+ <link rel="author" title="Gérard Talbot" href="http://www.gtalbot.org/BrowserBugsSection/css21testsuite/">
+ <link rel="help" href="https://www.w3.org/TR/css-text-3/#white-space-phase-2">
+ <link rel="match" href="../../../../../expected/wpt-import/css/css-text/white-space/../../reference/ref-filled-green-100px-square.xht">
+ <link rel="stylesheet" type="text/css" href="../../../fonts/ahem.css">
+
+ <style>
+ div
+ {
+ background-color: red;
+ color: green;
+ display: inline-block;
+ font-family: Ahem;
+ font-size: 50px;
+ line-height: 1;
+ }
+
+ span#pre-line
+ {
+ white-space: pre-line;
+ }
+ </style>
+
+ <p>Test passes if there is a filled green square and <strong>no red</strong>.
+
+ <div><span id="pre-line">AB&NewLine;</span><span> CD</span></div>
+
+<!--
+
+ &NewLine; == Line feed == &#x000A; == &#0010;
+
+-->
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-text/white-space/pre-line-with-space-and-newline.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/white-space/pre-line-with-space-and-newline.html
new file mode 100644
index 00000000000..f1ada7e85d3
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/white-space/pre-line-with-space-and-newline.html
@@ -0,0 +1,44 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>CSS Text Test: 'white-space: pre-line' with space and newline</title>
+<link rel="author" title="Oriol Brufau" href="mailto:[email protected]">
+<link rel="help" href="https://drafts.csswg.org/css-text/#white-space-phase-1">
+<link rel="help" href="https://crbug.com/1136688">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-text/white-space/../../reference/ref-filled-green-200px-square.html">
+<meta name="assert" content="
+ Checks that collapsible spaces immediately preceding a sequent break are removed.
+ That still applies if they are separated into different inline elements.
+ Also, if some text is inserted dynamically, the browser should not crash.">
+<link rel="stylesheet" type="text/css" href="../../../fonts/ahem.css" />
+<style>
+span {
+ font: 25px/1 Ahem;
+ background: red;
+ color: green;
+}
+i {
+ white-space: pre-line;
+}
+</style>
+<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
+<div class="static together">
+ <span>XXXXXXXX<i> &#10;</i>XXXXXXXX</span>
+</div>
+<div class="static separated">
+ <span>XXXXXXXX<i> </i><i>&#10;</i>XXXXXXXX</span>
+</div>
+<div class="dynamic together">
+ <span>XXXXXXXX<i> &#10;</i></span>
+</div>
+<div class="dynamic separated">
+ <span>XXXXXXXX<i> </i><i>&#10;</i></span>
+</div>
+<script>
+// Force layout
+document.body.offsetLeft;
+
+// Insert text, should not crash
+for (let span of document.querySelectorAll(".dynamic > span")) {
+ span.append("XXXXXXXX");
+}
+</script>
diff --git a/Tests/LibWeb/Text/expected/css/pre-line-preserves-line-feed-after-collapsible-spaces.txt b/Tests/LibWeb/Text/expected/css/pre-line-preserves-line-feed-after-collapsible-spaces.txt
new file mode 100644
index 00000000000..7c48f091a1b
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/pre-line-preserves-line-feed-after-collapsible-spaces.txt
@@ -0,0 +1,10 @@
+space-before-line-feed: height 40, "two" at line 2, x 0
+two-spaces-before-line-feed: height 40, "two" at line 2, x 0
+space-and-tab-before-line-feed: height 40, "two" at line 2, x 0
+two-line-feeds: height 60, "two" at line 3, x 0
+space-between-line-feeds: height 60, "two" at line 3, x 0
+spaces-between-line-feeds: height 80, "two" at line 4, x 0
+tab-between-line-feeds: height 60, "two" at line 3, x 0
+spaces-after-line-feed: height 40, "two" at line 2, x 0
+spaces-around-line-feed: height 40, "two" at line 2, x 0
+crlf-pairs: height 60, "two" at line 3, x 0
diff --git a/Tests/LibWeb/Text/input/css/pre-line-preserves-line-feed-after-collapsible-spaces.html b/Tests/LibWeb/Text/input/css/pre-line-preserves-line-feed-after-collapsible-spaces.html
new file mode 100644
index 00000000000..34230d28fe0
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/pre-line-preserves-line-feed-after-collapsible-spaces.html
@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ div { font: 16px/20px monospace; white-space: pre-line; width: 300px; }
+</style>
+<div id="host"></div>
+<script>
+ test(() => {
+ // Each line feed that white-space: pre-line preserves starts a new line, whatever collapsible white space
+ // comes before or after it. So "two" is on line 2 or 3, at the start of the line.
+ const cases = {
+ "space-before-line-feed": "one \ntwo",
+ "two-spaces-before-line-feed": "one \ntwo",
+ "space-and-tab-before-line-feed": "one \t\ntwo",
+ "two-line-feeds": "one\n\ntwo",
+ "space-between-line-feeds": "one\n \ntwo",
+ "spaces-between-line-feeds": "one\n \n \ntwo",
+ "tab-between-line-feeds": "one\t\n\t\ntwo",
+ "spaces-after-line-feed": "one\n two",
+ "spaces-around-line-feed": "one \n two",
+ "crlf-pairs": "one\r\n\r\ntwo",
+ };
+ const host = document.getElementById("host");
+ for (const [id, text] of Object.entries(cases)) {
+ const div = document.createElement("div");
+ div.textContent = text;
+ host.append(div);
+ const offset = text.indexOf("two");
+ const range = document.createRange();
+ range.setStart(div.firstChild, offset);
+ range.setEnd(div.firstChild, offset + 3);
+ const rect = range.getBoundingClientRect();
+ const first = document.createRange();
+ first.setStart(div.firstChild, 0);
+ first.setEnd(div.firstChild, 3);
+ const box = div.getBoundingClientRect();
+ println(`${id}: height ${box.height}, "two" at line ${(rect.top - first.getBoundingClientRect().top) / 20 + 1}, x ${rect.left - box.left}`);
+ }
+ });
+</script>