diff --git a/Userland/Libraries/LibJS/Runtime/Intl/CollatorConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Intl/CollatorConstructor.cpp index a20904cb789..6e0d4712391 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/CollatorConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/CollatorConstructor.cpp @@ -16,141 +16,6 @@ namespace JS::Intl { JS_DEFINE_ALLOCATOR(CollatorConstructor); -// 10.1.2 InitializeCollator ( collator, locales, options ), https://tc39.es/ecma402/#sec-initializecollator -static ThrowCompletionOr> initialize_collator(VM& vm, Collator& collator, Value locales_value, Value options_value) -{ - // 1. Let requestedLocales be ? CanonicalizeLocaleList(locales). - auto requested_locales = TRY(canonicalize_locale_list(vm, locales_value)); - - // 2. Set options to ? CoerceOptionsToObject(options). - auto* options = TRY(coerce_options_to_object(vm, options_value)); - - // 3. Let usage be ? GetOption(options, "usage", string, « "sort", "search" », "sort"). - auto usage = TRY(get_option(vm, *options, vm.names.usage, OptionType::String, { "sort"sv, "search"sv }, "sort"sv)); - - // 4. Set collator.[[Usage]] to usage. - collator.set_usage(usage.as_string().utf8_string_view()); - - // 5. If usage is "sort", then - // a. Let localeData be %Collator%.[[SortLocaleData]]. - // 6. Else, - // a. Let localeData be %Collator%.[[SearchLocaleData]]. - - // 7. Let opt be a new Record. - LocaleOptions opt {}; - - // 8. Let matcher be ? GetOption(options, "localeMatcher", string, « "lookup", "best fit" », "best fit"). - auto matcher = TRY(get_option(vm, *options, vm.names.localeMatcher, OptionType::String, { "lookup"sv, "best fit"sv }, "best fit"sv)); - - // 9. Set opt.[[localeMatcher]] to matcher. - opt.locale_matcher = matcher; - - // 10. Let collation be ? GetOption(options, "collation", string, empty, undefined). - auto collation = TRY(get_option(vm, *options, vm.names.collation, OptionType::String, {}, Empty {})); - - // 11. If collation is not undefined, then - if (!collation.is_undefined()) { - // a. If collation does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception. - if (!Unicode::is_type_identifier(collation.as_string().utf8_string_view())) - return vm.throw_completion(ErrorType::OptionIsNotValidValue, collation, "collation"sv); - - // 12. Set opt.[[co]] to collation. - opt.co = collation.as_string().utf8_string(); - } - - // 13. Let numeric be ? GetOption(options, "numeric", boolean, empty, undefined). - auto numeric = TRY(get_option(vm, *options, vm.names.numeric, OptionType::Boolean, {}, Empty {})); - - // 14. If numeric is not undefined, then - if (!numeric.is_undefined()) { - // a. Let numeric be ! ToString(numeric). - numeric = PrimitiveString::create(vm, MUST(numeric.to_string(vm))); - } - - // 15. Set opt.[[kn]] to numeric. - opt.kn = locale_key_from_value(numeric); - - // 16. Let caseFirst be ? GetOption(options, "caseFirst", string, « "upper", "lower", "false" », undefined). - auto case_first = TRY(get_option(vm, *options, vm.names.caseFirst, OptionType::String, { "upper"sv, "lower"sv, "false"sv }, Empty {})); - - // 17. Set opt.[[kf]] to caseFirst. - opt.kf = locale_key_from_value(case_first); - - // 18. Let relevantExtensionKeys be %Collator%.[[RelevantExtensionKeys]]. - auto relevant_extension_keys = Collator::relevant_extension_keys(); - - // 19. Let r be ResolveLocale(%Collator%.[[AvailableLocales]], requestedLocales, opt, relevantExtensionKeys, localeData). - auto result = resolve_locale(requested_locales, opt, relevant_extension_keys); - - // 20. Set collator.[[Locale]] to r.[[locale]]. - collator.set_locale(move(result.locale)); - - // 21. Let collation be r.[[co]]. - auto& collation_value = result.co; - - // 22. If collation is null, let collation be "default". - if (collation_value.has()) - collation_value = "default"_string; - - // 23. Set collator.[[Collation]] to collation. - collator.set_collation(move(collation_value.get())); - - // 24. If relevantExtensionKeys contains "kn", then - if (relevant_extension_keys.span().contains_slow("kn"sv)) { - // a. Set collator.[[Numeric]] to SameValue(r.[[kn]], "true"). - collator.set_numeric(result.kn == "true"_string); - } - - // 25. If relevantExtensionKeys contains "kf", then - if (relevant_extension_keys.span().contains_slow("kf"sv)) { - // a. Set collator.[[CaseFirst]] to r.[[kf]]. - if (auto* resolved_case_first = result.kf.get_pointer()) - collator.set_case_first(*resolved_case_first); - } - - // 26. Let sensitivity be ? GetOption(options, "sensitivity", string, « "base", "accent", "case", "variant" », undefined). - auto sensitivity = TRY(get_option(vm, *options, vm.names.sensitivity, OptionType::String, { "base"sv, "accent"sv, "case"sv, "variant"sv }, Empty {})); - - // 27. If sensitivity is undefined, then - if (sensitivity.is_undefined()) { - // a. If usage is "sort", then - if (collator.usage() == Unicode::Usage::Sort) { - // i. Let sensitivity be "variant". - sensitivity = PrimitiveString::create(vm, "variant"_string); - } - // b. Else, - else { - // i. Let dataLocale be r.[[dataLocale]]. - // ii. Let dataLocaleData be localeData.[[]]. - // iii. Let sensitivity be dataLocaleData.[[sensitivity]]. - sensitivity = PrimitiveString::create(vm, "base"_string); - } - } - - // 28. Set collator.[[Sensitivity]] to sensitivity. - collator.set_sensitivity(sensitivity.as_string().utf8_string_view()); - - // 29. Let ignorePunctuation be ? GetOption(options, "ignorePunctuation", boolean, empty, false). - auto ignore_punctuation = TRY(get_option(vm, *options, vm.names.ignorePunctuation, OptionType::Boolean, {}, false)); - - // 30. Set collator.[[IgnorePunctuation]] to ignorePunctuation. - collator.set_ignore_punctuation(ignore_punctuation.as_bool()); - - // Non-standard, create an ICU collator for this Intl object. - auto icu_collator = Unicode::Collator::create( - collator.locale(), - collator.usage(), - collator.collation(), - collator.sensitivity(), - collator.case_first(), - collator.numeric(), - collator.ignore_punctuation()); - collator.set_collator(move(icu_collator)); - - // 31. Return collator. - return collator; -} - // 10.1 The Intl.Collator Constructor, https://tc39.es/ecma402/#sec-the-intl-collator-constructor CollatorConstructor::CollatorConstructor(Realm& realm) : NativeFunction(realm.vm().names.Collator.as_string(), realm.intrinsics().function_prototype()) @@ -183,20 +48,151 @@ ThrowCompletionOr> CollatorConstructor::construct(FunctionO { auto& vm = this->vm(); - auto locales = vm.argument(0); - auto options = vm.argument(1); + auto locales_value = vm.argument(0); + auto options_value = vm.argument(1); // 2. Let internalSlotsList be « [[InitializedCollator]], [[Locale]], [[Usage]], [[Sensitivity]], [[IgnorePunctuation]], [[Collation]], [[BoundCompare]] ». - // 3. If %Collator%.[[RelevantExtensionKeys]] contains "kn", then - // a. Append [[Numeric]] as the last element of internalSlotsList. - // 4. If %Collator%.[[RelevantExtensionKeys]] contains "kf", then - // a. Append [[CaseFirst]] as the last element of internalSlotsList. + // 3. If %Intl.Collator%.[[RelevantExtensionKeys]] contains "kn", then + // a. Append [[Numeric]] to internalSlotsList. + // 4. If %Intl.Collator%.[[RelevantExtensionKeys]] contains "kf", then + // a. Append [[CaseFirst]] to internalSlotsList. - // 5. Let collator be ? OrdinaryCreateFromConstructor(newTarget, "%Collator.prototype%", internalSlotsList). + // 5. Let collator be ? OrdinaryCreateFromConstructor(newTarget, "%Intl.Collator.prototype%", internalSlotsList). auto collator = TRY(ordinary_create_from_constructor(vm, new_target, &Intrinsics::intl_collator_prototype)); - // 6. Return ? InitializeCollator(collator, locales, options). - return TRY(initialize_collator(vm, collator, locales, options)); + // 6. Let requestedLocales be ? CanonicalizeLocaleList(locales). + auto requested_locales = TRY(canonicalize_locale_list(vm, locales_value)); + + // 7. Set options to ? CoerceOptionsToObject(options). + auto* options = TRY(coerce_options_to_object(vm, options_value)); + + // 8. Let usage be ? GetOption(options, "usage", string, « "sort", "search" », "sort"). + auto usage = TRY(get_option(vm, *options, vm.names.usage, OptionType::String, { "sort"sv, "search"sv }, "sort"sv)); + + // 9. Set collator.[[Usage]] to usage. + collator->set_usage(usage.as_string().utf8_string_view()); + + // 10. If usage is "sort", then + // a. Let localeData be %Intl.Collator%.[[SortLocaleData]]. + // 11. Else, + // a. Let localeData be %Intl.Collator%.[[SearchLocaleData]]. + + // 12. Let opt be a new Record. + LocaleOptions opt {}; + + // 13. Let matcher be ? GetOption(options, "localeMatcher", string, « "lookup", "best fit" », "best fit"). + auto matcher = TRY(get_option(vm, *options, vm.names.localeMatcher, OptionType::String, { "lookup"sv, "best fit"sv }, "best fit"sv)); + + // 14. Set opt.[[localeMatcher]] to matcher. + opt.locale_matcher = matcher; + + // 15. Let collation be ? GetOption(options, "collation", string, empty, undefined). + auto collation = TRY(get_option(vm, *options, vm.names.collation, OptionType::String, {}, Empty {})); + + // 16. If collation is not undefined, then + if (!collation.is_undefined()) { + // a. If collation cannot be matched by the type Unicode locale nonterminal, throw a RangeError exception. + if (!Unicode::is_type_identifier(collation.as_string().utf8_string_view())) + return vm.throw_completion(ErrorType::OptionIsNotValidValue, collation, "collation"sv); + } + + // 17. Set opt.[[co]] to collation. + opt.co = locale_key_from_value(collation); + + // 18. Let numeric be ? GetOption(options, "numeric", boolean, empty, undefined). + auto numeric = TRY(get_option(vm, *options, vm.names.numeric, OptionType::Boolean, {}, Empty {})); + + // 19. If numeric is not undefined, then + if (!numeric.is_undefined()) { + // a. Set numeric to ! ToString(numeric). + numeric = PrimitiveString::create(vm, MUST(numeric.to_string(vm))); + } + + // 20. Set opt.[[kn]] to numeric. + opt.kn = locale_key_from_value(numeric); + + // 21. Let caseFirst be ? GetOption(options, "caseFirst", string, « "upper", "lower", "false" », undefined). + auto case_first = TRY(get_option(vm, *options, vm.names.caseFirst, OptionType::String, { "upper"sv, "lower"sv, "false"sv }, Empty {})); + + // 22. Set opt.[[kf]] to caseFirst. + opt.kf = locale_key_from_value(case_first); + + // 23. Let relevantExtensionKeys be %Intl.Collator%.[[RelevantExtensionKeys]]. + auto relevant_extension_keys = Collator::relevant_extension_keys(); + + // 24. Let r be ResolveLocale(%Intl.Collator%.[[AvailableLocales]], requestedLocales, opt, relevantExtensionKeys, localeData). + auto result = resolve_locale(requested_locales, opt, relevant_extension_keys); + + // 25. Set collator.[[Locale]] to r.[[Locale]]. + collator->set_locale(move(result.locale)); + + // 26. Set collation to r.[[co]]. + auto collation_value = move(result.co); + + // 27. If collation is null, set collation to "default". + if (collation_value.has()) + collation_value = "default"_string; + + // 28. Set collator.[[Collation]] to collation. + collator->set_collation(move(collation_value.get())); + + // 29. If relevantExtensionKeys contains "kn", then + if (relevant_extension_keys.span().contains_slow("kn"sv)) { + // a. Set collator.[[Numeric]] to SameValue(r.[[kn]], "true"). + collator->set_numeric(result.kn == "true"_string); + } + + // 30. If relevantExtensionKeys contains "kf", then + if (relevant_extension_keys.span().contains_slow("kf"sv)) { + // a. Set collator.[[CaseFirst]] to r.[[kf]]. + if (auto* resolved_case_first = result.kf.get_pointer()) + collator->set_case_first(*resolved_case_first); + } + + // 31. Let resolvedLocaleData be r.[[LocaleData]]. + + // 32. Let sensitivity be ? GetOption(options, "sensitivity", string, « "base", "accent", "case", "variant" », undefined). + auto sensitivity = TRY(get_option(vm, *options, vm.names.sensitivity, OptionType::String, { "base"sv, "accent"sv, "case"sv, "variant"sv }, Empty {})); + + // 33. If sensitivity is undefined, then + if (sensitivity.is_undefined()) { + // a. If usage is "sort", then + if (collator->usage() == Unicode::Usage::Sort) { + // i. Set sensitivity to "variant". + sensitivity = PrimitiveString::create(vm, "variant"_string); + } + // b. Else, + else { + // FIXME: i. Set sensitivity to resolvedLocaleData.[[sensitivity]]. + sensitivity = PrimitiveString::create(vm, "base"_string); + } + } + + // 34. Set collator.[[Sensitivity]] to sensitivity. + collator->set_sensitivity(sensitivity.as_string().utf8_string_view()); + + // FIXME: 35. Let defaultIgnorePunctuation be resolvedLocaleData.[[ignorePunctuation]]. + auto default_ignore_punctuation = false; + + // 36. Let ignorePunctuation be ? GetOption(options, "ignorePunctuation", boolean, empty, defaultIgnorePunctuation). + auto ignore_punctuation = TRY(get_option(vm, *options, vm.names.ignorePunctuation, OptionType::Boolean, {}, default_ignore_punctuation)); + + // 37. Set collator.[[IgnorePunctuation]] to ignorePunctuation. + collator->set_ignore_punctuation(ignore_punctuation.as_bool()); + + // Non-standard, create an ICU collator for this Intl object. + auto icu_collator = Unicode::Collator::create( + collator->locale(), + collator->usage(), + collator->collation(), + collator->sensitivity(), + collator->case_first(), + collator->numeric(), + collator->ignore_punctuation()); + collator->set_collator(move(icu_collator)); + + // 38. Return collator. + return collator; } // 10.2.2 Intl.Collator.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-intl.collator.supportedlocalesof