Bug 1296618 - Part 3: Remove destructuring of [id, args] arrays. r?gandalf
MozReview-Commit-ID: K85vHThuacY
--- a/toolkit/content/l20n-chrome-xul.js
+++ b/toolkit/content/l20n-chrome-xul.js
@@ -236,20 +236,19 @@ class LocalizationObserver {
getLocalizationForElement(elem) {
return this.get(elem.getAttribute('data-l10n-bundle') || 'main');
}
getKeysForElements(elems) {
return elems.map(elem => {
const id = elem.getAttribute('data-l10n-id');
const args = elem.getAttribute('data-l10n-args');
-
- return args ?
- [id, JSON.parse(args.replace(reHtml, match => htmlEntities[match]))] :
- id;
+ const escapedArgs = args ?
+ JSON.parse(args.replace(reHtml, match => htmlEntities[match])) : null;
+ return [id, escapedArgs];
});
}
getElementsTranslation(elemsByL10n) {
return Promise.all(
Array.from(elemsByL10n).map(
([l10n, elems]) => l10n.formatEntities(this.getKeysForElements(elems))
)
@@ -289,20 +288,17 @@ class L10nError extends Error {
function keysFromContext(ctx, keys, method, prev) {
const errors = [];
const translations = keys.map((key, i) => {
if (prev && prev[i] && prev[i][1].length === 0) {
// Use a previously formatted good value if there were no errors
return prev[i];
}
- const [id, args] = Array.isArray(key) ?
- key : [key, undefined];
-
- const result = method(ctx, id, args);
+ const result = method(ctx, key[0], key[1]);
errors.push(...result[1]);
// XXX Depending on the kind of errors it might be better to return prev[i]
// here; for instance, when the current translation is completely missing
return result;
});
return [translations, errors];
}
@@ -393,18 +389,21 @@ class Localization {
formatEntities(keys) {
return this.interactive.then(
bundles => this.formatWithFallback(bundles, keys, entityFromContext)
);
}
formatValues(...keys) {
+ const keyTuples = keys.map(
+ key => Array.isArray(key) ? key : [key, null]
+ );
return this.interactive.then(
- bundles => this.formatWithFallback(bundles, keys, valueFromContext)
+ bundles => this.formatWithFallback(bundles, keyTuples, valueFromContext)
);
}
formatValue(id, args) {
return this.formatValues([id, args]).then(
([val]) => val
);
}