This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
mock_l10n.js
120 lines (104 loc) · 3.51 KB
/
mock_l10n.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
/* globals assert */
(function(exports) {
'use strict';
exports.MockL10n = {
readyState: 'complete',
get: stringify,
_stringify: stringify,
formatValue: function(id, args) {
return Promise.resolve(stringify(id, args || ''));
},
translateFragment: function() {},
// XXX Remove in https://bugzil.la/1020137
localize: function(element, id, args) {
if (id) {
element.setAttribute('data-l10n-id', id);
} else {
element.removeAttribute('data-l10n-id');
element.removeAttribute('data-l10n-args');
element.textContent = '';
}
if (args) {
element.setAttribute('data-l10n-args', JSON.stringify(args));
} else {
element.removeAttribute('data-l10n-args');
}
element.textContent = id + (args ? JSON.stringify(args) : '');
},
setAttributes: function(element, id, args) {
element.setAttribute('data-l10n-id', id);
if (args) {
element.setAttribute('data-l10n-args', JSON.stringify(args));
}
},
getAttributes: function(element) {
return {
id: element.getAttribute('data-l10n-id'),
args: JSON.parse(element.getAttribute('data-l10n-args'))
};
},
ready: function(handler) {
setTimeout(handler);
},
once: function(handler) {
setTimeout(handler);
},
language: {
code: 'en-US',
direction: 'ltr'
},
qps: {
'fr-x-psaccent': {
id: 'fr-x-psaccent',
name: 'Pseudo English',
translate: stringify
}
}
};
/**
* Helper used to check that an element has been properly localized. This
* supports all the patterns described on MDN for localization best
* practices:
* - If 'expectedL10n' is a string it will check that the node's
* 'data-l10n-id' attribute is set and matches that id.
* - If 'expectedL10n' is an object then if it as an id and an args field it
* will check if the node's 'data-l10n-id' and 'data-l10n-args' fields
* match them.
* - If 'expectedL10n' is an object with a 'raw' field it will check that the
* node doesn't have a 'data-l10n-id' attribute and its 'textContent' field
* matches the'raw' field.
* - If 'expectedL10n' is an object with am 'html' field it will check that
* the node doesn't have a 'data-l10n-id' attribute and its 'innerHTML'
* field matches the'html' field.
*/
exports.l10nAssert = function(node, expectedL10n) {
if (typeof expectedL10n === 'string') {
assert.isTrue(node.hasAttribute('data-l10n-id'));
assert.equal(node.getAttribute('data-l10n-id'), expectedL10n);
} else if (expectedL10n.raw) {
assert.isFalse(node.hasAttribute('data-l10n-id'));
assert.equal(node.textContent, expectedL10n.raw);
} else if (expectedL10n.html) {
assert.isFalse(node.hasAttribute('data-l10n-id'));
assert.equal(node.innerHTML, expectedL10n.html);
} else if (expectedL10n.id) {
assert.isTrue(node.hasAttribute('data-l10n-id'));
assert.equal(node.getAttribute('data-l10n-id'), expectedL10n.id);
if (expectedL10n.args) {
assert.isTrue(node.hasAttribute('data-l10n-args'));
assert.deepEqual(
JSON.parse(node.getAttribute('data-l10n-args')),
expectedL10n.args
);
}
}
};
function stringify() {
return Array.prototype.reduce.call(arguments, function(prev, cur) {
if (typeof cur === 'object') {
cur = JSON.stringify(cur);
}
return prev + cur;
}, '');
}
}(this));