diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-09-01 22:26:06 -0400 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-09-02 17:56:42 +0100 |
commit | 27fc3cfe75df511eb657dbe6304683ef0bb4ec86 (patch) | |
tree | 83c1ee93f38b97b41a0279cd0f3cc51e62c86b46 | |
parent | 4de05faa8a85b5d6fd7304bab082c441b4a5afb2 (diff) | |
download | serenity-27fc3cfe75df511eb657dbe6304683ef0bb4ec86.zip |
LibJS: Handle existing Intl.Locale objects in CanonicalizeLocaleList
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp | 22 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Tests/builtins/Intl/Intl.getCanonicalLocales.js | 8 |
2 files changed, 22 insertions, 8 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp index 34aa2869f6..36283eb16c 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp @@ -12,6 +12,7 @@ #include <LibJS/Runtime/Array.h> #include <LibJS/Runtime/GlobalObject.h> #include <LibJS/Runtime/Intl/AbstractOperations.h> +#include <LibJS/Runtime/Intl/Locale.h> #include <LibUnicode/Locale.h> namespace JS::Intl { @@ -150,8 +151,7 @@ Vector<String> canonicalize_locale_list(GlobalObject& global_object, Value local Object* object = nullptr; // 3. If Type(locales) is String or Type(locales) is Object and locales has an [[InitializedLocale]] internal slot, then - // FIXME: When we have an Intl.Locale object, handle it it here. - if (locales.is_string()) { + if (locales.is_string() || (locales.is_object() && is<Locale>(locales.as_object()))) { // a. Let O be CreateArrayFromList(ยซ locales ยป). object = Array::create_from(global_object, { locales }); } @@ -195,14 +195,20 @@ Vector<String> canonicalize_locale_list(GlobalObject& global_object, Value local return {}; } + String tag; + // iii. If Type(kValue) is Object and kValue has an [[InitializedLocale]] internal slot, then - // 1. Let tag be kValue.[[Locale]]. + if (key_value.is_object() && is<Locale>(key_value.as_object())) { + // 1. Let tag be kValue.[[Locale]]. + tag = static_cast<Locale const&>(key_value.as_object()).locale(); + } // iv. Else, - // 1. Let tag be ? ToString(kValue). - // FIXME: When we have an Intl.Locale object, handle it it here. - auto tag = key_value.to_string(global_object); - if (vm.exception()) - return {}; + else { + // 1. Let tag be ? ToString(kValue). + tag = key_value.to_string(global_object); + if (vm.exception()) + return {}; + } // v. If IsStructurallyValidLanguageTag(tag) is false, throw a RangeError exception. auto locale_id = is_structurally_valid_language_tag(tag); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/Intl.getCanonicalLocales.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/Intl.getCanonicalLocales.js index 4164897e23..bda470aa46 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Intl/Intl.getCanonicalLocales.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/Intl.getCanonicalLocales.js @@ -115,4 +115,12 @@ describe("normal behavior", () => { "en-US-u-1k-aaa-2k-ccc", ]); }); + + test("canonicalize locale objects", () => { + const en = new Intl.Locale("en", { script: "Latn" }); + expect(Intl.getCanonicalLocales(en)).toEqual(["en-Latn"]); + + const es = new Intl.Locale("es", { region: "419" }); + expect(Intl.getCanonicalLocales([en, es])).toEqual(["en-Latn", "es-419"]); + }); }); |