Skip to content

Truly independent web browser

LibWeb: Break after each preserved space with break-spaces

Andreas Kling65300ca3b453
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
index 3e3c903b988..af8370b2579 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
@@ -1669,6 +1669,16 @@ impl<'context> InlineFormattingContext<'context> {
return TextItemWrapOpportunity::None;
}
if is_whitespace {
+ if style.white_space_collapse() == white_space_collapse::BREAK_SPACES {
+ // https://drafts.csswg.org/css-text-4/#valdef-white-space-collapse-break-spaces
+ // A soft wrap opportunity exists after every preserved white space character and after every other
+ // space separator (including between adjacent spaces).
+ return if line_ends_in_whitespace {
+ TextItemWrapOpportunity::BeforeText
+ } else {
+ TextItemWrapOpportunity::None
+ };
+ }
let next_inline_size =
inline_level_iterator::sequence_inline_size(self, remaining_items, false).unwrap_or_default();
return if next_inline_size > CssPixels::default() {
@@ -3083,6 +3093,9 @@ impl ContentSizingLines {
self.lines.finish();
return;
}
+ if opportunity == TextItemWrapOpportunity::BeforeText {
+ self.lines.finish();
+ }
self.lines
.append(border_box_inline_size, true, trailing_whitespace_inline_size);
return;
diff --git a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs b/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
index 67df79c7ed0..4a4f6a3fa61 100644
--- a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
+++ b/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
@@ -651,6 +651,7 @@ struct TextChunker<'text> {
allow_conditional_japanese_starters: bool,
font_variant_emoji: u8,
should_collapse_whitespace: bool,
+ break_after_preserved_tabs: bool,
should_wrap_lines: bool,
should_respect_linebreaks: bool,
unidirectional_ltr: bool,
@@ -675,6 +676,8 @@ impl<'text> TextChunker<'text> {
inputs.white_space_collapse,
white_space_collapse::COLLAPSE | white_space_collapse::PRESERVE_BREAKS
),
+ break_after_preserved_tabs: inputs.white_space_collapse == white_space_collapse::BREAK_SPACES
+ && inputs.should_wrap_lines,
should_wrap_lines: inputs.should_wrap_lines,
should_respect_linebreaks: inputs.should_respect_linebreaks,
unidirectional_ltr: inputs.unidirectional_ltr,
@@ -936,10 +939,7 @@ impl<'text> TextChunker<'text> {
}
pending.broken_on_tab = true;
- // consume any consecutive tabs
- while self.current_index < self.text.len() && self.current_code_point() == '\t' as u32 {
- self.current_index = self.next_grapheme_boundary();
- }
+ self.current_index = tab_chunk_end(self.text, self.current_index, self.break_after_preserved_tabs);
can_break_at_current_position = self.is_at_line_break_opportunity();
}
@@ -1126,3 +1126,29 @@ pub(crate) fn text_chunks(
})
})
}
+
+// Tabs occupy one UTF-16 code unit each. Other preserved whitespace is split below by next_chunk().
+fn tab_chunk_end(text: &[u16], start: usize, break_after_each_tab: bool) -> usize {
+ let mut end = start + 1;
+ while !break_after_each_tab && text.get(end) == Some(&u16::from(b'\t')) {
+ end += 1;
+ }
+ end
+}
+
+#[cfg(test)]
+mod tests {
+ use super::tab_chunk_end;
+
+ #[test]
+ fn preserved_tab_chunk_boundaries_respect_break_spaces() {
+ let text: Vec<_> = "\t\t\tX".encode_utf16().collect();
+ assert_eq!(tab_chunk_end(&text, 0, true), 1);
+ assert_eq!(tab_chunk_end(&text, 1, true), 2);
+ assert_eq!(tab_chunk_end(&text, 2, true), 3);
+ // pre/pre-wrap and nowrap retain the existing tab-run grouping.
+ assert_eq!(tab_chunk_end(&text, 0, false), 3);
+ assert_eq!(tab_chunk_end(&text, 2, false), 3);
+ assert_eq!(tab_chunk_end(&[u16::from(b'\t')], 0, true), 1);
+ }
+}
diff --git a/Tests/LibWeb/Text/expected/intrinsic-inline-content-sizing.txt b/Tests/LibWeb/Text/expected/intrinsic-inline-content-sizing.txt
index 782c95e2f80..0028ae48804 100644
--- a/Tests/LibWeb/Text/expected/intrinsic-inline-content-sizing.txt
+++ b/Tests/LibWeb/Text/expected/intrinsic-inline-content-sizing.txt
@@ -127,7 +127,7 @@ grid with an orthogonal item: 110x60, 110x60, 110x60, 110x60
grid with an aspect ratio item sized by its row: 130x80, 130x80, 130x80, 130x80
empty grid with fixed columns: 64x0, 64x0, 64x0, 64x0
row flex with a grid item distributing its columns: 150x20, 150x20, 150x20, 150x20
-words, min-content: [80,90]
+words, min-content: [100,60]
words, max-content: [200,30]
words, fit-content: [200,30]
collapsing spaces, min-content: [40,60]
@@ -142,43 +142,43 @@ preserved newlines, fit-content: [100,60]
no wrapping, min-content: [100,30]
no wrapping, max-content: [100,30]
no wrapping, fit-content: [100,30]
-preserved spaces, min-content: [100,60]
+preserved spaces, min-content: [60,120]
preserved spaces, max-content: [220,30]
preserved spaces, fit-content: [220,30]
-only spaces, min-content: [60,30]
+only spaces, min-content: [20,90]
only spaces, max-content: [60,30]
only spaces, fit-content: [60,30]
forced breaks, min-content: [60,120,40,60,0,30]
forced breaks, max-content: [60,120,40,60,0,30]
forced breaks, fit-content: [60,120,40,60,0,30]
-inline boundaries, min-content: [40,90,20,5,20,20,0,35,40,20]
+inline boundaries, min-content: [60,90,20,5,40,20,0,35,40,20]
inline boundaries, max-content: [140,30,20,5,40,20,60,5,40,20]
inline boundaries, fit-content: [140,30,20,5,40,20,60,5,40,20]
-vertical alignment, min-content: [60,60,20,5,40,20]
+vertical alignment, min-content: [80,60,20,5,40,20]
vertical alignment, max-content: [100,70,20,5,40,20]
vertical alignment, fit-content: [100,70,20,5,40,20]
-mixed line heights, min-content: [100,90,20,10,80,40]
+mixed line heights, min-content: [120,90,20,10,80,40]
mixed line heights, max-content: [140,60,20,10,80,40]
mixed line heights, fit-content: [140,60,20,10,80,40]
-tabs, min-content: [40,90]
+tabs, min-content: [80,60]
tabs, max-content: [160,30]
tabs, fit-content: [160,30]
-decorated inline, min-content: [67,60,20,-2,47,34,0,28,27,34]
+decorated inline, min-content: [87,60,20,-2,67,34,0,28,27,34]
decorated inline, max-content: [154,30,20,-2,94,34]
decorated inline, fit-content: [154,30,20,-2,94,34]
-percentage padding, min-content: [60,90,20,5,26,20,0,35,60,20]
+percentage padding, min-content: [80,60,20,5,68,20,0,35,20,20]
percentage padding, max-content: [140,60,20,5,94,20]
percentage padding, fit-content: [140,60,20,5,94,20]
-atomic inline, min-content: [40,120,0,30,40,60]
+atomic inline, min-content: [80,90,0,30,80,30]
atomic inline, max-content: [140,30,20,0,80,30]
atomic inline, fit-content: [140,30,20,0,80,30]
-floating inline, min-content: [40,120,0,30,40,60]
+floating inline, min-content: [60,30,0,30,60,60]
floating inline, max-content: [140,30,0,0,80,30]
floating inline, fit-content: [140,30,0,0,80,30]
-absolutely positioned inline, min-content: [20,60,20,0,80,30]
+absolutely positioned inline, min-content: [40,60,20,0,80,30]
absolutely positioned inline, max-content: [60,30,20,0,80,30]
absolutely positioned inline, fit-content: [60,30,20,0,80,30]
-block interruption, min-content: [40,120,0,30,40,60]
+block interruption, min-content: [60,120,0,30,60,60]
block interruption, max-content: [80,90,0,30,80,30]
block interruption, fit-content: [80,90,0,30,80,30]
mixed whitespace, min-content: [60,60,20,5,40,20,0,35,20,20]
@@ -190,7 +190,7 @@ overflow anywhere, fit-content: [180,30]
break word, min-content: [20,240]
break word, max-content: [180,30]
break word, fit-content: [180,30]
-text indent, min-content: [90,60]
+text indent, min-content: [110,60]
text indent, max-content: [190,30]
text indent, fit-content: [190,30]
scroll container paragraphs, min-content: [80,210,0,20,80,90,0,130,80,60,20,135,40,20]
@@ -216,5 +216,5 @@ normal, min-content: width 80, runs under inline size measurements 0
normal, max-content: width 200, runs under inline size measurements 0
pre-wrap, min-content: width 80, runs under inline size measurements 0
pre-wrap, max-content: width 200, runs under inline size measurements 0
-break-spaces, min-content: width 80, runs under inline size measurements 0
+break-spaces, min-content: width 100, runs under inline size measurements 0
break-spaces, max-content: width 200, runs under inline size measurements 0
diff --git a/Tests/LibWeb/Text/expected/latin1-line-break-opportunities.txt b/Tests/LibWeb/Text/expected/latin1-line-break-opportunities.txt
index 1e0171f6690..0ca2f37c47a 100644
--- a/Tests/LibWeb/Text/expected/latin1-line-break-opportunities.txt
+++ b/Tests/LibWeb/Text/expected/latin1-line-break-opportunities.txt
@@ -6,13 +6,13 @@
1 break-spaces: l'\u{e9}t\u{e9} | (voir | p. | 12) | \u{bf}Qu\u{e9}? | \u{a1}Hola!
2: ( | \u{e9}t\u{e9} | [ | \u{e9}t\u{e9} | { | \u{e9}t\u{e9} | \u{a1} | \u{e9}t\u{e9} | \u{bf} | \u{e9}t\u{e9}
2 pre-wrap: ( | \u{e9}t\u{e9} | [ | \u{e9}t\u{e9} | { | \u{e9}t\u{e9} | \u{a1} | \u{e9}t\u{e9} | \u{bf} | \u{e9}t\u{e9}
-2 break-spaces: ( | \u{e9}t\u{e9} | [ | \u{e9}t\u{e9} | { | \u{e9}t\u{e9} | \u{a1} | \u{e9}t\u{e9} | \u{bf} | \u{e9}t\u{e9}
+2 break-spaces: ( | | \u{e9}t\u{e9} | [ | \u{e9}t\u{e9} | { | \u{e9}t\u{e9} | \u{a1} | \u{e9}t\u{e9} | \u{bf} | \u{e9}t\u{e9}
3: \u{ab}\u{a0}Oui\u{a0}\u{bb} | dit- | il, | \u{ab} | non\u{a0}\u{bb}.
3 pre-wrap: \u{ab}\u{a0}Oui\u{a0}\u{bb} | dit- | il, | \u{ab} | non\u{a0}\u{bb}.
3 break-spaces: \u{ab}\u{a0}Oui\u{a0}\u{bb} | dit- | il, | \u{ab} | non\u{a0}\u{bb}.
4: \u{ab} | Oui | \u{bb} | dit- | il | \u{ab}\u{a0} | \u{e9}
4 pre-wrap: \u{ab} | Oui | \u{bb} | dit- | il | \u{ab}\u{a0} | \u{e9}
-4 break-spaces: \u{ab} | Oui | \u{bb} | dit- | il | \u{ab}\u{a0} | \u{e9}
+4 break-spaces: \u{ab} | | Oui | | \u{bb} | dit- | il | \u{ab}\u{a0} | \u{e9}
5: caf\u{e9}, | th\u{e9}; | \u{e9}t\u{e9}: | l\u{e0}! | o\u{f9}? | \u{e7}a) | \u{e7}\u{e0}] | \u{e7}a}
5 pre-wrap: caf\u{e9}, | th\u{e9}; | \u{e9}t\u{e9}: | l\u{e0}! | o\u{f9}? | \u{e7}a) | \u{e7}\u{e0}] | \u{e7}a}
5 break-spaces: caf\u{e9}, | th\u{e9}; | \u{e9}t\u{e9}: | l\u{e0}! | o\u{f9}? | \u{e7}a) | \u{e7}\u{e0}] | \u{e7}a}
@@ -45,7 +45,7 @@
14 break-spaces: \u{e9}\u{301}e\u{301} | o\u{308} | \u{e7}\u{327}
15: \u{e9} | ! | \u{e9} | \u{e9} | ) | \u{e9} | \u{e9} | , | \u{e9}
15 pre-wrap: \u{e9} | ! | \u{e9} | \u{e9} | ) | \u{e9} | \u{e9} | , | \u{e9}
-15 break-spaces: \u{e9} | ! | \u{e9} | \u{e9} | ) | \u{e9} | \u{e9} | , | \u{e9}
+15 break-spaces: \u{e9} | | ! | \u{e9} | \u{e9} | ) | \u{e9} | \u{e9} | , | \u{e9}
16: \u{e9}/ | \u{e9} | \u{e9}/ | \u{e9} | \u{e9} | / | \u{e9}
16 pre-wrap: \u{e9}/ | \u{e9} | \u{e9}/ | \u{e9} | \u{e9} | / | \u{e9}
16 break-spaces: \u{e9}/ | \u{e9} | \u{e9}/ | \u{e9} | \u{e9} | / | \u{e9}
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-text/white-space/white-space-intrinsic-size-021.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-text/white-space/white-space-intrinsic-size-021.txt
index 4609f790459..a1d426f1ef8 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-text/white-space/white-space-intrinsic-size-021.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-text/white-space/white-space-intrinsic-size-021.txt
@@ -2,8 +2,8 @@ Harness status: OK
Found 192 tests
-183 Pass
-9 Fail
+188 Pass
+4 Fail
Pass .container > div 1
Pass .container > div 2
Pass .container > div 3
@@ -26,7 +26,7 @@ Pass .container > div 19
Pass .container > div 20
Pass .container > div 21
Pass .container > div 22
-Fail .container > div 23
+Pass .container > div 23
Pass .container > div 24
Pass .container > div 25
Pass .container > div 26
@@ -50,7 +50,7 @@ Pass .container > div 43
Pass .container > div 44
Pass .container > div 45
Pass .container > div 46
-Fail .container > div 47
+Pass .container > div 47
Pass .container > div 48
Pass .container > div 49
Pass .container > div 50
@@ -66,7 +66,7 @@ Pass .container > div 59
Pass .container > div 60
Pass .container > div 61
Pass .container > div 62
-Fail .container > div 63
+Pass .container > div 63
Pass .container > div 64
Pass .container > div 65
Pass .container > div 66
@@ -74,7 +74,7 @@ Pass .container > div 67
Pass .container > div 68
Pass .container > div 69
Pass .container > div 70
-Fail .container > div 71
+Pass .container > div 71
Pass .container > div 72
Pass .container > div 73
Pass .container > div 74
@@ -98,7 +98,7 @@ Pass .container > div 91
Pass .container > div 92
Pass .container > div 93
Pass .container > div 94
-Fail .container > div 95
+Pass .container > div 95
Pass .container > div 96
Pass .container > div 97
Pass .container > div 98