tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

unicode-ext-canonicalize-subdivision.js (2788B)


      1 // Copyright (C) 2020 André Bargull. All rights reserved.
      2 // This code is governed by the BSD license found in the LICENSE file.
      3 
      4 /*---
      5 esid: sec-intl.getcanonicallocales
      6 description: >
      7  Test Unicode extension subtag canonicalisation for the "sd" extension key.
      8 info: |
      9  8.2.1 Intl.getCanonicalLocales (locales)
     10    1. Let ll be ? CanonicalizeLocaleList(locales).
     11    2. Return CreateArrayFromList(ll).
     12 
     13  9.2.1 CanonicalizeLocaleList (locales)
     14    ...
     15    7. Repeat, while k < len
     16      ...
     17      c. If kPresent is true, then
     18        ...
     19        v. If IsStructurallyValidLanguageTag(tag) is false, throw a RangeError exception.
     20        vi. Let canonicalizedTag be CanonicalizeUnicodeLocaleId(tag).
     21        ...
     22 
     23  UTS 35, §3.2.1 Canonical Unicode Locale Identifiers
     24    Use the bcp47 data to replace keys, types, tfields, and tvalues by their canonical forms.
     25    See Section 3.6.4 U Extension Data Files) and Section 3.7.1 T Extension Data Files. The
     26    aliases are in the alias attribute value, while the canonical is in the name attribute value.
     27 
     28    Replace aliases in special key values:
     29      If there is an 'sd' or 'rg' key, replace any subdivision alias in its value in the same way,
     30      using subdivisionAlias data.
     31 includes: [testIntl.js]
     32 ---*/
     33 
     34 const testData = {
     35  // <subdivisionAlias type="no23" replacement="no50" reason="deprecated"/>
     36  "no23": "no50",
     37 
     38  // <subdivisionAlias type="cn11" replacement="cnbj" reason="deprecated"/>
     39  "cn11": "cnbj",
     40 
     41  // <subdivisionAlias type="cz10a" replacement="cz110" reason="deprecated"/>
     42  "cz10a": "cz110",
     43 
     44  // <subdivisionAlias type="fra" replacement="frges" reason="deprecated"/>
     45  "fra": "frges",
     46 
     47  // <subdivisionAlias type="frg" replacement="frges" reason="deprecated"/>
     48  "frg": "frges",
     49 
     50  // <subdivisionAlias type="lud" replacement="lucl ludi lurd luvd luwi" reason="deprecated"/>
     51  "lud": "lucl",
     52 };
     53 
     54 for (let [alias, name] of Object.entries(testData)) {
     55  // Subdivision codes should always have a matching region subtag. This
     56  // shouldn't actually matter for canonicalisation, but let's not push our
     57  // luck and instead keep the language tag 'valid' per UTS 35, §3.6.5.
     58  let region = name.substring(0, 2).toUpperCase();
     59 
     60  let tag = `und-${region}-u-sd-${alias}`;
     61  let canonical = `und-${region}-u-sd-${name}`;
     62 
     63  // Make sure the test data is correct.
     64  assert.sameValue(isCanonicalizedStructurallyValidLanguageTag(tag), false,
     65                   "\"" + tag + "\" isn't a canonical language tag.");
     66  assert(isCanonicalizedStructurallyValidLanguageTag(canonical),
     67         "\"" + canonical + "\" is a canonical and structurally valid language tag.");
     68 
     69  let result = Intl.getCanonicalLocales(tag);
     70  assert.sameValue(result.length, 1);
     71  assert.sameValue(result[0], canonical);
     72 }
     73 
     74 reportCompare(0, 0);