forked from nvdnkpr/angular-localize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-localize.js
183 lines (173 loc) · 7.13 KB
/
angular-localize.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
/* global angular */
(function () {
'use strict';
angular.module('localize', ['ngSanitize'])
// SECURITY CONTEXT:
// This simple filter only properly sanitizes values
// that are rendered between HTML tags, e.g.
// <div>ESCAPED_CONTENT</div>
// It will fall short when used in any other context,
// e.g. within attributes not enclosed by double quotes
// or as value for event handlers or href attributes:
// https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)
.filter('escapeHTML', function () {
var config = {
encReg: /[<>&"]/g,
encMap: {
'<' : '<',
'>' : '>',
'&' : '&',
'"' : '"'
},
encFunc: function (c) {
return config.encMap[c];
}
};
return function (str) {
return String(str).replace(
config.encReg,
config.encFunc
);
};
})
.factory('localizeConfig', ['$window', function ($window) {
return {
i18n: $window.i18n,
// Only observe non-directive data-attributes:
observableAttrs: /^data-(?!ng-|localize)/
};
}])
.factory('localize', [
'$filter', 'localizeConfig',
function ($filter, localizeConfig) {
var i18n = localizeConfig.i18n,
escapeHTML = $filter('escapeHTML');
return function (key, data, escape) {
var func = i18n[key],
escapedData;
if (func) {
func = assertFunc(func);
if (escape) {
escapedData = {};
angular.forEach(data, function (value, key) {
escapedData[key] = escapeHTML(value);
});
}
return func(escapedData || data || {});
}
return key;
};
}
])
.filter('localize', ['localize', function (localize) {
return localize;
}])
.directive('localize', [
'$sanitize', '$filter', 'localizeConfig',
function ($sanitize, $filter, localizeConfig) {
var i18n = localizeConfig.i18n,
escapeHTML = $filter('escapeHTML');
return function (scope, elm, attrs) {
// Take the translation key from the element content
// if the localize attribute is empty:
var key = attrs.localize || elm.html(),
func = i18n[key],
isInput = /input|textarea/i.test(elm.prop('nodeName')),
data,
update,
hasObservers;
if (func) {
func = assertFunc(func);
if (isInput) {
update = function () {
attrs.$set('placeholder', func(attrs));
};
} else if (attrs.localize) {
// Localization is text only
update = function () {
elm.text(func(attrs));
};
} else {
// Localization can contain HTML
data = {};
update = function (key, value) {
if (key) {
data[key] = escapeHTML(value);
}
elm.html($sanitize(func(data)));
};
}
angular.forEach(attrs.$attr, function (attr, normAttr) {
if (localizeConfig.observableAttrs.test(attr)) {
attrs.$observe(
normAttr,
isInput || attrs.localize ? update :
function (value) {
update(normAttr, value);
}
);
hasObservers = true;
}
});
if (!hasObservers) {
update();
}
} else if (attrs.localize) {
// If there is no translation function,
// the key itself is the translation value:
if (isInput) {
attrs.$set('placeholder', key);
} else {
elm.text(key);
}
}
};
}
])
.factory('localizeFactory', [
'localizeConfig',
function (localizeConfig) {
var i18n = localizeConfig.i18n;
return function () {
var directiveObj = {
link: function (scope, elm, attrs) {
var name = directiveObj.name,
target = name.charAt(8).toLowerCase() + name.slice(9),
key = attrs[name],
func = i18n[key],
update,
hasObservers;
if (func) {
func = assertFunc(func);
update = function () {
attrs.$set(target, func(attrs));
};
angular.forEach(attrs.$attr, function (attr, normAttr) {
if (localizeConfig.observableAttrs.test(attr)) {
attrs.$observe(normAttr, update);
hasObservers = true;
}
});
if (!hasObservers) {
update();
}
} else {
attrs.$set(target, key);
}
}
};
return directiveObj;
};
}
]);
function assertFunc(func) {
var t = typeof func;
if (t === 'string') {
return function () { return func; };
} else if (t === 'function') {
return func;
}
// alternative for exception: function that returns func + ''
throw 'not a function';
}
}());