[Intl] Updates comments & adds tests to catch monkey-patching

Follow up cl for the change of CanonicalizeLocaleList in
https://tc39.github.io/proposal-intl-locale/#sec-canonicalizelocalelist

Bug: v8:8655
Change-Id: I2505057e03511806320104974519fd4b97848b53
Reviewed-on: https://chromium-review.googlesource.com/c/1423323
Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
Commit-Queue: Frank Tang <ftang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#59014}
This commit is contained in:
Frank Tang 2019-01-19 00:28:09 -08:00 committed by Commit Bot
parent 7ff145b792
commit 008a0d75a4
2 changed files with 44 additions and 4 deletions

View File

@ -851,14 +851,18 @@ Maybe<std::vector<std::string>> Intl::CanonicalizeLocaleList(
Nothing<std::vector<std::string>>());
// 7c ii. If Type(kValue) is not String or Object, throw a TypeError
// exception.
// 7c iii. Let tag be ? ToString(kValue).
// 7c iv. If IsStructurallyValidLanguageTag(tag) is false, throw a
// RangeError exception.
// 7c v. Let canonicalizedTag be CanonicalizeLanguageTag(tag).
// 7c iii. If Type(kValue) is Object and kValue has an [[InitializedLocale]]
// internal slot, then
std::string canonicalized_tag;
if (k_value->IsJSLocale()) {
// 7c iii. 1. Let tag be kValue.[[Locale]].
canonicalized_tag = JSLocale::ToString(Handle<JSLocale>::cast(k_value));
// 7c iv. Else,
} else {
// 7c iv 1. Let tag be ? ToString(kValue).
// 7c v. If IsStructurallyValidLanguageTag(tag) is false, throw a
// RangeError exception.
// 7c vi. Let canonicalizedTag be CanonicalizeLanguageTag(tag).
if (!CanonicalizeLanguageTag(isolate, k_value).To(&canonicalized_tag)) {
return Nothing<std::vector<std::string>>();
}

View File

@ -63,3 +63,39 @@ assertDoesNotThrow(() => col = new Intl.Collator(tag));
assertEquals(tag, lf.resolvedOptions().locale);
assertDoesNotThrow(() => col = new Intl.Collator([tag]));
assertEquals(tag, lf.resolvedOptions().locale);
// Test monkey patching won't impact the result.
class MyLocale extends Intl.Locale {
constructor(tag, options) {
super(tag, options);
}
toString() {
// this should not get called.
fail("toString should not be called")
}
}
let myLocale = new MyLocale(tag);
// Test with a Locale
assertDoesNotThrow(() => nf = new Intl.NumberFormat(myLocale));
assertEquals(tag, nf.resolvedOptions().locale);
// Test with a Array of one Locale
assertDoesNotThrow(() => nf = new Intl.NumberFormat([myLocale]));
assertEquals(tag, nf.resolvedOptions().locale);
var res = Intl.getCanonicalLocales(myLocale);
assertEquals(1, res.length);
assertEquals(tag, res[0]);
res = Intl.getCanonicalLocales([myLocale, "fr"]);
assertEquals(2, res.length);
assertEquals(tag, res[0]);
assertEquals("fr", res[1]);
res = Intl.getCanonicalLocales(["fr", myLocale]);
assertEquals(2, res.length);
assertEquals("fr", res[0]);
assertEquals(tag, res[1]);