| 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 |
/** |
| 6 |
* ListFormat internal properties. |
| 7 |
*/ |
| 8 |
function listFormatLocaleData() { |
| 9 |
// ListFormat don't support any extension keys. |
| 10 |
return {}; |
| 11 |
} |
| 12 |
var listFormatInternalProperties = { |
| 13 |
localeData: listFormatLocaleData, |
| 14 |
relevantExtensionKeys: [], |
| 15 |
}; |
| 16 |
|
| 17 |
/** |
| 18 |
* Intl.ListFormat ( [ locales [ , options ] ] ) |
| 19 |
* |
| 20 |
* Compute an internal properties object from |lazyListFormatData|. |
| 21 |
*/ |
| 22 |
function resolveListFormatInternals(lazyListFormatData) { |
| 23 |
assert(IsObject(lazyListFormatData), "lazy data not an object?"); |
| 24 |
|
| 25 |
var internalProps = std_Object_create(null); |
| 26 |
|
| 27 |
var ListFormat = listFormatInternalProperties; |
| 28 |
|
| 29 |
// Compute effective locale. |
| 30 |
|
| 31 |
// Step 9. |
| 32 |
var localeData = ListFormat.localeData; |
| 33 |
|
| 34 |
// Step 10. |
| 35 |
var r = ResolveLocale( |
| 36 |
"ListFormat", |
| 37 |
lazyListFormatData.requestedLocales, |
| 38 |
lazyListFormatData.opt, |
| 39 |
ListFormat.relevantExtensionKeys, |
| 40 |
localeData |
| 41 |
); |
| 42 |
|
| 43 |
// Step 11. |
| 44 |
internalProps.locale = r.locale; |
| 45 |
|
| 46 |
// Step 13. |
| 47 |
internalProps.type = lazyListFormatData.type; |
| 48 |
|
| 49 |
// Step 15. |
| 50 |
internalProps.style = lazyListFormatData.style; |
| 51 |
|
| 52 |
// Steps 16-23 (not applicable in our implementation). |
| 53 |
|
| 54 |
// The caller is responsible for associating |internalProps| with the right |
| 55 |
// object using |setInternalProperties|. |
| 56 |
return internalProps; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Returns an object containing the ListFormat internal properties of |obj|. |
| 61 |
*/ |
| 62 |
function getListFormatInternals(obj) { |
| 63 |
assert(IsObject(obj), "getListFormatInternals called with non-object"); |
| 64 |
assert( |
| 65 |
intl_GuardToListFormat(obj) !== null, |
| 66 |
"getListFormatInternals called with non-ListFormat" |
| 67 |
); |
| 68 |
|
| 69 |
var internals = getIntlObjectInternals(obj); |
| 70 |
assert( |
| 71 |
internals.type === "ListFormat", |
| 72 |
"bad type escaped getIntlObjectInternals" |
| 73 |
); |
| 74 |
|
| 75 |
// If internal properties have already been computed, use them. |
| 76 |
var internalProps = maybeInternalProperties(internals); |
| 77 |
if (internalProps) { |
| 78 |
return internalProps; |
| 79 |
} |
| 80 |
|
| 81 |
// Otherwise it's time to fully create them. |
| 82 |
internalProps = resolveListFormatInternals(internals.lazyData); |
| 83 |
setInternalProperties(internals, internalProps); |
| 84 |
return internalProps; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Intl.ListFormat ( [ locales [ , options ] ] ) |
| 89 |
* |
| 90 |
* Initializes an object as a ListFormat. |
| 91 |
* |
| 92 |
* This method is complicated a moderate bit by its implementing initialization |
| 93 |
* as a *lazy* concept. Everything that must happen now, does -- but we defer |
| 94 |
* all the work we can until the object is actually used as a ListFormat. |
| 95 |
* This later work occurs in |resolveListFormatInternals|; steps not noted |
| 96 |
* here occur there. |
| 97 |
*/ |
| 98 |
function InitializeListFormat(listFormat, locales, options) { |
| 99 |
assert(IsObject(listFormat), "InitializeListFormat called with non-object"); |
| 100 |
assert( |
| 101 |
intl_GuardToListFormat(listFormat) !== null, |
| 102 |
"InitializeListFormat called with non-ListFormat" |
| 103 |
); |
| 104 |
|
| 105 |
// Lazy ListFormat data has the following structure: |
| 106 |
// |
| 107 |
// { |
| 108 |
// requestedLocales: List of locales, |
| 109 |
// type: "conjunction" / "disjunction" / "unit", |
| 110 |
// style: "long" / "short" / "narrow", |
| 111 |
// |
| 112 |
// opt: // opt object computed in InitializeListFormat |
| 113 |
// { |
| 114 |
// localeMatcher: "lookup" / "best fit", |
| 115 |
// } |
| 116 |
// } |
| 117 |
// |
| 118 |
// Note that lazy data is only installed as a final step of initialization, |
| 119 |
// so every ListFormat lazy data object has *all* these properties, never a |
| 120 |
// subset of them. |
| 121 |
var lazyListFormatData = std_Object_create(null); |
| 122 |
|
| 123 |
// Step 3. |
| 124 |
var requestedLocales = CanonicalizeLocaleList(locales); |
| 125 |
lazyListFormatData.requestedLocales = requestedLocales; |
| 126 |
|
| 127 |
// Steps 4-5. |
| 128 |
if (options === undefined) { |
| 129 |
options = std_Object_create(null); |
| 130 |
} else if (!IsObject(options)) { |
| 131 |
ThrowTypeError( |
| 132 |
JSMSG_OBJECT_REQUIRED, |
| 133 |
options === null ? "null" : typeof options |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
// Step 6. |
| 138 |
var opt = new_Record(); |
| 139 |
lazyListFormatData.opt = opt; |
| 140 |
|
| 141 |
// Steps 7-8. |
| 142 |
var matcher = GetOption( |
| 143 |
options, |
| 144 |
"localeMatcher", |
| 145 |
"string", |
| 146 |
["lookup", "best fit"], |
| 147 |
"best fit" |
| 148 |
); |
| 149 |
opt.localeMatcher = matcher; |
| 150 |
|
| 151 |
// Compute formatting options. |
| 152 |
|
| 153 |
// Steps 12-13. |
| 154 |
var type = GetOption( |
| 155 |
options, |
| 156 |
"type", |
| 157 |
"string", |
| 158 |
["conjunction", "disjunction", "unit"], |
| 159 |
"conjunction" |
| 160 |
); |
| 161 |
lazyListFormatData.type = type; |
| 162 |
|
| 163 |
// Steps 14-15. |
| 164 |
var style = GetOption( |
| 165 |
options, |
| 166 |
"style", |
| 167 |
"string", |
| 168 |
["long", "short", "narrow"], |
| 169 |
"long" |
| 170 |
); |
| 171 |
lazyListFormatData.style = style; |
| 172 |
|
| 173 |
// We've done everything that must be done now: mark the lazy data as fully |
| 174 |
// computed and install it. |
| 175 |
initializeIntlObject(listFormat, "ListFormat", lazyListFormatData); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Returns the subset of the given locale list for which this locale list has a |
| 180 |
* matching (possibly fallback) locale. Locales appear in the same order in the |
| 181 |
* returned list as in the input list. |
| 182 |
*/ |
| 183 |
function Intl_ListFormat_supportedLocalesOf(locales /*, options*/) { |
| 184 |
var options = ArgumentsLength() > 1 ? GetArgument(1) : undefined; |
| 185 |
|
| 186 |
// Step 1. |
| 187 |
var availableLocales = "ListFormat"; |
| 188 |
|
| 189 |
// Step 2. |
| 190 |
var requestedLocales = CanonicalizeLocaleList(locales); |
| 191 |
|
| 192 |
// Step 3. |
| 193 |
return SupportedLocales(availableLocales, requestedLocales, options); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* StringListFromIterable ( iterable ) |
| 198 |
*/ |
| 199 |
function StringListFromIterable(iterable, methodName) { |
| 200 |
// Step 1. |
| 201 |
if (iterable === undefined) { |
| 202 |
return []; |
| 203 |
} |
| 204 |
|
| 205 |
// Step 3. |
| 206 |
var list = []; |
| 207 |
|
| 208 |
// Steps 2, 4-5. |
| 209 |
for (var element of allowContentIter(iterable)) { |
| 210 |
// Step 5.b.ii. |
| 211 |
if (typeof element !== "string") { |
| 212 |
ThrowTypeError( |
| 213 |
JSMSG_NOT_EXPECTED_TYPE, |
| 214 |
methodName, |
| 215 |
"string", |
| 216 |
typeof element |
| 217 |
); |
| 218 |
} |
| 219 |
|
| 220 |
// Step 5.b.iii. |
| 221 |
DefineDataProperty(list, list.length, element); |
| 222 |
} |
| 223 |
|
| 224 |
// Step 6. |
| 225 |
return list; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Intl.ListFormat.prototype.format ( list ) |
| 230 |
*/ |
| 231 |
function Intl_ListFormat_format(list) { |
| 232 |
// Step 1. |
| 233 |
var listFormat = this; |
| 234 |
|
| 235 |
// Steps 2-3. |
| 236 |
if ( |
| 237 |
!IsObject(listFormat) || |
| 238 |
(listFormat = intl_GuardToListFormat(listFormat)) === null |
| 239 |
) { |
| 240 |
return callFunction( |
| 241 |
intl_CallListFormatMethodIfWrapped, |
| 242 |
this, |
| 243 |
list, |
| 244 |
"Intl_ListFormat_format" |
| 245 |
); |
| 246 |
} |
| 247 |
|
| 248 |
// Step 4. |
| 249 |
var stringList = StringListFromIterable(list, "format"); |
| 250 |
|
| 251 |
// We can directly return if |stringList| contains less than two elements. |
| 252 |
if (stringList.length < 2) { |
| 253 |
return stringList.length === 0 ? "" : stringList[0]; |
| 254 |
} |
| 255 |
|
| 256 |
// Ensure the ListFormat internals are resolved. |
| 257 |
getListFormatInternals(listFormat); |
| 258 |
|
| 259 |
// Step 5. |
| 260 |
return intl_FormatList(listFormat, stringList, /* formatToParts = */ false); |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Intl.ListFormat.prototype.formatToParts ( list ) |
| 265 |
*/ |
| 266 |
function Intl_ListFormat_formatToParts(list) { |
| 267 |
// Step 1. |
| 268 |
var listFormat = this; |
| 269 |
|
| 270 |
// Steps 2-3. |
| 271 |
if ( |
| 272 |
!IsObject(listFormat) || |
| 273 |
(listFormat = intl_GuardToListFormat(listFormat)) === null |
| 274 |
) { |
| 275 |
return callFunction( |
| 276 |
intl_CallListFormatMethodIfWrapped, |
| 277 |
this, |
| 278 |
list, |
| 279 |
"Intl_ListFormat_formatToParts" |
| 280 |
); |
| 281 |
} |
| 282 |
|
| 283 |
// Step 4. |
| 284 |
var stringList = StringListFromIterable(list, "formatToParts"); |
| 285 |
|
| 286 |
// We can directly return if |stringList| contains less than two elements. |
| 287 |
if (stringList.length < 2) { |
| 288 |
return stringList.length === 0 |
| 289 |
? [] |
| 290 |
: [{ type: "element", value: stringList[0] }]; |
| 291 |
} |
| 292 |
|
| 293 |
// Ensure the ListFormat internals are resolved. |
| 294 |
getListFormatInternals(listFormat); |
| 295 |
|
| 296 |
// Step 5. |
| 297 |
return intl_FormatList(listFormat, stringList, /* formatToParts = */ true); |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Returns the resolved options for a ListFormat object. |
| 302 |
*/ |
| 303 |
function Intl_ListFormat_resolvedOptions() { |
| 304 |
// Step 1. |
| 305 |
var listFormat = this; |
| 306 |
|
| 307 |
// Steps 2-3. |
| 308 |
if ( |
| 309 |
!IsObject(listFormat) || |
| 310 |
(listFormat = intl_GuardToListFormat(listFormat)) === null |
| 311 |
) { |
| 312 |
return callFunction( |
| 313 |
intl_CallListFormatMethodIfWrapped, |
| 314 |
this, |
| 315 |
"Intl_ListFormat_resolvedOptions" |
| 316 |
); |
| 317 |
} |
| 318 |
|
| 319 |
var internals = getListFormatInternals(listFormat); |
| 320 |
|
| 321 |
// Steps 4-5. |
| 322 |
var result = { |
| 323 |
locale: internals.locale, |
| 324 |
type: internals.type, |
| 325 |
style: internals.style, |
| 326 |
}; |
| 327 |
|
| 328 |
// Step 6. |
| 329 |
return result; |
| 330 |
} |