Truly independent web browser
LibWeb: Keep an inline box’s end edges on the line its space ends
sideshowbarkerced28f9a4c53
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rsindex 71620c40e4e..3ad55742f03 100644--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs@@ -2987,12 +2987,17 @@ impl<'context> InlineFormattingContext<'context> {inline_level_iterator::inline_extent([&item]),);}+ // NB: The white space ends the line it breaks, and so do the inline boxes that end with it.+ line_builder.set_end_edges_of_whitespace_at_break(+ item.margin_end + item.border_end + item.padding_end,+ );let broke_line = if iterator.next_non_whitespace_text_allows_overflow_break(self) {line_builder.break_if_needed_before_overflow_breakable_item(sequence_inline_size)} else {line_builder.break_if_needed(sequence_inline_size)};line_builder.set_hanging_whitespace_inline_size(CssPixels::default());+ line_builder.set_end_edges_of_whitespace_at_break(CssPixels::default());if broke_line {line_builder.set_trailing_whitespace_on_previous_line(&item);let next_line_start = fragmentation::LinePosition {diff --git a/Libraries/LibWeb/Rust/src/layout/line_builder.rs b/Libraries/LibWeb/Rust/src/layout/line_builder.rsindex be1ba7fa91f..bc30e3f8963 100644--- a/Libraries/LibWeb/Rust/src/layout/line_builder.rs+++ b/Libraries/LibWeb/Rust/src/layout/line_builder.rs@@ -83,6 +83,8 @@ pub(crate) struct LineBuilder<'builder, 'context> {unbreakable_run_inline_size_interrupted_by_float: CssPixels,/// The advance of the preserved white space that a line is breaking at, which hangs at the end of that line.hanging_whitespace_inline_size: CssPixels,+ /// The end edges of the inline boxes that end with the white space a line is breaking at, which stay on that line.+ end_edges_of_whitespace_at_break: CssPixels,/// How many lines from the first are known to hold no content, while no line is known to: Every line until the/// first formatted line is one past floats that held nothing.lines_known_to_hold_no_content: Option<usize>,@@ -182,6 +184,7 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {outside_marker_line: context.parent.pending_outside_marker_line(),unbreakable_run_inline_size_interrupted_by_float: CssPixels::default(),hanging_whitespace_inline_size: CssPixels::default(),+ end_edges_of_whitespace_at_break: CssPixels::default(),lines_known_to_hold_no_content: Some(0),text_indentation: inline_formatting_context::TextIndentation::of(style, containing_inline_size),direction: style.direction(),@@ -361,6 +364,10 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {self.keep_line_of_collapsed_whitespace_edges();let line_index = self.ensure_last_line_index();+ if forced == ForcedBreak::No && !self.line(line_index).fragments.is_empty() {+ let end_edges = std::mem::take(&mut self.end_edges_of_whitespace_at_break);+ self.line_mut(line_index).advance_inline_length(end_edges);+ }{let mut line = self.line_mut(line_index);line.has_break = true;@@ -2021,6 +2028,10 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {self.hanging_whitespace_inline_size = inline_size;}+ pub(crate) fn set_end_edges_of_whitespace_at_break(&mut self, inline_size: CssPixels) {+ self.end_edges_of_whitespace_at_break = inline_size;+ }+pub(crate) fn set_unbreakable_run_inline_size_interrupted_by_float(&mut self, inline_size: CssPixels) {self.unbreakable_run_inline_size_interrupted_by_float = inline_size;}diff --git a/Tests/LibWeb/Text/expected/css/inline-box-end-edges-after-trailing-space-fit-on-line.txt b/Tests/LibWeb/Text/expected/css/inline-box-end-edges-after-trailing-space-fit-on-line.txtindex cc3eb80cd26..38c7a90cd5a 100644--- a/Tests/LibWeb/Text/expected/css/inline-box-end-edges-after-trailing-space-fit-on-line.txt+++ b/Tests/LibWeb/Text/expected/css/inline-box-end-edges-after-trailing-space-fit-on-line.txt@@ -10,3 +10,9 @@ space-after-text-nested: 2 line(s), widest 104pxspace-after-text-pre-wrap: 2 line(s), widest 106pxstart-padding-only: 1 line(s), widest 176pxend-padding-only: 1 line(s), widest 176px+right-padding: 5..35+right-margin: 5..25+right-two-spaces: 5..35+right-to-left: 5..35+center: 7.5..37.5+nested: 0..45diff --git a/Tests/LibWeb/Text/input/css/inline-box-end-edges-after-trailing-space-fit-on-line.html b/Tests/LibWeb/Text/input/css/inline-box-end-edges-after-trailing-space-fit-on-line.htmlindex 63590a627f9..97c11d28885 100644--- a/Tests/LibWeb/Text/input/css/inline-box-end-edges-after-trailing-space-fit-on-line.html+++ b/Tests/LibWeb/Text/input/css/inline-box-end-edges-after-trailing-space-fit-on-line.html@@ -4,6 +4,7 @@body { font: 10px/20px Ahem; }.w { width: 180px; }.p { padding: 0 16px; }+ .aligned { width: 35px; }</style><!-- The text is 160px wide, and 192px with both paddings, in a 180px container: Each span whose end edges are on theline with "bbbbbbbb" doesn't fit there, so it takes two lines, whatever white space comes before its end. -->@@ -20,6 +21,14 @@<!-- These fit: Only one side has padding. --><div class="w" id="start-padding-only"><span style="padding-left: 16px">aaaaaaa bbbbbbbb </span></div><div class="w" id="end-padding-only"><span style="padding-right: 16px">aaaaaaa bbbbbbbb </span></div>+<!-- Each line breaks at the space that ends a span, and the span's end edges stay on the line before the break, so+ they count toward the width that line is aligned by. -->+<div class="aligned" id="right-padding" style="text-align: right"><span style="padding-right: 10px">xx </span>yy</div>+<div class="aligned" id="right-margin" style="text-align: right"><span style="margin-right: 10px">xx </span>yy</div>+<div class="aligned" id="right-two-spaces" style="text-align: right"><span style="padding-right: 10px">xx </span>yy</div>+<div class="aligned" id="right-to-left" style="direction: rtl"><span style="padding-left: 10px">xx </span>yy</div>+<div class="aligned" id="center" style="text-align: center; width: 45px"><span style="padding-right: 10px">xx </span>yy</div>+<div class="aligned" id="nested" style="width: 45px"><span style="padding-right: 10px">x <b style="padding-right: 5px">x </b></span>yy</div><script>test(() => {for (const div of document.querySelectorAll(".w")) {@@ -27,5 +36,10 @@const rects = [...span.getClientRects()];println(`${div.id}: ${rects.length} line(s), widest ${Math.max(...rects.map(rect => rect.width))}px`);}+ for (const div of document.querySelectorAll(".aligned")) {+ const left = div.getBoundingClientRect().left;+ const rects = [...div.querySelector("span").getClientRects()];+ println(`${div.id}: ${rects.map(rect => `${rect.left - left}..${rect.right - left}`).join(", ")}`);+ }});</script>