Skip to content

Truly independent web browser

LibWeb: Trim a line’s collapsible spaces before aligning it

sideshowbarker5e36826b8cc8
diff --git a/Libraries/LibWeb/Rust/src/layout/line_builder.rs b/Libraries/LibWeb/Rust/src/layout/line_builder.rs
index 629a675b2e5..24dc96b03c5 100644
--- a/Libraries/LibWeb/Rust/src/layout/line_builder.rs
+++ b/Libraries/LibWeb/Rust/src/layout/line_builder.rs
@@ -1577,10 +1577,11 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
);
}
- // Mirroring must use the retained line width, so discarded trailing spaces don't become leading gaps.
- if self.context().atomic_items_are_right_to_left
- && self.line(line_index).inline_length_before_block_ellipsis.is_none()
- {
+ // https://drafts.csswg.org/css-text-3/#white-space-phase-2
+ // "A sequence of collapsible spaces at the end of a line is removed"
+ // NB: That happens before the line is aligned, so that its alignment doesn't count the spaces, and mirroring
+ // uses the retained line width, so that discarded trailing spaces don't become leading gaps.
+ if self.line(line_index).inline_length_before_block_ellipsis.is_none() {
self.line_mut(line_index).trim_trailing_whitespace();
}
diff --git a/Tests/LibWeb/Text/expected/collapsible-space-before-forced-break-in-aligned-line.txt b/Tests/LibWeb/Text/expected/collapsible-space-before-forced-break-in-aligned-line.txt
new file mode 100644
index 00000000000..de7b1d9bc75
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/collapsible-space-before-forced-break-in-aligned-line.txt
@@ -0,0 +1,8 @@
+right: 70..100 | 70..100
+center: 35..65 | 35..65
+end: 70..100 | 70..100
+right-two-spaces: 70..100 | 70..100
+right-space-outside-span: 70..100 | 70..100
+right-rtl: 0..30 | 0..30
+right-pre-wrap: 60..100 | 70..100
+right-no-break-space: 60..100 | 70..100
diff --git a/Tests/LibWeb/Text/input/collapsible-space-before-forced-break-in-aligned-line.html b/Tests/LibWeb/Text/input/collapsible-space-before-forced-break-in-aligned-line.html
new file mode 100644
index 00000000000..7b2a4789727
--- /dev/null
+++ b/Tests/LibWeb/Text/input/collapsible-space-before-forced-break-in-aligned-line.html
@@ -0,0 +1,29 @@
+<!DOCTYPE html>
+<style>
+ @font-face { font-family: Ahem; src: url("../../Ref/data/fonts/Ahem.ttf"); }
+ body { font: 10px/20px Ahem; }
+ .c { width: 100px; }
+</style>
+<div class="c" id="right" style="text-align: right"><span>aaa </span><br><span>bbb</span></div>
+<div class="c" id="center" style="text-align: center"><span>aaa </span><br><span>bbb</span></div>
+<div class="c" id="end" style="text-align: end"><span>aaa </span><br><span>bbb</span></div>
+<div class="c" id="right-two-spaces" style="text-align: right"><span>aaa </span><br><span>bbb</span></div>
+<div class="c" id="right-space-outside-span" style="text-align: right"><span>aaa</span> <br><span>bbb</span></div>
+<div class="c" id="right-rtl" dir="rtl" style="text-align: left"><span>aaa </span><br><span>bbb</span></div>
+<div class="c" id="right-pre-wrap" style="text-align: right; white-space: pre-wrap"><span>aaa </span><br><span>bbb</span></div>
+<div class="c" id="right-no-break-space" style="text-align: right"><span>aaa&nbsp;</span><br><span>bbb</span></div>
+<script src="include.js"></script>
+<script>
+ asyncTest(async done => {
+ await document.fonts.load("10px Ahem");
+ for (const c of document.querySelectorAll(".c")) {
+ const left = c.getBoundingClientRect().left;
+ const spans = [...c.querySelectorAll("span")].map(span => {
+ const rect = span.getClientRects()[0];
+ return `${rect.left - left}..${rect.right - left}`;
+ });
+ println(`${c.id}: ${spans.join(" | ")}`);
+ }
+ done();
+ });
+</script>