Skip to content

Truly independent web browser

LibWeb: Wrap before `nowrap` text when the text before it allows a break

Tim Ledbetterb9ed99346745
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
index 85ece837560..dbd24e3b794 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
@@ -2315,6 +2315,13 @@ impl<'context> InlineFormattingContext<'context> {
}
previous_text_item_allows_overflow_break_after = true;
}
+ } else if item.can_break_before || can_break_after_previous_overflow_item {
+ line_builder.break_if_needed(iterator.unbreakable_run_inline_size(
+ self,
+ &item,
+ !item.can_break_before,
+ ));
+ line_builder.note_soft_wrap_opportunity();
}
if self.stops_at_line_clamp_point(line_builder, &mut state_at_clamp_point) {
break;
@@ -3449,6 +3456,10 @@ impl ContentSizingLines {
if opportunity == TextItemWrapOpportunity::BeforeText {
self.lines.finish();
}
+ } else if self.lines.breaks_before_next_fragment()
+ && (item.can_break_before || previous_text_item_breaks_between_graphemes)
+ {
+ self.lines.finish();
}
self.lines.append(
border_box_inline_size,
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
index 825f071bc41..7bd098199a4 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
@@ -1129,7 +1129,7 @@ impl<'pass> InlineContentPreparer<'pass> {
if is_first_chunk
&& !can_break_before
&& let Some(previous_text) = self.previous_text.take()
- && text_context.should_wrap_lines
+ && (text_context.should_wrap_lines || self.previous_text_breaks_all)
{
let style = text_context.style;
let line_break_language = if style.line_break() == line_break::LOOSE {
@@ -1148,7 +1148,7 @@ impl<'pass> InlineContentPreparer<'pass> {
line_break_language,
)
};
- can_break_before = opportunity_with(style.word_break())
+ can_break_before = (text_context.should_wrap_lines && opportunity_with(style.word_break()))
|| (self.previous_text_breaks_all && opportunity_with(word_break::BREAK_ALL));
}
self.previous_text = None;
@@ -1982,15 +1982,15 @@ fn sequence_length<'item>(
ItemType::BlockLevelBox => break,
_ => {}
}
+ // NB: The sequence ends at its next soft wrap opportunity, such as the one after a hyphen.
+ if length > 0 && item.can_break_before {
+ break;
+ }
let style = context.style(context.style_source(item.node));
if style.text_wrap_mode() == text_wrap_mode::WRAP {
if item.type_ != ItemType::Text || item.is_collapsible_whitespace {
break;
}
- // NB: The sequence ends at its next soft wrap opportunity, such as the one after a hyphen.
- if length > 0 && item.can_break_before {
- break;
- }
if item.is_ascii_whitespace(context) {
break;
}
diff --git a/Tests/LibWeb/Layout/expected/wrap-before-nowrap-text.txt b/Tests/LibWeb/Layout/expected/wrap-before-nowrap-text.txt
new file mode 100644
index 00000000000..6829888f1db
--- /dev/null
+++ b/Tests/LibWeb/Layout/expected/wrap-before-nowrap-text.txt
@@ -0,0 +1,95 @@
+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 328 0+0+0] [BFC] children: not-inline
+ BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 310 0+0+8] children: not-inline
+ BlockContainer <div> at [8,8] [0+0+0 100 0+0+684] [0+0+0 20 0+0+10] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 8, rect: [8,8 80x10] baseline: 8
+ "aaaaaaaa"
+ TextNode <#text> (not painted)
+ InlineNode <span.nowrap> at [8,18] [0+0+0 60 0+0+0] [0+0+0 10 0+0+0]
+ frag 0 from TextNode start: 0, length: 6, rect: [8,18 60x10] baseline: 8
+ "dddddd"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [8,38] [0+0+0 100 0+0+684] [0+0+0 20 0+0+10] [BFC] children: inline
+ InlineNode <span> at [8,38] [0+0+0 80 0+0+0] [0+0+0 10 0+0+0]
+ frag 0 from TextNode start: 0, length: 8, rect: [8,38 80x10] baseline: 8
+ "aaaaaaaa"
+ TextNode <#text> (not painted)
+ InlineNode <span.nowrap> at [8,48] [0+0+0 60 0+0+0] [0+0+0 10 0+0+0]
+ frag 0 from TextNode start: 0, length: 6, rect: [8,48 60x10] baseline: 8
+ "dddddd"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [8,68] [0+0+0 100 0+0+684] [0+0+0 40 0+0+10] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 2, rect: [58,68 20x10] baseline: 8
+ "cc"
+ BlockContainer <span.float> at [8,68] floating [0+0+0 50 0+0+0] [0+0+0 30 0+0+0] [BFC] children: not-inline
+ TextNode <#text> (not painted)
+ InlineNode <span.nowrap> at [8,98] [0+0+0 60 0+0+0] [0+0+0 10 0+0+0]
+ frag 0 from TextNode start: 0, length: 6, rect: [8,98 60x10] baseline: 8
+ "dddddd"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [8,118] [0+0+0 100 0+0+684] [0+0+0 40 0+0+10] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 1, rect: [58,118 10x10] baseline: 8
+ "x"
+ frag 1 from TextNode start: 0, length: 2, rect: [58,128 20x10] baseline: 8
+ "cc"
+ BlockContainer <span.float> at [8,118] floating [0+0+0 50 0+0+0] [0+0+0 30 0+0+0] [BFC] children: not-inline
+ TextNode <#text> (not painted)
+ BreakNode <br> (not painted)
+ TextNode <#text> (not painted)
+ InlineNode <span.nowrap> at [8,148] [0+0+0 60 0+0+0] [0+0+0 10 0+0+0]
+ frag 0 from TextNode start: 0, length: 6, rect: [8,148 60x10] baseline: 8
+ "dddddd"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [8,168] [0+0+0 100 0+0+684] [0+0+0 10 0+0+10] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 8, rect: [8,168 80x10] baseline: 8
+ "aaaaaaaa"
+ TextNode <#text> (not painted)
+ InlineNode <span.nowrap> at [88,168] [0+0+0 60 0+0+0] [0+0+0 10 0+0+0]
+ frag 0 from TextNode start: 0, length: 6, rect: [88,168 60x10] baseline: 8
+ "dddddd"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [8,188] [0+0+0 40 0+0+744] [0+0+0 20 0+0+10] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 4, rect: [8,188 40x10] baseline: 8
+ "dddd"
+ TextNode <#text> (not painted)
+ InlineNode <span.nowrap> at [8,198] [0+0+0 40 0+0+0] [0+0+0 10 0+0+0]
+ frag 0 from TextNode start: 0, length: 4, rect: [8,198 40x10] baseline: 8
+ "bbbb"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [8,218] [0+0+0 40 0+0+744] [0+0+0 20 0+0+10] [BFC] children: inline
+ InlineNode <span> at [8,218] [0+0+0 40 0+0+0] [0+0+0 10 0+0+0]
+ frag 0 from TextNode start: 0, length: 4, rect: [8,218 40x10] baseline: 8
+ "dddd"
+ TextNode <#text> (not painted)
+ InlineNode <span.nowrap> at [8,228] [0+0+0 40 0+0+0] [0+0+0 10 0+0+0]
+ frag 0 from TextNode start: 0, length: 4, rect: [8,228 40x10] baseline: 8
+ "bbbb"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [8,248] [0+0+0 90 0+0+694] [0+0+0 10 0+0+10] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 6, rect: [8,248 60x10] baseline: 8
+ "aaaaaa"
+ TextNode <#text> (not painted)
+ InlineNode <span.nowrap> at [68,248] [0+0+0 30 0+0+0] [0+0+0 10 0+0+0]
+ frag 0 from TextNode start: 0, length: 3, rect: [68,248 30x10] baseline: 8
+ "ccc"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [8,268] [0+0+0 40 0+0+744] [0+0+0 20 0+0+10] [BFC] children: inline
+ InlineNode <span> at [8,268] [0+0+0 40 0+0+0] [0+0+0 10 0+0+0]
+ frag 0 from TextNode start: 0, length: 4, rect: [8,268 40x10] baseline: 8
+ "dddd"
+ TextNode <#text> (not painted)
+ InlineNode <span.nowrap> at [8,278] [0+0+0 40 0+0+0] [0+0+0 10 0+0+0]
+ frag 0 from TextNode start: 0, length: 4, rect: [8,278 40x10] baseline: 8
+ "bbbb"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [8,298] [0+0+0 40 0+0+744] [0+0+0 20 0+0+10] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 4, rect: [8,298 40x10] baseline: 8
+ "aaaa"
+ TextNode <#text> (not painted)
+ InlineNode <span.nowrap> at [8,308] [0+0+0 40 0+0+0] [0+0+0 10 0+0+0]
+ frag 0 from TextNode start: 0, length: 4, rect: [8,308 40x10] baseline: 8
+ "bbbb"
+ TextNode <#text> (not painted)
+
+SC for Viewport<#document> [0,0 800x600] (z-index: auto)
+ SC for BlockContainer<HTML> [0,0 800x328] (z-index: auto)
diff --git a/Tests/LibWeb/Layout/input/wrap-before-nowrap-text.html b/Tests/LibWeb/Layout/input/wrap-before-nowrap-text.html
new file mode 100644
index 00000000000..861602a2b36
--- /dev/null
+++ b/Tests/LibWeb/Layout/input/wrap-before-nowrap-text.html
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<style>
+ @font-face {
+ font-family: Ahem;
+ src: url("../../Ref/data/fonts/Ahem.ttf");
+ }
+ div {
+ font: 10px/10px Ahem;
+ width: 100px;
+ margin-bottom: 10px;
+ display: flow-root;
+ }
+ .float {
+ float: left;
+ width: 50px;
+ height: 30px;
+ }
+ .nowrap {
+ white-space: nowrap;
+ }
+</style>
+<div style="overflow-wrap: anywhere">aaaaaaaa<span class="nowrap">dddddd</span></div>
+<div><span style="overflow-wrap: anywhere">aaaaaaaa</span><span class="nowrap">dddddd</span></div>
+<div style="overflow-wrap: anywhere"><span class="float"></span>cc<span class="nowrap">dddddd</span></div>
+<div style="overflow-wrap: anywhere"><span class="float"></span>x<br>cc<span class="nowrap">dddddd</span></div>
+<div>aaaaaaaa<span class="nowrap">dddddd</span></div>
+<div style="width: 40px; word-break: break-all">dddd<span class="nowrap">bbbb</span></div>
+<div style="width: 40px"><span style="word-break: break-all">dddd</span><span class="nowrap">bbbb</span></div>
+<div style="width: 90px; overflow-wrap: anywhere">aaaaaa<span class="nowrap">ccc </span></div>
+<div style="width: min-content"><span style="word-break: break-all">dddd</span><span class="nowrap">bbbb</span></div>
+<div style="width: min-content; overflow-wrap: anywhere">aaaa<span class="nowrap">bbbb</span></div>
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/word-break/reference/break-boundary-2-chars-001-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/word-break/reference/break-boundary-2-chars-001-ref.html
new file mode 100644
index 00000000000..e0702afce52
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/word-break/reference/break-boundary-2-chars-001-ref.html
@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+
+ <meta charset="UTF-8">
+
+ <title>CSS Reference Test</title>
+
+ <link rel="author" title="Gérard Talbot" href="http://www.gtalbot.org/BrowserBugsSection/css21testsuite/">
+
+ <style>
+ div
+ {
+ display: inline-block;
+ font-size: 32px;
+ margin-right: 5ch;
+ white-space: pre;
+ width: 0;
+ }
+ </style>
+
+ <div>a<br>b<br>c<br>xyz<br>d<br>e<br>f</div><div>a<br>b<br>c<br>xyz<br>d<br>e<br>f</div><div>a<br>b<br>c<br>xyz<br>d<br>e<br>f</div><div>a<br>b<br>c<br>xyz<br>d<br>e<br>f</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-text/word-break/break-boundary-2-chars-001.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/word-break/break-boundary-2-chars-001.html
new file mode 100644
index 00000000000..b19e47e1f84
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/word-break/break-boundary-2-chars-001.html
@@ -0,0 +1,63 @@
+<!DOCTYPE html>
+
+ <meta charset="UTF-8">
+
+ <title>CSS Text Test: soft wrap opportunity at boundary between two characters</title>
+
+ <!--
+
+ CSS3 Text, §5.1 Line breaking details, 7th bullet, 2nd sentence
+ https://www.w3.org/TR/css-text-3/#line-break-details
+
+ "
+ For soft wrap opportunities defined by the boundary between
+ two characters, the white-space property on the nearest
+ common ancestor of the two characters controls breaking
+ "
+
+ -->
+
+ <link rel="author" title="Gérard Talbot" href="http://www.gtalbot.org/BrowserBugsSection/css21testsuite/">
+ <link rel="help" href="https://www.w3.org/TR/css-text-3/#word-break-property">
+ <link rel="help" href="https://www.w3.org/TR/css-text-3/#line-break-details">
+ <link rel="match" href="../../../../../expected/wpt-import/css/css-text/word-break/reference/break-boundary-2-chars-001-ref.html">
+
+ <meta content="This test checks that the word-break property does not apply to a run of text that is styled with 'white-space: pre' because the word-break property has no rendering effect in cases where lines of text are not allowed to break. Between the 'c' and 'x' and between the 'z' and 'd', there must be a line break because the 'white-space' declaration in effect in the nearest common ancestor of each of these pairs of two characters allows text wrapping." name="assert">
+
+ <style>
+ div
+ {
+ display: inline-block;
+ font-size: 32px;
+ margin-right: 5ch;
+ width: 0;
+ word-break: break-all;
+ }
+
+ span
+ {
+ white-space: pre;
+ }
+
+ div#first-sub-test
+ {
+ white-space: normal;
+ }
+
+ div#second-sub-test
+ {
+ white-space: pre-wrap;
+ }
+
+ div#third-sub-test
+ {
+ white-space: break-spaces;
+ }
+
+ div#fourth-sub-test
+ {
+ white-space: pre-line;
+ }
+ </style>
+
+ <div id="first-sub-test">abc<span>xyz</span>def</div><div id="second-sub-test">abc<span>xyz</span>def</div><div id="third-sub-test">abc<span>xyz</span>def</div><div id="fourth-sub-test">abc<span>xyz</span>def</div>