Skip to content

Truly independent web browser

LibWeb: Wrap after a space only when its `white-space` allows wrapping

Tim Ledbetter47cc11cda1ee
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
index 156cdd528b0..b91de4b03a5 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
@@ -1695,6 +1695,12 @@ impl<'context> InlineFormattingContext<'context> {
}
}
+ fn line_is_empty_or_ends_in_whitespace_that_wraps(&self, line: &line_box::LineBoxData) -> bool {
+ line.fragments.last().is_none_or(|fragment| {
+ fragment.ends_in_whitespace() && self.style(fragment.style_source).text_wrap_mode() == text_wrap_mode::WRAP
+ })
+ }
+
pub(crate) fn overflow_break_applies(&self, text_node: Node) -> bool {
self.overflow_break_applies_to_style(self.style(self.parent_node(text_node)))
}
@@ -2241,7 +2247,7 @@ impl<'context> InlineFormattingContext<'context> {
.line_data()
.line_boxes
.last()
- .is_some_and(line_box::LineBoxData::is_empty_or_ends_in_whitespace);
+ .is_some_and(|line| self.line_is_empty_or_ends_in_whitespace_that_wraps(line));
let opportunity = self.text_item_wrap_opportunity(
&item,
style,
@@ -3145,6 +3151,7 @@ pub(super) struct LinesWithoutLineBoxes {
inline_size: CssPixels,
has_fragments: bool,
ends_in_whitespace: bool,
+ trailing_whitespace_wraps: bool,
trailing_whitespace_inline_size: CssPixels,
follows_forced_break: bool,
floats_beside_line: FloatsBesideLine,
@@ -3180,6 +3187,14 @@ impl LinesWithoutLineBoxes {
!self.has_fragments || self.ends_in_whitespace
}
+ fn is_empty_or_ends_in_whitespace_that_wraps(&self) -> bool {
+ !self.has_fragments || (self.ends_in_whitespace && self.trailing_whitespace_wraps)
+ }
+
+ fn prevent_wrap_after_trailing_whitespace(&mut self) {
+ self.trailing_whitespace_wraps = false;
+ }
+
fn breaks_before_next_fragment(&self) -> bool {
self.breaks_at_every_opportunity && self.has_fragments
}
@@ -3202,6 +3217,7 @@ impl LinesWithoutLineBoxes {
self.inline_size += border_box_inline_size;
self.has_fragments = true;
self.ends_in_whitespace = ends_in_whitespace;
+ self.trailing_whitespace_wraps = true;
// NB: Adjacent all-space chunks form one trailing sequence, including across inline boundaries.
if ends_in_whitespace && border_box_inline_size == trailing_whitespace_inline_size {
self.trailing_whitespace_inline_size += trailing_whitespace_inline_size;
@@ -3384,7 +3400,8 @@ impl ContentSizingLines {
style,
is_whitespace,
remaining_items,
- line_is_empty_or_ends_in_whitespace || can_break_after_previous_grapheme_broken_item,
+ self.lines.is_empty_or_ends_in_whitespace_that_wraps()
+ || can_break_after_previous_grapheme_broken_item,
false,
)
} else {
@@ -3424,6 +3441,9 @@ impl ContentSizingLines {
item.ends_in_ascii_space(context),
trailing_whitespace_inline_size,
);
+ if style.text_wrap_mode() != text_wrap_mode::WRAP {
+ self.lines.prevent_wrap_after_trailing_whitespace();
+ }
}
}
}
diff --git a/Tests/LibWeb/Layout/expected/no-wrap-after-nowrap-space.txt b/Tests/LibWeb/Layout/expected/no-wrap-after-nowrap-space.txt
new file mode 100644
index 00000000000..cad3157f1cc
--- /dev/null
+++ b/Tests/LibWeb/Layout/expected/no-wrap-after-nowrap-space.txt
@@ -0,0 +1,86 @@
+Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline
+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 208 0+0+0] [BFC] children: not-inline
+ BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 190 0+0+8] children: not-inline
+ BlockContainer <div> at [8,8] [0+0+0 30 0+0+754] [0+0+0 10 0+0+10] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 1, rect: [38,8 10x10] baseline: 8
+ "c"
+ InlineNode <span.nowrap> at [8,8] [0+0+0 20 0+0+0] [0+0+0 10 0+0+0]
+ frag 0 from TextNode start: 0, length: 2, rect: [8,8 20x10] baseline: 8
+ "a "
+ TextNode <#text> (not painted)
+ InlineNode <span> at [28,8] [0+0+0 10 0+0+0] [0+0+0 10 0+0+0]
+ frag 0 from TextNode start: 0, length: 1, rect: [28,8 10x10] baseline: 8
+ "b"
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [8,28] [0+0+0 30 0+0+754] [0+0+0 10 0+0+10] [BFC] children: inline
+ InlineNode <span.nowrap> at [8,28] [0+0+0 20 0+0+0] [0+0+0 10 0+0+0]
+ frag 0 from TextNode start: 0, length: 2, rect: [8,28 20x10] baseline: 8
+ "a "
+ TextNode <#text> (not painted)
+ InlineNode <span> at [28,28] [0+0+0 20 0+0+0] [0+0+0 10 0+0+0]
+ frag 0 from TextNode start: 0, length: 2, rect: [28,28 20x10] baseline: 8
+ "bb"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [8,48] [0+0+0 30 0+0+754] [0+0+0 10 0+0+10] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 2, rect: [28,48 20x10] baseline: 8
+ "bb"
+ InlineNode <span.nowrap> at [8,48] [0+0+0 20 0+0+0] [0+0+0 10 0+0+0]
+ frag 0 from TextNode start: 0, length: 2, rect: [8,48 20x10] baseline: 8
+ "a "
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [8,68] [0+0+0 30 0+0+754] [0+0+0 10 0+0+10] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 2, rect: [28,68 20x10] baseline: 8
+ "bb"
+ InlineNode <span> at [8,68] [0+0+0 20 0+0+0] [0+0+0 10 0+0+0]
+ frag 0 from TextNode start: 0, length: 2, rect: [8,68 20x10] baseline: 8
+ "a "
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [8,88] [0+0+0 30 0+0+754] [0+0+0 10 0+0+10] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 1, rect: [8,88 10x10] baseline: 8
+ "a"
+ frag 1 from TextNode start: 0, length: 2, rect: [28,88 20x10] baseline: 8
+ "bb"
+ TextNode <#text> (not painted)
+ InlineNode <span.nowrap> at [18,88] [0+0+0 10 0+0+0] [0+0+0 10 0+0+0]
+ frag 0 from TextNode start: 0, length: 1, rect: [18,88 10x10] baseline: 8
+ " "
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [8,108] [0+0+0 30 0+0+754] [0+0+0 20 0+0+10] [BFC] children: inline
+ frag 0 from TextNode start: 1, length: 2, rect: [8,118 20x10] baseline: 8
+ "bb"
+ InlineNode <span.nowrap> at [8,108] [0+0+0 10 0+0+0] [0+0+0 10 0+0+0]
+ frag 0 from TextNode start: 0, length: 1, rect: [8,108 10x10] baseline: 8
+ "a"
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <div.min-content> at [8,138] [0+0+0 40 0+0+744] [0+0+0 10 0+0+10] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 2, rect: [28,138 20x10] baseline: 8
+ "bb"
+ InlineNode <span.nowrap> at [8,138] [0+0+0 20 0+0+0] [0+0+0 10 0+0+0]
+ frag 0 from TextNode start: 0, length: 2, rect: [8,138 20x10] baseline: 8
+ "a "
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <div.min-content> at [8,158] [0+0+0 40 0+0+744] [0+0+0 10 0+0+10] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 2, rect: [28,158 20x10] baseline: 8
+ "bb"
+ InlineNode <span> at [8,158] [0+0+0 20 0+0+0] [0+0+0 10 0+0+0]
+ frag 0 from TextNode start: 0, length: 2, rect: [8,158 20x10] baseline: 8
+ "a "
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <div.min-content> at [8,178] [0+0+0 20 0+0+764] [0+0+0 20 0+0+10] [BFC] children: inline
+ frag 0 from TextNode start: 1, length: 2, rect: [8,188 20x10] baseline: 8
+ "bb"
+ InlineNode <span.nowrap> at [8,178] [0+0+0 10 0+0+0] [0+0+0 10 0+0+0]
+ frag 0 from TextNode start: 0, length: 1, rect: [8,178 10x10] baseline: 8
+ "a"
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+
+SC for Viewport<#document> [0,0 800x600] (z-index: auto)
+ SC for BlockContainer<HTML> [0,0 800x208] (z-index: auto)
diff --git a/Tests/LibWeb/Layout/input/no-wrap-after-nowrap-space.html b/Tests/LibWeb/Layout/input/no-wrap-after-nowrap-space.html
new file mode 100644
index 00000000000..8c4a24accdb
--- /dev/null
+++ b/Tests/LibWeb/Layout/input/no-wrap-after-nowrap-space.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<style>
+ @font-face {
+ font-family: Ahem;
+ src: url("../../Ref/data/fonts/Ahem.ttf");
+ }
+ div {
+ font: 10px/10px Ahem;
+ width: 30px;
+ margin-bottom: 10px;
+ display: flow-root;
+ }
+ .nowrap {
+ white-space: nowrap;
+ }
+ .min-content {
+ width: min-content;
+ }
+</style>
+<div><span class="nowrap">a </span><span>b</span>c</div>
+<div><span class="nowrap">a </span><span>bb</span></div>
+<div><span class="nowrap">a </span>bb</div>
+<div><span style="white-space: pre">a </span>bb</div>
+<div>a<span class="nowrap"> </span>bb</div>
+<div><span class="nowrap">a</span> bb</div>
+<div class="min-content"><span class="nowrap">a </span>bb</div>
+<div class="min-content"><span style="white-space: pre">a </span>bb</div>
+<div class="min-content"><span class="nowrap">a</span> bb</div>