Truly independent web browser
LibWeb: Keep an empty inline box after hanging pre-wrap spaces
sideshowbarker7cd75b935b6d
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rsindex ed235244ade..3716176da74 100644--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs@@ -2554,10 +2554,17 @@ impl<'context> InlineFormattingContext<'context> {if item.is_collapsible_whitespace|| (item.type_ == inline_level_iterator::ItemType::Text && item.is_ascii_whitespace(self)){+ // NB: Preserved white space stays on the line, hanging off its end if it has to, so a box after+ // it comes after it on the line too.+ let preserved_whitespace_inline_size = if item.is_collapsible_whitespace {+ CssPixels::default()+ } else {+ inline_level_iterator::inline_extent([&item])+ };for empty_box in iterator.empty_inline_boxes_after_last_item() {line_builder.add_strut_of_empty_inline_box(empty_box.node,- pending_leading_edges + empty_box.leading_edges,+ pending_leading_edges + preserved_whitespace_inline_size + empty_box.leading_edges,);}}@@ -2800,11 +2807,20 @@ impl<'context> InlineFormattingContext<'context> {let overflow_break_allowed = !is_whitespace && self.overflow_break_applies_to_style(style);match opportunity {TextItemWrapOpportunity::BeforeWhitespaceSequence(sequence_inline_size) => {+ // https://drafts.csswg.org/css-text-3/#white-space-phase-2+ // "If 'white-space' is set to 'pre-wrap', the UA must (unconditionally) hang this+ // sequence, unless the sequence is followed by a forced line break"+ if !item.is_collapsible_whitespace {+ line_builder.set_hanging_whitespace_inline_size(+ inline_level_iterator::inline_extent([&item]),+ );+ }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());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 0f137b0dad4..f0e2edc7cea 100644--- a/Libraries/LibWeb/Rust/src/layout/line_builder.rs+++ b/Libraries/LibWeb/Rust/src/layout/line_builder.rs@@ -81,6 +81,8 @@ pub(crate) struct LineBuilder<'builder, 'context> {/// The outside markers that the first line box this builder lays out contributes to.outside_marker_line: Option<block_formatting_context::OutsideMarkerLine>,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,text_indentation: inline_formatting_context::TextIndentation,direction: u8,last_line_needs_update: bool,@@ -176,6 +178,7 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {nodes_with_edges_of_collapsed_whitespace: Vec::new(),outside_marker_line: context.parent.pending_outside_marker_line(),unbreakable_run_inline_size_interrupted_by_float: CssPixels::default(),+ hanging_whitespace_inline_size: CssPixels::default(),text_indentation: inline_formatting_context::TextIndentation::of(style, containing_inline_size),direction: style.direction(),last_line_needs_update: false,@@ -1472,11 +1475,16 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {marker.block_offset += block_offset + current_block_offset + shift;}{+ // NB: An inline box after preserved white space that hangs at the end of the line comes after that white+ // space, even though the line's inline length doesn't count it.+ let hanging_whitespace_inline_size = self.hanging_whitespace_inline_size;let mut line = self.line_mut(line_index);let content_inline_length = line.inline_length;let is_right_to_left = line.direction == direction::RTL;for strut in &mut line.inline_box_struts {- let offset = strut.inline_offset.min(content_inline_length);+ let offset = strut+ .inline_offset+ .min(content_inline_length + hanging_whitespace_inline_size);strut.inline_offset = marker_inline_offset+ if is_right_to_left {content_inline_length - offset@@ -1979,6 +1987,10 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {self.current_block_offset}+ pub(crate) fn set_hanging_whitespace_inline_size(&mut self, inline_size: CssPixels) {+ self.hanging_whitespace_inline_size = 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/pre-wrap-space-hangs-at-soft-wrap.txt b/Tests/LibWeb/Text/expected/css/pre-wrap-space-hangs-at-soft-wrap.txtnew file mode 100644index 00000000000..85f527449b5--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/pre-wrap-space-hangs-at-soft-wrap.txt@@ -0,0 +1,5 @@+pre-wrap: empty span at 60,0, height 40+pre-wrap-two-spaces: empty span at 80,0, height 40+break-spaces: empty span at 60,0, height 40+pre-line: empty span at 40,0, height 40+pre-wrap-rtl: empty span at 0,0, height 40diff --git a/Tests/LibWeb/Text/input/css/pre-wrap-space-hangs-at-soft-wrap.html b/Tests/LibWeb/Text/input/css/pre-wrap-space-hangs-at-soft-wrap.htmlnew file mode 100644index 00000000000..8b0019ac150--- /dev/null+++ b/Tests/LibWeb/Text/input/css/pre-wrap-space-hangs-at-soft-wrap.html@@ -0,0 +1,21 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<style>+ div { font: 20px/20px Ahem; width: 60px; white-space: pre-wrap; }+</style>+<!-- Preserved white space at the end of a line stays there, hanging if it has to, and so does an empty inline box after+ it. White space that collapses leaves the box right after the text. -->+<div id="pre-wrap">xx <span></span>yy</div>+<div id="pre-wrap-two-spaces">xx <span></span>yy</div>+<div id="break-spaces" style="white-space: break-spaces">xx <span></span>yy</div>+<div id="pre-line" style="white-space: pre-line">xx <span></span>yy</div>+<div id="pre-wrap-rtl" dir="rtl">xx <span></span>yy</div>+<script>+ test(() => {+ for (const div of document.querySelectorAll("div")) {+ const box = div.getBoundingClientRect();+ const span = div.querySelector("span").getBoundingClientRect();+ println(`${div.id}: empty span at ${span.left - box.left},${span.top - box.top}, height ${box.height}`);+ }+ });+</script>