Skip to content

Truly independent web browser

LibJS+LibUnicode: Preserve unknown locale text directions

Timothy Flynn37e0c14de752
diff --git a/Libraries/LibJS/Rust/src/runtime/intl/locale.rs b/Libraries/LibJS/Rust/src/runtime/intl/locale.rs
index a4007698887..35dbadc1251 100644
--- a/Libraries/LibJS/Rust/src/runtime/intl/locale.rs
+++ b/Libraries/LibJS/Rust/src/runtime/intl/locale.rs
@@ -449,26 +449,34 @@ pub fn time_zones_of_locale(vm: &Vm, locale_object: &Locale) -> Value {
}
// 15.5.14 TextDirectionOfLocale ( loc ), https://tc39.es/ecma402/#sec-textdirectionoflocale
-pub fn text_direction_of_locale(locale_object: &Locale) -> &'static str {
+pub fn text_direction_of_locale(locale_object: &Locale) -> Option<&'static str> {
// 1. Let locale be loc.[[Locale]].
let locale = locale_object.locale();
// 2. Let script be GetLocaleScript(locale).
+ let locale_id = locale_object.locale_id();
+
// 3. If script is undefined, then
- // a. Let maximal be the result of the Add Likely Subtags algorithm applied to locale. If an error is signaled, return undefined.
- // b. Set script to GetLocaleScript(maximal).
- // c. If script is undefined, return undefined.
- // NB: ICU handles maximizing the locale if there is no script.
+ let script = if let Some(script) = locale_id.language_id.script {
+ script
+ } else {
+ // a. Let maximal be the result of the Add Likely Subtags algorithm applied to locale. If an error is signaled, return undefined.
+ let maximal = unicode::add_likely_subtags(Utf16View::of_string(&locale))?;
+
+ // b. Set script to GetLocaleScript(maximal).
+ let script = unicode::parse_unicode_locale_id(Utf16View::of_string(&maximal))?
+ .language_id
+ .script;
+
+ // c. If script is undefined, return undefined.
+ script?
+ };
// 4. If the default general ordering of characters within a line in script is right-to-left, return "rtl".
// 5. If the default general ordering of characters within a line in script is left-to-right, return "ltr".
// 6. Return undefined.
- // FIXME: ICU does not provide a method to determine if a locale is neither rtl nor ltr.
- if unicode::is_locale_character_ordering_right_to_left(Utf16View::of_string(&locale)) {
- "rtl"
- } else {
- "ltr"
- }
+ unicode::is_script_right_to_left(Utf16View::of_string(&script))
+ .map(|right_to_left| if right_to_left { "rtl" } else { "ltr" })
}
struct FirstDayStringAndValue {
diff --git a/Libraries/LibJS/Rust/src/runtime/intl/locale_prototype.rs b/Libraries/LibJS/Rust/src/runtime/intl/locale_prototype.rs
index bb8d7c37170..50c70eec8b2 100644
--- a/Libraries/LibJS/Rust/src/runtime/intl/locale_prototype.rs
+++ b/Libraries/LibJS/Rust/src/runtime/intl/locale_prototype.rs
@@ -296,15 +296,13 @@ impl LocalePrototype {
let info = Object::create(vm, realm, Some(realm.object_prototype()));
// 4. Let dir be TextDirectionOfLocale(loc).
- let direction = text_direction_of_locale(&locale_object);
+ let direction = text_direction_of_locale(&locale_object)
+ .map(|direction| Value::from_string(PrimitiveString::create_from_utf8(vm, direction)))
+ .unwrap_or(Value::UNDEFINED);
// 5. Perform ! CreateDataPropertyOrThrow(info, "direction", dir).
- info.create_data_property_or_throw(
- vm,
- &vm.names.direction,
- Value::from_string(PrimitiveString::create_from_utf8(vm, direction)),
- )
- .must();
+ info.create_data_property_or_throw(vm, &vm.names.direction, direction)
+ .must();
// 6. Return info.
Ok(Value::from_object(info))
diff --git a/Libraries/LibJS/Rust/src/unicode/intl.rs b/Libraries/LibJS/Rust/src/unicode/intl.rs
index 68f1f1c084b..88d59a6ff2c 100644
--- a/Libraries/LibJS/Rust/src/unicode/intl.rs
+++ b/Libraries/LibJS/Rust/src/unicode/intl.rs
@@ -68,7 +68,7 @@ unsafe extern "C" {
fn unicode_is_locale_available(locale: UnicodeIntlText) -> bool;
fn unicode_add_likely_subtags(locale: UnicodeIntlText, output: UnicodeTextMappingOutput) -> bool;
fn unicode_remove_likely_subtags(locale: UnicodeIntlText, output: UnicodeTextMappingOutput) -> bool;
- fn unicode_is_locale_character_ordering_right_to_left(locale: UnicodeIntlText) -> bool;
+ fn unicode_is_script_right_to_left(script: UnicodeIntlText, right_to_left: *mut bool) -> bool;
fn unicode_default_locale(output: UnicodeTextMappingOutput);
fn unicode_available_keyword_values(
@@ -581,10 +581,12 @@ pub fn remove_likely_subtags(locale: Utf16View<'_>) -> Option<Utf16String> {
optional_text_output(|output| unsafe { unicode_remove_likely_subtags(UnicodeIntlText::of(locale), output) })
}
-/// Unicode::is_locale_character_ordering_right_to_left.
-pub fn is_locale_character_ordering_right_to_left(locale: Utf16View<'_>) -> bool {
- // SAFETY: The locale is valid for its length.
- unsafe { unicode_is_locale_character_ordering_right_to_left(UnicodeIntlText::of(locale)) }
+/// Unicode::is_script_right_to_left.
+pub fn is_script_right_to_left(script: Utf16View<'_>) -> Option<bool> {
+ let mut right_to_left = false;
+ // SAFETY: The script is valid for its length, and the output points to a live bool.
+ let has_direction = unsafe { unicode_is_script_right_to_left(UnicodeIntlText::of(script), &raw mut right_to_left) };
+ has_direction.then_some(right_to_left)
}
/// Unicode::available_keyword_values.
diff --git a/Libraries/LibUnicode/IntlExports.cpp b/Libraries/LibUnicode/IntlExports.cpp
index 04b6d427815..6781eac256b 100644
--- a/Libraries/LibUnicode/IntlExports.cpp
+++ b/Libraries/LibUnicode/IntlExports.cpp
@@ -174,9 +174,13 @@ extern "C" bool unicode_remove_likely_subtags(UnicodeIntlText locale, UnicodeTex
return true;
}
-extern "C" bool unicode_is_locale_character_ordering_right_to_left(UnicodeIntlText locale)
+extern "C" bool unicode_is_script_right_to_left(UnicodeIntlText script, bool* right_to_left)
{
- return Unicode::is_locale_character_ordering_right_to_left(text_view(locale));
+ auto result = Unicode::is_script_right_to_left(text_view(script));
+ if (!result.has_value())
+ return false;
+ *right_to_left = *result;
+ return true;
}
extern "C" void unicode_available_keyword_values(UnicodeIntlText locale, UnicodeIntlText key, void* context, UnicodeAppendString append)
diff --git a/Libraries/LibUnicode/IntlExports.h b/Libraries/LibUnicode/IntlExports.h
index 64e94773c13..7539927d055 100644
--- a/Libraries/LibUnicode/IntlExports.h
+++ b/Libraries/LibUnicode/IntlExports.h
@@ -51,7 +51,7 @@ void unicode_canonicalize_unicode_extension_values(UnicodeIntlText key, UnicodeI
bool unicode_is_locale_available(UnicodeIntlText locale);
bool unicode_add_likely_subtags(UnicodeIntlText locale, UnicodeTextMappingOutput);
bool unicode_remove_likely_subtags(UnicodeIntlText locale, UnicodeTextMappingOutput);
-bool unicode_is_locale_character_ordering_right_to_left(UnicodeIntlText locale);
+bool unicode_is_script_right_to_left(UnicodeIntlText script, bool* right_to_left);
void unicode_available_keyword_values(UnicodeIntlText locale, UnicodeIntlText key, void* context, UnicodeAppendString);
void unicode_available_calendars(void* context, UnicodeAppendString);
diff --git a/Libraries/LibUnicode/Locale.cpp b/Libraries/LibUnicode/Locale.cpp
index d778e65c718..18d4f309b30 100644
--- a/Libraries/LibUnicode/Locale.cpp
+++ b/Libraries/LibUnicode/Locale.cpp
@@ -17,6 +17,7 @@
#include <unicode/localebuilder.h>
#include <unicode/locid.h>
+#include <unicode/uscript.h>
namespace Unicode {
@@ -752,13 +753,22 @@ Optional<Utf16String> remove_likely_subtags(Utf16View locale)
return Utf16String::from_ascii_without_validation(result.string_view().bytes());
}
-bool is_locale_character_ordering_right_to_left(Utf16View locale)
+Optional<bool> is_script_right_to_left(Utf16View script)
{
- auto locale_data = LocaleData::for_locale(locale.bytes());
- if (!locale_data.has_value())
- return false; // Default to left-to-right
+ UErrorCode status = U_ZERO_ERROR;
+
+ UScriptCode script_code = USCRIPT_INVALID_CODE;
+ auto script_name = MUST(script.to_byte_string());
+ auto count = uscript_getCode(script_name.characters(), &script_code, 1, &status);
+ if (icu_failure(status) || count != 1)
+ return {};
+
+ // ICU's direction predicate returns false for scripts with no direction data. Common and Inherited also do
+ // not have a writing direction.
+ if (script_code == USCRIPT_COMMON || script_code == USCRIPT_INHERITED || uscript_getUsage(script_code) <= USCRIPT_USAGE_UNKNOWN)
+ return {};
- return static_cast<bool>(locale_data->locale().isRightToLeft());
+ return static_cast<bool>(uscript_isRightToLeft(script_code));
}
static void append_to_builder(StringBuilder& builder, StringView string)
diff --git a/Libraries/LibUnicode/Locale.h b/Libraries/LibUnicode/Locale.h
index cb970b27991..e870461e46f 100644
--- a/Libraries/LibUnicode/Locale.h
+++ b/Libraries/LibUnicode/Locale.h
@@ -196,6 +196,6 @@ Utf16String style_to_string(Style style);
Optional<Utf16String> add_likely_subtags(Utf16View);
Optional<Utf16String> remove_likely_subtags(Utf16View);
-bool is_locale_character_ordering_right_to_left(Utf16View locale);
+Optional<bool> is_script_right_to_left(Utf16View script);
}
diff --git a/Tests/LibJS/Runtime/builtins/Intl/Locale/Locale.prototype.getTextInfo.js b/Tests/LibJS/Runtime/builtins/Intl/Locale/Locale.prototype.getTextInfo.js
index ee491ac3c2b..cc01c6e17dc 100644
--- a/Tests/LibJS/Runtime/builtins/Intl/Locale/Locale.prototype.getTextInfo.js
+++ b/Tests/LibJS/Runtime/builtins/Intl/Locale/Locale.prototype.getTextInfo.js
@@ -20,7 +20,32 @@ describe("normal behavior", () => {
expect(new Intl.Locale("ar").getTextInfo().direction).toBe("rtl");
});
- test("fallback to ltr", () => {
- expect(new Intl.Locale("xx").getTextInfo().direction).toBe("ltr");
+ test("infer the script from likely subtags", () => {
+ expect(new Intl.Locale("und").getTextInfo().direction).toBe("ltr");
+ expect(new Intl.Locale("pa").getTextInfo().direction).toBe("ltr");
+ expect(new Intl.Locale("pa-PK").getTextInfo().direction).toBe("rtl");
+ });
+
+ test("unknown likely script", () => {
+ for (const tag of ["xx", "tlh", "qfz", "abcdefgh"]) {
+ const textInfo = new Intl.Locale(tag).getTextInfo();
+ expect(Object.hasOwn(textInfo, "direction")).toBeTrue();
+ expect(textInfo.direction).toBeUndefined();
+ }
+ });
+
+ test("explicit script takes precedence over the language", () => {
+ for (const language of ["und", "en", "ar", "tlh", "abcdefgh"]) {
+ expect(new Intl.Locale(`${language}-Latn`).getTextInfo().direction).toBe("ltr");
+ expect(new Intl.Locale(`${language}-Arab`).getTextInfo().direction).toBe("rtl");
+ }
+ });
+
+ test("scripts with unknown direction", () => {
+ for (const language of ["und", "en", "ar", "tlh", "abcdefgh"]) {
+ for (const script of ["Aaaa", "Qaaq", "True", "Zyyy", "Zinh", "Zzzz", "Brai", "Zxxx", "Aran"]) {
+ expect(new Intl.Locale(`${language}-${script}`).getTextInfo().direction).toBeUndefined();
+ }
+ }
});
});