Skip to content

Truly independent web browser

LibWeb: Grow balanced columns when lines break against orphans or widows

Aliaksandr Kalenike433efcd7deb
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index fc1b67db188..4e65be93820 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -3170,6 +3170,12 @@ impl<'pass> BlockFormattingContext<'pass> {
}
}
+ pub(crate) fn note_violated_break_avoidance(&self) {
+ if let Some(fragmentainer) = self.run.fragmentainer.as_deref() {
+ fragmentainer.outcome_mut().violates_break_avoidance = true;
+ }
+ }
+
pub(crate) fn note_unbreakable_line_root(&self, line_root: Node, lines_block_end_in_root: CssPixels) {
self.unbreakable_line_roots.borrow_mut().insert(line_root);
if line_root != self.run.box_ {
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
index 61aad69cf11..efb885c57ed 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
@@ -2492,11 +2492,15 @@ impl<'context> InlineFormattingContext<'context> {
&& !fragmentation.break_before_is_avoided
{
self.parent.request_early_break_before(self.containing_block);
+ } else {
+ self.parent.note_violated_break_avoidance();
}
} else if !stopped_at_line_limit && content_line_count.saturating_sub(stop) < widows {
let stop_keeping_widows = content_line_count.saturating_sub(widows);
if stop_keeping_widows >= orphans {
stop = stop_keeping_widows;
+ } else {
+ self.parent.note_violated_break_avoidance();
}
}
}
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-break/widows-orphans-002.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-break/widows-orphans-002.html
new file mode 100644
index 00000000000..7a663056161
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-break/widows-orphans-002.html
@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>CSS Fragmentation level 3 Test: widows and line boxes</title>
+<link rel="author" title="Florian Rivoal" href="https://florian.rivoal.net/">
+<link rel="help" href="https://drafts.csswg.org/css-break/#widows-orphans">
+<link rel="match" href="../../../../expected/wpt-import/css/css-break/reference/widows-orphans-001-ref.html">
+<meta name="assert" content="The widows property affects block containers that establish an inline formatting context">
+<style>
+div p { margin: 0; }
+
+div {
+ columns: 4;
+ column-gap: 0;
+ position: absolute; /* shrinkwrap. Using an explicit width in 'ch' with a monospace font would be fine as well if all UAs supported 'ch', but some don't. */
+ column-fill: balance-all; /* to make sure it balances as well on paged media */
+}
+div p {
+ orphans: 1; /* to avoid interference */
+ widows: 2;
+}
+
+/* The lines of each paragraphs should be kept together if the widows property applies,
+ or spread over the 4 columns if it fails to,
+ or all above one-another in a single column if multicol doesn't work. */
+
+</style>
+<body>
+ <p>Test passes if there are 4 “X” characters <strong>arranged in a 2 by 2 square</strong> below.
+
+ <div>
+ <p>X<br>X
+ <p>X<br>X
+ </div>
+</body>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-break/widows-orphans-004.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-break/widows-orphans-004.html
new file mode 100644
index 00000000000..d5e7f7bcbfe
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-break/widows-orphans-004.html
@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>CSS Fragmentation level 3 Test: orphans and line boxes</title>
+<link rel="author" title="Florian Rivoal" href="https://florian.rivoal.net/">
+<link rel="help" href="https://drafts.csswg.org/css-break/#widows-orphans">
+<link rel="match" href="../../../../expected/wpt-import/css/css-break/reference/widows-orphans-001-ref.html">
+<meta name="assert" content="The orphans property affects block containers that establish an inline formatting context">
+<style>
+div p { margin: 0; }
+
+div {
+ columns: 4;
+ column-gap: 0;
+ position: absolute; /* shrinkwrap. Using an explicit width in 'ch' with a monospace font would be fine as well if all UAs supported 'ch', but some don't. */
+ column-fill: balance-all; /* to make sure it balances as well on paged media */
+}
+div p {
+ widows: 1; /* to avoid interference */
+ orphans: 2;
+}
+
+/* The lines of each paragraphs should be kept together if the orphans property applies,
+ or spread over the 4 columns if it fails to,
+ or all above one-another in a single column if multicol doesn't work. */
+
+</style>
+<body>
+ <p>Test passes if there are 4 “X” characters <strong>arranged in a 2 by 2 square</strong> below.
+
+ <div>
+ <p>X<br>X
+ <p>X<br>X
+ </div>
+</body>