Skip to content
Merged
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
27 changes: 25 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;
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,15 @@ 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
Copy Markdown

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
Copy Markdown
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)
});
}

impl_service_preferences!(collation_type, numeric_ordering, case_first);
}
Loading
Loading