0e9aeacd
root
localization l20n
|
1
2
3
4
5
6
7
8
9
10
|
'use strict';
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
(function () {
'use strict';
var Client = bridge.client;
var channel = new BroadcastChannel('l20n-channel');
|
0e9aeacd
root
localization l20n
|
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
var reOverlay = /<|&#?\w+;/;
var allowed = {
elements: ['a', 'em', 'strong', 'small', 's', 'cite', 'q', 'dfn', 'abbr', 'data', 'time', 'code', 'var', 'samp', 'kbd', 'sub', 'sup', 'i', 'b', 'u', 'mark', 'ruby', 'rt', 'rp', 'bdi', 'bdo', 'span', 'br', 'wbr'],
attributes: {
global: ['title', 'aria-label', 'aria-valuetext', 'aria-moz-hint'],
a: ['download'],
area: ['download', 'alt'],
input: ['alt', 'placeholder'],
menuitem: ['label'],
menu: ['label'],
optgroup: ['label'],
option: ['label'],
track: ['label'],
img: ['alt'],
textarea: ['placeholder'],
th: ['abbr']
}
};
function overlayElement(element, translation) {
var value = translation.value;
if (typeof value === 'string') {
if (!reOverlay.test(value)) {
element.textContent = value;
} else {
var tmpl = element.ownerDocument.createElement('template');
tmpl.innerHTML = value;
overlay(element, tmpl.content);
}
}
for (var key in translation.attrs) {
var attrName = camelCaseToDashed(key);
if (isAttrAllowed({ name: attrName }, element)) {
element.setAttribute(attrName, translation.attrs[key]);
}
}
}
function overlay(sourceElement, translationElement) {
var result = translationElement.ownerDocument.createDocumentFragment();
var k = undefined,
attr = undefined;
var childElement = undefined;
while (childElement = translationElement.childNodes[0]) {
translationElement.removeChild(childElement);
if (childElement.nodeType === childElement.TEXT_NODE) {
result.appendChild(childElement);
continue;
}
var index = getIndexOfType(childElement);
var sourceChild = getNthElementOfType(sourceElement, childElement, index);
if (sourceChild) {
overlay(sourceChild, childElement);
result.appendChild(sourceChild);
continue;
}
if (isElementAllowed(childElement)) {
var sanitizedChild = childElement.ownerDocument.createElement(childElement.nodeName);
overlay(sanitizedChild, childElement);
result.appendChild(sanitizedChild);
continue;
}
result.appendChild(translationElement.ownerDocument.createTextNode(childElement.textContent));
}
sourceElement.textContent = '';
sourceElement.appendChild(result);
if (translationElement.attributes) {
for (k = 0, attr; attr = translationElement.attributes[k]; k++) {
if (isAttrAllowed(attr, sourceElement)) {
sourceElement.setAttribute(attr.name, attr.value);
}
}
}
}
function isElementAllowed(element) {
return allowed.elements.indexOf(element.tagName.toLowerCase()) !== -1;
}
function isAttrAllowed(attr, element) {
var attrName = attr.name.toLowerCase();
var tagName = element.tagName.toLowerCase();
if (allowed.attributes.global.indexOf(attrName) !== -1) {
return true;
}
if (!allowed.attributes[tagName]) {
return false;
}
if (allowed.attributes[tagName].indexOf(attrName) !== -1) {
return true;
}
if (tagName === 'input' && attrName === 'value') {
var type = element.type.toLowerCase();
if (type === 'submit' || type === 'button' || type === 'reset') {
return true;
}
}
return false;
}
function getNthElementOfType(context, element, index) {
var nthOfType = 0;
for (var i = 0, child = undefined; child = context.children[i]; i++) {
if (child.nodeType === child.ELEMENT_NODE && child.tagName === element.tagName) {
if (nthOfType === index) {
return child;
}
nthOfType++;
}
}
return null;
}
function getIndexOfType(element) {
var index = 0;
var child = undefined;
while (child = element.previousElementSibling) {
if (child.tagName === element.tagName) {
index++;
}
}
return index;
}
function camelCaseToDashed(string) {
if (string === 'ariaValueText') {
return 'aria-valuetext';
}
return string.replace(/[A-Z]/g, function (match) {
return '-' + match.toLowerCase();
}).replace(/^-/, '');
}
var reHtml = /[&<>]/g;
var htmlEntities = {
'&': '&',
'<': '<',
'>': '>'
};
|
c5169e0e
Renato De Donato
a new hope
|
168
169
170
171
172
173
|
function getResourceLinks(head) {
return Array.prototype.map.call(head.querySelectorAll('link[rel="localization"]'), function (el) {
return el.getAttribute('href');
});
}
|
0e9aeacd
root
localization l20n
|
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
function setAttributes(element, id, args) {
element.setAttribute('data-l10n-id', id);
if (args) {
element.setAttribute('data-l10n-args', JSON.stringify(args));
}
}
function getAttributes(element) {
return {
id: element.getAttribute('data-l10n-id'),
args: JSON.parse(element.getAttribute('data-l10n-args'))
};
}
function getTranslatables(element) {
var nodes = Array.from(element.querySelectorAll('[data-l10n-id]'));
if (typeof element.hasAttribute === 'function' && element.hasAttribute('data-l10n-id')) {
nodes.push(element);
}
return nodes;
}
|
c5169e0e
Renato De Donato
a new hope
|
198
|
function translateMutations(view, langs, mutations) {
|
0e9aeacd
root
localization l20n
|
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
var targets = new Set();
for (var _iterator = mutations, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
}
var mutation = _ref;
switch (mutation.type) {
case 'attributes':
targets.add(mutation.target);
break;
case 'childList':
for (var _iterator2 = mutation.addedNodes, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
var _ref2;
if (_isArray2) {
if (_i2 >= _iterator2.length) break;
_ref2 = _iterator2[_i2++];
} else {
_i2 = _iterator2.next();
if (_i2.done) break;
_ref2 = _i2.value;
}
var addedNode = _ref2;
if (addedNode.nodeType === addedNode.ELEMENT_NODE) {
if (addedNode.childElementCount) {
getTranslatables(addedNode).forEach(targets.add.bind(targets));
} else {
if (addedNode.hasAttribute('data-l10n-id')) {
targets.add(addedNode);
}
}
}
}
break;
}
}
if (targets.size === 0) {
return;
}
|
c5169e0e
Renato De Donato
a new hope
|
252
|
translateElements(view, langs, Array.from(targets));
|
0e9aeacd
root
localization l20n
|
253
254
|
}
|
c5169e0e
Renato De Donato
a new hope
|
255
256
|
function _translateFragment(view, langs, frag) {
return translateElements(view, langs, getTranslatables(frag));
|
0e9aeacd
root
localization l20n
|
257
258
|
}
|
c5169e0e
Renato De Donato
a new hope
|
259
|
function getElementsTranslation(view, langs, elems) {
|
0e9aeacd
root
localization l20n
|
260
261
262
263
264
265
266
267
|
var keys = elems.map(function (elem) {
var id = elem.getAttribute('data-l10n-id');
var args = elem.getAttribute('data-l10n-args');
return args ? [id, JSON.parse(args.replace(reHtml, function (match) {
return htmlEntities[match];
}))] : id;
});
|
c5169e0e
Renato De Donato
a new hope
|
268
|
return view._resolveEntities(langs, keys);
|
0e9aeacd
root
localization l20n
|
269
270
|
}
|
c5169e0e
Renato De Donato
a new hope
|
271
272
|
function translateElements(view, langs, elements) {
return getElementsTranslation(view, langs, elements).then(function (translations) {
|
0e9aeacd
root
localization l20n
|
273
274
275
276
277
|
return applyTranslations(view, elements, translations);
});
}
function applyTranslations(view, elems, translations) {
|
c5169e0e
Renato De Donato
a new hope
|
278
|
view._disconnect();
|
0e9aeacd
root
localization l20n
|
279
280
281
|
for (var i = 0; i < elems.length; i++) {
overlayElement(elems[i], translations[i]);
}
|
c5169e0e
Renato De Donato
a new hope
|
282
|
view._observe();
|
0e9aeacd
root
localization l20n
|
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
}
if (typeof NodeList === 'function' && !NodeList.prototype[Symbol.iterator]) {
NodeList.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
}
function documentReady() {
if (document.readyState !== 'loading') {
return Promise.resolve();
}
return new Promise(function (resolve) {
document.addEventListener('readystatechange', function onrsc() {
document.removeEventListener('readystatechange', onrsc);
resolve();
});
});
}
function getDirection(code) {
var tag = code.split('-')[0];
return ['ar', 'he', 'fa', 'ps', 'ur'].indexOf(tag) >= 0 ? 'rtl' : 'ltr';
}
|
c5169e0e
Renato De Donato
a new hope
|
307
308
309
310
311
312
313
|
var observerConfig = {
attributes: true,
characterData: false,
childList: true,
subtree: true,
attributeFilter: ['data-l10n-id', 'data-l10n-args']
};
|
a1a3bc73
Luigi Serra
graphs updates
|
314
|
|
c5169e0e
Renato De Donato
a new hope
|
315
|
var readiness = new WeakMap();
|
0e9aeacd
root
localization l20n
|
316
317
318
319
320
321
322
|
var View = (function () {
function View(client, doc) {
var _this = this;
_classCallCheck(this, View);
|
c5169e0e
Renato De Donato
a new hope
|
323
|
this._doc = doc;
|
0e9aeacd
root
localization l20n
|
324
325
326
327
328
|
this.pseudo = {
'fr-x-psaccent': createPseudo(this, 'fr-x-psaccent'),
'ar-x-psbidi': createPseudo(this, 'ar-x-psbidi')
};
|
c5169e0e
Renato De Donato
a new hope
|
329
|
this._interactive = documentReady().then(function () {
|
0e9aeacd
root
localization l20n
|
330
331
|
return init(_this, client);
});
|
0e9aeacd
root
localization l20n
|
332
|
|
c5169e0e
Renato De Donato
a new hope
|
333
334
335
336
337
338
339
|
var observer = new MutationObserver(onMutations.bind(this));
this._observe = function () {
return observer.observe(doc, observerConfig);
};
this._disconnect = function () {
return observer.disconnect();
};
|
a1a3bc73
Luigi Serra
graphs updates
|
340
|
|
c5169e0e
Renato De Donato
a new hope
|
341
342
|
var translateView = function (langs) {
return translateDocument(_this, langs);
|
a1a3bc73
Luigi Serra
graphs updates
|
343
|
};
|
c5169e0e
Renato De Donato
a new hope
|
344
345
346
347
348
|
client.on('translateDocument', translateView);
this.ready = this._interactive.then(function (client) {
return client.method('resolvedLanguages');
}).then(translateView);
}
|
a1a3bc73
Luigi Serra
graphs updates
|
349
|
|
c5169e0e
Renato De Donato
a new hope
|
350
351
352
353
|
View.prototype.requestLanguages = function requestLanguages(langs, global) {
return this._interactive.then(function (client) {
return client.method('requestLanguages', langs, global);
});
|
0e9aeacd
root
localization l20n
|
354
355
|
};
|
c5169e0e
Renato De Donato
a new hope
|
356
|
View.prototype._resolveEntities = function _resolveEntities(langs, keys) {
|
0e9aeacd
root
localization l20n
|
357
|
return this._interactive.then(function (client) {
|
c5169e0e
Renato De Donato
a new hope
|
358
|
return client.method('resolveEntities', client.id, langs, keys);
|
0e9aeacd
root
localization l20n
|
359
360
361
362
363
364
365
366
367
368
369
370
|
});
};
View.prototype.formatValue = function formatValue(id, args) {
return this._interactive.then(function (client) {
return client.method('formatValues', client.id, [[id, args]]);
}).then(function (values) {
return values[0];
});
};
View.prototype.formatValues = function formatValues() {
|
c5169e0e
Renato De Donato
a new hope
|
371
372
|
for (var _len = arguments.length, keys = Array(_len), _key = 0; _key < _len; _key++) {
keys[_key] = arguments[_key];
|
0e9aeacd
root
localization l20n
|
373
374
375
376
377
378
379
380
|
}
return this._interactive.then(function (client) {
return client.method('formatValues', client.id, keys);
});
};
View.prototype.translateFragment = function translateFragment(frag) {
|
c5169e0e
Renato De Donato
a new hope
|
381
|
var _this2 = this;
|
a1a3bc73
Luigi Serra
graphs updates
|
382
|
|
c5169e0e
Renato De Donato
a new hope
|
383
384
385
386
387
|
return this._interactive.then(function (client) {
return client.method('resolvedLanguages');
}).then(function (langs) {
return _translateFragment(_this2, langs, frag);
});
|
0e9aeacd
root
localization l20n
|
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
};
return View;
})();
View.prototype.setAttributes = setAttributes;
View.prototype.getAttributes = getAttributes;
function createPseudo(view, code) {
return {
getName: function () {
return view._interactive.then(function (client) {
return client.method('getName', code);
});
},
processString: function (str) {
return view._interactive.then(function (client) {
return client.method('processString', code, str);
});
}
};
}
function init(view, client) {
|
c5169e0e
Renato De Donato
a new hope
|
412
413
414
|
view._observe();
return client.method('registerView', client.id, getResourceLinks(view._doc.head)).then(function () {
return client;
|
0e9aeacd
root
localization l20n
|
415
416
417
|
});
}
|
c5169e0e
Renato De Donato
a new hope
|
418
419
|
function onMutations(mutations) {
var _this3 = this;
|
0e9aeacd
root
localization l20n
|
420
|
|
c5169e0e
Renato De Donato
a new hope
|
421
422
423
424
425
|
return this._interactive.then(function (client) {
return client.method('resolvedLanguages');
}).then(function (langs) {
return translateMutations(_this3, langs, mutations);
});
|
a1a3bc73
Luigi Serra
graphs updates
|
426
427
|
}
|
c5169e0e
Renato De Donato
a new hope
|
428
429
|
function translateDocument(view, langs) {
var html = view._doc.documentElement;
|
a1a3bc73
Luigi Serra
graphs updates
|
430
|
|
c5169e0e
Renato De Donato
a new hope
|
431
432
|
if (readiness.has(html)) {
return _translateFragment(view, langs, html).then(function () {
|
0e9aeacd
root
localization l20n
|
433
434
435
436
|
return setAllAndEmit(html, langs);
});
}
|
c5169e0e
Renato De Donato
a new hope
|
437
|
var translated = langs[0].code === html.getAttribute('lang') ? Promise.resolve() : _translateFragment(view, langs, html).then(function () {
|
0e9aeacd
root
localization l20n
|
438
439
440
441
442
|
return setLangDir(html, langs);
});
return translated.then(function () {
setLangs(html, langs);
|
c5169e0e
Renato De Donato
a new hope
|
443
|
readiness.set(html, true);
|
0e9aeacd
root
localization l20n
|
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
});
}
function setLangs(html, langs) {
var codes = langs.map(function (lang) {
return lang.code;
});
html.setAttribute('langs', codes.join(' '));
}
function setLangDir(html, langs) {
var code = langs[0].code;
html.setAttribute('lang', code);
html.setAttribute('dir', getDirection(code));
}
function setAllAndEmit(html, langs) {
setLangDir(html, langs);
setLangs(html, langs);
html.parentNode.dispatchEvent(new CustomEvent('DOMRetranslated', {
bubbles: false,
cancelable: false
}));
}
var client = new Client({
service: 'l20n',
endpoint: channel,
timeout: false
});
|
0e9aeacd
root
localization l20n
|
475
476
477
478
479
480
|
window.addEventListener('pageshow', function () {
return client.connect();
});
window.addEventListener('pagehide', function () {
return client.disconnect();
});
|
c5169e0e
Renato De Donato
a new hope
|
481
482
|
document.l10n = new View(client, document);
|
0e9aeacd
root
localization l20n
|
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
|
navigator.mozL10n = {
setAttributes: document.l10n.setAttributes,
getAttributes: document.l10n.getAttributes,
formatValue: function () {
var _document$l10n;
return (_document$l10n = document.l10n).formatValue.apply(_document$l10n, arguments);
},
translateFragment: function () {
var _document$l10n2;
return (_document$l10n2 = document.l10n).translateFragment.apply(_document$l10n2, arguments);
},
once: function (cb) {
return document.l10n.ready.then(cb);
},
ready: function (cb) {
return document.l10n.ready.then(function () {
document.addEventListener('DOMRetranslated', cb);
cb();
});
}
};
})();
|