diff --git a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp index 0d9de229f01..3c4037d8f67 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp @@ -54,6 +54,7 @@ void LocalePrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.numberingSystem, numbering_system, {}, Attribute::Configurable); define_native_accessor(vm.names.numeric, numeric, {}, Attribute::Configurable); define_native_accessor(vm.names.language, language, {}, Attribute::Configurable); + define_native_accessor(vm.names.script, script, {}, Attribute::Configurable); } // 14.3.5 Intl.Locale.prototype.toString ( ), https://tc39.es/ecma402/#sec-Intl.Locale.prototype.toString @@ -143,4 +144,27 @@ JS_DEFINE_NATIVE_GETTER(LocalePrototype::language) return js_string(vm, *locale->language_id.language); } +// 14.3.14 get Intl.Locale.prototype.script, https://tc39.es/ecma402/#sec-Intl.Locale.prototype.script +JS_DEFINE_NATIVE_GETTER(LocalePrototype::script) +{ + // 1. Let loc be the this value. + // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]). + auto* locale_object = typed_this(global_object); + if (!locale_object) + return {}; + + // 3. Let locale be loc.[[Locale]]. + auto locale = Unicode::parse_unicode_locale_id(locale_object->locale()); + + // 4. Assert: locale matches the unicode_locale_id production. + VERIFY(locale.has_value()); + + // 5. If the unicode_language_id production of locale does not contain the ["-" unicode_script_subtag] sequence, return undefined. + if (!locale->language_id.script.has_value()) + return js_undefined(); + + // 6. Return the substring of locale corresponding to the unicode_script_subtag production of the unicode_language_id. + return js_string(vm, *locale->language_id.script); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h index 8827c8cda92..c622f751b67 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h @@ -29,6 +29,7 @@ private: JS_DECLARE_NATIVE_GETTER(numbering_system); JS_DECLARE_NATIVE_GETTER(numeric); JS_DECLARE_NATIVE_GETTER(language); + JS_DECLARE_NATIVE_GETTER(script); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.script.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.script.js new file mode 100644 index 00000000000..34338cb653b --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.script.js @@ -0,0 +1,21 @@ +describe("errors", () => { + test("called on non-Locale object", () => { + expect(() => { + Intl.Locale.prototype.script; + }).toThrowWithMessage(TypeError, "Not a Intl.Locale object"); + }); +}); + +describe("normal behavior", () => { + test("basic functionality", () => { + expect(new Intl.Locale("en").script).toBeUndefined(); + expect(new Intl.Locale("en-Latn").script).toBe("Latn"); + expect(new Intl.Locale("en-GB").script).toBeUndefined(); + expect(new Intl.Locale("en", { script: "Latn" }).script).toBe("Latn"); + expect(new Intl.Locale("en", { region: "GB" }).script).toBeUndefined(); + + expect(new Intl.Locale("en-u-ca-abc").script).toBeUndefined(); + expect(new Intl.Locale("en", { calendar: "abc" }).script).toBeUndefined(); + expect(new Intl.Locale("en-x-abcd").script).toBeUndefined(); + }); +});