Skip to content

Truly independent web browser

LibWeb: Allow breaks next to punctuation with `word-break: break-all`

Tim Ledbetter86675840f58b
diff --git a/Libraries/LibUnicode/CharacterTypes.cpp b/Libraries/LibUnicode/CharacterTypes.cpp
index a095a643adc..ae672c68bea 100644
--- a/Libraries/LibUnicode/CharacterTypes.cpp
+++ b/Libraries/LibUnicode/CharacterTypes.cpp
@@ -550,6 +550,21 @@ LineBreakClass line_break_class(u32 code_point)
return LineBreakClass::CombiningMark;
case U_LB_CONDITIONAL_JAPANESE_STARTER:
return LineBreakClass::ConditionalJapaneseStarter;
+ case U_LB_OPEN_PUNCTUATION:
+ return LineBreakClass::OpenPunctuation;
+ case U_LB_CLOSE_PUNCTUATION:
+ case U_LB_CLOSE_PARENTHESIS:
+ return LineBreakClass::ClosePunctuation;
+ case U_LB_BREAK_SYMBOLS:
+ return LineBreakClass::BreakSymbols;
+ case U_LB_INFIX_NUMERIC:
+ return LineBreakClass::InfixNumericSeparator;
+ case U_LB_PREFIX_NUMERIC:
+ return LineBreakClass::PrefixNumeric;
+ case U_LB_POSTFIX_NUMERIC:
+ return LineBreakClass::PostfixNumeric;
+ case U_LB_HYPHEN:
+ return LineBreakClass::Hyphen;
default:
return LineBreakClass::Other;
}
diff --git a/Libraries/LibUnicode/CharacterTypes.h b/Libraries/LibUnicode/CharacterTypes.h
index b042b29b16e..8670fe2ab49 100644
--- a/Libraries/LibUnicode/CharacterTypes.h
+++ b/Libraries/LibUnicode/CharacterTypes.h
@@ -84,6 +84,13 @@ enum class LineBreakClass {
CombiningMark, // CM
ConditionalJapaneseStarter, // CJ
HangulJamo, // JL, JV, JT
+ OpenPunctuation, // OP
+ ClosePunctuation, // CL, CP
+ BreakSymbols, // SY
+ InfixNumericSeparator, // IS
+ PrefixNumeric, // PR
+ PostfixNumeric, // PO
+ Hyphen, // HY
Other,
};
diff --git a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs b/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
index 7534f0f0311..c6ff4db3961 100644
--- a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
+++ b/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
@@ -176,6 +176,13 @@ mod line_break_class {
pub(super) const COMBINING_MARK: u8 = 5;
pub(super) const CONDITIONAL_JAPANESE_STARTER: u8 = 6;
pub(super) const HANGUL_JAMO: u8 = 7;
+ pub(super) const OPEN_PUNCTUATION: u8 = 8;
+ pub(super) const CLOSE_PUNCTUATION: u8 = 9;
+ pub(super) const BREAK_SYMBOLS: u8 = 10;
+ pub(super) const INFIX_NUMERIC_SEPARATOR: u8 = 11;
+ pub(super) const PREFIX_NUMERIC: u8 = 12;
+ pub(super) const POSTFIX_NUMERIC: u8 = 13;
+ pub(super) const HYPHEN: u8 = 14;
}
fn line_break_class(code_point: u32) -> u8 {
@@ -843,6 +850,46 @@ pub(crate) fn soft_wrap_opportunity_between(
LineBreaker::new(&text, word_break, line_break, line_break_language).is_at_line_break_opportunity(boundary)
}
+/// 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 {
+ 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
+ // typographic letter units (and any typographic character units resolving to the NU (“numeric”), AL
+ // (“alphabetic”), or SA (“Southeast Asian”) line breaking classes [UAX14]) are instead treated as ID (“ideographic
+ // characters”) for the purpose of line-breaking.
+ let is_treated_as_ideographic = |class| {
+ matches!(
+ class,
+ ALPHABETIC
+ | NUMERIC
+ | AMBIGUOUS
+ | COMPLEX_CONTEXT
+ | IDEOGRAPHIC
+ | CONDITIONAL_JAPANESE_STARTER
+ | HANGUL_JAMO
+ )
+ };
+ let before_class = line_break_class(before);
+ let after_class = line_break_class(after);
+ // NB: Treating a character as ID only adds soft wrap opportunities next to the classes that UAX #14 keeps with AL,
+ // HL or NU but not with ID.
+ match (
+ is_treated_as_ideographic(before_class),
+ is_treated_as_ideographic(after_class),
+ ) {
+ (true, true) => true,
+ (true, false) => matches!(after_class, OPEN_PUNCTUATION | PREFIX_NUMERIC),
+ (false, true) => {
+ matches!(
+ before_class,
+ CLOSE_PUNCTUATION | BREAK_SYMBOLS | INFIX_NUMERIC_SEPARATOR | POSTFIX_NUMERIC
+ ) || (before_class == HYPHEN && after_class == NUMERIC)
+ }
+ (false, false) => false,
+ }
+}
+
/// Finds the soft wrap opportunities of a text.
struct LineBreaker<'text> {
text: &'text [u16],
@@ -887,21 +934,8 @@ impl<'text> LineBreaker<'text> {
if index >= self.text.len() {
return false;
}
- use line_break_class::*;
- let breaks_anywhere = |code_point| {
- matches!(
- line_break_class(code_point),
- ALPHABETIC
- | NUMERIC
- | COMPLEX_CONTEXT
- | IDEOGRAPHIC
- | CONDITIONAL_JAPANESE_STARTER
- | HANGUL_JAMO
- )
- };
if let Some(previous_code_point) = get_previous_code_point()
- && breaks_anywhere(previous_code_point)
- && breaks_anywhere(code_point_at(self.text, index))
+ && break_all_adds_soft_wrap_opportunity(previous_code_point, code_point_at(self.text, index))
{
return true;
}
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-text/word-break/word-break-break-all-024.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/word-break/word-break-break-all-024.html
new file mode 100644
index 00000000000..45388813592
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/word-break/word-break-break-all-024.html
@@ -0,0 +1,35 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>CSS Text Test: word-break: break-all</title>
+<link rel="author" title="Javier Fernandez Garcia-Boente" href="mailto:[email protected]">
+<link rel="help" title="5.2. Breaking Rules for Letters: the word-break property" href="https://drafts.csswg.org/css-text-3/#word-break-property">
+<link rel="help" href="https://drafts.csswg.org/css-text-3/#valdef-word-break-break-all">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-text/word-break/reference/word-break-break-all-010-ref.html">
+<meta name="assert" content="break-all breaks before the first backslash character because UAX14 rules forbid to break after PR class.">
+<link rel="stylesheet" type="text/css" href="../../../fonts/ahem.css" />
+<style>
+div {
+ position: relative;
+ font-size: 20px;
+ font-family: Ahem;
+ line-height: 1em;
+}
+.red {
+ position: absolute;
+ width: 100px;
+ height: 100px;
+ background: green;
+ color: red;
+ z-index: -1;
+}
+.test {
+ color: green;
+ width: 4ch;
+ word-break: break-all;
+}
+</style>
+<body>
+ <p>Test passes if there is a <strong>filled green square</strong> and <strong>no red</strong>.</p>
+ <div class="red">XXX<br>XX</div>
+ <div class="test">XXX\X</div>
+</body>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-text/word-break/word-break-break-all-025.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/word-break/word-break-break-all-025.html
new file mode 100644
index 00000000000..4de0ca33bfd
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/word-break/word-break-break-all-025.html
@@ -0,0 +1,35 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>CSS Text Test: word-break: break-all</title>
+<link rel="author" title="Javier Fernandez Garcia-Boente" href="mailto:[email protected]">
+<link rel="help" title="5.2. Breaking Rules for Letters: the word-break property" href="https://drafts.csswg.org/css-text-3/#word-break-property">
+<link rel="help" href="https://drafts.csswg.org/css-text-3/#valdef-word-break-break-all">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-text/word-break/reference/word-break-break-all-010-ref.html">
+<meta name="assert" content="break-all breaks before the first backslash character following UAX14 rules about PR class symbols.">
+<link rel="stylesheet" type="text/css" href="../../../fonts/ahem.css" />
+<style>
+div {
+ position: relative;
+ font-size: 20px;
+ font-family: Ahem;
+ line-height: 1em;
+}
+.red {
+ position: absolute;
+ width: 100px;
+ height: 100px;
+ background: green;
+ color: red;
+ z-index: -1;
+}
+.test {
+ color: green;
+ width: 3ch;
+ word-break: break-all;
+}
+</style>
+<body>
+ <p>Test passes if there is a <strong>filled green square</strong> and <strong>no red</strong>.</p>
+ <div class="red">XXX<br>XX</div>
+ <div class="test">XXX\X</div>
+</body>
diff --git a/Tests/LibWeb/Text/expected/css/word-break-soft-wrap-opportunities.txt b/Tests/LibWeb/Text/expected/css/word-break-soft-wrap-opportunities.txt
index d29cadb29c0..a06cf400079 100644
--- a/Tests/LibWeb/Text/expected/css/word-break-soft-wrap-opportunities.txt
+++ b/Tests/LibWeb/Text/expected/css/word-break-soft-wrap-opportunities.txt
@@ -1,3 +1,21 @@
break-all "မာစာ": "မာ|စာ"
break-all "क्" + "ष": "क्ष"
+break-all "a+a+a": "a|+a|+a"
+break-all "a$a": "a|$a"
+break-all "a\\a": "a|\\a"
+break-all "a%a": "a%|a"
+break-all "a.a": "a.|a"
+break-all "a,a": "a,|a"
+break-all "a:a": "a:|a"
+break-all "a/a": "a/|a"
+break-all "a(a)a": "a|(a)|a"
+break-all "a[a]a": "a|[a]|a"
+break-all "1.5": "1.|5"
+break-all "1%1$1": "1%|1|$1"
+break-all "(-1)": "(-|1)"
+break-all "a-a": "a-|a"
+break-all "a!a": "a!|a"
+break-all "a\"a": "a\"a"
+break-all "a a": "a a"
+break-all "a»a": "a»a"
keep-all "フォフ": "フォフ"
diff --git a/Tests/LibWeb/Text/input/css/word-break-soft-wrap-opportunities.html b/Tests/LibWeb/Text/input/css/word-break-soft-wrap-opportunities.html
index 1a8e297b83e..84183acf900 100644
--- a/Tests/LibWeb/Text/input/css/word-break-soft-wrap-opportunities.html
+++ b/Tests/LibWeb/Text/input/css/word-break-soft-wrap-opportunities.html
@@ -40,6 +40,24 @@
["break-all", "\u1019\u102c\u1005\u102c"],
// A grapheme cluster split between two text nodes.
["break-all", "\u0915\u094d\u0937", 2],
+ ["break-all", "a+a+a"],
+ ["break-all", "a$a"],
+ ["break-all", "a\\a"],
+ ["break-all", "a%a"],
+ ["break-all", "a.a"],
+ ["break-all", "a,a"],
+ ["break-all", "a:a"],
+ ["break-all", "a/a"],
+ ["break-all", "a(a)a"],
+ ["break-all", "a[a]a"],
+ ["break-all", "1.5"],
+ ["break-all", "1%1$1"],
+ ["break-all", "(-1)"],
+ ["break-all", "a-a"],
+ ["break-all", "a!a"],
+ ["break-all", 'a"a'],
+ ["break-all", "a\u00a0a"],
+ ["break-all", "a\u00bba"],
["keep-all", "\u30d5\u30a9\u30d5"],
]) {
text.style.wordBreak = wordBreak;