This repository has been archived by the owner on Apr 17, 2019. It is now read-only.
/
html.js
411 lines (363 loc) · 12.9 KB
/
html.js
1
2
3
4
5
6
7
8
9
10
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
define(function (require) {
'use strict';
var DEBUG = false;
var L20n = require('../l20n');
var io = require('./platform/io');
// absolute URLs start with a slash or contain a colon (for schema)
var reAbsolute = /^\/|:/;
// http://www.w3.org/International/questions/qa-scripts
// XXX: Bug 884308: this should be defined by each localization independently
var rtlLocales = ['ar', 'fa', 'he', 'ps', 'ur'];
var whitelist = {
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' ],
a: [ 'download' ],
area: [ 'download', 'alt' ],
// value is special-cased in isAttrAllowed
input: [ 'alt', 'placeholder' ],
menuitem: [ 'label' ],
menu: [ 'label' ],
optgroup: [ 'label' ],
option: [ 'label' ],
track: [ 'label' ],
img: [ 'alt' ],
textarea: [ 'placeholder' ],
th: [ 'abbr']
}
};
// Start-up logic (pre-bootstrap)
// =========================================================================
var ctx = L20n.getContext(document.location.host);
bindPublicAPI(ctx);
var localizeHandler;
var documentLocalized = false;
// if the DOM is loaded, bootstrap now to fire 'DocumentLocalized'
if (document.readyState === 'complete') {
window.setTimeout(bootstrap);
} else {
// or wait for the DOM to be interactive to try to pretranslate it
// using the inline resources
waitFor('interactive', bootstrap);
}
function waitFor(state, callback) {
if (document.readyState === state) {
return callback();
}
document.addEventListener('readystatechange', function() {
if (document.readyState === state) {
callback();
}
});
}
function bindPublicAPI(ctx) {
if (DEBUG) {
ctx.addEventListener('error', console.error.bind(console));
ctx.addEventListener('warning', console.warn.bind(console));
}
ctx.localizeNode = function localizeNode(node) {
var nodes = getNodes(node);
var many = localizeHandler.extend(nodes.ids);
for (var i = 0; i < nodes.nodes.length; i++) {
translateNode(nodes.nodes[i], nodes.ids[i],
many.entities[nodes.ids[i]]);
}
};
ctx.once = function once(callback) {
if (documentLocalized) {
callback();
} else {
var callAndRemove = function callAndRemove() {
document.removeEventListener('DocumentLocalized', callAndRemove);
callback();
};
document.addEventListener('DocumentLocalized', callAndRemove);
}
};
document.l10n = ctx;
}
// Bootstrap: set up the context and call requestLocales()
// ==========================================================================
function bootstrap() {
var headNode = document.head;
var data =
headNode.querySelector('script[type="application/l10n-data+json"]');
if (data) {
ctx.updateData(JSON.parse(data.textContent));
}
var link = headNode.querySelector('link[rel="localization"]');
if (link) {
loadManifest(link.href);
return collectNodes();
}
var scripts = headNode.querySelectorAll('script[type="application/l20n"]');
if (scripts.length) {
for (var i = 0; i < scripts.length; i++) {
if (scripts[i].hasAttribute('src')) {
ctx.linkResource(scripts[i].src);
} else {
ctx.addResource(scripts[i].textContent);
}
}
ctx.requestLocales();
return collectNodes();
}
console.error('L20n: No resources found. (Put them above l20n.js.)');
}
function loadManifest(url) {
io.load(url, function(err, text) {
var manifest = parseManifest(text, url);
setupCtxFromManifest(manifest);
});
}
function parseManifest(text, url) {
var manifest = JSON.parse(text);
manifest.resources = manifest.resources.map(
relativeToManifest.bind(this, url));
return manifest;
}
function setDocumentLanguage(loc) {
document.documentElement.lang = loc;
document.documentElement.dir =
rtlLocales.indexOf(loc) === -1 ? 'ltr' : 'rtl';
}
function setupCtxFromManifest(manifest) {
// register available locales
ctx.registerLocales(manifest.default_locale, manifest.locales);
ctx.registerLocaleNegotiator(function(available, requested, defLoc) {
// lazy-require Intl
var Intl = require('./intl').Intl;
var fallbackChain = Intl.prioritizeLocales(available, requested, defLoc);
setDocumentLanguage(fallbackChain[0]);
return fallbackChain;
});
// add resources
var re = /{{\s*locale\s*}}/;
manifest.resources.forEach(function(uri) {
if (re.test(uri)) {
ctx.linkResource(uri.replace.bind(uri, re));
} else {
ctx.linkResource(uri);
}
});
// For now we just take navigator.language, but we'd prefer to get a list
// of locales that the user can read sorted by user's preference, see:
// https://bugzilla.mozilla.org/show_bug.cgi?id=889335
// For IE we use navigator.browserLanguage, see:
// http://msdn.microsoft.com/en-us/library/ie/ms533542%28v=vs.85%29.aspx
ctx.requestLocales(navigator.language || navigator.browserLanguage);
return manifest;
}
function relativeToManifest(manifestUrl, url) {
if (reAbsolute.test(url)) {
return url;
}
var dirs = manifestUrl.split('/')
.slice(0, -1)
.concat(url.split('/'))
.filter(function(elem) {
return elem !== '.';
});
return dirs.join('/');
}
function fireLocalizedEvent() {
var event = document.createEvent('Event');
event.initEvent('DocumentLocalized', false, false);
document.dispatchEvent(event);
}
// DOM Localization
// ==========================================================================
function collectNodes() {
var nodes = getNodes(document);
localizeHandler = ctx.localize(nodes.ids, function localizeHandler(l10n) {
if (!nodes) {
nodes = getNodes(document);
}
for (var i = 0; i < nodes.nodes.length; i++) {
translateNode(nodes.nodes[i],
nodes.ids[i],
l10n.entities[nodes.ids[i]]);
}
// 'locales' in l10n.reason means that localize has been
// called because of locale change
if ('locales' in l10n.reason && l10n.reason.locales.length) {
setDocumentLanguage(l10n.reason.locales[0]);
}
nodes = null;
if (!documentLocalized) {
documentLocalized = true;
fireLocalizedEvent();
}
});
}
function getNodes(node) {
var nodes = node.querySelectorAll('[data-l10n-id]');
var ids = [];
if (node.hasAttribute && node.hasAttribute('data-l10n-id')) {
// include the root node in nodes (and ids)
nodes = Array.prototype.slice.call(nodes);
nodes.push(node);
}
for (var i = 0; i < nodes.length; i++) {
ids.push(nodes[i].getAttribute('data-l10n-id'));
}
return {
ids: ids,
nodes: nodes
};
}
function camelCaseToDashed(string) {
return string
.replace(/[A-Z]/g, function (match) {
return '-' + match.toLowerCase();
})
.replace(/^-/, '');
}
function translateNode(node, id, entity) {
if (!entity) {
return;
}
if (entity.value) {
// if there is no HTML in the translation nor no HTML entities are used,
// just replace the textContent
if (entity.value.indexOf('<') === -1 &&
entity.value.indexOf('&') === -1) {
node.textContent = entity.value;
} else {
// otherwise, start with an inert template element and move its
// children into `node` but such that `node`'s own children are not
// replaced
var translation = document.createElement('template');
translation.innerHTML = entity.value;
// overlay the node with the DocumentFragment
overlayElement(node, translation.content);
}
}
Object.keys(entity.attributes).forEach(function(key) {
var attrName = camelCaseToDashed(key);
if (isAttrAllowed({ name: attrName }, node)) {
node.setAttribute(attrName, entity.attributes[key]);
}
});
}
// The goal of overlayElement is to move the children of `translationElement`
// into `sourceElement` such that `sourceElement`'s own children are not
// replaced, but onle have their text nodes and their attributes modified.
//
// We want to make it possible for localizers to apply text-level semantics to
// the translations and make use of HTML entities. At the same time, we
// don't trust translations so we need to filter unsafe elements and
// attribtues out and we don't want to break the Web by replacing elements to
// which third-party code might have created references (e.g. two-way
// bindings in MVC frameworks).
function overlayElement(sourceElement, translationElement) {
var result = document.createDocumentFragment();
// take one node from translationElement at a time and check it against the
// whitelist or try to match it with a corresponding element in the source
var childElement;
while (childElement = translationElement.childNodes[0]) {
translationElement.removeChild(childElement);
if (childElement.nodeType === Node.TEXT_NODE) {
result.appendChild(childElement);
continue;
}
var sourceChild = getElementOfType(sourceElement, childElement);
if (sourceChild) {
// there is a corresponding element in the source, let's use it
overlayElement(sourceChild, childElement);
result.appendChild(sourceChild);
continue;
}
if (isElementAllowed(childElement)) {
for (var k = 0, attr; attr = childElement.attributes[k]; k++) {
if (!isAttrAllowed(attr, childElement)) {
childElement.removeAttribute(attr.name);
}
}
result.appendChild(childElement);
continue;
}
// otherwise just take this child's textContent
var text = new Text(childElement.textContent);
result.appendChild(text);
}
// clear `sourceElement` and append `result` which by this time contains
// `sourceElement`'s original children, overlayed with translation
sourceElement.textContent = '';
sourceElement.appendChild(result);
// if we're overlaying a nested element, translate the whitelisted
// attributes; top-level attributes are handled in `translateNode`
// XXX attributes previously set here for another language should be
// cleared if a new language doesn't use them; https://bugzil.la/922577
if (translationElement.attributes) {
for (var k = 0, attr; attr = translationElement.attributes[k]; k++) {
if (isAttrAllowed(attr, sourceElement)) {
sourceElement.setAttribute(attr.name, attr.value);
}
}
}
}
// XXX the whitelist should be amendable; https://bugzil.la/922573
function isElementAllowed(element) {
return whitelist.elements.indexOf(element.tagName.toLowerCase()) !== -1;
}
function isAttrAllowed(attr, element) {
var attrName = attr.name.toLowerCase();
var tagName = element.tagName.toLowerCase();
// is it a globally safe attribute?
if (whitelist.attributes.global.indexOf(attrName) !== -1) {
return true;
}
// are there no whitelisted attributes for this element?
if (!whitelist.attributes[tagName]) {
return false;
}
// is it allowed on this element?
// XXX the whitelist should be amendable; https://bugzil.la/922573
if (whitelist.attributes[tagName].indexOf(attrName) !== -1) {
return true;
}
// special case for value on inputs with type button, reset, submit
if (tagName === 'input' && attrName === 'value') {
var type = element.type.toLowerCase();
if (type === 'submit' || type === 'button' || type === 'reset') {
return true;
}
}
return false;
}
// ideally, we'd use querySelector(':scope > ELEMENT:nth-of-type(index)'),
// but 1) :scope is not widely supported yet and 2) it doesn't work with
// DocumentFragments. :scope is needed to query only immediate children
// https://developer.mozilla.org/en-US/docs/Web/CSS/:scope
function getElementOfType(context, element) {
var index = getIndexOfType(element);
var nthOfType = 0;
for (var i = 0, child; child = context.children[i]; i++) {
if (child.nodeType === Node.ELEMENT_NODE &&
child.tagName === element.tagName) {
if (nthOfType === index) {
return child;
}
nthOfType++;
}
}
return null;
}
function getIndexOfType(element) {
var index = 0;
var child;
while (child = element.previousElementSibling) {
if (child.tagName === element.tagName) {
index++;
}
}
return index;
}
// same as exports = L20n;
return L20n;
});