| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
/* Portions Copyright Norbert Lindenberg 2011-2012. */ |
| 6 |
|
| 7 |
/** |
| 8 |
* Compute an internal properties object from |lazyCollatorData|. |
| 9 |
*/ |
| 10 |
function resolveCollatorInternals(lazyCollatorData) { |
| 11 |
assert(IsObject(lazyCollatorData), "lazy data not an object?"); |
| 12 |
|
| 13 |
var internalProps = std_Object_create(null); |
| 14 |
|
| 15 |
var Collator = collatorInternalProperties; |
| 16 |
|
| 17 |
// Step 5. |
| 18 |
internalProps.usage = lazyCollatorData.usage; |
| 19 |
|
| 20 |
// Steps 6-7. |
| 21 |
var collatorIsSorting = lazyCollatorData.usage === "sort"; |
| 22 |
var localeData = collatorIsSorting |
| 23 |
? Collator.sortLocaleData |
| 24 |
: Collator.searchLocaleData; |
| 25 |
|
| 26 |
// Compute effective locale. |
| 27 |
// Step 16. |
| 28 |
var relevantExtensionKeys = Collator.relevantExtensionKeys; |
| 29 |
|
| 30 |
// Step 17. |
| 31 |
var r = ResolveLocale( |
| 32 |
"Collator", |
| 33 |
lazyCollatorData.requestedLocales, |
| 34 |
lazyCollatorData.opt, |
| 35 |
relevantExtensionKeys, |
| 36 |
localeData |
| 37 |
); |
| 38 |
|
| 39 |
// Step 18. |
| 40 |
internalProps.locale = r.locale; |
| 41 |
|
| 42 |
// Step 19. |
| 43 |
var collation = r.co; |
| 44 |
|
| 45 |
// Step 20. |
| 46 |
if (collation === null) { |
| 47 |
collation = "default"; |
| 48 |
} |
| 49 |
|
| 50 |
// Step 21. |
| 51 |
internalProps.collation = collation; |
| 52 |
|
| 53 |
// Step 22. |
| 54 |
internalProps.numeric = r.kn === "true"; |
| 55 |
|
| 56 |
// Step 23. |
| 57 |
internalProps.caseFirst = r.kf; |
| 58 |
|
| 59 |
// Compute remaining collation options. |
| 60 |
// Step 25. |
| 61 |
var s = lazyCollatorData.rawSensitivity; |
| 62 |
if (s === undefined) { |
| 63 |
// In theory the default sensitivity for the "search" collator is |
| 64 |
// locale dependent; in reality the CLDR/ICU default strength is |
| 65 |
// always tertiary. Therefore use "variant" as the default value for |
| 66 |
// both collation modes. |
| 67 |
s = "variant"; |
| 68 |
} |
| 69 |
|
| 70 |
// Step 26. |
| 71 |
internalProps.sensitivity = s; |
| 72 |
|
| 73 |
// Step 28. |
| 74 |
var ignorePunctuation = lazyCollatorData.ignorePunctuation; |
| 75 |
if (ignorePunctuation === undefined) { |
| 76 |
var actualLocale = collatorActualLocale(r.dataLocale); |
| 77 |
ignorePunctuation = intl_isIgnorePunctuation(actualLocale); |
| 78 |
} |
| 79 |
internalProps.ignorePunctuation = ignorePunctuation; |
| 80 |
|
| 81 |
// The caller is responsible for associating |internalProps| with the right |
| 82 |
// object using |setInternalProperties|. |
| 83 |
return internalProps; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Returns an object containing the Collator internal properties of |obj|. |
| 88 |
*/ |
| 89 |
function getCollatorInternals(obj) { |
| 90 |
assert(IsObject(obj), "getCollatorInternals called with non-object"); |
| 91 |
assert( |
| 92 |
intl_GuardToCollator(obj) !== null, |
| 93 |
"getCollatorInternals called with non-Collator" |
| 94 |
); |
| 95 |
|
| 96 |
var internals = getIntlObjectInternals(obj); |
| 97 |
assert( |
| 98 |
internals.type === "Collator", |
| 99 |
"bad type escaped getIntlObjectInternals" |
| 100 |
); |
| 101 |
|
| 102 |
// If internal properties have already been computed, use them. |
| 103 |
var internalProps = maybeInternalProperties(internals); |
| 104 |
if (internalProps) { |
| 105 |
return internalProps; |
| 106 |
} |
| 107 |
|
| 108 |
// Otherwise it's time to fully create them. |
| 109 |
internalProps = resolveCollatorInternals(internals.lazyData); |
| 110 |
setInternalProperties(internals, internalProps); |
| 111 |
return internalProps; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Initializes an object as a Collator. |
| 116 |
* |
| 117 |
* This method is complicated a moderate bit by its implementing initialization |
| 118 |
* as a *lazy* concept. Everything that must happen now, does -- but we defer |
| 119 |
* all the work we can until the object is actually used as a Collator. This |
| 120 |
* later work occurs in |resolveCollatorInternals|; steps not noted here occur |
| 121 |
* there. |
| 122 |
* |
| 123 |
* Spec: ECMAScript Internationalization API Specification, 10.1.1. |
| 124 |
*/ |
| 125 |
function InitializeCollator(collator, locales, options) { |
| 126 |
assert(IsObject(collator), "InitializeCollator called with non-object"); |
| 127 |
assert( |
| 128 |
intl_GuardToCollator(collator) !== null, |
| 129 |
"InitializeCollator called with non-Collator" |
| 130 |
); |
| 131 |
|
| 132 |
// Lazy Collator data has the following structure: |
| 133 |
// |
| 134 |
// { |
| 135 |
// requestedLocales: List of locales, |
| 136 |
// usage: "sort" / "search", |
| 137 |
// opt: // opt object computed in InitializeCollator |
| 138 |
// { |
| 139 |
// localeMatcher: "lookup" / "best fit", |
| 140 |
// co: string matching a Unicode extension type / undefined |
| 141 |
// kn: true / false / undefined, |
| 142 |
// kf: "upper" / "lower" / "false" / undefined |
| 143 |
// } |
| 144 |
// rawSensitivity: "base" / "accent" / "case" / "variant" / undefined, |
| 145 |
// ignorePunctuation: true / false / undefined |
| 146 |
// } |
| 147 |
// |
| 148 |
// Note that lazy data is only installed as a final step of initialization, |
| 149 |
// so every Collator lazy data object has *all* these properties, never a |
| 150 |
// subset of them. |
| 151 |
var lazyCollatorData = std_Object_create(null); |
| 152 |
|
| 153 |
// Step 1. |
| 154 |
var requestedLocales = CanonicalizeLocaleList(locales); |
| 155 |
lazyCollatorData.requestedLocales = requestedLocales; |
| 156 |
|
| 157 |
// Steps 2-3. |
| 158 |
// |
| 159 |
// If we ever need more speed here at startup, we should try to detect the |
| 160 |
// case where |options === undefined| and then directly use the default |
| 161 |
// value for each option. For now, just keep it simple. |
| 162 |
if (options === undefined) { |
| 163 |
options = std_Object_create(null); |
| 164 |
} else { |
| 165 |
options = ToObject(options); |
| 166 |
} |
| 167 |
|
| 168 |
// Compute options that impact interpretation of locale. |
| 169 |
// Step 4. |
| 170 |
var u = GetOption(options, "usage", "string", ["sort", "search"], "sort"); |
| 171 |
lazyCollatorData.usage = u; |
| 172 |
|
| 173 |
// Step 8. |
| 174 |
var opt = new_Record(); |
| 175 |
lazyCollatorData.opt = opt; |
| 176 |
|
| 177 |
// Steps 9-10. |
| 178 |
var matcher = GetOption( |
| 179 |
options, |
| 180 |
"localeMatcher", |
| 181 |
"string", |
| 182 |
["lookup", "best fit"], |
| 183 |
"best fit" |
| 184 |
); |
| 185 |
opt.localeMatcher = matcher; |
| 186 |
|
| 187 |
// https://github.com/tc39/ecma402/pull/459 |
| 188 |
var collation = GetOption( |
| 189 |
options, |
| 190 |
"collation", |
| 191 |
"string", |
| 192 |
undefined, |
| 193 |
undefined |
| 194 |
); |
| 195 |
if (collation !== undefined) { |
| 196 |
collation = intl_ValidateAndCanonicalizeUnicodeExtensionType( |
| 197 |
collation, |
| 198 |
"collation", |
| 199 |
"co" |
| 200 |
); |
| 201 |
} |
| 202 |
opt.co = collation; |
| 203 |
|
| 204 |
// Steps 11-13. |
| 205 |
var numericValue = GetOption( |
| 206 |
options, |
| 207 |
"numeric", |
| 208 |
"boolean", |
| 209 |
undefined, |
| 210 |
undefined |
| 211 |
); |
| 212 |
if (numericValue !== undefined) { |
| 213 |
numericValue = numericValue ? "true" : "false"; |
| 214 |
} |
| 215 |
opt.kn = numericValue; |
| 216 |
|
| 217 |
// Steps 14-15. |
| 218 |
var caseFirstValue = GetOption( |
| 219 |
options, |
| 220 |
"caseFirst", |
| 221 |
"string", |
| 222 |
["upper", "lower", "false"], |
| 223 |
undefined |
| 224 |
); |
| 225 |
opt.kf = caseFirstValue; |
| 226 |
|
| 227 |
// Compute remaining collation options. |
| 228 |
// Step 24. |
| 229 |
var s = GetOption( |
| 230 |
options, |
| 231 |
"sensitivity", |
| 232 |
"string", |
| 233 |
["base", "accent", "case", "variant"], |
| 234 |
undefined |
| 235 |
); |
| 236 |
lazyCollatorData.rawSensitivity = s; |
| 237 |
|
| 238 |
// Step 27. |
| 239 |
var ip = GetOption(options, "ignorePunctuation", "boolean", undefined, undefined); |
| 240 |
lazyCollatorData.ignorePunctuation = ip; |
| 241 |
|
| 242 |
// Step 29. |
| 243 |
// |
| 244 |
// We've done everything that must be done now: mark the lazy data as fully |
| 245 |
// computed and install it. |
| 246 |
initializeIntlObject(collator, "Collator", lazyCollatorData); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Returns the subset of the given locale list for which this locale list has a |
| 251 |
* matching (possibly fallback) locale. Locales appear in the same order in the |
| 252 |
* returned list as in the input list. |
| 253 |
* |
| 254 |
* Spec: ECMAScript Internationalization API Specification, 10.2.2. |
| 255 |
*/ |
| 256 |
function Intl_Collator_supportedLocalesOf(locales /*, options*/) { |
| 257 |
var options = ArgumentsLength() > 1 ? GetArgument(1) : undefined; |
| 258 |
|
| 259 |
// Step 1. |
| 260 |
var availableLocales = "Collator"; |
| 261 |
|
| 262 |
// Step 2. |
| 263 |
var requestedLocales = CanonicalizeLocaleList(locales); |
| 264 |
|
| 265 |
// Step 3. |
| 266 |
return SupportedLocales(availableLocales, requestedLocales, options); |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Collator internal properties. |
| 271 |
* |
| 272 |
* Spec: ECMAScript Internationalization API Specification, 9.1 and 10.2.3. |
| 273 |
*/ |
| 274 |
var collatorInternalProperties = { |
| 275 |
sortLocaleData: collatorSortLocaleData, |
| 276 |
searchLocaleData: collatorSearchLocaleData, |
| 277 |
relevantExtensionKeys: ["co", "kf", "kn"], |
| 278 |
}; |
| 279 |
|
| 280 |
/** |
| 281 |
* Returns the actual locale used when a collator for |locale| is constructed. |
| 282 |
*/ |
| 283 |
function collatorActualLocale(locale) { |
| 284 |
assert(typeof locale === "string", "locale should be string"); |
| 285 |
|
| 286 |
// If |locale| is the default locale (e.g. da-DK), but only supported |
| 287 |
// through a fallback (da), we need to get the actual locale before we |
| 288 |
// can call intl_isUpperCaseFirst. Also see intl_BestAvailableLocale. |
| 289 |
return BestAvailableLocaleIgnoringDefault("Collator", locale); |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Returns the default caseFirst values for the given locale. The first |
| 294 |
* element in the returned array denotes the default value per ES2017 Intl, |
| 295 |
* 9.1 Internal slots of Service Constructors. |
| 296 |
*/ |
| 297 |
function collatorSortCaseFirst(locale) { |
| 298 |
var actualLocale = collatorActualLocale(locale); |
| 299 |
if (intl_isUpperCaseFirst(actualLocale)) { |
| 300 |
return ["upper", "false", "lower"]; |
| 301 |
} |
| 302 |
|
| 303 |
// Default caseFirst values for all other languages. |
| 304 |
return ["false", "lower", "upper"]; |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Returns the default caseFirst value for the given locale. |
| 309 |
*/ |
| 310 |
function collatorSortCaseFirstDefault(locale) { |
| 311 |
var actualLocale = collatorActualLocale(locale); |
| 312 |
if (intl_isUpperCaseFirst(actualLocale)) { |
| 313 |
return "upper"; |
| 314 |
} |
| 315 |
|
| 316 |
// Default caseFirst value for all other languages. |
| 317 |
return "false"; |
| 318 |
} |
| 319 |
|
| 320 |
function collatorSortLocaleData() { |
| 321 |
/* eslint-disable object-shorthand */ |
| 322 |
return { |
| 323 |
co: intl_availableCollations, |
| 324 |
kn: function() { |
| 325 |
return ["false", "true"]; |
| 326 |
}, |
| 327 |
kf: collatorSortCaseFirst, |
| 328 |
default: { |
| 329 |
co: function() { |
| 330 |
// The first element of the collations array must be |null| |
| 331 |
// per ES2017 Intl, 10.2.3 Internal Slots. |
| 332 |
return null; |
| 333 |
}, |
| 334 |
kn: function() { |
| 335 |
return "false"; |
| 336 |
}, |
| 337 |
kf: collatorSortCaseFirstDefault, |
| 338 |
}, |
| 339 |
}; |
| 340 |
/* eslint-enable object-shorthand */ |
| 341 |
} |
| 342 |
|
| 343 |
function collatorSearchLocaleData() { |
| 344 |
/* eslint-disable object-shorthand */ |
| 345 |
return { |
| 346 |
co: function() { |
| 347 |
return [null]; |
| 348 |
}, |
| 349 |
kn: function() { |
| 350 |
return ["false", "true"]; |
| 351 |
}, |
| 352 |
kf: function() { |
| 353 |
return ["false", "lower", "upper"]; |
| 354 |
}, |
| 355 |
default: { |
| 356 |
co: function() { |
| 357 |
return null; |
| 358 |
}, |
| 359 |
kn: function() { |
| 360 |
return "false"; |
| 361 |
}, |
| 362 |
kf: function() { |
| 363 |
return "false"; |
| 364 |
}, |
| 365 |
}, |
| 366 |
}; |
| 367 |
/* eslint-enable object-shorthand */ |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* Create function to be cached and returned by Intl.Collator.prototype.compare. |
| 372 |
* |
| 373 |
* Spec: ECMAScript Internationalization API Specification, 10.3.3.1. |
| 374 |
*/ |
| 375 |
function createCollatorCompare(collator) { |
| 376 |
// This function is not inlined in $Intl_Collator_compare_get to avoid |
| 377 |
// creating a call-object on each call to $Intl_Collator_compare_get. |
| 378 |
return function(x, y) { |
| 379 |
// Step 1 (implicit). |
| 380 |
|
| 381 |
// Step 2. |
| 382 |
assert(IsObject(collator), "collatorCompareToBind called with non-object"); |
| 383 |
assert( |
| 384 |
intl_GuardToCollator(collator) !== null, |
| 385 |
"collatorCompareToBind called with non-Collator" |
| 386 |
); |
| 387 |
|
| 388 |
// Steps 3-6 |
| 389 |
var X = ToString(x); |
| 390 |
var Y = ToString(y); |
| 391 |
|
| 392 |
// Step 7. |
| 393 |
return intl_CompareStrings(collator, X, Y); |
| 394 |
}; |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
* Returns a function bound to this Collator that compares x (converted to a |
| 399 |
* String value) and y (converted to a String value), |
| 400 |
* and returns a number less than 0 if x < y, 0 if x = y, or a number greater |
| 401 |
* than 0 if x > y according to the sort order for the locale and collation |
| 402 |
* options of this Collator object. |
| 403 |
* |
| 404 |
* Spec: ECMAScript Internationalization API Specification, 10.3.3. |
| 405 |
*/ |
| 406 |
// Uncloned functions with `$` prefix are allocated as extended function |
| 407 |
// to store the original name in `SetCanonicalName`. |
| 408 |
function $Intl_Collator_compare_get() { |
| 409 |
// Step 1. |
| 410 |
var collator = this; |
| 411 |
|
| 412 |
// Steps 2-3. |
| 413 |
if ( |
| 414 |
!IsObject(collator) || |
| 415 |
(collator = intl_GuardToCollator(collator)) === null |
| 416 |
) { |
| 417 |
return callFunction( |
| 418 |
intl_CallCollatorMethodIfWrapped, |
| 419 |
this, |
| 420 |
"$Intl_Collator_compare_get" |
| 421 |
); |
| 422 |
} |
| 423 |
|
| 424 |
var internals = getCollatorInternals(collator); |
| 425 |
|
| 426 |
// Step 4. |
| 427 |
if (internals.boundCompare === undefined) { |
| 428 |
// Steps 4.a-c. |
| 429 |
internals.boundCompare = createCollatorCompare(collator); |
| 430 |
} |
| 431 |
|
| 432 |
// Step 5. |
| 433 |
return internals.boundCompare; |
| 434 |
} |
| 435 |
SetCanonicalName($Intl_Collator_compare_get, "get compare"); |
| 436 |
|
| 437 |
/** |
| 438 |
* Returns the resolved options for a Collator object. |
| 439 |
* |
| 440 |
* Spec: ECMAScript Internationalization API Specification, 10.3.4. |
| 441 |
*/ |
| 442 |
function Intl_Collator_resolvedOptions() { |
| 443 |
// Step 1. |
| 444 |
var collator = this; |
| 445 |
|
| 446 |
// Steps 2-3. |
| 447 |
if ( |
| 448 |
!IsObject(collator) || |
| 449 |
(collator = intl_GuardToCollator(collator)) === null |
| 450 |
) { |
| 451 |
return callFunction( |
| 452 |
intl_CallCollatorMethodIfWrapped, |
| 453 |
this, |
| 454 |
"Intl_Collator_resolvedOptions" |
| 455 |
); |
| 456 |
} |
| 457 |
|
| 458 |
var internals = getCollatorInternals(collator); |
| 459 |
|
| 460 |
// Steps 4-5. |
| 461 |
var result = { |
| 462 |
locale: internals.locale, |
| 463 |
usage: internals.usage, |
| 464 |
sensitivity: internals.sensitivity, |
| 465 |
ignorePunctuation: internals.ignorePunctuation, |
| 466 |
collation: internals.collation, |
| 467 |
numeric: internals.numeric, |
| 468 |
caseFirst: internals.caseFirst, |
| 469 |
}; |
| 470 |
|
| 471 |
// Step 6. |
| 472 |
return result; |
| 473 |
} |