Skip to content

Truly independent web browser

LibWeb: Allow loose line breaks before hyphens after `break-all` letters

Tim Ledbetter14e835c80eaf
diff --git a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs b/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
index c6ff4db3961..fc13785230d 100644
--- a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
+++ b/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
@@ -851,7 +851,7 @@ pub(crate) fn soft_wrap_opportunity_between(
}
/// Whether `word-break: break-all` has a soft wrap opportunity between two characters that `word-break: normal` lacks.
-fn break_all_adds_soft_wrap_opportunity(before: u32, after: u32) -> bool {
+fn break_all_adds_soft_wrap_opportunity(before: u32, after: u32, line_break: u8) -> bool {
use line_break_class::*;
// https://drafts.csswg.org/css-text-3/#valdef-word-break-break-all
// Breaking is allowed within “words”: specifically, in addition to soft wrap opportunities allowed for normal, any
@@ -879,7 +879,15 @@ fn break_all_adds_soft_wrap_opportunity(before: u32, after: u32) -> bool {
is_treated_as_ideographic(after_class),
) {
(true, true) => true,
- (true, false) => matches!(after_class, OPEN_PUNCTUATION | PREFIX_NUMERIC),
+ (true, false) => {
+ matches!(after_class, OPEN_PUNCTUATION | PREFIX_NUMERIC)
+ // https://drafts.csswg.org/css-text-3/#line-break-property
+ // The following breaks are allowed for loose line breaking if the preceding character belongs to the
+ // Unicode line breaking class ID [UAX14] (including when the preceding character is treated as ID due
+ // to word-break: break-all), and are otherwise forbidden:
+ // - breaks before hyphens: ‐ U+2010, – U+2013
+ || (line_break == line_break::LOOSE && matches!(after, 0x2010 | 0x2013))
+ }
(false, true) => {
matches!(
before_class,
@@ -895,6 +903,7 @@ struct LineBreaker<'text> {
text: &'text [u16],
line_segmenter: LineSegmenter<'text>,
word_break: u8,
+ line_break: u8,
}
impl<'text> LineBreaker<'text> {
@@ -903,6 +912,7 @@ impl<'text> LineBreaker<'text> {
text,
line_segmenter: LineSegmenter::new(text, line_break, line_break_language),
word_break,
+ line_break,
}
}
@@ -935,7 +945,11 @@ impl<'text> LineBreaker<'text> {
return false;
}
if let Some(previous_code_point) = get_previous_code_point()
- && break_all_adds_soft_wrap_opportunity(previous_code_point, code_point_at(self.text, index))
+ && break_all_adds_soft_wrap_opportunity(
+ previous_code_point,
+ code_point_at(self.text, index),
+ self.line_break,
+ )
{
return true;
}
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/line-break/reference/line-break-loose-hyphens-002-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/line-break/reference/line-break-loose-hyphens-002-ref.html
new file mode 100644
index 00000000000..a542fe86ebc
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/line-break/reference/line-break-loose-hyphens-002-ref.html
@@ -0,0 +1,19 @@
+<!doctype html>
+<html lang=en>
+<meta charset=utf-8>
+<title>Test reference</title>
+<link rel=author title="Florian Rivoal" href="https://florian.rivoal.net">
+<style>
+div {
+ font-family: monospace;
+ width: 2ch;
+ white-space: pre;
+ border: solid cyan;
+}
+</style>
+
+<p>The test passes if the second line in each box below starts with a hyphen.
+(Note: the hyphens in each box do no need to be the same size.)
+
+<div>aa<br>‐a</div>
+<div>aa<br>–a</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-text/line-break/line-break-loose-hyphens-002.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/line-break/line-break-loose-hyphens-002.html
new file mode 100644
index 00000000000..4613771489a
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/line-break/line-break-loose-hyphens-002.html
@@ -0,0 +1,24 @@
+<!doctype html>
+<html lang=en>
+<meta charset=utf-8>
+<title>CSS test: hyphens and line-break:loose</title>
+<link rel=author title="Florian Rivoal" href="https://florian.rivoal.net">
+<link rel=help href="https://drafts.csswg.org/css-text-3/#propdef-line-break">
+<link rel=help href="https://drafts.csswg.org/css-text-3/#word-break-property">
+<link rel=match href="../../../../../expected/wpt-import/css/css-text/line-break/reference/line-break-loose-hyphens-002-ref.html">
+<meta name=assert content="breaks before U+2010 or U+2013 when line-break is loose are allowed if the preceding character is treated as having class ID due to word-break:break-all.">
+<style>
+div {
+ font-family: monospace;
+ width: 2ch;
+ line-break: loose;
+ word-break: break-all;
+ border: solid cyan;
+}
+</style>
+
+<p>The test passes if the second line in each box below starts with a hyphen.
+(Note: the hyphens in each box do no need to be the same size.)
+
+<div>aa‐a</div>
+<div>aa–a</div>