Truly independent web browser
LibWeb: Select automatic quotation marks by content language
sideshowbarker9f4874160a72
diff --git a/Libraries/LibUnicode/ICU.cpp b/Libraries/LibUnicode/ICU.cppindex b27d88a1ef3..ceaa82322b6 100644--- a/Libraries/LibUnicode/ICU.cpp+++ b/Libraries/LibUnicode/ICU.cpp@@ -87,6 +87,29 @@ LocaleData::LocaleData(icu::Locale locale){}+icu::UnicodeString const& LocaleData::quotation_mark(ULocaleDataDelimiterType mark)+{+ if (!m_quotation_marks.has_value()) {+ m_quotation_marks.emplace();+ UErrorCode status = U_ZERO_ERROR;+ icu::LocalULocaleDataPointer data { ulocdata_open(m_locale.getName(), &status) };+ if (icu_success(status) && status != U_USING_DEFAULT_WARNING) {+ for (size_t i = 0; i < m_quotation_marks->size(); ++i) {+ status = U_ZERO_ERROR;+ auto delimiter = static_cast<ULocaleDataDelimiterType>(i);+ auto length = ulocdata_getDelimiter(data.getAlias(), delimiter, nullptr, 0, &status);+ if ((icu_failure(status) && status != U_BUFFER_OVERFLOW_ERROR) || length <= 0)+ continue;+ auto& string = (*m_quotation_marks)[i];+ status = U_ZERO_ERROR;+ ulocdata_getDelimiter(data.getAlias(), delimiter, string.getBuffer(length), length, &status);+ string.releaseBuffer(icu_success(status) ? length : 0);+ }+ }+ }+ return (*m_quotation_marks)[mark];+}+Optional<Utf16String> LocaleData::canonicalize(StringView locale){auto locale_data = LocaleData::for_locale(locale);diff --git a/Libraries/LibUnicode/ICU.h b/Libraries/LibUnicode/ICU.hindex d8543ba0ee8..4a1caf427bd 100644--- a/Libraries/LibUnicode/ICU.h+++ b/Libraries/LibUnicode/ICU.h@@ -6,6 +6,7 @@#pragma once+#include <AK/Array.h>#include <AK/Optional.h>#include <AK/OwnPtr.h>#include <AK/SourceLocation.h>@@ -21,6 +22,7 @@#include <unicode/stringpiece.h>#include <unicode/uiter.h>#include <unicode/uloc.h>+#include <unicode/ulocdata.h>#include <unicode/unistr.h>#include <unicode/utypes.h>#include <unicode/uversion.h>@@ -61,6 +63,7 @@ public:Optional<icu::DateTimePatternGenerator&> date_time_pattern_generator();icu::TimeZoneNames& time_zone_names();+ icu::UnicodeString const& quotation_mark(ULocaleDataDelimiterType);Optional<DigitalFormat> const& digital_format() { return m_digital_format; }void set_digital_format(DigitalFormat digital_format) { m_digital_format = move(digital_format); }@@ -77,6 +80,7 @@ private:OwnPtr<icu::DateTimePatternGenerator> m_date_time_pattern_generator;OwnPtr<icu::TimeZoneNames> m_time_zone_names;Optional<DigitalFormat> m_digital_format;+ Optional<Array<icu::UnicodeString, 4>> m_quotation_marks;};// Cache entries and their ICU objects are confined to the calling thread.diff --git a/Libraries/LibUnicode/Locale.cpp b/Libraries/LibUnicode/Locale.cppindex c2d95e13809..41981dd2e84 100644--- a/Libraries/LibUnicode/Locale.cpp+++ b/Libraries/LibUnicode/Locale.cpp@@ -901,3 +901,15 @@ extern "C" void unicode_default_locale(UnicodeTextMappingOutput output)for (size_t i = 0; i < locale.length_in_code_units(); ++i)destination[i] = locale.code_unit_at(i);}++extern "C" size_t unicode_quotation_mark(u16 const* language, size_t length, bool open, bool alternate, u16 const** result)+{+ auto data = Unicode::LocaleData::for_locale(Utf16View { reinterpret_cast<char16_t const*>(language), length });+ if (!data.has_value())+ return 0;+ auto type = alternate ? (open ? ULOCDATA_ALT_QUOTATION_START : ULOCDATA_ALT_QUOTATION_END)+ : (open ? ULOCDATA_QUOTATION_START : ULOCDATA_QUOTATION_END);+ auto const& mark = data->quotation_mark(type);+ *result = reinterpret_cast<u16 const*>(mark.getBuffer());+ return mark.length();+}diff --git a/Libraries/LibUnicode/TextMapping.h b/Libraries/LibUnicode/TextMapping.hindex 59560cdc649..7e22dcbf45f 100644--- a/Libraries/LibUnicode/TextMapping.h+++ b/Libraries/LibUnicode/TextMapping.h@@ -40,4 +40,5 @@ size_t unicode_disambiguated_time_zone_offsets(u16 const* time_zone, size_t lengbool unicode_time_zone_transition(u16 const* time_zone, size_t length, i64 seconds, u32 nanoseconds, u8 direction, bool include_given_time, u8 transition_rule, i64* transition_milliseconds);bool unicode_time_zone_display_name(u8 const* locale, size_t locale_length, u8 const* time_zone, size_t time_zone_length, bool in_dst, double time, UnicodeTextMappingOutput);void unicode_default_locale(UnicodeTextMappingOutput);+size_t unicode_quotation_mark(u16 const* language, size_t length, bool open, bool alternate, u16 const** result);}diff --git a/Libraries/LibWeb/Rust/src/layout/generated_content.rs b/Libraries/LibWeb/Rust/src/layout/generated_content.rsindex 17ea3c5ae9b..aa90761d9aa 100644--- a/Libraries/LibWeb/Rust/src/layout/generated_content.rs+++ b/Libraries/LibWeb/Rust/src/layout/generated_content.rs@@ -242,21 +242,48 @@ fn quotes_data(value: Option<&StyleValueData>) -> QuotesData<'_> {}}-fn quote_string<'a>(quotes: &QuotesData<'a>, open: bool, depth: u32) -> &'a [u16] {- match quotes {+unsafe extern "C" {+ fn unicode_quotation_mark(+ language: *const u16,+ length: usize,+ open: bool,+ alternate: bool,+ result: *mut *const u16,+ ) -> usize;+}++#[cfg(test)]+#[unsafe(export_name = "unicode_quotation_mark")]+extern "C" fn quotation_mark_test_stub(_: *const u16, _: usize, _: bool, _: bool, _: *mut *const u16) -> usize {+ 0+}++fn append_quote(text: &mut Vec<u16>, quotes: &QuotesData<'_>, open: bool, depth: u32, language: &[u16]) {+ let mark: &[u16] = match quotes {QuotesData::None => &[],- // FIXME: "A typographically appropriate used value for quotes is automatically chosen by the UA- // based on the content language of the element and/or its parent."- QuotesData::Auto => match (open, depth) {- (true, 0) => &[0x201C],- (true, _) => &[0x2018],- (false, 0) => &[0x201D],- (false, _) => &[0x2019],- },+ QuotesData::Auto => {+ if !language.is_empty() {+ let mut mark = std::ptr::null();+ // SAFETY: The language is readable during the call. The thread's locale cache owns the returned mark.+ let length = unsafe {+ unicode_quotation_mark(language.as_ptr(), language.len(), open, depth != 0, &raw mut mark)+ };+ if length != 0 {+ text.extend_from_slice(unsafe { ak::slice_from_raw_parts(mark, length) });+ return;+ }+ }+ match (open, depth) {+ (true, 0) => &[0x201C],+ (true, _) => &[0x2018],+ (false, 0) => &[0x201D],+ (false, _) => &[0x2019],+ }+ }QuotesData::Specified(values) => {// If the depth is greater than the number of pairs, the last pair is repeated.let Some(last_pair) = (values.len() / 2).checked_sub(1) else {- return &[];+ return;};let level = (depth as usize).min(last_pair);let quote = &values[level * 2 + usize::from(!open)];@@ -265,7 +292,8 @@ fn quote_string<'a>(quotes: &QuotesData<'a>, open: bool, depth: u32) -> &'a [u16_ => panic!("a quote is a string"),}}- }+ };+ text.extend_from_slice(mark);}// counter( <counter-name>, <counter-style>? )@@ -420,6 +448,7 @@ pub(crate) fn resolve_content(tree_scope: u32,style: ComputedValuesView<'_>,quote_depth: Option<u32>,+ quote_language: &[u16],mut values: Option<&mut dyn CounterValues>,) -> ResolvedContent {let content = style.content_value();@@ -481,7 +510,7 @@ pub(crate) fn resolve_content(run.made = true;run.shows_quote_depth = true;if let Some(level) = &mut quote_nesting_level {- run.text.extend_from_slice(quote_string("es, true, *level));+ append_quote(&mut run.text, "es, true, *level, quote_language);*level += 1;}}@@ -498,7 +527,7 @@ pub(crate) fn resolve_content(let run = pending_text.get_or_insert_with(new_run);run.made = true;run.shows_quote_depth = true;- run.text.extend_from_slice(quote_string("es, false, *level));+ append_quote(&mut run.text, "es, false, *level, quote_language);}// NB: A text of such close-quotes alone makes no item, but is one whatever the depth.Some(_) | None => pending_text.get_or_insert_with(new_run).shows_quote_depth = true,diff --git a/Libraries/LibWeb/Rust/src/layout/tree_plan/content_items.rs b/Libraries/LibWeb/Rust/src/layout/tree_plan/content_items.rsindex fb9df1d5d32..f1d6494c4f2 100644--- a/Libraries/LibWeb/Rust/src/layout/tree_plan/content_items.rs+++ b/Libraries/LibWeb/Rust/src/layout/tree_plan/content_items.rs@@ -249,6 +249,7 @@ pub(crate) fn fill_pending_content(pending.tree_scope,style,Some(pending.quote_depth.unwrap_or(0)),+ engine.element_language_tag(engine.inheritance_parent(owner.element).unwrap_or(owner.element)),Some(values),);for item in resolved.items {diff --git a/Libraries/LibWeb/Rust/src/layout/tree_plan/planner.rs b/Libraries/LibWeb/Rust/src/layout/tree_plan/planner.rsindex f7811eed7d2..5470ff8a9ce 100644--- a/Libraries/LibWeb/Rust/src/layout/tree_plan/planner.rs+++ b/Libraries/LibWeb/Rust/src/layout/tree_plan/planner.rs@@ -3312,7 +3312,7 @@ impl<'a> Planner<'a> {};}- let resolved = resolve_content(arena, tree_scope, view, None, None);+ let resolved = resolve_content(arena, tree_scope, view, None, &[], None);if resolved.renders_list_item_counter_value {self.report_list_item_counter_rendering(element);}diff --git a/Libraries/LibWeb/Rust/src/layout/tree_update_marks.rs b/Libraries/LibWeb/Rust/src/layout/tree_update_marks.rsindex 82a35d243b5..a422e9f95d5 100644--- a/Libraries/LibWeb/Rust/src/layout/tree_update_marks.rs+++ b/Libraries/LibWeb/Rust/src/layout/tree_update_marks.rs@@ -699,8 +699,16 @@ impl FfiBoxMarks {arena.set_needs_layout_update(row, true);}}- if self.language_changed && super::rendered_text::enroll_text_after_language_change(arena, row) {- arena.set_needs_layout_update(row, true);+ if self.language_changed {+ if let MarkedBox::PseudoElement { generator, .. } = target+ && arena.style_view(row).is_some_and(|style| style.content_opens_or_closes_quotes()+ && matches!(style.quotes_value(), Some(crate::css::style_value::StyleValueData::Keyword { keyword }) if *keyword == crate::css::css_enums::keyword::AUTO))+ {+ arena.mark_layout_tree_update(Some(generator), FfiLayoutTreeUpdateMark::style_change(layout_tree_update_reuse_reason::PSEUDO_ELEMENT_CHANGE));+ }+ if super::rendered_text::enroll_text_after_language_change(arena, row) {+ arena.set_needs_layout_update(row, true);+ }}if self.has_editing_facts {let mut stamp = |flag: NodeFlag, value: bool| {diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-content/reference/quotes-030-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-content/reference/quotes-030-ref.htmlnew file mode 100644index 00000000000..70aa063e917--- /dev/null+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-content/reference/quotes-030-ref.html@@ -0,0 +1,12 @@+<!doctype html>+<html lang="en">+<meta charset=utf-8>+<title>CSS Generated Content reference: quotes</title>+<link rel="author" title="Jonathan Kew" href="mailto:[email protected]">+<style>+body { font: 32px Arial, Helvetica, Noto Sans, DejaVu Sans, FreeSans, sans-serif; }+</style>+<body>+<p>Test passes if the quote marks in both lines match:+<p>One “two <span lang="ja">‘three <span lang="fr">『four』</span>’</span>”+<p>One “two <span lang="ja">‘three <span lang="fr">『four』</span>’</span>”diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-content/reference/quotes-034-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-content/reference/quotes-034-ref.htmlnew file mode 100644index 00000000000..bfe7163f132--- /dev/null+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-content/reference/quotes-034-ref.html@@ -0,0 +1,31 @@+<!doctype html>+<html lang="en">+<meta charset=utf-8>+<title>CSS Generated Content reference: quotes</title>+<style>+body { font: 15px serif; }+</style>+<body>+<p>Test passes if quote marks in each pair lines match:++<p lang="fr">«fr FALLBACK»</p>+<p lang="fr">«fr FALLBACK»</p>++<p lang="en">“en FALLBACK”</p>+<p lang="en">“en FALLBACK”</p>++<p lang="fi">”fi FALLBACK”</p>+<p lang="fi">”fi FALLBACK”</p>++<p lang="de">„de FALLBACK“</p>+<p lang="de">„de FALLBACK“</p>++<p lang="he">”he FALLBACK”</p>+<p lang="he">”he FALLBACK”</p>++<p lang="ja">「ja FALLBACK」</p>+<p lang="ja">「ja FALLBACK」</p>++<p lang="aa">“root FALLBACK”</p>++<p lang="zz">“root FALLBACK”</p>diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-content/reference/quotes-lang-dynamic-001-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-content/reference/quotes-lang-dynamic-001-ref.htmlnew file mode 100644index 00000000000..14ef7659621--- /dev/null+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-content/reference/quotes-lang-dynamic-001-ref.html@@ -0,0 +1,7 @@+<!doctype html>+<meta charset=utf-8>+<title>CSS-content test: changing the lang attribute affects quotes</title>+<link rel="author" title="Xiaocheng Hu" href="mailto:[email protected]">+<link rel=help href="https://drafts.csswg.org/css-content/#quotes">++<div>«First letter»</div>diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-content/quotes-030.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-content/quotes-030.htmlnew file mode 100644index 00000000000..a6a2f35ef7b--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-content/quotes-030.html@@ -0,0 +1,19 @@+<!doctype html>+<html lang="en">+<meta charset=utf-8>+<title>CSS Generated Content test: quotes</title>+<meta name="assert" content="quotes:auto with mixed languages uses appropriate CLDR quotes for each language,+based on the parent language (not the language of the element itself)">+<link rel="author" title="Jonathan Kew" href="mailto:[email protected]">+<link rel="help" href="https://drafts.csswg.org/css-content-3/#quotes">+<link rel="help" href="https://github.com/w3c/csswg-drafts/issues/5478#issuecomment-701507076">+<link rel=match href="../../../../expected/wpt-import/css/css-content/reference/quotes-030-ref.html">+<style>+body { font: 32px Arial, Helvetica, Noto Sans, DejaVu Sans, FreeSans, sans-serif; }+q { quotes: auto; }+q q { quotes: auto; } /* potentially overriding quotes:match-parent from UA sheet */+</style>+<body>+<p>Test passes if the quote marks in both lines match:+<p>One <q>two <q lang="ja">three <q lang="fr">four</q></q></q>+<p>One “two <span lang="ja">‘three <span lang="fr">『four』</span>’</span>”diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-content/quotes-034.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-content/quotes-034.htmlnew file mode 100644index 00000000000..9a49f1689dc--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-content/quotes-034.html@@ -0,0 +1,34 @@+<!doctype html>+<html lang="en">+<meta charset=utf-8>+<title>CSS Generated Content test: quotes</title>+<meta name="assert" content="In the case of an unknown region, the quotes displayed should be based on the fallback order.">+<link rel="help" href="https://drafts.csswg.org/css-content-3/#quotes">+<link rel=match href="../../../../expected/wpt-import/css/css-content/reference/quotes-034-ref.html">+<style>+body { font: 15px serif; }+</style>+<body>+<p>Test passes if quote marks in each pair lines match:</p>++<p lang="fr"><q>fr FALLBACK</q></p>+<p lang="fr-FR"><q>fr FALLBACK</q></p>++<p lang="en"><q>en FALLBACK</q></p>+<p lang="en-EN"><q>en FALLBACK</q></p>++<p lang="fi"><q>fi FALLBACK</q></p>+<p lang="fi-FI"><q>fi FALLBACK</q></p>++<p lang="de"><q>de FALLBACK</q></p>+<p lang="de-DE"><q>de FALLBACK</q></p>++<p lang="he"><q>he FALLBACK</q></p>+<p lang="he-HE"><q>he FALLBACK</q></p>++<p lang="ja"><q>ja FALLBACK</q></p>+<p lang="ja-JA"><q>ja FALLBACK</q></p>++<p lang="aa"><q>root FALLBACK</q></p>++<p lang="zz"><q>root FALLBACK</q></p>diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-content/quotes-lang-dynamic-001.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-content/quotes-lang-dynamic-001.htmlnew file mode 100644index 00000000000..3a8b69a5ac1--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-content/quotes-lang-dynamic-001.html@@ -0,0 +1,13 @@+<!doctype html>+<meta charset=utf-8>+<title>CSS-content test: changing the lang attribute affects quotes</title>+<link rel="author" title="Xiaocheng Hu" href="mailto:[email protected]">+<link rel="match" href="../../../../expected/wpt-import/css/css-content/reference/quotes-lang-dynamic-001-ref.html">+<link rel=help href="https://drafts.csswg.org/css-content/#quotes">++<div id="root" lang="en"><q>First letter</q></div>++<script>+ document.body.offsetTop;+ root.setAttribute('lang', 'fr');+</script>