Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 10 additions & 105 deletions core/engine/src/builtins/intl/collator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,15 @@ use icu_collator::{
provider::CollationMetadataV1,
};

use icu_locale::{
Locale, extensions::unicode, extensions_unicode_key as key, preferences::PreferenceKey,
subtags::subtag,
};
use icu_provider::DataMarkerAttributes;
use icu_locale::{Locale, extensions::unicode};

use crate::{
Context, JsArgs, JsData, JsNativeError, JsResult, JsString, JsValue,
builtins::{
BuiltInBuilder, BuiltInConstructor, BuiltInObject, IntrinsicObject, OrdinaryObject,
options::get_option,
},
context::{
icu::IntlProvider,
intrinsics::{Intrinsics, StandardConstructor, StandardConstructors},
},
context::intrinsics::{Intrinsics, StandardConstructor, StandardConstructors},
js_string,
native_function::NativeFunction,
object::{
Expand All @@ -36,7 +29,7 @@ use crate::{

use super::{
Service,
locale::{canonicalize_locale_list, filter_locales, resolve_locale, validate_extension},
locale::{canonicalize_locale_list, filter_locales, resolve_locale},
options::{IntlOptions, coerce_options_to_object},
};

Expand Down Expand Up @@ -72,95 +65,7 @@ impl Collator {
impl Service for Collator {
type LangMarker = CollationMetadataV1;

type LocaleOptions = CollatorPreferences;

fn resolve(locale: &mut Locale, options: &mut Self::LocaleOptions, provider: &IntlProvider) {
let mut locale_preferences = CollatorPreferences::from(&*locale);
locale_preferences.collation_type = locale_preferences.collation_type.take().filter(|co| {
let attr = DataMarkerAttributes::from_str_or_panic(co.as_str());
co != &CollationType::Search
&& validate_extension::<Self::LangMarker>(locale.id.clone(), attr, provider)
});
locale.extensions.unicode.clear();

options.locale_preferences = (&*locale).into();

options.collation_type = options
.collation_type
.take()
.filter(|co| {
let attr = DataMarkerAttributes::from_str_or_panic(co.as_str());
co != &CollationType::Search
&& validate_extension::<Self::LangMarker>(locale.id.clone(), attr, provider)
})
.inspect(|co| {
if Some(co) == locale_preferences.collation_type.as_ref()
&& let Some(co) = co.unicode_extension_value()
{
locale.extensions.unicode.keywords.set(key!("co"), co);
}
})
.or_else(|| {
if let Some(co) = locale_preferences
.collation_type
.as_ref()
.and_then(CollationType::unicode_extension_value)
{
locale.extensions.unicode.keywords.set(key!("co"), co);
}
locale_preferences.collation_type
});

options.numeric_ordering = options
.numeric_ordering
.take()
.inspect(|kn| {
if Some(kn) == locale_preferences.numeric_ordering.as_ref()
&& let Some(mut kn) = kn.unicode_extension_value()
{
if kn.as_single_subtag() == Some(&subtag!("true")) {
kn = unicode::Value::new_empty();
}
locale.extensions.unicode.keywords.set(key!("kn"), kn);
}
})
.or_else(|| {
if let Some(mut kn) = locale_preferences
.numeric_ordering
.as_ref()
.and_then(CollationNumericOrdering::unicode_extension_value)
{
if kn.as_single_subtag() == Some(&subtag!("true")) {
kn = unicode::Value::new_empty();
}
locale.extensions.unicode.keywords.set(key!("kn"), kn);
}

locale_preferences.numeric_ordering
});

options.case_first = options
.case_first
.take()
.inspect(|kf| {
if Some(kf) == locale_preferences.case_first.as_ref()
&& let Some(kn) = kf.unicode_extension_value()
{
locale.extensions.unicode.keywords.set(key!("kf"), kn);
}
})
.or_else(|| {
if let Some(kf) = locale_preferences
.case_first
.as_ref()
.and_then(CollationCaseFirst::unicode_extension_value)
{
locale.extensions.unicode.keywords.set(key!("kf"), kf);
}

locale_preferences.case_first
});
}
type Preferences = CollatorPreferences;
}

impl IntrinsicObject for Collator {
Expand Down Expand Up @@ -285,7 +190,7 @@ impl BuiltInConstructor for Collator {

let mut intl_options = IntlOptions {
matcher,
service_options: {
preferences: {
let mut prefs = CollatorPreferences::default();
prefs.collation_type = collation;
prefs.numeric_ordering = numeric.map(|kn| {
Expand All @@ -312,16 +217,16 @@ impl BuiltInConstructor for Collator {
// 21. Let collation be r.[[co]].
// 22. If collation is null, let collation be "default".
// 23. Set collator.[[Collation]] to collation.
let collation = intl_options.service_options.collation_type;
let collation = intl_options.preferences.collation_type;

// 24. If relevantExtensionKeys contains "kn", then
// a. Set collator.[[Numeric]] to SameValue(r.[[kn]], "true").
let numeric =
intl_options.service_options.numeric_ordering == Some(CollationNumericOrdering::True);
intl_options.preferences.numeric_ordering == Some(CollationNumericOrdering::True);

// 25. If relevantExtensionKeys contains "kf", then
// a. Set collator.[[CaseFirst]] to r.[[kf]].
let case_first = intl_options.service_options.case_first;
let case_first = intl_options.preferences.case_first;

// 26. Let sensitivity be ? GetOption(options, "sensitivity", string, « "base", "accent", "case", "variant" », undefined).
// 28. Set collator.[[Sensitivity]] to sensitivity.
Expand Down Expand Up @@ -354,12 +259,12 @@ impl BuiltInConstructor for Collator {
options.max_variable = max_variable;

if usage == Usage::Search {
intl_options.service_options.collation_type = Some(CollationType::Search);
intl_options.preferences.collation_type = Some(CollationType::Search);
}

let collator = icu_collator::Collator::try_new_with_buffer_provider(
context.intl_provider().erased_provider(),
intl_options.service_options,
intl_options.preferences,
options,
)
.map_err(|e| JsNativeError::typ().with_message(e.to_string()))?;
Expand Down
72 changes: 70 additions & 2 deletions core/engine/src/builtins/intl/collator/options.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
use std::str::FromStr;

use icu_collator::{
CollatorPreferences,
options::{CaseLevel, Strength},
preferences::CollationCaseFirst,
preferences::{CollationCaseFirst, CollationType},
provider::CollationMetadataV1,
};
use icu_locale::{LanguageIdentifier, preferences::PreferenceKey};
use icu_provider::{
DataMarkerAttributes,
prelude::icu_locale_core::{extensions::unicode, preferences::LocalePreferences},
};

use crate::{
Context, JsNativeError, JsResult, JsValue,
builtins::options::{OptionType, ParsableOptionType},
builtins::{
intl::{ServicePreferences, locale::validate_extension},
options::{OptionType, ParsableOptionType},
},
context::icu::IntlProvider,
};

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -97,3 +108,60 @@ impl OptionType for CollationCaseFirst {
}
}
}

impl ServicePreferences for CollatorPreferences {
fn validate(&mut self, id: &LanguageIdentifier, provider: &IntlProvider) {
self.collation_type = self.collation_type.take().filter(|co| {
let attr = DataMarkerAttributes::from_str_or_panic(co.as_str());
co != &CollationType::Search
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good, this is Ecma specific functionality so it should be here in boa

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, extension validation like this seems to be a good candidate for a future "icu4x-ecma402" glue crate

&& validate_extension::<CollationMetadataV1>(id, attr, provider)
});
}

fn as_unicode(&self) -> unicode::Unicode {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in some cases we have impl From Preferences for Locale

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also thought that, but after looking at the preferences code, it seems like it was commented out:
https://github.com/unicode-org/icu4x/blob/5fb31d7ae061d304088f1e79897f7435f1846da6/components/locale_core/src/preferences/mod.rs#L519-L533

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably there just wasnt a need for it yet.

let mut exts = unicode::Unicode::new();

if let Some(co) = self.collation_type
&& let Some(value) = co.unicode_extension_value()
{
exts.keywords.set(unicode::key!("co"), value);
}

if let Some(kn) = self.numeric_ordering
&& let Some(value) = kn.unicode_extension_value()
{
exts.keywords.set(unicode::key!("kn"), value);
}

if let Some(kf) = self.case_first
&& let Some(value) = kf.unicode_extension_value()
{
exts.keywords.set(unicode::key!("kf"), value);
}

exts
}

fn extended(&self, other: &Self) -> Self {
let mut result = *self;
result.extend(*other);
result
}

fn intersection(&self, other: &Self) -> Self {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preferences have a merge operation already

Copy link
Member Author

@jedel1043 jedel1043 Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This the opposite though; it takes two preference objects and gets the preferences that are the same in both.

We need this because ECMA402 requires correctly setting up the locale with the extensions that were "used" by the locale resolution algorithm. Here I implemented that by first extending the locale prefs with the option prefs, then taking the intersection of the full prefs with the locale prefs to see which ones were not changed.

let mut inter = *self;
if inter.locale_preferences != other.locale_preferences {
inter.locale_preferences = LocalePreferences::default();
}
if inter.collation_type != other.collation_type {
inter.collation_type.take();
}
if inter.case_first != other.case_first {
inter.case_first.take();
}
if inter.numeric_ordering != other.numeric_ordering {
inter.numeric_ordering.take();
}
inter
}
}
Loading
Loading