Skip to content

Truly independent web browser

LibUnicode+LibWeb: Allow breaks after atomic inlines before punctuation

Tim Ledbettere2b69f8c2489
diff --git a/Libraries/LibUnicode/CharacterTypes.cpp b/Libraries/LibUnicode/CharacterTypes.cpp
index 1cbcd4a210d..bac5e1a3c0a 100644
--- a/Libraries/LibUnicode/CharacterTypes.cpp
+++ b/Libraries/LibUnicode/CharacterTypes.cpp
@@ -565,6 +565,12 @@ LineBreakClass line_break_class(u32 code_point)
return LineBreakClass::PostfixNumeric;
case U_LB_HYPHEN:
return LineBreakClass::Hyphen;
+ case U_LB_GLUE:
+ return LineBreakClass::Glue;
+ case U_LB_WORD_JOINER:
+ return LineBreakClass::WordJoiner;
+ case U_LB_ZWJ:
+ return LineBreakClass::ZeroWidthJoiner;
default:
return LineBreakClass::Other;
}
diff --git a/Libraries/LibUnicode/CharacterTypes.h b/Libraries/LibUnicode/CharacterTypes.h
index bdfdc6a3de3..a9c4180678a 100644
--- a/Libraries/LibUnicode/CharacterTypes.h
+++ b/Libraries/LibUnicode/CharacterTypes.h
@@ -91,6 +91,9 @@ enum class LineBreakClass {
PrefixNumeric, // PR
PostfixNumeric, // PO
Hyphen, // HY
+ Glue, // GL
+ WordJoiner, // WJ
+ ZeroWidthJoiner, // ZWJ
Other,
};
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
index 34ac5595a51..bc708e49c42 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
@@ -1267,29 +1267,29 @@ impl<'pass> InlineContentPreparer<'pass> {
&& let Some(previous_text) = self.previous_text.take()
&& wraps_at_boundary
{
- let style = text_context.style;
- let line_break_language = if style.line_break() == line_break::LOOSE {
- self.pass.arena().text_line_break_language(text_node)
+ if previous_node.is_some_and(|node| !self.facts(node).is_text_node()) {
+ can_break_before = text_chunker::soft_wrap_opportunity_after_atomic_inline(&full_text[chunk.start..]);
} else {
- text_chunker::LineBreakLanguage::Other
- };
- let opportunity_with = |word_break| {
- text_chunker::soft_wrap_opportunity_between(
- previous_text,
- &full_text[chunk.start..],
- self.code_unit_before_previous_text,
- self.regional_indicators_before_text,
- word_break,
- style.line_break(),
- line_break_language,
- )
- };
- can_break_before = opportunity_with(style.word_break())
- || (self.previous_text_breaks_all && opportunity_with(word_break::BREAK_ALL))
- // NB: UAX #14 keeps U+00A0 NO-BREAK SPACE with U+FFFC, but CSS Text has a soft wrap opportunity
- // between an atomic inline and it.
- || (std::ptr::eq(previous_text, OBJECT_REPLACEMENT_CHARACTER.as_slice())
- && full_text.get(chunk.start) == Some(&0x00A0));
+ let style = text_context.style;
+ let line_break_language = if style.line_break() == line_break::LOOSE {
+ self.pass.arena().text_line_break_language(text_node)
+ } else {
+ text_chunker::LineBreakLanguage::Other
+ };
+ let opportunity_with = |word_break| {
+ text_chunker::soft_wrap_opportunity_between(
+ previous_text,
+ &full_text[chunk.start..],
+ self.code_unit_before_previous_text,
+ self.regional_indicators_before_text,
+ word_break,
+ style.line_break(),
+ line_break_language,
+ )
+ };
+ can_break_before = opportunity_with(style.word_break())
+ || (self.previous_text_breaks_all && opportunity_with(word_break::BREAK_ALL));
+ }
}
if !keeps_boundary {
self.previous_text = None;
@@ -1380,9 +1380,8 @@ impl<'pass> InlineContentPreparer<'pass> {
// NO-BREAK SPACE. However, with the exception of U+00A0 NO-BREAK SPACE, there must be no soft wrap
// opportunity between atomic inlines and adjacent characters belonging to the Unicode GL, WJ, or ZWJ line
// breaking classes."
- // NB: The line breaker allows the break before the box. For the one after it, the box stands in the text
- // before what follows it as U+FFFC OBJECT REPLACEMENT CHARACTER, which UAX #14 lets text break from
- // except for those classes, and which the text's own line breaking properties then decide.
+ // NB: The line breaker allows the break before the box, and the text after it looks for the one after it.
+ // The box stands in the text before that text as U+FFFC OBJECT REPLACEMENT CHARACTER.
self.previous_text = Some(&OBJECT_REPLACEMENT_CHARACTER);
self.previous_node = Some(node);
self.previous_text_breaks_all = false;
diff --git a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs b/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
index 7b2068ba80a..4a3b0d5da0a 100644
--- a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
+++ b/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
@@ -184,6 +184,9 @@ mod line_break_class {
pub(super) const PREFIX_NUMERIC: u8 = 12;
pub(super) const POSTFIX_NUMERIC: u8 = 13;
pub(super) const HYPHEN: u8 = 14;
+ pub(super) const GLUE: u8 = 15;
+ pub(super) const WORD_JOINER: u8 = 16;
+ pub(super) const ZERO_WIDTH_JOINER: u8 = 17;
}
fn line_break_class(code_point: u32) -> u8 {
@@ -918,6 +921,31 @@ pub(crate) fn soft_wrap_opportunity_between(
LineBreaker::new(&text, word_break, line_break, line_break_language).is_at_line_break_opportunity(boundary)
}
+/// Whether there is a soft wrap opportunity between an atomic inline and the text after it.
+pub(crate) fn soft_wrap_opportunity_after_atomic_inline(after: &[u16]) -> bool {
+ let Some(&first) = after.first() else {
+ return false;
+ };
+ // NB: Whitespace gives its own soft wrap opportunities.
+ if first <= 0x7f && code_point_is_ascii_space(u32::from(first)) {
+ return false;
+ }
+ // https://drafts.csswg.org/css-text-3/#line-break-details
+ // [...] with the exception of U+00A0 NO-BREAK SPACE, there must be no soft wrap opportunity between atomic inlines
+ // and adjacent characters belonging to the Unicode GL, WJ, or ZWJ line breaking classes.
+ // AD-HOC: A combining mark stays with the atomic inline, as UAX #14 keeps it with any character before it, rather
+ // than start the next line on its own.
+ let code_point = code_point_at(after, 0);
+ code_point == 0x00A0
+ || !matches!(
+ line_break_class(code_point),
+ line_break_class::GLUE
+ | line_break_class::WORD_JOINER
+ | line_break_class::ZERO_WIDTH_JOINER
+ | line_break_class::COMBINING_MARK
+ )
+}
+
/// 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, line_break: u8) -> bool {
use line_break_class::*;
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/line-breaking/reference/line-breaking-atomic-009-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/line-breaking/reference/line-breaking-atomic-009-ref.html
new file mode 100644
index 00000000000..1300284c460
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/line-breaking/reference/line-breaking-atomic-009-ref.html
@@ -0,0 +1,14 @@
+<!doctype html>
+<html>
+<meta charset="utf-8">
+<title>Test reference</title>
+<link rel=author title="Florian Rivoal" href="https://florian.rivoal.net">
+<style>
+div {
+ font-size: 4em;
+}
+</style>
+
+<p>Test passes if there the “:” is under the “A” in the text below.
+
+<div>A<br>:</div>
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/line-breaking/reference/line-breaking-replaced-006-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/line-breaking/reference/line-breaking-replaced-006-ref.html
new file mode 100644
index 00000000000..44ea76621c4
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/line-breaking/reference/line-breaking-replaced-006-ref.html
@@ -0,0 +1,16 @@
+<!doctype html>
+<html>
+<meta charset="utf-8">
+<title>Test reference</title>
+<link rel=author title="Florian Rivoal" href="https://florian.rivoal.net">
+<link rel="stylesheet" type="text/css" href="../../../../fonts/ahem.css" />
+<style>
+div {
+ font: 50px/1 Ahem;
+ color: green;
+}
+</style>
+
+<p>Test passes if there is a filled green square and <strong>no red</strong>.
+
+<div><img src="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" width=50><br>:</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-text/line-breaking/line-breaking-atomic-009.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/line-breaking/line-breaking-atomic-009.html
new file mode 100644
index 00000000000..acc6598b5dd
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/line-breaking/line-breaking-atomic-009.html
@@ -0,0 +1,21 @@
+<!doctype html>
+<html>
+<meta charset="utf-8">
+<title>CSS Text — atomic inline and punctuation</title>
+<meta name=assert content="There's a soft wrap opportunity between an atomic inline following punctuation">
+<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/line-breaking/reference/line-breaking-atomic-009-ref.html">
+<link rel=author title="Florian Rivoal" href="https://florian.rivoal.net">
+<style>
+div {
+ font-size: 4em;
+ width: 0;
+}
+span {
+ display: inline-block;
+}
+</style>
+
+<p>Test passes if there the “:” is under the “A” in the text below.
+
+<div><span>A</span>:</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-text/line-breaking/line-breaking-replaced-006.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/line-breaking/line-breaking-replaced-006.html
new file mode 100644
index 00000000000..f829bf3897c
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/line-breaking/line-breaking-replaced-006.html
@@ -0,0 +1,29 @@
+<!doctype html>
+<html>
+<meta charset="utf-8">
+<title>CSS Text — replaced element and punctuation</title>
+<meta name=assert content="There's a soft wrap opportunity between a replaced element punctuation">
+<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/line-breaking/reference/line-breaking-replaced-006-ref.html">
+<link rel=author title="Florian Rivoal" href="https://florian.rivoal.net">
+<link rel="stylesheet" type="text/css" href="../../../fonts/ahem.css" />
+<style>
+div {
+ font: 50px/1 Ahem;
+}
+.red {
+ position: absolute;
+ z-index: -1;
+ color: red;
+ white-space: pre;
+}
+.test {
+ color: green;
+ width: 0;
+}
+</style>
+
+<p>Test passes if there is a filled green square and <strong>no red</strong>.
+
+<div class=red><img src="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" width=50><br>:</div>
+<div class=test><img src="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" width=50>:</div>