From 1a3e6e8a7be35c48c709563d78d93cb2eacb23da Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 12 Jan 2022 14:18:22 -0500 Subject: [PATCH] LibJS: Add [[LanguageDisplay]] to Intl.DisplayNames's resolvedOptions --- .../Runtime/Intl/DisplayNamesPrototype.cpp | 4 ++ .../DisplayNames.prototype.resolvedOptions.js | 52 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesPrototype.cpp index 0a15918ac1..16d169ce28 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesPrototype.cpp @@ -116,6 +116,10 @@ JS_DEFINE_NATIVE_FUNCTION(DisplayNamesPrototype::resolved_options) MUST(options->create_data_property_or_throw(vm.names.type, js_string(vm, display_names->type_string()))); MUST(options->create_data_property_or_throw(vm.names.fallback, js_string(vm, display_names->fallback_string()))); + // NOTE: Step 4c indicates languageDisplay must not be undefined, but it is only set when the type option is language. + if (display_names->has_language_display()) + MUST(options->create_data_property_or_throw(vm.names.languageDisplay, js_string(vm, display_names->language_display_string()))); + // 5. Return options. return options; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/DisplayNames/DisplayNames.prototype.resolvedOptions.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/DisplayNames/DisplayNames.prototype.resolvedOptions.js index d919e288eb..ad7b30488f 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Intl/DisplayNames/DisplayNames.prototype.resolvedOptions.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/DisplayNames/DisplayNames.prototype.resolvedOptions.js @@ -26,6 +26,58 @@ describe("correct behavior", () => { style: "short", type: "language", fallback: "code", + languageDisplay: "dialect", + }); + + const ar = new Intl.DisplayNames("ar", { type: "calendar" }); + expect(ar.resolvedOptions()).toEqual({ + locale: "ar", + style: "long", + type: "calendar", + fallback: "code", + }); + + const fr = new Intl.DisplayNames("fr", { type: "dateTimeField" }); + expect(fr.resolvedOptions()).toEqual({ + locale: "fr", + style: "long", + type: "dateTimeField", + fallback: "code", + }); + }); + + test("all valid language displays", () => { + const en = new Intl.DisplayNames("en", { type: "language" }); + expect(en.resolvedOptions()).toEqual({ + locale: "en", + style: "long", + type: "language", + fallback: "code", + languageDisplay: "dialect", + }); + + const es419 = new Intl.DisplayNames("es-419", { + type: "language", + languageDisplay: "dialect", + }); + expect(es419.resolvedOptions()).toEqual({ + locale: "es-419", + style: "long", + type: "language", + fallback: "code", + languageDisplay: "dialect", + }); + + const zhHant = new Intl.DisplayNames(["zh-Hant"], { + type: "language", + languageDisplay: "standard", + }); + expect(zhHant.resolvedOptions()).toEqual({ + locale: "zh-Hant", + style: "long", + type: "language", + fallback: "code", + languageDisplay: "standard", }); });