Skip to content

Truly independent web browser

LibUnicode: Canonicalize the case of locale keyword values

Timothy Flynn91aabbdac3e8
diff --git a/Libraries/LibUnicode/Locale.cpp b/Libraries/LibUnicode/Locale.cpp
index c5812146d29..d778e65c718 100644
--- a/Libraries/LibUnicode/Locale.cpp
+++ b/Libraries/LibUnicode/Locale.cpp
@@ -571,11 +571,12 @@ Optional<Utf16String> canonicalize_unicode_locale_id(Utf16View locale)
Utf16String canonicalize_unicode_extension_values(StringView key, Utf16View value)
{
VERIFY(value.has_ascii_storage());
+ auto lowercase_value = value.to_ascii_lowercase();
UErrorCode status = U_ZERO_ERROR;
icu::LocaleBuilder builder;
- builder.setUnicodeLocaleKeyword(icu_string_piece(key), icu_string_piece(value.bytes()));
+ builder.setUnicodeLocaleKeyword(icu_string_piece(key), icu_string_piece(lowercase_value.ascii_view()));
auto locale = builder.build(status);
verify_icu_success(status);
@@ -583,6 +584,9 @@ Utf16String canonicalize_unicode_extension_values(StringView key, Utf16View valu
auto result = locale.getUnicodeKeywordValue<StringBuilder>(icu_string_piece(key), status);
verify_icu_success(status);
+ if (result.string_view() == "true"sv)
+ return {};
+
return Utf16String::from_ascii_without_validation(result.string_view().bytes());
}
diff --git a/Tests/LibJS/Runtime/builtins/Intl/Locale/Locale.prototype.calendar.js b/Tests/LibJS/Runtime/builtins/Intl/Locale/Locale.prototype.calendar.js
index c4cf0df4351..fa89878d999 100644
--- a/Tests/LibJS/Runtime/builtins/Intl/Locale/Locale.prototype.calendar.js
+++ b/Tests/LibJS/Runtime/builtins/Intl/Locale/Locale.prototype.calendar.js
@@ -19,4 +19,21 @@ describe("normal behavior", () => {
expect(new Intl.Locale("en", { calendar: "ethiopic-amete-alem" }).calendar).toBe("ethioaa");
expect(new Intl.Locale("en-u-ca-ethiopic-amete-alem").calendar).toBe("ethioaa");
});
+
+ test("option values are canonicalized without regard to case", () => {
+ const values = [
+ ["GREGORY", "gregory"],
+ ["UNKNOWN", "unknown"],
+ ["ISLAMICC", "islamic-civil"],
+ ["ethiopic-AMETE-alem", "ethioaa"],
+ ];
+
+ for (const tag of ["en", "en-u-ca-abc"]) {
+ for (const [value, expected] of values) {
+ const locale = new Intl.Locale(tag, { calendar: value });
+ expect(locale.calendar).toBe(expected);
+ expect(locale.toString()).toBe(`en-u-ca-${expected}`);
+ }
+ }
+ });
});
diff --git a/Tests/LibJS/Runtime/builtins/Intl/Locale/Locale.prototype.collation.js b/Tests/LibJS/Runtime/builtins/Intl/Locale/Locale.prototype.collation.js
index 685a86b4064..c1b6a1cdd3d 100644
--- a/Tests/LibJS/Runtime/builtins/Intl/Locale/Locale.prototype.collation.js
+++ b/Tests/LibJS/Runtime/builtins/Intl/Locale/Locale.prototype.collation.js
@@ -13,4 +13,19 @@ describe("normal behavior", () => {
expect(new Intl.Locale("en", { collation: "abc" }).collation).toBe("abc");
expect(new Intl.Locale("en-u-co-abc", { collation: "def" }).collation).toBe("def");
});
+
+ test("option values are canonicalized without regard to case", () => {
+ const values = [
+ ["PHONEBK", "phonebk"],
+ ["UNKNOWN", "unknown"],
+ ];
+
+ for (const tag of ["en", "en-u-co-abc"]) {
+ for (const [value, expected] of values) {
+ const locale = new Intl.Locale(tag, { collation: value });
+ expect(locale.collation).toBe(expected);
+ expect(locale.toString()).toBe(`en-u-co-${expected}`);
+ }
+ }
+ });
});
diff --git a/Tests/LibJS/Runtime/builtins/Intl/Locale/Locale.prototype.firstDayOfWeek.js b/Tests/LibJS/Runtime/builtins/Intl/Locale/Locale.prototype.firstDayOfWeek.js
index 06373f4525c..5271e7127cf 100644
--- a/Tests/LibJS/Runtime/builtins/Intl/Locale/Locale.prototype.firstDayOfWeek.js
+++ b/Tests/LibJS/Runtime/builtins/Intl/Locale/Locale.prototype.firstDayOfWeek.js
@@ -30,8 +30,31 @@ describe("normal behavior", () => {
});
test("non-standard options", () => {
- [100, Infinity, NaN, "hello", 152n, true].forEach(value => {
- expect(new Intl.Locale("en", { firstDayOfWeek: value }).firstDayOfWeek).toBe(value.toString());
+ [100, Infinity, NaN, "hello", 152n].forEach(value => {
+ expect(new Intl.Locale("en", { firstDayOfWeek: value }).firstDayOfWeek).toBe(
+ value.toString().toLowerCase()
+ );
});
});
+
+ test("option values are canonicalized without regard to case", () => {
+ for (const tag of ["en", "en-u-fw-tue"]) {
+ for (const value of ["MON", "Unknown", NaN, Infinity]) {
+ const expected = String(value).toLowerCase();
+ const locale = new Intl.Locale(tag, { firstDayOfWeek: value });
+ expect(locale.firstDayOfWeek).toBe(expected);
+ expect(locale.toString()).toBe(`en-u-fw-${expected}`);
+ }
+ }
+ });
+
+ test("true is canonicalized to an empty extension value", () => {
+ for (const tag of ["en", "en-u-fw-tue"]) {
+ for (const value of [true, "true", "TRUE"]) {
+ const locale = new Intl.Locale(tag, { firstDayOfWeek: value });
+ expect(locale.firstDayOfWeek).toBe("");
+ expect(locale.toString()).toBe("en-u-fw");
+ }
+ }
+ });
});
diff --git a/Tests/LibJS/Runtime/builtins/Intl/Locale/Locale.prototype.numberingSystem.js b/Tests/LibJS/Runtime/builtins/Intl/Locale/Locale.prototype.numberingSystem.js
index 25310f322e0..c192f88b451 100644
--- a/Tests/LibJS/Runtime/builtins/Intl/Locale/Locale.prototype.numberingSystem.js
+++ b/Tests/LibJS/Runtime/builtins/Intl/Locale/Locale.prototype.numberingSystem.js
@@ -13,4 +13,19 @@ describe("normal behavior", () => {
expect(new Intl.Locale("en", { numberingSystem: "abc" }).numberingSystem).toBe("abc");
expect(new Intl.Locale("en-u-nu-abc", { numberingSystem: "def" }).numberingSystem).toBe("def");
});
+
+ test("option values are canonicalized without regard to case", () => {
+ const values = [
+ ["LATN", "latn"],
+ ["UNKNOWN", "unknown"],
+ ];
+
+ for (const tag of ["en", "en-u-nu-abc"]) {
+ for (const [value, expected] of values) {
+ const locale = new Intl.Locale(tag, { numberingSystem: value });
+ expect(locale.numberingSystem).toBe(expected);
+ expect(locale.toString()).toBe(`en-u-nu-${expected}`);
+ }
+ }
+ });
});