govuk_tech_docs 3.3.1 → 3.4.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/publish.yaml +10 -7
- data/.github/workflows/test.yaml +8 -3
- data/.nvmrc +1 -1
- data/.rubocop.yml +3 -0
- data/CHANGELOG.md +21 -1
- data/Gemfile +1 -1
- data/govuk_tech_docs.gemspec +13 -9
- data/lib/assets/stylesheets/_govuk_tech_docs.scss +13 -0
- data/lib/govuk_tech_docs/api_reference/api_reference_extension.rb +1 -1
- data/lib/govuk_tech_docs/api_reference/api_reference_renderer.rb +10 -10
- data/lib/govuk_tech_docs/path_helpers.rb +40 -10
- data/lib/govuk_tech_docs/redirects.rb +2 -2
- data/lib/govuk_tech_docs/table_of_contents/heading.rb +2 -2
- data/lib/govuk_tech_docs/table_of_contents/heading_tree_renderer.rb +5 -5
- data/lib/govuk_tech_docs/table_of_contents/helpers.rb +3 -3
- data/lib/govuk_tech_docs/version.rb +1 -1
- data/lib/govuk_tech_docs.rb +4 -4
- data/lib/source/favicon.ico +0 -0
- data/lib/source/layouts/_footer.erb +1 -1
- data/lib/source/layouts/_header.erb +15 -17
- data/node_modules/govuk-frontend/govuk/all-ie8.scss +8 -0
- data/node_modules/govuk-frontend/govuk/all.js +4918 -3796
- data/node_modules/govuk-frontend/govuk/common/closest-attribute-value.js +54 -49
- data/node_modules/govuk-frontend/govuk/common/govuk-frontend-version.js +17 -0
- data/node_modules/govuk-frontend/govuk/common/index.js +172 -152
- data/node_modules/govuk-frontend/govuk/common/normalise-dataset.js +334 -321
- data/node_modules/govuk-frontend/govuk/common.js +171 -151
- data/node_modules/govuk-frontend/govuk/components/_all.scss +3 -2
- data/node_modules/govuk-frontend/govuk/components/accordion/_index.scss +26 -7
- data/node_modules/govuk-frontend/govuk/components/accordion/accordion.js +2203 -1650
- data/node_modules/govuk-frontend/govuk/components/back-link/_index.scss +24 -16
- data/node_modules/govuk-frontend/govuk/components/breadcrumbs/_index.scss +34 -11
- data/node_modules/govuk-frontend/govuk/components/button/_index.scss +49 -9
- data/node_modules/govuk-frontend/govuk/components/button/button.js +961 -916
- data/node_modules/govuk-frontend/govuk/components/character-count/character-count.js +2142 -2038
- data/node_modules/govuk-frontend/govuk/components/checkboxes/_index.scss +6 -6
- data/node_modules/govuk-frontend/govuk/components/checkboxes/checkboxes.js +1204 -1145
- data/node_modules/govuk-frontend/govuk/components/details/details.js +826 -799
- data/node_modules/govuk-frontend/govuk/components/error-summary/error-summary.js +1097 -1044
- data/node_modules/govuk-frontend/govuk/components/exit-this-page/_exit-this-page.scss +2 -0
- data/node_modules/govuk-frontend/govuk/components/exit-this-page/_index.scss +97 -0
- data/node_modules/govuk-frontend/govuk/components/exit-this-page/exit-this-page.js +2120 -0
- data/node_modules/govuk-frontend/govuk/components/file-upload/_index.scss +6 -1
- data/node_modules/govuk-frontend/govuk/components/footer/_index.scss +0 -7
- data/node_modules/govuk-frontend/govuk/components/header/_index.scss +6 -0
- data/node_modules/govuk-frontend/govuk/components/header/header.js +683 -1003
- data/node_modules/govuk-frontend/govuk/components/input/_index.scss +15 -3
- data/node_modules/govuk-frontend/govuk/components/notification-banner/notification-banner.js +786 -751
- data/node_modules/govuk-frontend/govuk/components/radios/_index.scss +5 -5
- data/node_modules/govuk-frontend/govuk/components/radios/radios.js +1151 -1105
- data/node_modules/govuk-frontend/govuk/components/select/_index.scss +7 -1
- data/node_modules/govuk-frontend/govuk/components/skip-link/skip-link.js +1045 -1014
- data/node_modules/govuk-frontend/govuk/components/summary-list/_index.scss +107 -0
- data/node_modules/govuk-frontend/govuk/components/tabs/tabs.js +1514 -1268
- data/node_modules/govuk-frontend/govuk/components/tag/_index.scss +18 -18
- data/node_modules/govuk-frontend/govuk/components/textarea/_index.scss +8 -1
- data/node_modules/govuk-frontend/govuk/core/_all.scss +1 -0
- data/node_modules/govuk-frontend/govuk/core/_govuk-frontend-version.scss +5 -0
- data/node_modules/govuk-frontend/govuk/helpers/_colour.scss +5 -2
- data/node_modules/govuk-frontend/govuk/helpers/_focused.scss +1 -1
- data/node_modules/govuk-frontend/govuk/helpers/_font-faces.scss +1 -1
- data/node_modules/govuk-frontend/govuk/helpers/_visually-hidden.scss +12 -0
- data/node_modules/govuk-frontend/govuk/i18n.js +371 -364
- data/node_modules/govuk-frontend/govuk/objects/_template.scss +20 -0
- data/node_modules/govuk-frontend/govuk/objects/_width-container.scss +1 -1
- data/node_modules/govuk-frontend/govuk/settings/_colours-organisations.scss +4 -0
- data/node_modules/govuk-frontend/govuk/settings/_ie8.scss +16 -0
- data/node_modules/govuk-frontend/govuk/settings/_links.scss +5 -1
- data/node_modules/govuk-frontend/govuk/settings/_measurements.scss +5 -5
- data/node_modules/govuk-frontend/govuk/tools/_ie8.scss +38 -2
- data/node_modules/govuk-frontend/govuk/vendor/polyfills/DOMTokenList.js +243 -241
- data/node_modules/govuk-frontend/govuk/vendor/polyfills/Date/now.js +14 -12
- data/node_modules/govuk-frontend/govuk/vendor/polyfills/Document.js +18 -16
- data/node_modules/govuk-frontend/govuk/vendor/polyfills/Element/prototype/classList.js +553 -545
- data/node_modules/govuk-frontend/govuk/vendor/polyfills/Element/prototype/closest.js +40 -36
- data/node_modules/govuk-frontend/govuk/vendor/polyfills/Element/prototype/dataset.js +257 -250
- data/node_modules/govuk-frontend/govuk/vendor/polyfills/Element/prototype/matches.js +22 -20
- data/node_modules/govuk-frontend/govuk/vendor/polyfills/Element/prototype/nextElementSibling.js +204 -197
- data/node_modules/govuk-frontend/govuk/vendor/polyfills/Element/prototype/previousElementSibling.js +204 -197
- data/node_modules/govuk-frontend/govuk/vendor/polyfills/Element.js +109 -105
- data/node_modules/govuk-frontend/govuk/vendor/polyfills/Event.js +407 -399
- data/node_modules/govuk-frontend/govuk/vendor/polyfills/Function/prototype/bind.js +242 -238
- data/node_modules/govuk-frontend/govuk/vendor/polyfills/Object/defineProperty.js +73 -71
- data/node_modules/govuk-frontend/govuk/vendor/polyfills/String/prototype/trim.js +15 -13
- data/node_modules/govuk-frontend/govuk/vendor/polyfills/Window.js +18 -16
- data/node_modules/govuk-frontend/govuk-prototype-kit/init.js +1 -0
- data/package-lock.json +2708 -8
- data/package.json +1 -1
- metadata +25 -7
- data/.ruby-version +0 -1
@@ -1,2177 +1,2281 @@
|
|
1
1
|
(function (global, factory) {
|
2
|
-
|
3
|
-
|
4
|
-
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
3
|
+
typeof define === 'function' && define.amd ? define('GOVUKFrontend.CharacterCount', factory) :
|
4
|
+
(global.GOVUKFrontend = global.GOVUKFrontend || {}, global.GOVUKFrontend.CharacterCount = factory());
|
5
5
|
}(this, (function () { 'use strict';
|
6
6
|
|
7
|
-
|
7
|
+
// @ts-nocheck
|
8
|
+
(function (undefined) {
|
8
9
|
|
9
|
-
// Detection from https://
|
10
|
-
var detect = (
|
10
|
+
// Detection from https://raw.githubusercontent.com/Financial-Times/polyfill-service/1f3c09b402f65bf6e393f933a15ba63f1b86ef1f/packages/polyfill-library/polyfills/Element/prototype/matches/detect.js
|
11
|
+
var detect = (
|
12
|
+
'document' in this && "matches" in document.documentElement
|
13
|
+
);
|
11
14
|
|
12
15
|
if (detect) return
|
13
16
|
|
14
|
-
// Polyfill from https://
|
15
|
-
|
16
|
-
|
17
|
-
|
17
|
+
// Polyfill from https://raw.githubusercontent.com/Financial-Times/polyfill-service/1f3c09b402f65bf6e393f933a15ba63f1b86ef1f/packages/polyfill-library/polyfills/Element/prototype/matches/polyfill.js
|
18
|
+
Element.prototype.matches = Element.prototype.webkitMatchesSelector || Element.prototype.oMatchesSelector || Element.prototype.msMatchesSelector || Element.prototype.mozMatchesSelector || function matches(selector) {
|
19
|
+
var element = this;
|
20
|
+
var elements = (element.document || element.ownerDocument).querySelectorAll(selector);
|
21
|
+
var index = 0;
|
18
22
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
// Detection from https://github.com/Financial-Times/polyfill-service/blob/master/packages/polyfill-library/polyfills/Object/defineProperty/detect.js
|
24
|
-
var detect = (
|
25
|
-
// In IE8, defineProperty could only act on DOM elements, so full support
|
26
|
-
// for the feature requires the ability to set a property on an arbitrary object
|
27
|
-
'defineProperty' in Object && (function() {
|
28
|
-
try {
|
29
|
-
var a = {};
|
30
|
-
Object.defineProperty(a, 'test', {value:42});
|
31
|
-
return true;
|
32
|
-
} catch(e) {
|
33
|
-
return false
|
34
|
-
}
|
35
|
-
}())
|
36
|
-
);
|
37
|
-
|
38
|
-
if (detect) return
|
39
|
-
|
40
|
-
// Polyfill from https://cdn.polyfill.io/v2/polyfill.js?features=Object.defineProperty&flags=always
|
41
|
-
(function (nativeDefineProperty) {
|
42
|
-
|
43
|
-
var supportsAccessors = Object.prototype.hasOwnProperty('__defineGetter__');
|
44
|
-
var ERR_ACCESSORS_NOT_SUPPORTED = 'Getters & setters cannot be defined on this javascript engine';
|
45
|
-
var ERR_VALUE_ACCESSORS = 'A property cannot both have accessors and be writable or have a value';
|
46
|
-
|
47
|
-
Object.defineProperty = function defineProperty(object, property, descriptor) {
|
48
|
-
|
49
|
-
// Where native support exists, assume it
|
50
|
-
if (nativeDefineProperty && (object === window || object === document || object === Element.prototype || object instanceof Element)) {
|
51
|
-
return nativeDefineProperty(object, property, descriptor);
|
52
|
-
}
|
53
|
-
|
54
|
-
if (object === null || !(object instanceof Object || typeof object === 'object')) {
|
55
|
-
throw new TypeError('Object.defineProperty called on non-object');
|
56
|
-
}
|
57
|
-
|
58
|
-
if (!(descriptor instanceof Object)) {
|
59
|
-
throw new TypeError('Property description must be an object');
|
60
|
-
}
|
61
|
-
|
62
|
-
var propertyString = String(property);
|
63
|
-
var hasValueOrWritable = 'value' in descriptor || 'writable' in descriptor;
|
64
|
-
var getterType = 'get' in descriptor && typeof descriptor.get;
|
65
|
-
var setterType = 'set' in descriptor && typeof descriptor.set;
|
66
|
-
|
67
|
-
// handle descriptor.get
|
68
|
-
if (getterType) {
|
69
|
-
if (getterType !== 'function') {
|
70
|
-
throw new TypeError('Getter must be a function');
|
71
|
-
}
|
72
|
-
if (!supportsAccessors) {
|
73
|
-
throw new TypeError(ERR_ACCESSORS_NOT_SUPPORTED);
|
74
|
-
}
|
75
|
-
if (hasValueOrWritable) {
|
76
|
-
throw new TypeError(ERR_VALUE_ACCESSORS);
|
77
|
-
}
|
78
|
-
Object.__defineGetter__.call(object, propertyString, descriptor.get);
|
79
|
-
} else {
|
80
|
-
object[propertyString] = descriptor.value;
|
81
|
-
}
|
82
|
-
|
83
|
-
// handle descriptor.set
|
84
|
-
if (setterType) {
|
85
|
-
if (setterType !== 'function') {
|
86
|
-
throw new TypeError('Setter must be a function');
|
87
|
-
}
|
88
|
-
if (!supportsAccessors) {
|
89
|
-
throw new TypeError(ERR_ACCESSORS_NOT_SUPPORTED);
|
90
|
-
}
|
91
|
-
if (hasValueOrWritable) {
|
92
|
-
throw new TypeError(ERR_VALUE_ACCESSORS);
|
93
|
-
}
|
94
|
-
Object.__defineSetter__.call(object, propertyString, descriptor.set);
|
95
|
-
}
|
96
|
-
|
97
|
-
// OK to define value unconditionally - if a getter has been specified as well, an error would be thrown above
|
98
|
-
if ('value' in descriptor) {
|
99
|
-
object[propertyString] = descriptor.value;
|
100
|
-
}
|
101
|
-
|
102
|
-
return object;
|
103
|
-
};
|
104
|
-
}(Object.defineProperty));
|
105
|
-
})
|
106
|
-
.call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
|
107
|
-
|
108
|
-
(function(undefined) {
|
109
|
-
// Detection from https://github.com/Financial-Times/polyfill-service/blob/master/packages/polyfill-library/polyfills/Function/prototype/bind/detect.js
|
110
|
-
var detect = 'bind' in Function.prototype;
|
23
|
+
while (elements[index] && elements[index] !== element) {
|
24
|
+
++index;
|
25
|
+
}
|
111
26
|
|
112
|
-
|
27
|
+
return !!elements[index];
|
28
|
+
};
|
113
29
|
|
114
|
-
|
115
|
-
Object.defineProperty(Function.prototype, 'bind', {
|
116
|
-
value: function bind(that) { // .length is 1
|
117
|
-
// add necessary es5-shim utilities
|
118
|
-
var $Array = Array;
|
119
|
-
var $Object = Object;
|
120
|
-
var ObjectPrototype = $Object.prototype;
|
121
|
-
var ArrayPrototype = $Array.prototype;
|
122
|
-
var Empty = function Empty() {};
|
123
|
-
var to_string = ObjectPrototype.toString;
|
124
|
-
var hasToStringTag = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol';
|
125
|
-
var isCallable; /* inlined from https://npmjs.com/is-callable */ var fnToStr = Function.prototype.toString, tryFunctionObject = function tryFunctionObject(value) { try { fnToStr.call(value); return true; } catch (e) { return false; } }, fnClass = '[object Function]', genClass = '[object GeneratorFunction]'; isCallable = function isCallable(value) { if (typeof value !== 'function') { return false; } if (hasToStringTag) { return tryFunctionObject(value); } var strClass = to_string.call(value); return strClass === fnClass || strClass === genClass; };
|
126
|
-
var array_slice = ArrayPrototype.slice;
|
127
|
-
var array_concat = ArrayPrototype.concat;
|
128
|
-
var array_push = ArrayPrototype.push;
|
129
|
-
var max = Math.max;
|
130
|
-
// /add necessary es5-shim utilities
|
131
|
-
|
132
|
-
// 1. Let Target be the this value.
|
133
|
-
var target = this;
|
134
|
-
// 2. If IsCallable(Target) is false, throw a TypeError exception.
|
135
|
-
if (!isCallable(target)) {
|
136
|
-
throw new TypeError('Function.prototype.bind called on incompatible ' + target);
|
137
|
-
}
|
138
|
-
// 3. Let A be a new (possibly empty) internal list of all of the
|
139
|
-
// argument values provided after thisArg (arg1, arg2 etc), in order.
|
140
|
-
// XXX slicedArgs will stand in for "A" if used
|
141
|
-
var args = array_slice.call(arguments, 1); // for normal call
|
142
|
-
// 4. Let F be a new native ECMAScript object.
|
143
|
-
// 11. Set the [[Prototype]] internal property of F to the standard
|
144
|
-
// built-in Function prototype object as specified in 15.3.3.1.
|
145
|
-
// 12. Set the [[Call]] internal property of F as described in
|
146
|
-
// 15.3.4.5.1.
|
147
|
-
// 13. Set the [[Construct]] internal property of F as described in
|
148
|
-
// 15.3.4.5.2.
|
149
|
-
// 14. Set the [[HasInstance]] internal property of F as described in
|
150
|
-
// 15.3.4.5.3.
|
151
|
-
var bound;
|
152
|
-
var binder = function () {
|
153
|
-
|
154
|
-
if (this instanceof bound) {
|
155
|
-
// 15.3.4.5.2 [[Construct]]
|
156
|
-
// When the [[Construct]] internal method of a function object,
|
157
|
-
// F that was created using the bind function is called with a
|
158
|
-
// list of arguments ExtraArgs, the following steps are taken:
|
159
|
-
// 1. Let target be the value of F's [[TargetFunction]]
|
160
|
-
// internal property.
|
161
|
-
// 2. If target has no [[Construct]] internal method, a
|
162
|
-
// TypeError exception is thrown.
|
163
|
-
// 3. Let boundArgs be the value of F's [[BoundArgs]] internal
|
164
|
-
// property.
|
165
|
-
// 4. Let args be a new list containing the same values as the
|
166
|
-
// list boundArgs in the same order followed by the same
|
167
|
-
// values as the list ExtraArgs in the same order.
|
168
|
-
// 5. Return the result of calling the [[Construct]] internal
|
169
|
-
// method of target providing args as the arguments.
|
170
|
-
|
171
|
-
var result = target.apply(
|
172
|
-
this,
|
173
|
-
array_concat.call(args, array_slice.call(arguments))
|
174
|
-
);
|
175
|
-
if ($Object(result) === result) {
|
176
|
-
return result;
|
177
|
-
}
|
178
|
-
return this;
|
179
|
-
|
180
|
-
} else {
|
181
|
-
// 15.3.4.5.1 [[Call]]
|
182
|
-
// When the [[Call]] internal method of a function object, F,
|
183
|
-
// which was created using the bind function is called with a
|
184
|
-
// this value and a list of arguments ExtraArgs, the following
|
185
|
-
// steps are taken:
|
186
|
-
// 1. Let boundArgs be the value of F's [[BoundArgs]] internal
|
187
|
-
// property.
|
188
|
-
// 2. Let boundThis be the value of F's [[BoundThis]] internal
|
189
|
-
// property.
|
190
|
-
// 3. Let target be the value of F's [[TargetFunction]] internal
|
191
|
-
// property.
|
192
|
-
// 4. Let args be a new list containing the same values as the
|
193
|
-
// list boundArgs in the same order followed by the same
|
194
|
-
// values as the list ExtraArgs in the same order.
|
195
|
-
// 5. Return the result of calling the [[Call]] internal method
|
196
|
-
// of target providing boundThis as the this value and
|
197
|
-
// providing args as the arguments.
|
198
|
-
|
199
|
-
// equiv: target.call(this, ...boundArgs, ...args)
|
200
|
-
return target.apply(
|
201
|
-
that,
|
202
|
-
array_concat.call(args, array_slice.call(arguments))
|
203
|
-
);
|
204
|
-
|
205
|
-
}
|
30
|
+
}).call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
|
206
31
|
|
207
|
-
|
32
|
+
// @ts-nocheck
|
208
33
|
|
209
|
-
|
210
|
-
// a. Let L be the length property of Target minus the length of A.
|
211
|
-
// b. Set the length own property of F to either 0 or L, whichever is
|
212
|
-
// larger.
|
213
|
-
// 16. Else set the length own property of F to 0.
|
34
|
+
(function(undefined) {
|
214
35
|
|
215
|
-
|
36
|
+
// Detection from https://raw.githubusercontent.com/Financial-Times/polyfill-service/1f3c09b402f65bf6e393f933a15ba63f1b86ef1f/packages/polyfill-library/polyfills/Element/prototype/closest/detect.js
|
37
|
+
var detect = (
|
38
|
+
'document' in this && "closest" in document.documentElement
|
39
|
+
);
|
216
40
|
|
217
|
-
|
218
|
-
// specified in 15.3.5.1.
|
219
|
-
var boundArgs = [];
|
220
|
-
for (var i = 0; i < boundLength; i++) {
|
221
|
-
array_push.call(boundArgs, '$' + i);
|
222
|
-
}
|
41
|
+
if (detect) return
|
223
42
|
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
// for ex.) all use of eval or Function costructor throws an exception.
|
228
|
-
// However in all of these environments Function.prototype.bind exists
|
229
|
-
// and so this code will never be executed.
|
230
|
-
bound = Function('binder', 'return function (' + boundArgs.join(',') + '){ return binder.apply(this, arguments); }')(binder);
|
231
|
-
|
232
|
-
if (target.prototype) {
|
233
|
-
Empty.prototype = target.prototype;
|
234
|
-
bound.prototype = new Empty();
|
235
|
-
// Clean up dangling references.
|
236
|
-
Empty.prototype = null;
|
237
|
-
}
|
43
|
+
// Polyfill from https://raw.githubusercontent.com/Financial-Times/polyfill-service/1f3c09b402f65bf6e393f933a15ba63f1b86ef1f/packages/polyfill-library/polyfills/Element/prototype/closest/polyfill.js
|
44
|
+
Element.prototype.closest = function closest(selector) {
|
45
|
+
var node = this;
|
238
46
|
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
// TODO
|
243
|
-
// 19. Let thrower be the [[ThrowTypeError]] function Object (13.2.3).
|
244
|
-
// 20. Call the [[DefineOwnProperty]] internal method of F with
|
245
|
-
// arguments "caller", PropertyDescriptor {[[Get]]: thrower, [[Set]]:
|
246
|
-
// thrower, [[Enumerable]]: false, [[Configurable]]: false}, and
|
247
|
-
// false.
|
248
|
-
// 21. Call the [[DefineOwnProperty]] internal method of F with
|
249
|
-
// arguments "arguments", PropertyDescriptor {[[Get]]: thrower,
|
250
|
-
// [[Set]]: thrower, [[Enumerable]]: false, [[Configurable]]: false},
|
251
|
-
// and false.
|
252
|
-
|
253
|
-
// TODO
|
254
|
-
// NOTE Function objects created using Function.prototype.bind do not
|
255
|
-
// have a prototype property or the [[Code]], [[FormalParameters]], and
|
256
|
-
// [[Scope]] internal properties.
|
257
|
-
// XXX can't delete prototype in pure-js.
|
258
|
-
|
259
|
-
// 22. Return F.
|
260
|
-
return bound;
|
47
|
+
while (node) {
|
48
|
+
if (node.matches(selector)) return node;
|
49
|
+
else node = 'SVGElement' in window && node instanceof SVGElement ? node.parentNode : node.parentElement;
|
261
50
|
}
|
262
|
-
});
|
263
|
-
})
|
264
|
-
.call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
|
265
51
|
|
266
|
-
|
52
|
+
return null;
|
53
|
+
};
|
267
54
|
|
268
|
-
|
269
|
-
var detect = ('Window' in this);
|
55
|
+
}).call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
|
270
56
|
|
271
|
-
|
57
|
+
/**
|
58
|
+
* Returns the value of the given attribute closest to the given element (including itself)
|
59
|
+
*
|
60
|
+
* @deprecated Will be made private in v5.0
|
61
|
+
* @param {Element} $element - The element to start walking the DOM tree up
|
62
|
+
* @param {string} attributeName - The name of the attribute
|
63
|
+
* @returns {string | null} Attribute value
|
64
|
+
*/
|
65
|
+
function closestAttributeValue ($element, attributeName) {
|
66
|
+
var $closestElementWithAttribute = $element.closest('[' + attributeName + ']');
|
67
|
+
return $closestElementWithAttribute
|
68
|
+
? $closestElementWithAttribute.getAttribute(attributeName)
|
69
|
+
: null
|
70
|
+
}
|
272
71
|
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
}
|
72
|
+
/**
|
73
|
+
* Common helpers which do not require polyfill.
|
74
|
+
*
|
75
|
+
* IMPORTANT: If a helper require a polyfill, please isolate it in its own module
|
76
|
+
* so that the polyfill can be properly tree-shaken and does not burden
|
77
|
+
* the components that do not need that helper
|
78
|
+
*
|
79
|
+
* @module common/index
|
80
|
+
*/
|
283
81
|
|
284
|
-
|
285
|
-
|
82
|
+
/**
|
83
|
+
* Config flattening function
|
84
|
+
*
|
85
|
+
* Takes any number of objects, flattens them into namespaced key-value pairs,
|
86
|
+
* (e.g. {'i18n.showSection': 'Show section'}) and combines them together, with
|
87
|
+
* greatest priority on the LAST item passed in.
|
88
|
+
*
|
89
|
+
* @deprecated Will be made private in v5.0
|
90
|
+
* @returns {Object<string, unknown>} A flattened object of key-value pairs.
|
91
|
+
*/
|
92
|
+
function mergeConfigs (/* configObject1, configObject2, ...configObjects */) {
|
93
|
+
/**
|
94
|
+
* Function to take nested objects and flatten them to a dot-separated keyed
|
95
|
+
* object. Doing this means we don't need to do any deep/recursive merging of
|
96
|
+
* each of our objects, nor transform our dataset from a flat list into a
|
97
|
+
* nested object.
|
98
|
+
*
|
99
|
+
* @param {Object<string, unknown>} configObject - Deeply nested object
|
100
|
+
* @returns {Object<string, unknown>} Flattened object with dot-separated keys
|
101
|
+
*/
|
102
|
+
var flattenObject = function (configObject) {
|
103
|
+
// Prepare an empty return object
|
104
|
+
/** @type {Object<string, unknown>} */
|
105
|
+
var flattenedObject = {};
|
106
|
+
|
107
|
+
/**
|
108
|
+
* Our flattening function, this is called recursively for each level of
|
109
|
+
* depth in the object. At each level we prepend the previous level names to
|
110
|
+
* the key using `prefix`.
|
111
|
+
*
|
112
|
+
* @param {Partial<Object<string, unknown>>} obj - Object to flatten
|
113
|
+
* @param {string} [prefix] - Optional dot-separated prefix
|
114
|
+
*/
|
115
|
+
var flattenLoop = function (obj, prefix) {
|
116
|
+
// Loop through keys...
|
117
|
+
for (var key in obj) {
|
118
|
+
// Check to see if this is a prototypical key/value,
|
119
|
+
// if it is, skip it.
|
120
|
+
if (!Object.prototype.hasOwnProperty.call(obj, key)) {
|
121
|
+
continue
|
122
|
+
}
|
123
|
+
var value = obj[key];
|
124
|
+
var prefixedKey = prefix ? prefix + '.' + key : key;
|
125
|
+
if (typeof value === 'object') {
|
126
|
+
// If the value is a nested object, recurse over that too
|
127
|
+
flattenLoop(value, prefixedKey);
|
128
|
+
} else {
|
129
|
+
// Otherwise, add this value to our return object
|
130
|
+
flattenedObject[prefixedKey] = value;
|
131
|
+
}
|
132
|
+
}
|
133
|
+
};
|
286
134
|
|
287
|
-
|
135
|
+
// Kick off the recursive loop
|
136
|
+
flattenLoop(configObject);
|
137
|
+
return flattenedObject
|
138
|
+
};
|
288
139
|
|
289
|
-
//
|
290
|
-
|
140
|
+
// Start with an empty object as our base
|
141
|
+
/** @type {Object<string, unknown>} */
|
142
|
+
var formattedConfigObject = {};
|
291
143
|
|
292
|
-
|
144
|
+
// Loop through each of the remaining passed objects and push their keys
|
145
|
+
// one-by-one into configObject. Any duplicate keys will override the existing
|
146
|
+
// key with the new value.
|
147
|
+
for (var i = 0; i < arguments.length; i++) {
|
148
|
+
var obj = flattenObject(arguments[i]);
|
149
|
+
for (var key in obj) {
|
150
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
151
|
+
formattedConfigObject[key] = obj[key];
|
152
|
+
}
|
153
|
+
}
|
154
|
+
}
|
293
155
|
|
294
|
-
|
295
|
-
|
156
|
+
return formattedConfigObject
|
157
|
+
}
|
296
158
|
|
297
|
-
|
159
|
+
/**
|
160
|
+
* Extracts keys starting with a particular namespace from a flattened config
|
161
|
+
* object, removing the namespace in the process.
|
162
|
+
*
|
163
|
+
* @deprecated Will be made private in v5.0
|
164
|
+
* @param {Object<string, unknown>} configObject - The object to extract key-value pairs from.
|
165
|
+
* @param {string} namespace - The namespace to filter keys with.
|
166
|
+
* @returns {Object<string, unknown>} Flattened object with dot-separated key namespace removed
|
167
|
+
* @throws {Error} Config object required
|
168
|
+
* @throws {Error} Namespace string required
|
169
|
+
*/
|
170
|
+
function extractConfigByNamespace (configObject, namespace) {
|
171
|
+
// Check we have what we need
|
172
|
+
if (!configObject || typeof configObject !== 'object') {
|
173
|
+
throw new Error('Provide a `configObject` of type "object".')
|
174
|
+
}
|
298
175
|
|
299
|
-
|
300
|
-
|
176
|
+
if (!namespace || typeof namespace !== 'string') {
|
177
|
+
throw new Error('Provide a `namespace` of type "string" to filter the `configObject` by.')
|
178
|
+
}
|
301
179
|
|
302
|
-
|
180
|
+
/** @type {Object<string, unknown>} */
|
181
|
+
var newObject = {};
|
182
|
+
|
183
|
+
for (var key in configObject) {
|
184
|
+
// Split the key into parts, using . as our namespace separator
|
185
|
+
var keyParts = key.split('.');
|
186
|
+
// Check if the first namespace matches the configured namespace
|
187
|
+
if (Object.prototype.hasOwnProperty.call(configObject, key) && keyParts[0] === namespace) {
|
188
|
+
// Remove the first item (the namespace) from the parts array,
|
189
|
+
// but only if there is more than one part (we don't want blank keys!)
|
190
|
+
if (keyParts.length > 1) {
|
191
|
+
keyParts.shift();
|
192
|
+
}
|
193
|
+
// Join the remaining parts back together
|
194
|
+
var newKey = keyParts.join('.');
|
195
|
+
// Add them to our new object
|
196
|
+
newObject[newKey] = configObject[key];
|
197
|
+
}
|
198
|
+
}
|
199
|
+
return newObject
|
200
|
+
}
|
303
201
|
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
}
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
.call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
|
313
|
-
|
314
|
-
(function(undefined) {
|
315
|
-
|
316
|
-
// Detection from https://github.com/Financial-Times/polyfill-service/blob/master/packages/polyfill-library/polyfills/Element/detect.js
|
317
|
-
var detect = ('Element' in this && 'HTMLElement' in this);
|
318
|
-
|
319
|
-
if (detect) return
|
320
|
-
|
321
|
-
// Polyfill from https://cdn.polyfill.io/v2/polyfill.js?features=Element&flags=always
|
322
|
-
(function () {
|
323
|
-
|
324
|
-
// IE8
|
325
|
-
if (window.Element && !window.HTMLElement) {
|
326
|
-
window.HTMLElement = window.Element;
|
327
|
-
return;
|
328
|
-
}
|
329
|
-
|
330
|
-
// create Element constructor
|
331
|
-
window.Element = window.HTMLElement = new Function('return function Element() {}')();
|
332
|
-
|
333
|
-
// generate sandboxed iframe
|
334
|
-
var vbody = document.appendChild(document.createElement('body'));
|
335
|
-
var frame = vbody.appendChild(document.createElement('iframe'));
|
336
|
-
|
337
|
-
// use sandboxed iframe to replicate Element functionality
|
338
|
-
var frameDocument = frame.contentWindow.document;
|
339
|
-
var prototype = Element.prototype = frameDocument.appendChild(frameDocument.createElement('*'));
|
340
|
-
var cache = {};
|
341
|
-
|
342
|
-
// polyfill Element.prototype on an element
|
343
|
-
var shiv = function (element, deep) {
|
344
|
-
var
|
345
|
-
childNodes = element.childNodes || [],
|
346
|
-
index = -1,
|
347
|
-
key, value, childNode;
|
348
|
-
|
349
|
-
if (element.nodeType === 1 && element.constructor !== Element) {
|
350
|
-
element.constructor = Element;
|
351
|
-
|
352
|
-
for (key in cache) {
|
353
|
-
value = cache[key];
|
354
|
-
element[key] = value;
|
355
|
-
}
|
356
|
-
}
|
357
|
-
|
358
|
-
while (childNode = deep && childNodes[++index]) {
|
359
|
-
shiv(childNode, deep);
|
360
|
-
}
|
361
|
-
|
362
|
-
return element;
|
363
|
-
};
|
364
|
-
|
365
|
-
var elements = document.getElementsByTagName('*');
|
366
|
-
var nativeCreateElement = document.createElement;
|
367
|
-
var interval;
|
368
|
-
var loopLimit = 100;
|
369
|
-
|
370
|
-
prototype.attachEvent('onpropertychange', function (event) {
|
371
|
-
var
|
372
|
-
propertyName = event.propertyName,
|
373
|
-
nonValue = !cache.hasOwnProperty(propertyName),
|
374
|
-
newValue = prototype[propertyName],
|
375
|
-
oldValue = cache[propertyName],
|
376
|
-
index = -1,
|
377
|
-
element;
|
378
|
-
|
379
|
-
while (element = elements[++index]) {
|
380
|
-
if (element.nodeType === 1) {
|
381
|
-
if (nonValue || element[propertyName] === oldValue) {
|
382
|
-
element[propertyName] = newValue;
|
383
|
-
}
|
384
|
-
}
|
385
|
-
}
|
386
|
-
|
387
|
-
cache[propertyName] = newValue;
|
388
|
-
});
|
389
|
-
|
390
|
-
prototype.constructor = Element;
|
391
|
-
|
392
|
-
if (!prototype.hasAttribute) {
|
393
|
-
// <Element>.hasAttribute
|
394
|
-
prototype.hasAttribute = function hasAttribute(name) {
|
395
|
-
return this.getAttribute(name) !== null;
|
396
|
-
};
|
397
|
-
}
|
398
|
-
|
399
|
-
// Apply Element prototype to the pre-existing DOM as soon as the body element appears.
|
400
|
-
function bodyCheck() {
|
401
|
-
if (!(loopLimit--)) clearTimeout(interval);
|
402
|
-
if (document.body && !document.body.prototype && /(complete|interactive)/.test(document.readyState)) {
|
403
|
-
shiv(document, true);
|
404
|
-
if (interval && document.body.prototype) clearTimeout(interval);
|
405
|
-
return (!!document.body.prototype);
|
406
|
-
}
|
407
|
-
return false;
|
408
|
-
}
|
409
|
-
if (!bodyCheck()) {
|
410
|
-
document.onreadystatechange = bodyCheck;
|
411
|
-
interval = setInterval(bodyCheck, 25);
|
412
|
-
}
|
413
|
-
|
414
|
-
// Apply to any new elements created after load
|
415
|
-
document.createElement = function createElement(nodeName) {
|
416
|
-
var element = nativeCreateElement(String(nodeName).toLowerCase());
|
417
|
-
return shiv(element);
|
418
|
-
};
|
419
|
-
|
420
|
-
// remove sandboxed iframe
|
421
|
-
document.removeChild(vbody);
|
422
|
-
}());
|
423
|
-
|
424
|
-
})
|
425
|
-
.call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
|
426
|
-
|
427
|
-
(function(undefined) {
|
428
|
-
|
429
|
-
// Detection from https://github.com/Financial-Times/polyfill-service/blob/master/packages/polyfill-library/polyfills/Event/detect.js
|
430
|
-
var detect = (
|
431
|
-
(function(global) {
|
432
|
-
|
433
|
-
if (!('Event' in global)) return false;
|
434
|
-
if (typeof global.Event === 'function') return true;
|
435
|
-
|
436
|
-
try {
|
437
|
-
|
438
|
-
// In IE 9-11, the Event object exists but cannot be instantiated
|
439
|
-
new Event('click');
|
440
|
-
return true;
|
441
|
-
} catch(e) {
|
442
|
-
return false;
|
443
|
-
}
|
444
|
-
}(this))
|
445
|
-
);
|
446
|
-
|
447
|
-
if (detect) return
|
448
|
-
|
449
|
-
// Polyfill from https://cdn.polyfill.io/v2/polyfill.js?features=Event&flags=always
|
450
|
-
(function () {
|
451
|
-
var unlistenableWindowEvents = {
|
452
|
-
click: 1,
|
453
|
-
dblclick: 1,
|
454
|
-
keyup: 1,
|
455
|
-
keypress: 1,
|
456
|
-
keydown: 1,
|
457
|
-
mousedown: 1,
|
458
|
-
mouseup: 1,
|
459
|
-
mousemove: 1,
|
460
|
-
mouseover: 1,
|
461
|
-
mouseenter: 1,
|
462
|
-
mouseleave: 1,
|
463
|
-
mouseout: 1,
|
464
|
-
storage: 1,
|
465
|
-
storagecommit: 1,
|
466
|
-
textinput: 1
|
467
|
-
};
|
468
|
-
|
469
|
-
// This polyfill depends on availability of `document` so will not run in a worker
|
470
|
-
// However, we asssume there are no browsers with worker support that lack proper
|
471
|
-
// support for `Event` within the worker
|
472
|
-
if (typeof document === 'undefined' || typeof window === 'undefined') return;
|
473
|
-
|
474
|
-
function indexOf(array, element) {
|
475
|
-
var
|
476
|
-
index = -1,
|
477
|
-
length = array.length;
|
478
|
-
|
479
|
-
while (++index < length) {
|
480
|
-
if (index in array && array[index] === element) {
|
481
|
-
return index;
|
482
|
-
}
|
483
|
-
}
|
484
|
-
|
485
|
-
return -1;
|
486
|
-
}
|
487
|
-
|
488
|
-
var existingProto = (window.Event && window.Event.prototype) || null;
|
489
|
-
window.Event = Window.prototype.Event = function Event(type, eventInitDict) {
|
490
|
-
if (!type) {
|
491
|
-
throw new Error('Not enough arguments');
|
492
|
-
}
|
493
|
-
|
494
|
-
var event;
|
495
|
-
// Shortcut if browser supports createEvent
|
496
|
-
if ('createEvent' in document) {
|
497
|
-
event = document.createEvent('Event');
|
498
|
-
var bubbles = eventInitDict && eventInitDict.bubbles !== undefined ? eventInitDict.bubbles : false;
|
499
|
-
var cancelable = eventInitDict && eventInitDict.cancelable !== undefined ? eventInitDict.cancelable : false;
|
500
|
-
|
501
|
-
event.initEvent(type, bubbles, cancelable);
|
502
|
-
|
503
|
-
return event;
|
504
|
-
}
|
505
|
-
|
506
|
-
event = document.createEventObject();
|
507
|
-
|
508
|
-
event.type = type;
|
509
|
-
event.bubbles = eventInitDict && eventInitDict.bubbles !== undefined ? eventInitDict.bubbles : false;
|
510
|
-
event.cancelable = eventInitDict && eventInitDict.cancelable !== undefined ? eventInitDict.cancelable : false;
|
511
|
-
|
512
|
-
return event;
|
513
|
-
};
|
514
|
-
if (existingProto) {
|
515
|
-
Object.defineProperty(window.Event, 'prototype', {
|
516
|
-
configurable: false,
|
517
|
-
enumerable: false,
|
518
|
-
writable: true,
|
519
|
-
value: existingProto
|
520
|
-
});
|
521
|
-
}
|
522
|
-
|
523
|
-
if (!('createEvent' in document)) {
|
524
|
-
window.addEventListener = Window.prototype.addEventListener = Document.prototype.addEventListener = Element.prototype.addEventListener = function addEventListener() {
|
525
|
-
var
|
526
|
-
element = this,
|
527
|
-
type = arguments[0],
|
528
|
-
listener = arguments[1];
|
529
|
-
|
530
|
-
if (element === window && type in unlistenableWindowEvents) {
|
531
|
-
throw new Error('In IE8 the event: ' + type + ' is not available on the window object. Please see https://github.com/Financial-Times/polyfill-service/issues/317 for more information.');
|
532
|
-
}
|
533
|
-
|
534
|
-
if (!element._events) {
|
535
|
-
element._events = {};
|
536
|
-
}
|
537
|
-
|
538
|
-
if (!element._events[type]) {
|
539
|
-
element._events[type] = function (event) {
|
540
|
-
var
|
541
|
-
list = element._events[event.type].list,
|
542
|
-
events = list.slice(),
|
543
|
-
index = -1,
|
544
|
-
length = events.length,
|
545
|
-
eventElement;
|
546
|
-
|
547
|
-
event.preventDefault = function preventDefault() {
|
548
|
-
if (event.cancelable !== false) {
|
549
|
-
event.returnValue = false;
|
550
|
-
}
|
551
|
-
};
|
552
|
-
|
553
|
-
event.stopPropagation = function stopPropagation() {
|
554
|
-
event.cancelBubble = true;
|
555
|
-
};
|
556
|
-
|
557
|
-
event.stopImmediatePropagation = function stopImmediatePropagation() {
|
558
|
-
event.cancelBubble = true;
|
559
|
-
event.cancelImmediate = true;
|
560
|
-
};
|
561
|
-
|
562
|
-
event.currentTarget = element;
|
563
|
-
event.relatedTarget = event.fromElement || null;
|
564
|
-
event.target = event.target || event.srcElement || element;
|
565
|
-
event.timeStamp = new Date().getTime();
|
566
|
-
|
567
|
-
if (event.clientX) {
|
568
|
-
event.pageX = event.clientX + document.documentElement.scrollLeft;
|
569
|
-
event.pageY = event.clientY + document.documentElement.scrollTop;
|
570
|
-
}
|
571
|
-
|
572
|
-
while (++index < length && !event.cancelImmediate) {
|
573
|
-
if (index in events) {
|
574
|
-
eventElement = events[index];
|
575
|
-
|
576
|
-
if (indexOf(list, eventElement) !== -1 && typeof eventElement === 'function') {
|
577
|
-
eventElement.call(element, event);
|
578
|
-
}
|
579
|
-
}
|
580
|
-
}
|
581
|
-
};
|
582
|
-
|
583
|
-
element._events[type].list = [];
|
584
|
-
|
585
|
-
if (element.attachEvent) {
|
586
|
-
element.attachEvent('on' + type, element._events[type]);
|
587
|
-
}
|
588
|
-
}
|
589
|
-
|
590
|
-
element._events[type].list.push(listener);
|
591
|
-
};
|
592
|
-
|
593
|
-
window.removeEventListener = Window.prototype.removeEventListener = Document.prototype.removeEventListener = Element.prototype.removeEventListener = function removeEventListener() {
|
594
|
-
var
|
595
|
-
element = this,
|
596
|
-
type = arguments[0],
|
597
|
-
listener = arguments[1],
|
598
|
-
index;
|
599
|
-
|
600
|
-
if (element._events && element._events[type] && element._events[type].list) {
|
601
|
-
index = indexOf(element._events[type].list, listener);
|
602
|
-
|
603
|
-
if (index !== -1) {
|
604
|
-
element._events[type].list.splice(index, 1);
|
605
|
-
|
606
|
-
if (!element._events[type].list.length) {
|
607
|
-
if (element.detachEvent) {
|
608
|
-
element.detachEvent('on' + type, element._events[type]);
|
609
|
-
}
|
610
|
-
delete element._events[type];
|
611
|
-
}
|
612
|
-
}
|
613
|
-
}
|
614
|
-
};
|
615
|
-
|
616
|
-
window.dispatchEvent = Window.prototype.dispatchEvent = Document.prototype.dispatchEvent = Element.prototype.dispatchEvent = function dispatchEvent(event) {
|
617
|
-
if (!arguments.length) {
|
618
|
-
throw new Error('Not enough arguments');
|
619
|
-
}
|
620
|
-
|
621
|
-
if (!event || typeof event.type !== 'string') {
|
622
|
-
throw new Error('DOM Events Exception 0');
|
623
|
-
}
|
624
|
-
|
625
|
-
var element = this, type = event.type;
|
626
|
-
|
627
|
-
try {
|
628
|
-
if (!event.bubbles) {
|
629
|
-
event.cancelBubble = true;
|
630
|
-
|
631
|
-
var cancelBubbleEvent = function (event) {
|
632
|
-
event.cancelBubble = true;
|
633
|
-
|
634
|
-
(element || window).detachEvent('on' + type, cancelBubbleEvent);
|
635
|
-
};
|
636
|
-
|
637
|
-
this.attachEvent('on' + type, cancelBubbleEvent);
|
638
|
-
}
|
639
|
-
|
640
|
-
this.fireEvent('on' + type, event);
|
641
|
-
} catch (error) {
|
642
|
-
event.target = element;
|
643
|
-
|
644
|
-
do {
|
645
|
-
event.currentTarget = element;
|
646
|
-
|
647
|
-
if ('_events' in element && typeof element._events[type] === 'function') {
|
648
|
-
element._events[type].call(element, event);
|
649
|
-
}
|
650
|
-
|
651
|
-
if (typeof element['on' + type] === 'function') {
|
652
|
-
element['on' + type].call(element, event);
|
653
|
-
}
|
654
|
-
|
655
|
-
element = element.nodeType === 9 ? element.parentWindow : element.parentNode;
|
656
|
-
} while (element && !event.cancelBubble);
|
657
|
-
}
|
658
|
-
|
659
|
-
return true;
|
660
|
-
};
|
661
|
-
|
662
|
-
// Add the DOMContentLoaded Event
|
663
|
-
document.attachEvent('onreadystatechange', function() {
|
664
|
-
if (document.readyState === 'complete') {
|
665
|
-
document.dispatchEvent(new Event('DOMContentLoaded', {
|
666
|
-
bubbles: true
|
667
|
-
}));
|
668
|
-
}
|
669
|
-
});
|
670
|
-
}
|
671
|
-
}());
|
672
|
-
|
673
|
-
})
|
674
|
-
.call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
|
675
|
-
|
676
|
-
(function(undefined) {
|
677
|
-
|
678
|
-
// Detection from https://raw.githubusercontent.com/Financial-Times/polyfill-service/master/packages/polyfill-library/polyfills/DOMTokenList/detect.js
|
679
|
-
var detect = (
|
680
|
-
'DOMTokenList' in this && (function (x) {
|
681
|
-
return 'classList' in x ? !x.classList.toggle('x', false) && !x.className : true;
|
682
|
-
})(document.createElement('x'))
|
683
|
-
);
|
202
|
+
/**
|
203
|
+
* @template {Node} ElementType
|
204
|
+
* @callback nodeListIterator
|
205
|
+
* @param {ElementType} value - The current node being iterated on
|
206
|
+
* @param {number} index - The current index in the iteration
|
207
|
+
* @param {NodeListOf<ElementType>} nodes - NodeList from querySelectorAll()
|
208
|
+
* @returns {void}
|
209
|
+
*/
|
684
210
|
|
685
|
-
|
211
|
+
// @ts-nocheck
|
212
|
+
(function (undefined) {
|
686
213
|
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
var defineGetter = function (object, name, fn, configurable) {
|
702
|
-
if (Object.defineProperty)
|
703
|
-
Object.defineProperty(object, name, {
|
704
|
-
configurable: false === dpSupport ? true : !!configurable,
|
705
|
-
get: fn
|
706
|
-
});
|
214
|
+
// Detection from https://github.com/Financial-Times/polyfill-service/blob/master/packages/polyfill-library/polyfills/Object/defineProperty/detect.js
|
215
|
+
var detect = (
|
216
|
+
// In IE8, defineProperty could only act on DOM elements, so full support
|
217
|
+
// for the feature requires the ability to set a property on an arbitrary object
|
218
|
+
'defineProperty' in Object && (function() {
|
219
|
+
try {
|
220
|
+
var a = {};
|
221
|
+
Object.defineProperty(a, 'test', {value:42});
|
222
|
+
return true;
|
223
|
+
} catch(e) {
|
224
|
+
return false
|
225
|
+
}
|
226
|
+
}())
|
227
|
+
);
|
707
228
|
|
708
|
-
|
709
|
-
};
|
229
|
+
if (detect) return
|
710
230
|
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
231
|
+
// Polyfill from https://cdn.polyfill.io/v2/polyfill.js?features=Object.defineProperty&flags=always
|
232
|
+
(function (nativeDefineProperty) {
|
233
|
+
|
234
|
+
var supportsAccessors = Object.prototype.hasOwnProperty('__defineGetter__');
|
235
|
+
var ERR_ACCESSORS_NOT_SUPPORTED = 'Getters & setters cannot be defined on this javascript engine';
|
236
|
+
var ERR_VALUE_ACCESSORS = 'A property cannot both have accessors and be writable or have a value';
|
237
|
+
|
238
|
+
Object.defineProperty = function defineProperty(object, property, descriptor) {
|
239
|
+
|
240
|
+
// Where native support exists, assume it
|
241
|
+
if (nativeDefineProperty && (object === window || object === document || object === Element.prototype || object instanceof Element)) {
|
242
|
+
return nativeDefineProperty(object, property, descriptor);
|
243
|
+
}
|
244
|
+
|
245
|
+
if (object === null || !(object instanceof Object || typeof object === 'object')) {
|
246
|
+
throw new TypeError('Object.defineProperty called on non-object');
|
247
|
+
}
|
248
|
+
|
249
|
+
if (!(descriptor instanceof Object)) {
|
250
|
+
throw new TypeError('Property description must be an object');
|
251
|
+
}
|
252
|
+
|
253
|
+
var propertyString = String(property);
|
254
|
+
var hasValueOrWritable = 'value' in descriptor || 'writable' in descriptor;
|
255
|
+
var getterType = 'get' in descriptor && typeof descriptor.get;
|
256
|
+
var setterType = 'set' in descriptor && typeof descriptor.set;
|
257
|
+
|
258
|
+
// handle descriptor.get
|
259
|
+
if (getterType) {
|
260
|
+
if (getterType !== 'function') {
|
261
|
+
throw new TypeError('Getter must be a function');
|
262
|
+
}
|
263
|
+
if (!supportsAccessors) {
|
264
|
+
throw new TypeError(ERR_ACCESSORS_NOT_SUPPORTED);
|
265
|
+
}
|
266
|
+
if (hasValueOrWritable) {
|
267
|
+
throw new TypeError(ERR_VALUE_ACCESSORS);
|
268
|
+
}
|
269
|
+
Object.__defineGetter__.call(object, propertyString, descriptor.get);
|
270
|
+
} else {
|
271
|
+
object[propertyString] = descriptor.value;
|
272
|
+
}
|
273
|
+
|
274
|
+
// handle descriptor.set
|
275
|
+
if (setterType) {
|
276
|
+
if (setterType !== 'function') {
|
277
|
+
throw new TypeError('Setter must be a function');
|
278
|
+
}
|
279
|
+
if (!supportsAccessors) {
|
280
|
+
throw new TypeError(ERR_ACCESSORS_NOT_SUPPORTED);
|
281
|
+
}
|
282
|
+
if (hasValueOrWritable) {
|
283
|
+
throw new TypeError(ERR_VALUE_ACCESSORS);
|
284
|
+
}
|
285
|
+
Object.__defineSetter__.call(object, propertyString, descriptor.set);
|
286
|
+
}
|
287
|
+
|
288
|
+
// OK to define value unconditionally - if a getter has been specified as well, an error would be thrown above
|
289
|
+
if ('value' in descriptor) {
|
290
|
+
object[propertyString] = descriptor.value;
|
291
|
+
}
|
292
|
+
|
293
|
+
return object;
|
294
|
+
};
|
295
|
+
}(Object.defineProperty));
|
296
|
+
})
|
297
|
+
.call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
|
718
298
|
|
299
|
+
// @ts-nocheck
|
300
|
+
(function (undefined) {
|
719
301
|
|
720
|
-
|
721
|
-
|
722
|
-
var tokens = [];
|
723
|
-
var tokenMap = {};
|
724
|
-
var length = 0;
|
725
|
-
var maxLength = 0;
|
726
|
-
var addIndexGetter = function (i) {
|
727
|
-
defineGetter(that, i, function () {
|
728
|
-
preop();
|
729
|
-
return tokens[i];
|
730
|
-
}, false);
|
302
|
+
// Detection from https://github.com/Financial-Times/polyfill-service/blob/master/packages/polyfill-library/polyfills/Document/detect.js
|
303
|
+
var detect = ("Document" in this);
|
731
304
|
|
732
|
-
|
733
|
-
var reindex = function () {
|
305
|
+
if (detect) return
|
734
306
|
|
735
|
-
|
736
|
-
|
737
|
-
for (; maxLength < length; ++maxLength) {
|
738
|
-
addIndexGetter(maxLength);
|
739
|
-
}
|
740
|
-
};
|
307
|
+
// Polyfill from https://cdn.polyfill.io/v2/polyfill.js?features=Document&flags=always
|
308
|
+
if ((typeof WorkerGlobalScope === "undefined") && (typeof importScripts !== "function")) {
|
741
309
|
|
742
|
-
|
743
|
-
var preop = function () {
|
744
|
-
var error;
|
745
|
-
var i;
|
746
|
-
var args = arguments;
|
747
|
-
var rSpace = /\s+/;
|
748
|
-
|
749
|
-
/** Validate the token/s passed to an instance method, if any. */
|
750
|
-
if (args.length)
|
751
|
-
for (i = 0; i < args.length; ++i)
|
752
|
-
if (rSpace.test(args[i])) {
|
753
|
-
error = new SyntaxError('String "' + args[i] + '" ' + "contains" + ' an invalid character');
|
754
|
-
error.code = 5;
|
755
|
-
error.name = "InvalidCharacterError";
|
756
|
-
throw error;
|
757
|
-
}
|
310
|
+
if (this.HTMLDocument) { // IE8
|
758
311
|
|
312
|
+
// HTMLDocument is an extension of Document. If the browser has HTMLDocument but not Document, the former will suffice as an alias for the latter.
|
313
|
+
this.Document = this.HTMLDocument;
|
759
314
|
|
760
|
-
|
761
|
-
if (typeof el[prop] === "object") {
|
762
|
-
tokens = ("" + el[prop].baseVal).replace(/^\s+|\s+$/g, "").split(rSpace);
|
763
|
-
} else {
|
764
|
-
tokens = ("" + el[prop]).replace(/^\s+|\s+$/g, "").split(rSpace);
|
765
|
-
}
|
315
|
+
} else {
|
766
316
|
|
767
|
-
|
768
|
-
|
317
|
+
// Create an empty function to act as the missing constructor for the document object, attach the document object as its prototype. The function needs to be anonymous else it is hoisted and causes the feature detect to prematurely pass, preventing the assignments below being made.
|
318
|
+
this.Document = this.HTMLDocument = document.constructor = (new Function('return function Document() {}')());
|
319
|
+
this.Document.prototype = document;
|
320
|
+
}
|
321
|
+
}
|
769
322
|
|
770
|
-
/** Repopulate the internal token lists */
|
771
|
-
tokenMap = {};
|
772
|
-
for (i = 0; i < tokens.length; ++i)
|
773
|
-
tokenMap[tokens[i]] = true;
|
774
|
-
length = tokens.length;
|
775
|
-
reindex();
|
776
|
-
};
|
777
323
|
|
778
|
-
|
779
|
-
|
324
|
+
})
|
325
|
+
.call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
|
780
326
|
|
781
|
-
|
782
|
-
defineGetter(that, "length", function () {
|
783
|
-
preop();
|
784
|
-
return length;
|
785
|
-
});
|
327
|
+
// @ts-nocheck
|
786
328
|
|
787
|
-
|
788
|
-
that.toLocaleString =
|
789
|
-
that.toString = function () {
|
790
|
-
preop();
|
791
|
-
return tokens.join(" ");
|
792
|
-
};
|
329
|
+
(function(undefined) {
|
793
330
|
|
794
|
-
|
795
|
-
|
796
|
-
return tokens[idx];
|
797
|
-
};
|
331
|
+
// Detection from https://github.com/Financial-Times/polyfill-service/blob/master/packages/polyfill-library/polyfills/Element/detect.js
|
332
|
+
var detect = ('Element' in this && 'HTMLElement' in this);
|
798
333
|
|
799
|
-
|
800
|
-
preop();
|
801
|
-
return !!tokenMap[token];
|
802
|
-
};
|
334
|
+
if (detect) return
|
803
335
|
|
804
|
-
|
805
|
-
|
336
|
+
// Polyfill from https://cdn.polyfill.io/v2/polyfill.js?features=Element&flags=always
|
337
|
+
(function () {
|
806
338
|
|
807
|
-
|
808
|
-
|
809
|
-
|
810
|
-
|
811
|
-
|
812
|
-
}
|
813
|
-
}
|
339
|
+
// IE8
|
340
|
+
if (window.Element && !window.HTMLElement) {
|
341
|
+
window.HTMLElement = window.Element;
|
342
|
+
return;
|
343
|
+
}
|
814
344
|
|
815
|
-
|
816
|
-
|
817
|
-
|
818
|
-
|
819
|
-
|
820
|
-
|
821
|
-
|
822
|
-
|
823
|
-
|
824
|
-
|
825
|
-
|
345
|
+
// create Element constructor
|
346
|
+
window.Element = window.HTMLElement = new Function('return function Element() {}')();
|
347
|
+
|
348
|
+
// generate sandboxed iframe
|
349
|
+
var vbody = document.appendChild(document.createElement('body'));
|
350
|
+
var frame = vbody.appendChild(document.createElement('iframe'));
|
351
|
+
|
352
|
+
// use sandboxed iframe to replicate Element functionality
|
353
|
+
var frameDocument = frame.contentWindow.document;
|
354
|
+
var prototype = Element.prototype = frameDocument.appendChild(frameDocument.createElement('*'));
|
355
|
+
var cache = {};
|
356
|
+
|
357
|
+
// polyfill Element.prototype on an element
|
358
|
+
var shiv = function (element, deep) {
|
359
|
+
var
|
360
|
+
childNodes = element.childNodes || [],
|
361
|
+
index = -1,
|
362
|
+
key, value, childNode;
|
363
|
+
|
364
|
+
if (element.nodeType === 1 && element.constructor !== Element) {
|
365
|
+
element.constructor = Element;
|
366
|
+
|
367
|
+
for (key in cache) {
|
368
|
+
value = cache[key];
|
369
|
+
element[key] = value;
|
370
|
+
}
|
371
|
+
}
|
372
|
+
|
373
|
+
while (childNode = deep && childNodes[++index]) {
|
374
|
+
shiv(childNode, deep);
|
375
|
+
}
|
376
|
+
|
377
|
+
return element;
|
378
|
+
};
|
379
|
+
|
380
|
+
var elements = document.getElementsByTagName('*');
|
381
|
+
var nativeCreateElement = document.createElement;
|
382
|
+
var interval;
|
383
|
+
var loopLimit = 100;
|
384
|
+
|
385
|
+
prototype.attachEvent('onpropertychange', function (event) {
|
386
|
+
var
|
387
|
+
propertyName = event.propertyName,
|
388
|
+
nonValue = !cache.hasOwnProperty(propertyName),
|
389
|
+
newValue = prototype[propertyName],
|
390
|
+
oldValue = cache[propertyName],
|
391
|
+
index = -1,
|
392
|
+
element;
|
393
|
+
|
394
|
+
while (element = elements[++index]) {
|
395
|
+
if (element.nodeType === 1) {
|
396
|
+
if (nonValue || element[propertyName] === oldValue) {
|
397
|
+
element[propertyName] = newValue;
|
398
|
+
}
|
399
|
+
}
|
400
|
+
}
|
401
|
+
|
402
|
+
cache[propertyName] = newValue;
|
403
|
+
});
|
404
|
+
|
405
|
+
prototype.constructor = Element;
|
406
|
+
|
407
|
+
if (!prototype.hasAttribute) {
|
408
|
+
// <Element>.hasAttribute
|
409
|
+
prototype.hasAttribute = function hasAttribute(name) {
|
410
|
+
return this.getAttribute(name) !== null;
|
411
|
+
};
|
412
|
+
}
|
826
413
|
|
827
|
-
|
828
|
-
|
829
|
-
|
830
|
-
|
831
|
-
|
832
|
-
|
833
|
-
|
834
|
-
|
835
|
-
|
836
|
-
|
837
|
-
|
838
|
-
|
839
|
-
|
840
|
-
|
841
|
-
length = t.length >>> 0;
|
842
|
-
|
843
|
-
/** Update the targeted attribute of the attached element. */
|
844
|
-
if (typeof el[prop] === "object") {
|
845
|
-
el[prop].baseVal = tokens.join(" ");
|
846
|
-
} else {
|
847
|
-
el[prop] = tokens.join(" ");
|
848
|
-
}
|
849
|
-
reindex();
|
850
|
-
};
|
414
|
+
// Apply Element prototype to the pre-existing DOM as soon as the body element appears.
|
415
|
+
function bodyCheck() {
|
416
|
+
if (!(loopLimit--)) clearTimeout(interval);
|
417
|
+
if (document.body && !document.body.prototype && /(complete|interactive)/.test(document.readyState)) {
|
418
|
+
shiv(document, true);
|
419
|
+
if (interval && document.body.prototype) clearTimeout(interval);
|
420
|
+
return (!!document.body.prototype);
|
421
|
+
}
|
422
|
+
return false;
|
423
|
+
}
|
424
|
+
if (!bodyCheck()) {
|
425
|
+
document.onreadystatechange = bodyCheck;
|
426
|
+
interval = setInterval(bodyCheck, 25);
|
427
|
+
}
|
851
428
|
|
852
|
-
|
853
|
-
|
429
|
+
// Apply to any new elements created after load
|
430
|
+
document.createElement = function createElement(nodeName) {
|
431
|
+
var element = nativeCreateElement(String(nodeName).toLowerCase());
|
432
|
+
return shiv(element);
|
433
|
+
};
|
854
434
|
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
that.add(token);
|
859
|
-
return true;
|
860
|
-
} else {
|
861
|
-
that.remove(token);
|
862
|
-
return false;
|
863
|
-
}
|
864
|
-
}
|
435
|
+
// remove sandboxed iframe
|
436
|
+
document.removeChild(vbody);
|
437
|
+
}());
|
865
438
|
|
866
|
-
|
867
|
-
|
868
|
-
that.remove(token);
|
869
|
-
return false;
|
870
|
-
}
|
439
|
+
})
|
440
|
+
.call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
|
871
441
|
|
872
|
-
|
873
|
-
that.add(token);
|
874
|
-
return true;
|
875
|
-
};
|
442
|
+
// @ts-nocheck
|
876
443
|
|
877
|
-
|
878
|
-
};
|
444
|
+
(function(undefined) {
|
879
445
|
|
880
|
-
|
881
|
-
|
446
|
+
// Detection from https://raw.githubusercontent.com/Financial-Times/polyfill-library/13cf7c340974d128d557580b5e2dafcd1b1192d1/polyfills/Element/prototype/dataset/detect.js
|
447
|
+
var detect = (function(){
|
448
|
+
if (!document.documentElement.dataset) {
|
449
|
+
return false;
|
882
450
|
}
|
451
|
+
var el = document.createElement('div');
|
452
|
+
el.setAttribute("data-a-b", "c");
|
453
|
+
return el.dataset && el.dataset.aB == "c";
|
454
|
+
}());
|
455
|
+
|
456
|
+
if (detect) return
|
457
|
+
|
458
|
+
// Polyfill derived from https://raw.githubusercontent.com/Financial-Times/polyfill-library/13cf7c340974d128d557580b5e2dafcd1b1192d1/polyfills/Element/prototype/dataset/polyfill.js
|
459
|
+
Object.defineProperty(Element.prototype, 'dataset', {
|
460
|
+
get: function() {
|
461
|
+
var element = this;
|
462
|
+
var attributes = this.attributes;
|
463
|
+
var map = {};
|
464
|
+
|
465
|
+
for (var i = 0; i < attributes.length; i++) {
|
466
|
+
var attribute = attributes[i];
|
467
|
+
|
468
|
+
// This regex has been edited from the original polyfill, to add
|
469
|
+
// support for period (.) separators in data-* attribute names. These
|
470
|
+
// are allowed in the HTML spec, but were not covered by the original
|
471
|
+
// polyfill's regex. We use periods in our i18n implementation.
|
472
|
+
if (attribute && attribute.name && (/^data-\w[.\w-]*$/).test(attribute.name)) {
|
473
|
+
var name = attribute.name;
|
474
|
+
var value = attribute.value;
|
475
|
+
|
476
|
+
var propName = name.substr(5).replace(/-./g, function (prop) {
|
477
|
+
return prop.charAt(1).toUpperCase();
|
478
|
+
});
|
479
|
+
|
480
|
+
// If this browser supports __defineGetter__ and __defineSetter__,
|
481
|
+
// continue using defineProperty. If not (like IE 8 and below), we use
|
482
|
+
// a hacky fallback which at least gives an object in the right format
|
483
|
+
if ('__defineGetter__' in Object.prototype && '__defineSetter__' in Object.prototype) {
|
484
|
+
Object.defineProperty(map, propName, {
|
485
|
+
enumerable: true,
|
486
|
+
get: function() {
|
487
|
+
return this.value;
|
488
|
+
}.bind({value: value || ''}),
|
489
|
+
set: function setter(name, value) {
|
490
|
+
if (typeof value !== 'undefined') {
|
491
|
+
this.setAttribute(name, value);
|
492
|
+
} else {
|
493
|
+
this.removeAttribute(name);
|
494
|
+
}
|
495
|
+
}.bind(element, name)
|
496
|
+
});
|
497
|
+
} else {
|
498
|
+
map[propName] = value;
|
499
|
+
}
|
883
500
|
|
884
|
-
// Add second argument to native DOMTokenList.toggle() if necessary
|
885
|
-
(function () {
|
886
|
-
var e = document.createElement('span');
|
887
|
-
if (!('classList' in e)) return;
|
888
|
-
e.classList.toggle('x', false);
|
889
|
-
if (!e.classList.contains('x')) return;
|
890
|
-
e.classList.constructor.prototype.toggle = function toggle(token /*, force*/) {
|
891
|
-
var force = arguments[1];
|
892
|
-
if (force === undefined) {
|
893
|
-
var add = !this.contains(token);
|
894
|
-
this[add ? 'add' : 'remove'](token);
|
895
|
-
return add;
|
896
|
-
}
|
897
|
-
force = !!force;
|
898
|
-
this[force ? 'add' : 'remove'](token);
|
899
|
-
return force;
|
900
|
-
};
|
901
|
-
}());
|
902
|
-
|
903
|
-
// Add multiple arguments to native DOMTokenList.add() if necessary
|
904
|
-
(function () {
|
905
|
-
var e = document.createElement('span');
|
906
|
-
if (!('classList' in e)) return;
|
907
|
-
e.classList.add('a', 'b');
|
908
|
-
if (e.classList.contains('b')) return;
|
909
|
-
var native = e.classList.constructor.prototype.add;
|
910
|
-
e.classList.constructor.prototype.add = function () {
|
911
|
-
var args = arguments;
|
912
|
-
var l = arguments.length;
|
913
|
-
for (var i = 0; i < l; i++) {
|
914
|
-
native.call(this, args[i]);
|
915
|
-
}
|
916
|
-
};
|
917
|
-
}());
|
918
|
-
|
919
|
-
// Add multiple arguments to native DOMTokenList.remove() if necessary
|
920
|
-
(function () {
|
921
|
-
var e = document.createElement('span');
|
922
|
-
if (!('classList' in e)) return;
|
923
|
-
e.classList.add('a');
|
924
|
-
e.classList.add('b');
|
925
|
-
e.classList.remove('a', 'b');
|
926
|
-
if (!e.classList.contains('b')) return;
|
927
|
-
var native = e.classList.constructor.prototype.remove;
|
928
|
-
e.classList.constructor.prototype.remove = function () {
|
929
|
-
var args = arguments;
|
930
|
-
var l = arguments.length;
|
931
|
-
for (var i = 0; i < l; i++) {
|
932
|
-
native.call(this, args[i]);
|
933
501
|
}
|
934
|
-
}
|
935
|
-
}());
|
502
|
+
}
|
936
503
|
|
937
|
-
|
504
|
+
return map;
|
505
|
+
}
|
506
|
+
});
|
938
507
|
|
939
|
-
}).call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
|
508
|
+
}).call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
|
940
509
|
|
941
|
-
|
510
|
+
// @ts-nocheck
|
511
|
+
(function (undefined) {
|
942
512
|
|
943
|
-
|
944
|
-
|
945
|
-
'document' in this && "classList" in document.documentElement && 'Element' in this && 'classList' in Element.prototype && (function () {
|
946
|
-
var e = document.createElement('span');
|
947
|
-
e.classList.add('a', 'b');
|
948
|
-
return e.classList.contains('b');
|
949
|
-
}())
|
950
|
-
);
|
513
|
+
// Detection from https://github.com/mdn/content/blob/cf607d68522cd35ee7670782d3ee3a361eaef2e4/files/en-us/web/javascript/reference/global_objects/string/trim/index.md#polyfill
|
514
|
+
var detect = ('trim' in String.prototype);
|
951
515
|
|
952
|
-
|
516
|
+
if (detect) return
|
953
517
|
|
954
|
-
|
955
|
-
|
956
|
-
|
957
|
-
var defineGetter = function (object, name, fn, configurable) {
|
958
|
-
if (Object.defineProperty)
|
959
|
-
Object.defineProperty(object, name, {
|
960
|
-
configurable: false === dpSupport ? true : !!configurable,
|
961
|
-
get: fn
|
962
|
-
});
|
963
|
-
|
964
|
-
else object.__defineGetter__(name, fn);
|
518
|
+
// Polyfill from https://github.com/mdn/content/blob/cf607d68522cd35ee7670782d3ee3a361eaef2e4/files/en-us/web/javascript/reference/global_objects/string/trim/index.md#polyfill
|
519
|
+
String.prototype.trim = function () {
|
520
|
+
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
|
965
521
|
};
|
966
|
-
/** Ensure the browser allows Object.defineProperty to be used on native JavaScript objects. */
|
967
|
-
try {
|
968
|
-
defineGetter({}, "support");
|
969
|
-
}
|
970
|
-
catch (e) {
|
971
|
-
dpSupport = false;
|
972
|
-
}
|
973
|
-
/** Polyfills a property with a DOMTokenList */
|
974
|
-
var addProp = function (o, name, attr) {
|
975
|
-
|
976
|
-
defineGetter(o.prototype, name, function () {
|
977
|
-
var tokenList;
|
978
|
-
|
979
|
-
var THIS = this,
|
980
|
-
|
981
|
-
/** Prevent this from firing twice for some reason. What the hell, IE. */
|
982
|
-
gibberishProperty = "__defineGetter__" + "DEFINE_PROPERTY" + name;
|
983
|
-
if(THIS[gibberishProperty]) return tokenList;
|
984
|
-
THIS[gibberishProperty] = true;
|
985
|
-
|
986
|
-
/**
|
987
|
-
* IE8 can't define properties on native JavaScript objects, so we'll use a dumb hack instead.
|
988
|
-
*
|
989
|
-
* What this is doing is creating a dummy element ("reflection") inside a detached phantom node ("mirror")
|
990
|
-
* that serves as the target of Object.defineProperty instead. While we could simply use the subject HTML
|
991
|
-
* element instead, this would conflict with element types which use indexed properties (such as forms and
|
992
|
-
* select lists).
|
993
|
-
*/
|
994
|
-
if (false === dpSupport) {
|
995
|
-
|
996
|
-
var visage;
|
997
|
-
var mirror = addProp.mirror || document.createElement("div");
|
998
|
-
var reflections = mirror.childNodes;
|
999
|
-
var l = reflections.length;
|
1000
|
-
|
1001
|
-
for (var i = 0; i < l; ++i)
|
1002
|
-
if (reflections[i]._R === THIS) {
|
1003
|
-
visage = reflections[i];
|
1004
|
-
break;
|
1005
|
-
}
|
1006
|
-
|
1007
|
-
/** Couldn't find an element's reflection inside the mirror. Materialise one. */
|
1008
|
-
visage || (visage = mirror.appendChild(document.createElement("div")));
|
1009
|
-
|
1010
|
-
tokenList = DOMTokenList.call(visage, THIS, attr);
|
1011
|
-
} else tokenList = new DOMTokenList(THIS, attr);
|
1012
|
-
|
1013
|
-
defineGetter(THIS, name, function () {
|
1014
|
-
return tokenList;
|
1015
|
-
});
|
1016
|
-
delete THIS[gibberishProperty];
|
1017
522
|
|
1018
|
-
|
1019
|
-
|
1020
|
-
|
523
|
+
}).call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
|
524
|
+
|
525
|
+
/* eslint-disable es-x/no-string-prototype-trim -- Polyfill imported */
|
1021
526
|
|
1022
|
-
addProp(global.Element, "classList", "className");
|
1023
|
-
addProp(global.HTMLElement, "classList", "className");
|
1024
|
-
addProp(global.HTMLLinkElement, "relList", "rel");
|
1025
|
-
addProp(global.HTMLAnchorElement, "relList", "rel");
|
1026
|
-
addProp(global.HTMLAreaElement, "relList", "rel");
|
1027
|
-
}(this));
|
1028
|
-
|
1029
|
-
}).call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
|
1030
|
-
|
1031
|
-
/**
|
1032
|
-
* Common helpers which do not require polyfill.
|
1033
|
-
*
|
1034
|
-
* IMPORTANT: If a helper require a polyfill, please isolate it in its own module
|
1035
|
-
* so that the polyfill can be properly tree-shaken and does not burden
|
1036
|
-
* the components that do not need that helper
|
1037
|
-
*
|
1038
|
-
* @module common/index
|
1039
|
-
*/
|
1040
|
-
|
1041
|
-
/**
|
1042
|
-
* Config flattening function
|
1043
|
-
*
|
1044
|
-
* Takes any number of objects, flattens them into namespaced key-value pairs,
|
1045
|
-
* (e.g. {'i18n.showSection': 'Show section'}) and combines them together, with
|
1046
|
-
* greatest priority on the LAST item passed in.
|
1047
|
-
*
|
1048
|
-
* @returns {object} A flattened object of key-value pairs.
|
1049
|
-
*/
|
1050
|
-
function mergeConfigs (/* configObject1, configObject2, ...configObjects */) {
|
1051
527
|
/**
|
1052
|
-
*
|
1053
|
-
* object. Doing this means we don't need to do any deep/recursive merging of
|
1054
|
-
* each of our objects, nor transform our dataset from a flat list into a
|
1055
|
-
* nested object.
|
528
|
+
* Normalise string
|
1056
529
|
*
|
1057
|
-
*
|
1058
|
-
*
|
530
|
+
* 'If it looks like a duck, and it quacks like a duck…' 🦆
|
531
|
+
*
|
532
|
+
* If the passed value looks like a boolean or a number, convert it to a boolean
|
533
|
+
* or number.
|
534
|
+
*
|
535
|
+
* Designed to be used to convert config passed via data attributes (which are
|
536
|
+
* always strings) into something sensible.
|
537
|
+
*
|
538
|
+
* @deprecated Will be made private in v5.0
|
539
|
+
* @param {string} value - The value to normalise
|
540
|
+
* @returns {string | boolean | number | undefined} Normalised data
|
1059
541
|
*/
|
1060
|
-
|
1061
|
-
|
1062
|
-
|
1063
|
-
|
1064
|
-
// Our flattening function, this is called recursively for each level of
|
1065
|
-
// depth in the object. At each level we prepend the previous level names to
|
1066
|
-
// the key using `prefix`.
|
1067
|
-
var flattenLoop = function (obj, prefix) {
|
1068
|
-
// Loop through keys...
|
1069
|
-
for (var key in obj) {
|
1070
|
-
// Check to see if this is a prototypical key/value,
|
1071
|
-
// if it is, skip it.
|
1072
|
-
if (!Object.prototype.hasOwnProperty.call(obj, key)) {
|
1073
|
-
continue
|
1074
|
-
}
|
1075
|
-
var value = obj[key];
|
1076
|
-
var prefixedKey = prefix ? prefix + '.' + key : key;
|
1077
|
-
if (typeof value === 'object') {
|
1078
|
-
// If the value is a nested object, recurse over that too
|
1079
|
-
flattenLoop(value, prefixedKey);
|
1080
|
-
} else {
|
1081
|
-
// Otherwise, add this value to our return object
|
1082
|
-
flattenedObject[prefixedKey] = value;
|
1083
|
-
}
|
1084
|
-
}
|
1085
|
-
};
|
542
|
+
function normaliseString (value) {
|
543
|
+
if (typeof value !== 'string') {
|
544
|
+
return value
|
545
|
+
}
|
1086
546
|
|
1087
|
-
|
1088
|
-
flattenLoop(configObject);
|
1089
|
-
return flattenedObject
|
1090
|
-
};
|
547
|
+
var trimmedValue = value.trim();
|
1091
548
|
|
1092
|
-
|
1093
|
-
|
1094
|
-
|
1095
|
-
// Loop through each of the remaining passed objects and push their keys
|
1096
|
-
// one-by-one into configObject. Any duplicate keys will override the existing
|
1097
|
-
// key with the new value.
|
1098
|
-
for (var i = 0; i < arguments.length; i++) {
|
1099
|
-
var obj = flattenObject(arguments[i]);
|
1100
|
-
for (var key in obj) {
|
1101
|
-
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
1102
|
-
formattedConfigObject[key] = obj[key];
|
1103
|
-
}
|
549
|
+
if (trimmedValue === 'true') {
|
550
|
+
return true
|
1104
551
|
}
|
1105
|
-
}
|
1106
552
|
|
1107
|
-
|
1108
|
-
|
1109
|
-
|
1110
|
-
/**
|
1111
|
-
* Extracts keys starting with a particular namespace from a flattened config
|
1112
|
-
* object, removing the namespace in the process.
|
1113
|
-
*
|
1114
|
-
* @param {object} configObject - The object to extract key-value pairs from.
|
1115
|
-
* @param {string} namespace - The namespace to filter keys with.
|
1116
|
-
* @returns {object} Flattened object with dot-separated key namespace removed
|
1117
|
-
*/
|
1118
|
-
function extractConfigByNamespace (configObject, namespace) {
|
1119
|
-
// Check we have what we need
|
1120
|
-
if (!configObject || typeof configObject !== 'object') {
|
1121
|
-
throw new Error('Provide a `configObject` of type "object".')
|
1122
|
-
}
|
1123
|
-
if (!namespace || typeof namespace !== 'string') {
|
1124
|
-
throw new Error('Provide a `namespace` of type "string" to filter the `configObject` by.')
|
1125
|
-
}
|
1126
|
-
var newObject = {};
|
1127
|
-
for (var key in configObject) {
|
1128
|
-
// Split the key into parts, using . as our namespace separator
|
1129
|
-
var keyParts = key.split('.');
|
1130
|
-
// Check if the first namespace matches the configured namespace
|
1131
|
-
if (Object.prototype.hasOwnProperty.call(configObject, key) && keyParts[0] === namespace) {
|
1132
|
-
// Remove the first item (the namespace) from the parts array,
|
1133
|
-
// but only if there is more than one part (we don't want blank keys!)
|
1134
|
-
if (keyParts.length > 1) {
|
1135
|
-
keyParts.shift();
|
1136
|
-
}
|
1137
|
-
// Join the remaining parts back together
|
1138
|
-
var newKey = keyParts.join('.');
|
1139
|
-
// Add them to our new object
|
1140
|
-
newObject[newKey] = configObject[key];
|
553
|
+
if (trimmedValue === 'false') {
|
554
|
+
return false
|
1141
555
|
}
|
1142
|
-
}
|
1143
|
-
return newObject
|
1144
|
-
}
|
1145
|
-
|
1146
|
-
/**
|
1147
|
-
* @callback nodeListIterator
|
1148
|
-
* @param {Element} value - The current node being iterated on
|
1149
|
-
* @param {number} index - The current index in the iteration
|
1150
|
-
* @param {NodeListOf<Element>} nodes - NodeList from querySelectorAll()
|
1151
|
-
* @returns {undefined}
|
1152
|
-
*/
|
1153
|
-
|
1154
|
-
/**
|
1155
|
-
* Internal support for selecting messages to render, with placeholder
|
1156
|
-
* interpolation and locale-aware number formatting and pluralisation
|
1157
|
-
*
|
1158
|
-
* @class
|
1159
|
-
* @private
|
1160
|
-
* @param {TranslationsFlattened} translations - Key-value pairs of the translation strings to use.
|
1161
|
-
* @param {object} [config] - Configuration options for the function.
|
1162
|
-
* @param {string} config.locale - An overriding locale for the PluralRules functionality.
|
1163
|
-
*/
|
1164
|
-
function I18n (translations, config) {
|
1165
|
-
// Make list of translations available throughout function
|
1166
|
-
this.translations = translations || {};
|
1167
|
-
|
1168
|
-
// The locale to use for PluralRules and NumberFormat
|
1169
|
-
this.locale = (config && config.locale) || document.documentElement.lang || 'en';
|
1170
|
-
}
|
1171
|
-
|
1172
|
-
/**
|
1173
|
-
* The most used function - takes the key for a given piece of UI text and
|
1174
|
-
* returns the appropriate string.
|
1175
|
-
*
|
1176
|
-
* @param {string} lookupKey - The lookup key of the string to use.
|
1177
|
-
* @param {object} options - Any options passed with the translation string, e.g: for string interpolation.
|
1178
|
-
* @returns {string} The appropriate translation string.
|
1179
|
-
*/
|
1180
|
-
I18n.prototype.t = function (lookupKey, options) {
|
1181
|
-
if (!lookupKey) {
|
1182
|
-
// Print a console error if no lookup key has been provided
|
1183
|
-
throw new Error('i18n: lookup key missing')
|
1184
|
-
}
|
1185
|
-
|
1186
|
-
// If the `count` option is set, determine which plural suffix is needed and
|
1187
|
-
// change the lookupKey to match. We check to see if it's undefined instead of
|
1188
|
-
// falsy, as this could legitimately be 0.
|
1189
|
-
if (options && typeof options.count !== 'undefined') {
|
1190
|
-
// Get the plural suffix
|
1191
|
-
lookupKey = lookupKey + '.' + this.getPluralSuffix(lookupKey, options.count);
|
1192
|
-
}
|
1193
|
-
|
1194
|
-
if (lookupKey in this.translations) {
|
1195
|
-
// Fetch the translation string for that lookup key
|
1196
|
-
var translationString = this.translations[lookupKey];
|
1197
|
-
|
1198
|
-
// Check for ${} placeholders in the translation string
|
1199
|
-
if (translationString.match(/%{(.\S+)}/)) {
|
1200
|
-
if (!options) {
|
1201
|
-
throw new Error('i18n: cannot replace placeholders in string if no option data provided')
|
1202
|
-
}
|
1203
556
|
|
1204
|
-
|
1205
|
-
|
1206
|
-
|
557
|
+
// Empty / whitespace-only strings are considered finite so we need to check
|
558
|
+
// the length of the trimmed string as well
|
559
|
+
if (trimmedValue.length > 0 && isFinite(Number(trimmedValue))) {
|
560
|
+
return Number(trimmedValue)
|
1207
561
|
}
|
1208
|
-
} else {
|
1209
|
-
// If the key wasn't found in our translations object,
|
1210
|
-
// return the lookup key itself as the fallback
|
1211
|
-
return lookupKey
|
1212
|
-
}
|
1213
|
-
};
|
1214
|
-
|
1215
|
-
/**
|
1216
|
-
* Takes a translation string with placeholders, and replaces the placeholders
|
1217
|
-
* with the provided data
|
1218
|
-
*
|
1219
|
-
* @param {string} translationString - The translation string
|
1220
|
-
* @param {object} options - Any options passed with the translation string, e.g: for string interpolation.
|
1221
|
-
* @returns {string} The translation string to output, with ${} placeholders replaced
|
1222
|
-
*/
|
1223
|
-
I18n.prototype.replacePlaceholders = function (translationString, options) {
|
1224
|
-
var formatter;
|
1225
|
-
|
1226
|
-
if (this.hasIntlNumberFormatSupport()) {
|
1227
|
-
formatter = new Intl.NumberFormat(this.locale);
|
1228
|
-
}
|
1229
562
|
|
1230
|
-
|
1231
|
-
|
1232
|
-
var placeholderValue = options[placeholderKey];
|
1233
|
-
|
1234
|
-
// If a user has passed `false` as the value for the placeholder
|
1235
|
-
// treat it as though the value should not be displayed
|
1236
|
-
if (placeholderValue === false) {
|
1237
|
-
return ''
|
1238
|
-
}
|
563
|
+
return value
|
564
|
+
}
|
1239
565
|
|
1240
|
-
|
1241
|
-
|
1242
|
-
|
1243
|
-
|
566
|
+
/**
|
567
|
+
* Normalise dataset
|
568
|
+
*
|
569
|
+
* Loop over an object and normalise each value using normaliseData function
|
570
|
+
*
|
571
|
+
* @deprecated Will be made private in v5.0
|
572
|
+
* @param {DOMStringMap} dataset - HTML element dataset
|
573
|
+
* @returns {Object<string, unknown>} Normalised dataset
|
574
|
+
*/
|
575
|
+
function normaliseDataset (dataset) {
|
576
|
+
/** @type {Object<string, unknown>} */
|
577
|
+
var out = {};
|
1244
578
|
|
1245
|
-
|
1246
|
-
|
1247
|
-
throw new Error('i18n: no data found to replace ' + placeholderWithBraces + ' placeholder in string')
|
579
|
+
for (var key in dataset) {
|
580
|
+
out[key] = normaliseString(dataset[key]);
|
1248
581
|
}
|
1249
|
-
|
1250
|
-
|
1251
|
-
|
1252
|
-
/**
|
1253
|
-
* Check to see if the browser supports Intl and Intl.PluralRules.
|
1254
|
-
*
|
1255
|
-
* It requires all conditions to be met in order to be supported:
|
1256
|
-
* - The browser supports the Intl class (true in IE11)
|
1257
|
-
* - The implementation of Intl supports PluralRules (NOT true in IE11)
|
1258
|
-
* - The browser/OS has plural rules for the current locale (browser dependent)
|
1259
|
-
*
|
1260
|
-
* @returns {boolean} Returns true if all conditions are met. Returns false otherwise.
|
1261
|
-
*/
|
1262
|
-
I18n.prototype.hasIntlPluralRulesSupport = function () {
|
1263
|
-
return Boolean(window.Intl && ('PluralRules' in window.Intl && Intl.PluralRules.supportedLocalesOf(this.locale).length))
|
1264
|
-
};
|
1265
|
-
|
1266
|
-
/**
|
1267
|
-
* Check to see if the browser supports Intl and Intl.NumberFormat.
|
1268
|
-
*
|
1269
|
-
* It requires all conditions to be met in order to be supported:
|
1270
|
-
* - The browser supports the Intl class (true in IE11)
|
1271
|
-
* - The implementation of Intl supports NumberFormat (also true in IE11)
|
1272
|
-
* - The browser/OS has number formatting rules for the current locale (browser dependent)
|
1273
|
-
*
|
1274
|
-
* @returns {boolean} Returns true if all conditions are met. Returns false otherwise.
|
1275
|
-
*/
|
1276
|
-
I18n.prototype.hasIntlNumberFormatSupport = function () {
|
1277
|
-
return Boolean(window.Intl && ('NumberFormat' in window.Intl && Intl.NumberFormat.supportedLocalesOf(this.locale).length))
|
1278
|
-
};
|
1279
|
-
|
1280
|
-
/**
|
1281
|
-
* Get the appropriate suffix for the plural form.
|
1282
|
-
*
|
1283
|
-
* Uses Intl.PluralRules (or our own fallback implementation) to get the
|
1284
|
-
* 'preferred' form to use for the given count.
|
1285
|
-
*
|
1286
|
-
* Checks that a translation has been provided for that plural form – if it
|
1287
|
-
* hasn't, it'll fall back to the 'other' plural form (unless that doesn't exist
|
1288
|
-
* either, in which case an error will be thrown)
|
1289
|
-
*
|
1290
|
-
* @param {string} lookupKey - The lookup key of the string to use.
|
1291
|
-
* @param {number} count - Number used to determine which pluralisation to use.
|
1292
|
-
* @returns {PluralRule} The suffix associated with the correct pluralisation for this locale.
|
1293
|
-
*/
|
1294
|
-
I18n.prototype.getPluralSuffix = function (lookupKey, count) {
|
1295
|
-
// Validate that the number is actually a number.
|
1296
|
-
//
|
1297
|
-
// Number(count) will turn anything that can't be converted to a Number type
|
1298
|
-
// into 'NaN'. isFinite filters out NaN, as it isn't a finite number.
|
1299
|
-
count = Number(count);
|
1300
|
-
if (!isFinite(count)) { return 'other' }
|
1301
|
-
|
1302
|
-
var preferredForm;
|
1303
|
-
|
1304
|
-
// Check to verify that all the requirements for Intl.PluralRules are met.
|
1305
|
-
// If so, we can use that instead of our custom implementation. Otherwise,
|
1306
|
-
// use the hardcoded fallback.
|
1307
|
-
if (this.hasIntlPluralRulesSupport()) {
|
1308
|
-
preferredForm = new Intl.PluralRules(this.locale).select(count);
|
1309
|
-
} else {
|
1310
|
-
preferredForm = this.selectPluralFormUsingFallbackRules(count);
|
582
|
+
|
583
|
+
return out
|
1311
584
|
}
|
1312
585
|
|
1313
|
-
|
1314
|
-
|
1315
|
-
|
1316
|
-
|
1317
|
-
|
1318
|
-
|
1319
|
-
|
1320
|
-
|
1321
|
-
|
1322
|
-
|
586
|
+
/**
|
587
|
+
* Internal support for selecting messages to render, with placeholder
|
588
|
+
* interpolation and locale-aware number formatting and pluralisation
|
589
|
+
*
|
590
|
+
* @class
|
591
|
+
* @private
|
592
|
+
* @param {Object<string, unknown>} translations - Key-value pairs of the translation strings to use.
|
593
|
+
* @param {object} [config] - Configuration options for the function.
|
594
|
+
* @param {string} [config.locale] - An overriding locale for the PluralRules functionality.
|
595
|
+
*/
|
596
|
+
function I18n (translations, config) {
|
597
|
+
// Make list of translations available throughout function
|
598
|
+
this.translations = translations || {};
|
1323
599
|
|
1324
|
-
|
1325
|
-
|
1326
|
-
} else {
|
1327
|
-
throw new Error(
|
1328
|
-
'i18n: Plural form ".other" is required for "' + this.locale + '" locale'
|
1329
|
-
)
|
1330
|
-
}
|
1331
|
-
};
|
1332
|
-
|
1333
|
-
/**
|
1334
|
-
* Get the plural form using our fallback implementation
|
1335
|
-
*
|
1336
|
-
* This is split out into a separate function to make it easier to test the
|
1337
|
-
* fallback behaviour in an environment where Intl.PluralRules exists.
|
1338
|
-
*
|
1339
|
-
* @param {number} count - Number used to determine which pluralisation to use.
|
1340
|
-
* @returns {PluralRule} The pluralisation form for count in this locale.
|
1341
|
-
*/
|
1342
|
-
I18n.prototype.selectPluralFormUsingFallbackRules = function (count) {
|
1343
|
-
// Currently our custom code can only handle positive integers, so let's
|
1344
|
-
// make sure our number is one of those.
|
1345
|
-
count = Math.abs(Math.floor(count));
|
1346
|
-
|
1347
|
-
var ruleset = this.getPluralRulesForLocale();
|
1348
|
-
|
1349
|
-
if (ruleset) {
|
1350
|
-
return I18n.pluralRules[ruleset](count)
|
600
|
+
// The locale to use for PluralRules and NumberFormat
|
601
|
+
this.locale = (config && config.locale) || document.documentElement.lang || 'en';
|
1351
602
|
}
|
1352
603
|
|
1353
|
-
|
1354
|
-
|
1355
|
-
|
1356
|
-
|
1357
|
-
|
1358
|
-
|
1359
|
-
|
1360
|
-
|
1361
|
-
|
1362
|
-
|
1363
|
-
|
1364
|
-
|
1365
|
-
|
1366
|
-
|
1367
|
-
I18n.prototype.getPluralRulesForLocale = function () {
|
1368
|
-
var locale = this.locale;
|
1369
|
-
var localeShort = locale.split('-')[0];
|
1370
|
-
|
1371
|
-
// Look through the plural rules map to find which `pluralRule` is
|
1372
|
-
// appropriate for our current `locale`.
|
1373
|
-
for (var pluralRule in I18n.pluralRulesMap) {
|
1374
|
-
if (Object.prototype.hasOwnProperty.call(I18n.pluralRulesMap, pluralRule)) {
|
1375
|
-
var languages = I18n.pluralRulesMap[pluralRule];
|
1376
|
-
for (var i = 0; i < languages.length; i++) {
|
1377
|
-
if (languages[i] === locale || languages[i] === localeShort) {
|
1378
|
-
return pluralRule
|
1379
|
-
}
|
1380
|
-
}
|
1381
|
-
}
|
1382
|
-
}
|
1383
|
-
};
|
1384
|
-
|
1385
|
-
/**
|
1386
|
-
* Map of plural rules to languages where those rules apply.
|
1387
|
-
*
|
1388
|
-
* Note: These groups are named for the most dominant or recognisable language
|
1389
|
-
* that uses each system. The groupings do not imply that the languages are
|
1390
|
-
* related to one another. Many languages have evolved the same systems
|
1391
|
-
* independently of one another.
|
1392
|
-
*
|
1393
|
-
* Code to support more languages can be found in the i18n spike:
|
1394
|
-
* {@link https://github.com/alphagov/govuk-frontend/blob/spike-i18n-support/src/govuk/i18n.mjs}
|
1395
|
-
*
|
1396
|
-
* Languages currently supported:
|
1397
|
-
*
|
1398
|
-
* Arabic: Arabic (ar)
|
1399
|
-
* Chinese: Burmese (my), Chinese (zh), Indonesian (id), Japanese (ja),
|
1400
|
-
* Javanese (jv), Korean (ko), Malay (ms), Thai (th), Vietnamese (vi)
|
1401
|
-
* French: Armenian (hy), Bangla (bn), French (fr), Gujarati (gu), Hindi (hi),
|
1402
|
-
* Persian Farsi (fa), Punjabi (pa), Zulu (zu)
|
1403
|
-
* German: Afrikaans (af), Albanian (sq), Azerbaijani (az), Basque (eu),
|
1404
|
-
* Bulgarian (bg), Catalan (ca), Danish (da), Dutch (nl), English (en),
|
1405
|
-
* Estonian (et), Finnish (fi), Georgian (ka), German (de), Greek (el),
|
1406
|
-
* Hungarian (hu), Luxembourgish (lb), Norwegian (no), Somali (so),
|
1407
|
-
* Swahili (sw), Swedish (sv), Tamil (ta), Telugu (te), Turkish (tr),
|
1408
|
-
* Urdu (ur)
|
1409
|
-
* Irish: Irish Gaelic (ga)
|
1410
|
-
* Russian: Russian (ru), Ukrainian (uk)
|
1411
|
-
* Scottish: Scottish Gaelic (gd)
|
1412
|
-
* Spanish: European Portuguese (pt-PT), Italian (it), Spanish (es)
|
1413
|
-
* Welsh: Welsh (cy)
|
1414
|
-
*
|
1415
|
-
* @type {Object<PluralRuleName, string[]>}
|
1416
|
-
*/
|
1417
|
-
I18n.pluralRulesMap = {
|
1418
|
-
arabic: ['ar'],
|
1419
|
-
chinese: ['my', 'zh', 'id', 'ja', 'jv', 'ko', 'ms', 'th', 'vi'],
|
1420
|
-
french: ['hy', 'bn', 'fr', 'gu', 'hi', 'fa', 'pa', 'zu'],
|
1421
|
-
german: [
|
1422
|
-
'af', 'sq', 'az', 'eu', 'bg', 'ca', 'da', 'nl', 'en', 'et', 'fi', 'ka',
|
1423
|
-
'de', 'el', 'hu', 'lb', 'no', 'so', 'sw', 'sv', 'ta', 'te', 'tr', 'ur'
|
1424
|
-
],
|
1425
|
-
irish: ['ga'],
|
1426
|
-
russian: ['ru', 'uk'],
|
1427
|
-
scottish: ['gd'],
|
1428
|
-
spanish: ['pt-PT', 'it', 'es'],
|
1429
|
-
welsh: ['cy']
|
1430
|
-
};
|
1431
|
-
|
1432
|
-
/**
|
1433
|
-
* Different pluralisation rule sets
|
1434
|
-
*
|
1435
|
-
* Returns the appropriate suffix for the plural form associated with `n`.
|
1436
|
-
* Possible suffixes: 'zero', 'one', 'two', 'few', 'many', 'other' (the actual
|
1437
|
-
* meaning of each differs per locale). 'other' should always exist, even in
|
1438
|
-
* languages without plurals, such as Chinese.
|
1439
|
-
* {@link https://cldr.unicode.org/index/cldr-spec/plural-rules}
|
1440
|
-
*
|
1441
|
-
* The count must be a positive integer. Negative numbers and decimals aren't accounted for
|
1442
|
-
*
|
1443
|
-
* @type {Object<string, function(number): PluralRule>}
|
1444
|
-
*/
|
1445
|
-
I18n.pluralRules = {
|
1446
|
-
arabic: function (n) {
|
1447
|
-
if (n === 0) { return 'zero' }
|
1448
|
-
if (n === 1) { return 'one' }
|
1449
|
-
if (n === 2) { return 'two' }
|
1450
|
-
if (n % 100 >= 3 && n % 100 <= 10) { return 'few' }
|
1451
|
-
if (n % 100 >= 11 && n % 100 <= 99) { return 'many' }
|
1452
|
-
return 'other'
|
1453
|
-
},
|
1454
|
-
chinese: function () {
|
1455
|
-
return 'other'
|
1456
|
-
},
|
1457
|
-
french: function (n) {
|
1458
|
-
return n === 0 || n === 1 ? 'one' : 'other'
|
1459
|
-
},
|
1460
|
-
german: function (n) {
|
1461
|
-
return n === 1 ? 'one' : 'other'
|
1462
|
-
},
|
1463
|
-
irish: function (n) {
|
1464
|
-
if (n === 1) { return 'one' }
|
1465
|
-
if (n === 2) { return 'two' }
|
1466
|
-
if (n >= 3 && n <= 6) { return 'few' }
|
1467
|
-
if (n >= 7 && n <= 10) { return 'many' }
|
1468
|
-
return 'other'
|
1469
|
-
},
|
1470
|
-
russian: function (n) {
|
1471
|
-
var lastTwo = n % 100;
|
1472
|
-
var last = lastTwo % 10;
|
1473
|
-
if (last === 1 && lastTwo !== 11) { return 'one' }
|
1474
|
-
if (last >= 2 && last <= 4 && !(lastTwo >= 12 && lastTwo <= 14)) { return 'few' }
|
1475
|
-
if (last === 0 || (last >= 5 && last <= 9) || (lastTwo >= 11 && lastTwo <= 14)) { return 'many' }
|
1476
|
-
// Note: The 'other' suffix is only used by decimal numbers in Russian.
|
1477
|
-
// We don't anticipate it being used, but it's here for consistency.
|
1478
|
-
return 'other'
|
1479
|
-
},
|
1480
|
-
scottish: function (n) {
|
1481
|
-
if (n === 1 || n === 11) { return 'one' }
|
1482
|
-
if (n === 2 || n === 12) { return 'two' }
|
1483
|
-
if ((n >= 3 && n <= 10) || (n >= 13 && n <= 19)) { return 'few' }
|
1484
|
-
return 'other'
|
1485
|
-
},
|
1486
|
-
spanish: function (n) {
|
1487
|
-
if (n === 1) { return 'one' }
|
1488
|
-
if (n % 1000000 === 0 && n !== 0) { return 'many' }
|
1489
|
-
return 'other'
|
1490
|
-
},
|
1491
|
-
welsh: function (n) {
|
1492
|
-
if (n === 0) { return 'zero' }
|
1493
|
-
if (n === 1) { return 'one' }
|
1494
|
-
if (n === 2) { return 'two' }
|
1495
|
-
if (n === 3) { return 'few' }
|
1496
|
-
if (n === 6) { return 'many' }
|
1497
|
-
return 'other'
|
1498
|
-
}
|
1499
|
-
};
|
1500
|
-
|
1501
|
-
/**
|
1502
|
-
* Supported languages for plural rules
|
1503
|
-
*
|
1504
|
-
* @typedef {'arabic' | 'chinese' | 'french' | 'german' | 'irish' | 'russian' | 'scottish' | 'spanish' | 'welsh'} PluralRuleName
|
1505
|
-
*/
|
1506
|
-
|
1507
|
-
/**
|
1508
|
-
* Plural rule category mnemonic tags
|
1509
|
-
*
|
1510
|
-
* @typedef {'zero' | 'one' | 'two' | 'few' | 'many' | 'other'} PluralRule
|
1511
|
-
*/
|
1512
|
-
|
1513
|
-
/**
|
1514
|
-
* Translated message by plural rule they correspond to.
|
1515
|
-
*
|
1516
|
-
* Allows to group pluralised messages under a single key when passing
|
1517
|
-
* translations to a component's constructor
|
1518
|
-
*
|
1519
|
-
* @typedef {object} TranslationPluralForms
|
1520
|
-
* @property {string} [other] - General plural form
|
1521
|
-
* @property {string} [zero] - Plural form used with 0
|
1522
|
-
* @property {string} [one] - Plural form used with 1
|
1523
|
-
* @property {string} [two] - Plural form used with 2
|
1524
|
-
* @property {string} [few] - Plural form used for a few
|
1525
|
-
* @property {string} [many] - Plural form used for many
|
1526
|
-
*/
|
1527
|
-
|
1528
|
-
/**
|
1529
|
-
* Translated messages (flattened)
|
1530
|
-
*
|
1531
|
-
* @private
|
1532
|
-
* @typedef {Object<string, string> | {}} TranslationsFlattened
|
1533
|
-
*/
|
1534
|
-
|
1535
|
-
(function(undefined) {
|
1536
|
-
|
1537
|
-
// Detection from https://raw.githubusercontent.com/Financial-Times/polyfill-library/13cf7c340974d128d557580b5e2dafcd1b1192d1/polyfills/Element/prototype/dataset/detect.js
|
1538
|
-
var detect = (function(){
|
1539
|
-
if (!document.documentElement.dataset) {
|
1540
|
-
return false;
|
604
|
+
/**
|
605
|
+
* The most used function - takes the key for a given piece of UI text and
|
606
|
+
* returns the appropriate string.
|
607
|
+
*
|
608
|
+
* @param {string} lookupKey - The lookup key of the string to use.
|
609
|
+
* @param {Object<string, unknown>} [options] - Any options passed with the translation string, e.g: for string interpolation.
|
610
|
+
* @returns {string} The appropriate translation string.
|
611
|
+
* @throws {Error} Lookup key required
|
612
|
+
* @throws {Error} Options required for `${}` placeholders
|
613
|
+
*/
|
614
|
+
I18n.prototype.t = function (lookupKey, options) {
|
615
|
+
if (!lookupKey) {
|
616
|
+
// Print a console error if no lookup key has been provided
|
617
|
+
throw new Error('i18n: lookup key missing')
|
1541
618
|
}
|
1542
|
-
var el = document.createElement('div');
|
1543
|
-
el.setAttribute("data-a-b", "c");
|
1544
|
-
return el.dataset && el.dataset.aB == "c";
|
1545
|
-
}());
|
1546
619
|
|
1547
|
-
|
620
|
+
// If the `count` option is set, determine which plural suffix is needed and
|
621
|
+
// change the lookupKey to match. We check to see if it's numeric instead of
|
622
|
+
// falsy, as this could legitimately be 0.
|
623
|
+
if (options && typeof options.count === 'number') {
|
624
|
+
// Get the plural suffix
|
625
|
+
lookupKey = lookupKey + '.' + this.getPluralSuffix(lookupKey, options.count);
|
626
|
+
}
|
1548
627
|
|
1549
|
-
|
1550
|
-
|
1551
|
-
get: function() {
|
1552
|
-
var element = this;
|
1553
|
-
var attributes = this.attributes;
|
1554
|
-
var map = {};
|
1555
|
-
|
1556
|
-
for (var i = 0; i < attributes.length; i++) {
|
1557
|
-
var attribute = attributes[i];
|
1558
|
-
|
1559
|
-
// This regex has been edited from the original polyfill, to add
|
1560
|
-
// support for period (.) separators in data-* attribute names. These
|
1561
|
-
// are allowed in the HTML spec, but were not covered by the original
|
1562
|
-
// polyfill's regex. We use periods in our i18n implementation.
|
1563
|
-
if (attribute && attribute.name && (/^data-\w[.\w-]*$/).test(attribute.name)) {
|
1564
|
-
var name = attribute.name;
|
1565
|
-
var value = attribute.value;
|
1566
|
-
|
1567
|
-
var propName = name.substr(5).replace(/-./g, function (prop) {
|
1568
|
-
return prop.charAt(1).toUpperCase();
|
1569
|
-
});
|
1570
|
-
|
1571
|
-
// If this browser supports __defineGetter__ and __defineSetter__,
|
1572
|
-
// continue using defineProperty. If not (like IE 8 and below), we use
|
1573
|
-
// a hacky fallback which at least gives an object in the right format
|
1574
|
-
if ('__defineGetter__' in Object.prototype && '__defineSetter__' in Object.prototype) {
|
1575
|
-
Object.defineProperty(map, propName, {
|
1576
|
-
enumerable: true,
|
1577
|
-
get: function() {
|
1578
|
-
return this.value;
|
1579
|
-
}.bind({value: value || ''}),
|
1580
|
-
set: function setter(name, value) {
|
1581
|
-
if (typeof value !== 'undefined') {
|
1582
|
-
this.setAttribute(name, value);
|
1583
|
-
} else {
|
1584
|
-
this.removeAttribute(name);
|
1585
|
-
}
|
1586
|
-
}.bind(element, name)
|
1587
|
-
});
|
1588
|
-
} else {
|
1589
|
-
map[propName] = value;
|
1590
|
-
}
|
628
|
+
// Fetch the translation string for that lookup key
|
629
|
+
var translationString = this.translations[lookupKey];
|
1591
630
|
|
631
|
+
if (typeof translationString === 'string') {
|
632
|
+
// Check for ${} placeholders in the translation string
|
633
|
+
if (translationString.match(/%{(.\S+)}/)) {
|
634
|
+
if (!options) {
|
635
|
+
throw new Error('i18n: cannot replace placeholders in string if no option data provided')
|
1592
636
|
}
|
637
|
+
|
638
|
+
return this.replacePlaceholders(translationString, options)
|
639
|
+
} else {
|
640
|
+
return translationString
|
1593
641
|
}
|
1594
|
-
|
1595
|
-
|
642
|
+
} else {
|
643
|
+
// If the key wasn't found in our translations object,
|
644
|
+
// return the lookup key itself as the fallback
|
645
|
+
return lookupKey
|
1596
646
|
}
|
1597
|
-
}
|
1598
|
-
|
1599
|
-
}).call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
|
1600
|
-
|
1601
|
-
(function(undefined) {
|
1602
|
-
|
1603
|
-
// Detection from https://github.com/mdn/content/blob/cf607d68522cd35ee7670782d3ee3a361eaef2e4/files/en-us/web/javascript/reference/global_objects/string/trim/index.md#polyfill
|
1604
|
-
var detect = ('trim' in String.prototype);
|
1605
|
-
|
1606
|
-
if (detect) return
|
647
|
+
};
|
1607
648
|
|
1608
|
-
|
1609
|
-
|
1610
|
-
|
1611
|
-
|
649
|
+
/**
|
650
|
+
* Takes a translation string with placeholders, and replaces the placeholders
|
651
|
+
* with the provided data
|
652
|
+
*
|
653
|
+
* @param {string} translationString - The translation string
|
654
|
+
* @param {Object<string, unknown>} options - Any options passed with the translation string, e.g: for string interpolation.
|
655
|
+
* @returns {string} The translation string to output, with ${} placeholders replaced
|
656
|
+
*/
|
657
|
+
I18n.prototype.replacePlaceholders = function (translationString, options) {
|
658
|
+
/** @type {Intl.NumberFormat | undefined} */
|
659
|
+
var formatter;
|
1612
660
|
|
1613
|
-
|
1614
|
-
|
1615
|
-
|
1616
|
-
* Normalise string
|
1617
|
-
*
|
1618
|
-
* 'If it looks like a duck, and it quacks like a duck…' 🦆
|
1619
|
-
*
|
1620
|
-
* If the passed value looks like a boolean or a number, convert it to a boolean
|
1621
|
-
* or number.
|
1622
|
-
*
|
1623
|
-
* Designed to be used to convert config passed via data attributes (which are
|
1624
|
-
* always strings) into something sensible.
|
1625
|
-
*
|
1626
|
-
* @param {string} value - The value to normalise
|
1627
|
-
* @returns {string | boolean | number | undefined} Normalised data
|
1628
|
-
*/
|
1629
|
-
function normaliseString (value) {
|
1630
|
-
if (typeof value !== 'string') {
|
1631
|
-
return value
|
1632
|
-
}
|
661
|
+
if (this.hasIntlNumberFormatSupport()) {
|
662
|
+
formatter = new Intl.NumberFormat(this.locale);
|
663
|
+
}
|
1633
664
|
|
1634
|
-
|
665
|
+
return translationString.replace(
|
666
|
+
/%{(.\S+)}/g,
|
667
|
+
|
668
|
+
/**
|
669
|
+
* Replace translation string placeholders
|
670
|
+
*
|
671
|
+
* @param {string} placeholderWithBraces - Placeholder with braces
|
672
|
+
* @param {string} placeholderKey - Placeholder key
|
673
|
+
* @returns {string} Placeholder value
|
674
|
+
*/
|
675
|
+
function (placeholderWithBraces, placeholderKey) {
|
676
|
+
if (Object.prototype.hasOwnProperty.call(options, placeholderKey)) {
|
677
|
+
var placeholderValue = options[placeholderKey];
|
678
|
+
|
679
|
+
// If a user has passed `false` as the value for the placeholder
|
680
|
+
// treat it as though the value should not be displayed
|
681
|
+
if (placeholderValue === false || (
|
682
|
+
typeof placeholderValue !== 'number' &&
|
683
|
+
typeof placeholderValue !== 'string')
|
684
|
+
) {
|
685
|
+
return ''
|
686
|
+
}
|
1635
687
|
|
1636
|
-
|
1637
|
-
|
1638
|
-
|
688
|
+
// If the placeholder's value is a number, localise the number formatting
|
689
|
+
if (typeof placeholderValue === 'number') {
|
690
|
+
return formatter ? formatter.format(placeholderValue) : placeholderValue.toString()
|
691
|
+
}
|
1639
692
|
|
1640
|
-
|
1641
|
-
|
1642
|
-
|
693
|
+
return placeholderValue
|
694
|
+
} else {
|
695
|
+
throw new Error('i18n: no data found to replace ' + placeholderWithBraces + ' placeholder in string')
|
696
|
+
}
|
697
|
+
})
|
698
|
+
};
|
1643
699
|
|
1644
|
-
|
1645
|
-
|
1646
|
-
|
1647
|
-
|
1648
|
-
|
700
|
+
/**
|
701
|
+
* Check to see if the browser supports Intl and Intl.PluralRules.
|
702
|
+
*
|
703
|
+
* It requires all conditions to be met in order to be supported:
|
704
|
+
* - The browser supports the Intl class (true in IE11)
|
705
|
+
* - The implementation of Intl supports PluralRules (NOT true in IE11)
|
706
|
+
* - The browser/OS has plural rules for the current locale (browser dependent)
|
707
|
+
*
|
708
|
+
* @returns {boolean} Returns true if all conditions are met. Returns false otherwise.
|
709
|
+
*/
|
710
|
+
I18n.prototype.hasIntlPluralRulesSupport = function () {
|
711
|
+
return Boolean(window.Intl && ('PluralRules' in window.Intl && Intl.PluralRules.supportedLocalesOf(this.locale).length))
|
712
|
+
};
|
1649
713
|
|
1650
|
-
|
1651
|
-
|
1652
|
-
|
1653
|
-
|
1654
|
-
|
1655
|
-
|
1656
|
-
|
1657
|
-
|
1658
|
-
|
1659
|
-
|
1660
|
-
|
1661
|
-
|
1662
|
-
|
1663
|
-
|
1664
|
-
for (var key in dataset) {
|
1665
|
-
out[key] = normaliseString(dataset[key]);
|
1666
|
-
}
|
714
|
+
/**
|
715
|
+
* Check to see if the browser supports Intl and Intl.NumberFormat.
|
716
|
+
*
|
717
|
+
* It requires all conditions to be met in order to be supported:
|
718
|
+
* - The browser supports the Intl class (true in IE11)
|
719
|
+
* - The implementation of Intl supports NumberFormat (also true in IE11)
|
720
|
+
* - The browser/OS has number formatting rules for the current locale (browser dependent)
|
721
|
+
*
|
722
|
+
* @returns {boolean} Returns true if all conditions are met. Returns false otherwise.
|
723
|
+
*/
|
724
|
+
I18n.prototype.hasIntlNumberFormatSupport = function () {
|
725
|
+
return Boolean(window.Intl && ('NumberFormat' in window.Intl && Intl.NumberFormat.supportedLocalesOf(this.locale).length))
|
726
|
+
};
|
1667
727
|
|
1668
|
-
|
1669
|
-
|
728
|
+
/**
|
729
|
+
* Get the appropriate suffix for the plural form.
|
730
|
+
*
|
731
|
+
* Uses Intl.PluralRules (or our own fallback implementation) to get the
|
732
|
+
* 'preferred' form to use for the given count.
|
733
|
+
*
|
734
|
+
* Checks that a translation has been provided for that plural form – if it
|
735
|
+
* hasn't, it'll fall back to the 'other' plural form (unless that doesn't exist
|
736
|
+
* either, in which case an error will be thrown)
|
737
|
+
*
|
738
|
+
* @param {string} lookupKey - The lookup key of the string to use.
|
739
|
+
* @param {number} count - Number used to determine which pluralisation to use.
|
740
|
+
* @returns {PluralRule} The suffix associated with the correct pluralisation for this locale.
|
741
|
+
* @throws {Error} Plural form `.other` required when preferred plural form is missing
|
742
|
+
*/
|
743
|
+
I18n.prototype.getPluralSuffix = function (lookupKey, count) {
|
744
|
+
// Validate that the number is actually a number.
|
745
|
+
//
|
746
|
+
// Number(count) will turn anything that can't be converted to a Number type
|
747
|
+
// into 'NaN'. isFinite filters out NaN, as it isn't a finite number.
|
748
|
+
count = Number(count);
|
749
|
+
if (!isFinite(count)) { return 'other' }
|
750
|
+
|
751
|
+
var preferredForm;
|
752
|
+
|
753
|
+
// Check to verify that all the requirements for Intl.PluralRules are met.
|
754
|
+
// If so, we can use that instead of our custom implementation. Otherwise,
|
755
|
+
// use the hardcoded fallback.
|
756
|
+
if (this.hasIntlPluralRulesSupport()) {
|
757
|
+
preferredForm = new Intl.PluralRules(this.locale).select(count);
|
758
|
+
} else {
|
759
|
+
preferredForm = this.selectPluralFormUsingFallbackRules(count);
|
760
|
+
}
|
1670
761
|
|
1671
|
-
|
762
|
+
// Use the correct plural form if provided
|
763
|
+
if (lookupKey + '.' + preferredForm in this.translations) {
|
764
|
+
return preferredForm
|
765
|
+
// Fall back to `other` if the plural form is missing, but log a warning
|
766
|
+
// to the console
|
767
|
+
} else if (lookupKey + '.other' in this.translations) {
|
768
|
+
if (console && 'warn' in console) {
|
769
|
+
console.warn('i18n: Missing plural form ".' + preferredForm + '" for "' +
|
770
|
+
this.locale + '" locale. Falling back to ".other".');
|
771
|
+
}
|
1672
772
|
|
1673
|
-
|
1674
|
-
|
1675
|
-
|
1676
|
-
|
773
|
+
return 'other'
|
774
|
+
// If the required `other` plural form is missing, all we can do is error
|
775
|
+
} else {
|
776
|
+
throw new Error(
|
777
|
+
'i18n: Plural form ".other" is required for "' + this.locale + '" locale'
|
778
|
+
)
|
779
|
+
}
|
780
|
+
};
|
1677
781
|
|
1678
|
-
|
782
|
+
/**
|
783
|
+
* Get the plural form using our fallback implementation
|
784
|
+
*
|
785
|
+
* This is split out into a separate function to make it easier to test the
|
786
|
+
* fallback behaviour in an environment where Intl.PluralRules exists.
|
787
|
+
*
|
788
|
+
* @param {number} count - Number used to determine which pluralisation to use.
|
789
|
+
* @returns {PluralRule} The pluralisation form for count in this locale.
|
790
|
+
*/
|
791
|
+
I18n.prototype.selectPluralFormUsingFallbackRules = function (count) {
|
792
|
+
// Currently our custom code can only handle positive integers, so let's
|
793
|
+
// make sure our number is one of those.
|
794
|
+
count = Math.abs(Math.floor(count));
|
1679
795
|
|
1680
|
-
|
1681
|
-
Element.prototype.matches = Element.prototype.webkitMatchesSelector || Element.prototype.oMatchesSelector || Element.prototype.msMatchesSelector || Element.prototype.mozMatchesSelector || function matches(selector) {
|
1682
|
-
var element = this;
|
1683
|
-
var elements = (element.document || element.ownerDocument).querySelectorAll(selector);
|
1684
|
-
var index = 0;
|
796
|
+
var ruleset = this.getPluralRulesForLocale();
|
1685
797
|
|
1686
|
-
|
1687
|
-
|
798
|
+
if (ruleset) {
|
799
|
+
return I18n.pluralRules[ruleset](count)
|
1688
800
|
}
|
1689
801
|
|
1690
|
-
return
|
802
|
+
return 'other'
|
1691
803
|
};
|
1692
804
|
|
1693
|
-
|
1694
|
-
|
1695
|
-
|
1696
|
-
|
1697
|
-
|
1698
|
-
|
1699
|
-
|
1700
|
-
|
1701
|
-
|
1702
|
-
|
805
|
+
/**
|
806
|
+
* Work out which pluralisation rules to use for the current locale
|
807
|
+
*
|
808
|
+
* The locale may include a regional indicator (such as en-GB), but we don't
|
809
|
+
* usually care about this part, as pluralisation rules are usually the same
|
810
|
+
* regardless of region. There are exceptions, however, (e.g. Portuguese) so
|
811
|
+
* this searches by both the full and shortened locale codes, just to be sure.
|
812
|
+
*
|
813
|
+
* @returns {string | undefined} The name of the pluralisation rule to use (a key for one
|
814
|
+
* of the functions in this.pluralRules)
|
815
|
+
*/
|
816
|
+
I18n.prototype.getPluralRulesForLocale = function () {
|
817
|
+
var locale = this.locale;
|
818
|
+
var localeShort = locale.split('-')[0];
|
819
|
+
|
820
|
+
// Look through the plural rules map to find which `pluralRule` is
|
821
|
+
// appropriate for our current `locale`.
|
822
|
+
for (var pluralRule in I18n.pluralRulesMap) {
|
823
|
+
if (Object.prototype.hasOwnProperty.call(I18n.pluralRulesMap, pluralRule)) {
|
824
|
+
var languages = I18n.pluralRulesMap[pluralRule];
|
825
|
+
for (var i = 0; i < languages.length; i++) {
|
826
|
+
if (languages[i] === locale || languages[i] === localeShort) {
|
827
|
+
return pluralRule
|
828
|
+
}
|
829
|
+
}
|
830
|
+
}
|
831
|
+
}
|
832
|
+
};
|
1703
833
|
|
1704
|
-
|
1705
|
-
|
1706
|
-
|
834
|
+
/**
|
835
|
+
* Map of plural rules to languages where those rules apply.
|
836
|
+
*
|
837
|
+
* Note: These groups are named for the most dominant or recognisable language
|
838
|
+
* that uses each system. The groupings do not imply that the languages are
|
839
|
+
* related to one another. Many languages have evolved the same systems
|
840
|
+
* independently of one another.
|
841
|
+
*
|
842
|
+
* Code to support more languages can be found in the i18n spike:
|
843
|
+
* {@link https://github.com/alphagov/govuk-frontend/blob/spike-i18n-support/src/govuk/i18n.mjs}
|
844
|
+
*
|
845
|
+
* Languages currently supported:
|
846
|
+
*
|
847
|
+
* Arabic: Arabic (ar)
|
848
|
+
* Chinese: Burmese (my), Chinese (zh), Indonesian (id), Japanese (ja),
|
849
|
+
* Javanese (jv), Korean (ko), Malay (ms), Thai (th), Vietnamese (vi)
|
850
|
+
* French: Armenian (hy), Bangla (bn), French (fr), Gujarati (gu), Hindi (hi),
|
851
|
+
* Persian Farsi (fa), Punjabi (pa), Zulu (zu)
|
852
|
+
* German: Afrikaans (af), Albanian (sq), Azerbaijani (az), Basque (eu),
|
853
|
+
* Bulgarian (bg), Catalan (ca), Danish (da), Dutch (nl), English (en),
|
854
|
+
* Estonian (et), Finnish (fi), Georgian (ka), German (de), Greek (el),
|
855
|
+
* Hungarian (hu), Luxembourgish (lb), Norwegian (no), Somali (so),
|
856
|
+
* Swahili (sw), Swedish (sv), Tamil (ta), Telugu (te), Turkish (tr),
|
857
|
+
* Urdu (ur)
|
858
|
+
* Irish: Irish Gaelic (ga)
|
859
|
+
* Russian: Russian (ru), Ukrainian (uk)
|
860
|
+
* Scottish: Scottish Gaelic (gd)
|
861
|
+
* Spanish: European Portuguese (pt-PT), Italian (it), Spanish (es)
|
862
|
+
* Welsh: Welsh (cy)
|
863
|
+
*
|
864
|
+
* @type {Object<string, string[]>}
|
865
|
+
*/
|
866
|
+
I18n.pluralRulesMap = {
|
867
|
+
arabic: ['ar'],
|
868
|
+
chinese: ['my', 'zh', 'id', 'ja', 'jv', 'ko', 'ms', 'th', 'vi'],
|
869
|
+
french: ['hy', 'bn', 'fr', 'gu', 'hi', 'fa', 'pa', 'zu'],
|
870
|
+
german: [
|
871
|
+
'af', 'sq', 'az', 'eu', 'bg', 'ca', 'da', 'nl', 'en', 'et', 'fi', 'ka',
|
872
|
+
'de', 'el', 'hu', 'lb', 'no', 'so', 'sw', 'sv', 'ta', 'te', 'tr', 'ur'
|
873
|
+
],
|
874
|
+
irish: ['ga'],
|
875
|
+
russian: ['ru', 'uk'],
|
876
|
+
scottish: ['gd'],
|
877
|
+
spanish: ['pt-PT', 'it', 'es'],
|
878
|
+
welsh: ['cy']
|
879
|
+
};
|
1707
880
|
|
1708
|
-
|
1709
|
-
|
1710
|
-
|
881
|
+
/**
|
882
|
+
* Different pluralisation rule sets
|
883
|
+
*
|
884
|
+
* Returns the appropriate suffix for the plural form associated with `n`.
|
885
|
+
* Possible suffixes: 'zero', 'one', 'two', 'few', 'many', 'other' (the actual
|
886
|
+
* meaning of each differs per locale). 'other' should always exist, even in
|
887
|
+
* languages without plurals, such as Chinese.
|
888
|
+
* {@link https://cldr.unicode.org/index/cldr-spec/plural-rules}
|
889
|
+
*
|
890
|
+
* The count must be a positive integer. Negative numbers and decimals aren't accounted for
|
891
|
+
*
|
892
|
+
* @type {Object<string, function(number): PluralRule>}
|
893
|
+
*/
|
894
|
+
I18n.pluralRules = {
|
895
|
+
/* eslint-disable jsdoc/require-jsdoc */
|
896
|
+
arabic: function (n) {
|
897
|
+
if (n === 0) { return 'zero' }
|
898
|
+
if (n === 1) { return 'one' }
|
899
|
+
if (n === 2) { return 'two' }
|
900
|
+
if (n % 100 >= 3 && n % 100 <= 10) { return 'few' }
|
901
|
+
if (n % 100 >= 11 && n % 100 <= 99) { return 'many' }
|
902
|
+
return 'other'
|
903
|
+
},
|
904
|
+
chinese: function () {
|
905
|
+
return 'other'
|
906
|
+
},
|
907
|
+
french: function (n) {
|
908
|
+
return n === 0 || n === 1 ? 'one' : 'other'
|
909
|
+
},
|
910
|
+
german: function (n) {
|
911
|
+
return n === 1 ? 'one' : 'other'
|
912
|
+
},
|
913
|
+
irish: function (n) {
|
914
|
+
if (n === 1) { return 'one' }
|
915
|
+
if (n === 2) { return 'two' }
|
916
|
+
if (n >= 3 && n <= 6) { return 'few' }
|
917
|
+
if (n >= 7 && n <= 10) { return 'many' }
|
918
|
+
return 'other'
|
919
|
+
},
|
920
|
+
russian: function (n) {
|
921
|
+
var lastTwo = n % 100;
|
922
|
+
var last = lastTwo % 10;
|
923
|
+
if (last === 1 && lastTwo !== 11) { return 'one' }
|
924
|
+
if (last >= 2 && last <= 4 && !(lastTwo >= 12 && lastTwo <= 14)) { return 'few' }
|
925
|
+
if (last === 0 || (last >= 5 && last <= 9) || (lastTwo >= 11 && lastTwo <= 14)) { return 'many' }
|
926
|
+
// Note: The 'other' suffix is only used by decimal numbers in Russian.
|
927
|
+
// We don't anticipate it being used, but it's here for consistency.
|
928
|
+
return 'other'
|
929
|
+
},
|
930
|
+
scottish: function (n) {
|
931
|
+
if (n === 1 || n === 11) { return 'one' }
|
932
|
+
if (n === 2 || n === 12) { return 'two' }
|
933
|
+
if ((n >= 3 && n <= 10) || (n >= 13 && n <= 19)) { return 'few' }
|
934
|
+
return 'other'
|
935
|
+
},
|
936
|
+
spanish: function (n) {
|
937
|
+
if (n === 1) { return 'one' }
|
938
|
+
if (n % 1000000 === 0 && n !== 0) { return 'many' }
|
939
|
+
return 'other'
|
940
|
+
},
|
941
|
+
welsh: function (n) {
|
942
|
+
if (n === 0) { return 'zero' }
|
943
|
+
if (n === 1) { return 'one' }
|
944
|
+
if (n === 2) { return 'two' }
|
945
|
+
if (n === 3) { return 'few' }
|
946
|
+
if (n === 6) { return 'many' }
|
947
|
+
return 'other'
|
1711
948
|
}
|
1712
|
-
|
1713
|
-
return null;
|
949
|
+
/* eslint-enable jsdoc/require-jsdoc */
|
1714
950
|
};
|
1715
951
|
|
1716
|
-
|
1717
|
-
|
1718
|
-
|
1719
|
-
|
1720
|
-
|
1721
|
-
|
1722
|
-
|
1723
|
-
|
1724
|
-
|
1725
|
-
|
1726
|
-
|
1727
|
-
|
1728
|
-
|
1729
|
-
|
1730
|
-
}
|
1731
|
-
|
1732
|
-
|
1733
|
-
|
1734
|
-
|
1735
|
-
|
1736
|
-
|
1737
|
-
|
1738
|
-
|
1739
|
-
|
1740
|
-
|
1741
|
-
|
1742
|
-
|
1743
|
-
|
1744
|
-
|
1745
|
-
|
1746
|
-
|
1747
|
-
|
1748
|
-
|
1749
|
-
|
1750
|
-
|
1751
|
-
|
1752
|
-
|
1753
|
-
|
1754
|
-
|
1755
|
-
|
1756
|
-
|
1757
|
-
|
1758
|
-
|
1759
|
-
|
1760
|
-
|
1761
|
-
|
1762
|
-
|
1763
|
-
|
1764
|
-
|
1765
|
-
|
1766
|
-
|
1767
|
-
|
1768
|
-
|
1769
|
-
|
1770
|
-
|
1771
|
-
|
1772
|
-
|
1773
|
-
|
1774
|
-
|
1775
|
-
|
1776
|
-
|
1777
|
-
|
1778
|
-
function
|
1779
|
-
|
1780
|
-
|
952
|
+
/**
|
953
|
+
* Plural rule category mnemonic tags
|
954
|
+
*
|
955
|
+
* @typedef {'zero' | 'one' | 'two' | 'few' | 'many' | 'other'} PluralRule
|
956
|
+
*/
|
957
|
+
|
958
|
+
/**
|
959
|
+
* Translated message by plural rule they correspond to.
|
960
|
+
*
|
961
|
+
* Allows to group pluralised messages under a single key when passing
|
962
|
+
* translations to a component's constructor
|
963
|
+
*
|
964
|
+
* @typedef {object} TranslationPluralForms
|
965
|
+
* @property {string} [other] - General plural form
|
966
|
+
* @property {string} [zero] - Plural form used with 0
|
967
|
+
* @property {string} [one] - Plural form used with 1
|
968
|
+
* @property {string} [two] - Plural form used with 2
|
969
|
+
* @property {string} [few] - Plural form used for a few
|
970
|
+
* @property {string} [many] - Plural form used for many
|
971
|
+
*/
|
972
|
+
|
973
|
+
// @ts-nocheck
|
974
|
+
(function (undefined) {
|
975
|
+
|
976
|
+
// Detection from https://github.com/Financial-Times/polyfill-library/blob/v3.111.0/polyfills/Date/now/detect.js
|
977
|
+
var detect = ('Date' in self && 'now' in self.Date && 'getTime' in self.Date.prototype);
|
978
|
+
|
979
|
+
if (detect) return
|
980
|
+
|
981
|
+
// Polyfill from https://polyfill.io/v3/polyfill.js?version=3.111.0&features=Date.now&flags=always
|
982
|
+
Date.now = function () {
|
983
|
+
return new Date().getTime();
|
984
|
+
};
|
985
|
+
|
986
|
+
}).call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
|
987
|
+
|
988
|
+
// @ts-nocheck
|
989
|
+
(function (undefined) {
|
990
|
+
|
991
|
+
// Detection from https://raw.githubusercontent.com/Financial-Times/polyfill-service/master/packages/polyfill-library/polyfills/DOMTokenList/detect.js
|
992
|
+
var detect = (
|
993
|
+
'DOMTokenList' in this && (function (x) {
|
994
|
+
return 'classList' in x ? !x.classList.toggle('x', false) && !x.className : true;
|
995
|
+
})(document.createElement('x'))
|
996
|
+
);
|
997
|
+
|
998
|
+
if (detect) return
|
999
|
+
|
1000
|
+
// Polyfill from https://raw.githubusercontent.com/Financial-Times/polyfill-service/master/packages/polyfill-library/polyfills/DOMTokenList/polyfill.js
|
1001
|
+
(function (global) {
|
1002
|
+
var nativeImpl = "DOMTokenList" in global && global.DOMTokenList;
|
1003
|
+
|
1004
|
+
if (
|
1005
|
+
!nativeImpl ||
|
1006
|
+
(
|
1007
|
+
!!document.createElementNS &&
|
1008
|
+
!!document.createElementNS('http://www.w3.org/2000/svg', 'svg') &&
|
1009
|
+
!(document.createElementNS("http://www.w3.org/2000/svg", "svg").classList instanceof DOMTokenList)
|
1010
|
+
)
|
1011
|
+
) {
|
1012
|
+
global.DOMTokenList = (function() { // eslint-disable-line no-unused-vars
|
1013
|
+
var dpSupport = true;
|
1014
|
+
var defineGetter = function (object, name, fn, configurable) {
|
1015
|
+
if (Object.defineProperty)
|
1016
|
+
Object.defineProperty(object, name, {
|
1017
|
+
configurable: false === dpSupport ? true : !!configurable,
|
1018
|
+
get: fn
|
1019
|
+
});
|
1020
|
+
|
1021
|
+
else object.__defineGetter__(name, fn);
|
1022
|
+
};
|
1023
|
+
|
1024
|
+
/** Ensure the browser allows Object.defineProperty to be used on native JavaScript objects. */
|
1025
|
+
try {
|
1026
|
+
defineGetter({}, "support");
|
1027
|
+
}
|
1028
|
+
catch (e) {
|
1029
|
+
dpSupport = false;
|
1030
|
+
}
|
1031
|
+
|
1032
|
+
|
1033
|
+
var _DOMTokenList = function (el, prop) {
|
1034
|
+
var that = this;
|
1035
|
+
var tokens = [];
|
1036
|
+
var tokenMap = {};
|
1037
|
+
var length = 0;
|
1038
|
+
var maxLength = 0;
|
1039
|
+
var addIndexGetter = function (i) {
|
1040
|
+
defineGetter(that, i, function () {
|
1041
|
+
preop();
|
1042
|
+
return tokens[i];
|
1043
|
+
}, false);
|
1044
|
+
|
1045
|
+
};
|
1046
|
+
var reindex = function () {
|
1047
|
+
|
1048
|
+
/** Define getter functions for array-like access to the tokenList's contents. */
|
1049
|
+
if (length >= maxLength)
|
1050
|
+
for (; maxLength < length; ++maxLength) {
|
1051
|
+
addIndexGetter(maxLength);
|
1052
|
+
}
|
1053
|
+
};
|
1054
|
+
|
1055
|
+
/** Helper function called at the start of each class method. Internal use only. */
|
1056
|
+
var preop = function () {
|
1057
|
+
var error;
|
1058
|
+
var i;
|
1059
|
+
var args = arguments;
|
1060
|
+
var rSpace = /\s+/;
|
1061
|
+
|
1062
|
+
/** Validate the token/s passed to an instance method, if any. */
|
1063
|
+
if (args.length)
|
1064
|
+
for (i = 0; i < args.length; ++i)
|
1065
|
+
if (rSpace.test(args[i])) {
|
1066
|
+
error = new SyntaxError('String "' + args[i] + '" ' + "contains" + ' an invalid character');
|
1067
|
+
error.code = 5;
|
1068
|
+
error.name = "InvalidCharacterError";
|
1069
|
+
throw error;
|
1070
|
+
}
|
1071
|
+
|
1072
|
+
|
1073
|
+
/** Split the new value apart by whitespace*/
|
1074
|
+
if (typeof el[prop] === "object") {
|
1075
|
+
tokens = ("" + el[prop].baseVal).replace(/^\s+|\s+$/g, "").split(rSpace);
|
1076
|
+
} else {
|
1077
|
+
tokens = ("" + el[prop]).replace(/^\s+|\s+$/g, "").split(rSpace);
|
1078
|
+
}
|
1079
|
+
|
1080
|
+
/** Avoid treating blank strings as single-item token lists */
|
1081
|
+
if ("" === tokens[0]) tokens = [];
|
1082
|
+
|
1083
|
+
/** Repopulate the internal token lists */
|
1084
|
+
tokenMap = {};
|
1085
|
+
for (i = 0; i < tokens.length; ++i)
|
1086
|
+
tokenMap[tokens[i]] = true;
|
1087
|
+
length = tokens.length;
|
1088
|
+
reindex();
|
1089
|
+
};
|
1090
|
+
|
1091
|
+
/** Populate our internal token list if the targeted attribute of the subject element isn't empty. */
|
1092
|
+
preop();
|
1093
|
+
|
1094
|
+
/** Return the number of tokens in the underlying string. Read-only. */
|
1095
|
+
defineGetter(that, "length", function () {
|
1096
|
+
preop();
|
1097
|
+
return length;
|
1098
|
+
});
|
1099
|
+
|
1100
|
+
/** Override the default toString/toLocaleString methods to return a space-delimited list of tokens when typecast. */
|
1101
|
+
that.toLocaleString =
|
1102
|
+
that.toString = function () {
|
1103
|
+
preop();
|
1104
|
+
return tokens.join(" ");
|
1105
|
+
};
|
1106
|
+
|
1107
|
+
that.item = function (idx) {
|
1108
|
+
preop();
|
1109
|
+
return tokens[idx];
|
1110
|
+
};
|
1111
|
+
|
1112
|
+
that.contains = function (token) {
|
1113
|
+
preop();
|
1114
|
+
return !!tokenMap[token];
|
1115
|
+
};
|
1116
|
+
|
1117
|
+
that.add = function () {
|
1118
|
+
preop.apply(that, args = arguments);
|
1119
|
+
|
1120
|
+
for (var args, token, i = 0, l = args.length; i < l; ++i) {
|
1121
|
+
token = args[i];
|
1122
|
+
if (!tokenMap[token]) {
|
1123
|
+
tokens.push(token);
|
1124
|
+
tokenMap[token] = true;
|
1125
|
+
}
|
1126
|
+
}
|
1127
|
+
|
1128
|
+
/** Update the targeted attribute of the attached element if the token list's changed. */
|
1129
|
+
if (length !== tokens.length) {
|
1130
|
+
length = tokens.length >>> 0;
|
1131
|
+
if (typeof el[prop] === "object") {
|
1132
|
+
el[prop].baseVal = tokens.join(" ");
|
1133
|
+
} else {
|
1134
|
+
el[prop] = tokens.join(" ");
|
1135
|
+
}
|
1136
|
+
reindex();
|
1137
|
+
}
|
1138
|
+
};
|
1139
|
+
|
1140
|
+
that.remove = function () {
|
1141
|
+
preop.apply(that, args = arguments);
|
1142
|
+
|
1143
|
+
/** Build a hash of token names to compare against when recollecting our token list. */
|
1144
|
+
for (var args, ignore = {}, i = 0, t = []; i < args.length; ++i) {
|
1145
|
+
ignore[args[i]] = true;
|
1146
|
+
delete tokenMap[args[i]];
|
1147
|
+
}
|
1148
|
+
|
1149
|
+
/** Run through our tokens list and reassign only those that aren't defined in the hash declared above. */
|
1150
|
+
for (i = 0; i < tokens.length; ++i)
|
1151
|
+
if (!ignore[tokens[i]]) t.push(tokens[i]);
|
1152
|
+
|
1153
|
+
tokens = t;
|
1154
|
+
length = t.length >>> 0;
|
1155
|
+
|
1156
|
+
/** Update the targeted attribute of the attached element. */
|
1157
|
+
if (typeof el[prop] === "object") {
|
1158
|
+
el[prop].baseVal = tokens.join(" ");
|
1159
|
+
} else {
|
1160
|
+
el[prop] = tokens.join(" ");
|
1161
|
+
}
|
1162
|
+
reindex();
|
1163
|
+
};
|
1164
|
+
|
1165
|
+
that.toggle = function (token, force) {
|
1166
|
+
preop.apply(that, [token]);
|
1167
|
+
|
1168
|
+
/** Token state's being forced. */
|
1169
|
+
if (undefined !== force) {
|
1170
|
+
if (force) {
|
1171
|
+
that.add(token);
|
1172
|
+
return true;
|
1173
|
+
} else {
|
1174
|
+
that.remove(token);
|
1175
|
+
return false;
|
1176
|
+
}
|
1177
|
+
}
|
1178
|
+
|
1179
|
+
/** Token already exists in tokenList. Remove it, and return FALSE. */
|
1180
|
+
if (tokenMap[token]) {
|
1181
|
+
that.remove(token);
|
1182
|
+
return false;
|
1183
|
+
}
|
1184
|
+
|
1185
|
+
/** Otherwise, add the token and return TRUE. */
|
1186
|
+
that.add(token);
|
1187
|
+
return true;
|
1188
|
+
};
|
1189
|
+
|
1190
|
+
return that;
|
1191
|
+
};
|
1192
|
+
|
1193
|
+
return _DOMTokenList;
|
1194
|
+
}());
|
1195
|
+
}
|
1196
|
+
|
1197
|
+
// Add second argument to native DOMTokenList.toggle() if necessary
|
1198
|
+
(function () {
|
1199
|
+
var e = document.createElement('span');
|
1200
|
+
if (!('classList' in e)) return;
|
1201
|
+
e.classList.toggle('x', false);
|
1202
|
+
if (!e.classList.contains('x')) return;
|
1203
|
+
e.classList.constructor.prototype.toggle = function toggle(token /*, force*/) {
|
1204
|
+
var force = arguments[1];
|
1205
|
+
if (force === undefined) {
|
1206
|
+
var add = !this.contains(token);
|
1207
|
+
this[add ? 'add' : 'remove'](token);
|
1208
|
+
return add;
|
1209
|
+
}
|
1210
|
+
force = !!force;
|
1211
|
+
this[force ? 'add' : 'remove'](token);
|
1212
|
+
return force;
|
1213
|
+
};
|
1214
|
+
}());
|
1215
|
+
|
1216
|
+
// Add multiple arguments to native DOMTokenList.add() if necessary
|
1217
|
+
(function () {
|
1218
|
+
var e = document.createElement('span');
|
1219
|
+
if (!('classList' in e)) return;
|
1220
|
+
e.classList.add('a', 'b');
|
1221
|
+
if (e.classList.contains('b')) return;
|
1222
|
+
var native = e.classList.constructor.prototype.add;
|
1223
|
+
e.classList.constructor.prototype.add = function () {
|
1224
|
+
var args = arguments;
|
1225
|
+
var l = arguments.length;
|
1226
|
+
for (var i = 0; i < l; i++) {
|
1227
|
+
native.call(this, args[i]);
|
1228
|
+
}
|
1229
|
+
};
|
1230
|
+
}());
|
1231
|
+
|
1232
|
+
// Add multiple arguments to native DOMTokenList.remove() if necessary
|
1233
|
+
(function () {
|
1234
|
+
var e = document.createElement('span');
|
1235
|
+
if (!('classList' in e)) return;
|
1236
|
+
e.classList.add('a');
|
1237
|
+
e.classList.add('b');
|
1238
|
+
e.classList.remove('a', 'b');
|
1239
|
+
if (!e.classList.contains('b')) return;
|
1240
|
+
var native = e.classList.constructor.prototype.remove;
|
1241
|
+
e.classList.constructor.prototype.remove = function () {
|
1242
|
+
var args = arguments;
|
1243
|
+
var l = arguments.length;
|
1244
|
+
for (var i = 0; i < l; i++) {
|
1245
|
+
native.call(this, args[i]);
|
1246
|
+
}
|
1247
|
+
};
|
1248
|
+
}());
|
1249
|
+
|
1250
|
+
}(this));
|
1251
|
+
|
1252
|
+
}).call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
|
1253
|
+
|
1254
|
+
// @ts-nocheck
|
1255
|
+
|
1256
|
+
(function(undefined) {
|
1257
|
+
|
1258
|
+
// Detection from https://raw.githubusercontent.com/Financial-Times/polyfill-service/8717a9e04ac7aff99b4980fbedead98036b0929a/packages/polyfill-library/polyfills/Element/prototype/classList/detect.js
|
1259
|
+
var detect = (
|
1260
|
+
'document' in this && "classList" in document.documentElement && 'Element' in this && 'classList' in Element.prototype && (function () {
|
1261
|
+
var e = document.createElement('span');
|
1262
|
+
e.classList.add('a', 'b');
|
1263
|
+
return e.classList.contains('b');
|
1264
|
+
}())
|
1265
|
+
);
|
1266
|
+
|
1267
|
+
if (detect) return
|
1268
|
+
|
1269
|
+
// Polyfill from https://cdn.polyfill.io/v2/polyfill.js?features=Element.prototype.classList&flags=always
|
1270
|
+
(function (global) {
|
1271
|
+
var dpSupport = true;
|
1272
|
+
var defineGetter = function (object, name, fn, configurable) {
|
1273
|
+
if (Object.defineProperty)
|
1274
|
+
Object.defineProperty(object, name, {
|
1275
|
+
configurable: false === dpSupport ? true : !!configurable,
|
1276
|
+
get: fn
|
1277
|
+
});
|
1278
|
+
|
1279
|
+
else object.__defineGetter__(name, fn);
|
1280
|
+
};
|
1281
|
+
/** Ensure the browser allows Object.defineProperty to be used on native JavaScript objects. */
|
1282
|
+
try {
|
1283
|
+
defineGetter({}, "support");
|
1284
|
+
}
|
1285
|
+
catch (e) {
|
1286
|
+
dpSupport = false;
|
1287
|
+
}
|
1288
|
+
/** Polyfills a property with a DOMTokenList */
|
1289
|
+
var addProp = function (o, name, attr) {
|
1290
|
+
|
1291
|
+
defineGetter(o.prototype, name, function () {
|
1292
|
+
var tokenList;
|
1293
|
+
|
1294
|
+
var THIS = this,
|
1295
|
+
|
1296
|
+
/** Prevent this from firing twice for some reason. What the hell, IE. */
|
1297
|
+
gibberishProperty = "__defineGetter__" + "DEFINE_PROPERTY" + name;
|
1298
|
+
if(THIS[gibberishProperty]) return tokenList;
|
1299
|
+
THIS[gibberishProperty] = true;
|
1300
|
+
|
1301
|
+
/**
|
1302
|
+
* IE8 can't define properties on native JavaScript objects, so we'll use a dumb hack instead.
|
1303
|
+
*
|
1304
|
+
* What this is doing is creating a dummy element ("reflection") inside a detached phantom node ("mirror")
|
1305
|
+
* that serves as the target of Object.defineProperty instead. While we could simply use the subject HTML
|
1306
|
+
* element instead, this would conflict with element types which use indexed properties (such as forms and
|
1307
|
+
* select lists).
|
1308
|
+
*/
|
1309
|
+
if (false === dpSupport) {
|
1310
|
+
|
1311
|
+
var visage;
|
1312
|
+
var mirror = addProp.mirror || document.createElement("div");
|
1313
|
+
var reflections = mirror.childNodes;
|
1314
|
+
var l = reflections.length;
|
1315
|
+
|
1316
|
+
for (var i = 0; i < l; ++i)
|
1317
|
+
if (reflections[i]._R === THIS) {
|
1318
|
+
visage = reflections[i];
|
1319
|
+
break;
|
1320
|
+
}
|
1321
|
+
|
1322
|
+
/** Couldn't find an element's reflection inside the mirror. Materialise one. */
|
1323
|
+
visage || (visage = mirror.appendChild(document.createElement("div")));
|
1324
|
+
|
1325
|
+
tokenList = DOMTokenList.call(visage, THIS, attr);
|
1326
|
+
} else tokenList = new DOMTokenList(THIS, attr);
|
1327
|
+
|
1328
|
+
defineGetter(THIS, name, function () {
|
1329
|
+
return tokenList;
|
1330
|
+
});
|
1331
|
+
delete THIS[gibberishProperty];
|
1332
|
+
|
1333
|
+
return tokenList;
|
1334
|
+
}, true);
|
1335
|
+
};
|
1336
|
+
|
1337
|
+
addProp(global.Element, "classList", "className");
|
1338
|
+
addProp(global.HTMLElement, "classList", "className");
|
1339
|
+
addProp(global.HTMLLinkElement, "relList", "rel");
|
1340
|
+
addProp(global.HTMLAnchorElement, "relList", "rel");
|
1341
|
+
addProp(global.HTMLAreaElement, "relList", "rel");
|
1342
|
+
}(this));
|
1343
|
+
|
1344
|
+
}).call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
|
1345
|
+
|
1346
|
+
// @ts-nocheck
|
1347
|
+
(function (undefined) {
|
1348
|
+
|
1349
|
+
// Detection from https://github.com/Financial-Times/polyfill-service/blob/master/packages/polyfill-library/polyfills/Window/detect.js
|
1350
|
+
var detect = ('Window' in this);
|
1351
|
+
|
1352
|
+
if (detect) return
|
1353
|
+
|
1354
|
+
// Polyfill from https://cdn.polyfill.io/v2/polyfill.js?features=Window&flags=always
|
1355
|
+
if ((typeof WorkerGlobalScope === "undefined") && (typeof importScripts !== "function")) {
|
1356
|
+
(function (global) {
|
1357
|
+
if (global.constructor) {
|
1358
|
+
global.Window = global.constructor;
|
1359
|
+
} else {
|
1360
|
+
(global.Window = global.constructor = new Function('return function Window() {}')()).prototype = this;
|
1361
|
+
}
|
1362
|
+
}(this));
|
1781
1363
|
}
|
1782
1364
|
|
1783
|
-
|
1784
|
-
|
1785
|
-
|
1365
|
+
})
|
1366
|
+
.call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
|
1367
|
+
|
1368
|
+
// @ts-nocheck
|
1369
|
+
|
1370
|
+
(function(undefined) {
|
1371
|
+
|
1372
|
+
// Detection from https://github.com/Financial-Times/polyfill-service/blob/master/packages/polyfill-library/polyfills/Event/detect.js
|
1373
|
+
var detect = (
|
1374
|
+
(function(global) {
|
1375
|
+
|
1376
|
+
if (!('Event' in global)) return false;
|
1377
|
+
if (typeof global.Event === 'function') return true;
|
1378
|
+
|
1379
|
+
try {
|
1380
|
+
|
1381
|
+
// In IE 9-11, the Event object exists but cannot be instantiated
|
1382
|
+
new Event('click');
|
1383
|
+
return true;
|
1384
|
+
} catch(e) {
|
1385
|
+
return false;
|
1386
|
+
}
|
1387
|
+
}(this))
|
1388
|
+
);
|
1389
|
+
|
1390
|
+
if (detect) return
|
1391
|
+
|
1392
|
+
// Polyfill from https://cdn.polyfill.io/v2/polyfill.js?features=Event&flags=always
|
1393
|
+
(function () {
|
1394
|
+
var unlistenableWindowEvents = {
|
1395
|
+
click: 1,
|
1396
|
+
dblclick: 1,
|
1397
|
+
keyup: 1,
|
1398
|
+
keypress: 1,
|
1399
|
+
keydown: 1,
|
1400
|
+
mousedown: 1,
|
1401
|
+
mouseup: 1,
|
1402
|
+
mousemove: 1,
|
1403
|
+
mouseover: 1,
|
1404
|
+
mouseenter: 1,
|
1405
|
+
mouseleave: 1,
|
1406
|
+
mouseout: 1,
|
1407
|
+
storage: 1,
|
1408
|
+
storagecommit: 1,
|
1409
|
+
textinput: 1
|
1410
|
+
};
|
1411
|
+
|
1412
|
+
// This polyfill depends on availability of `document` so will not run in a worker
|
1413
|
+
// However, we asssume there are no browsers with worker support that lack proper
|
1414
|
+
// support for `Event` within the worker
|
1415
|
+
if (typeof document === 'undefined' || typeof window === 'undefined') return;
|
1416
|
+
|
1417
|
+
function indexOf(array, element) {
|
1418
|
+
var
|
1419
|
+
index = -1,
|
1420
|
+
length = array.length;
|
1421
|
+
|
1422
|
+
while (++index < length) {
|
1423
|
+
if (index in array && array[index] === element) {
|
1424
|
+
return index;
|
1425
|
+
}
|
1426
|
+
}
|
1427
|
+
|
1428
|
+
return -1;
|
1429
|
+
}
|
1430
|
+
|
1431
|
+
var existingProto = (window.Event && window.Event.prototype) || null;
|
1432
|
+
window.Event = Window.prototype.Event = function Event(type, eventInitDict) {
|
1433
|
+
if (!type) {
|
1434
|
+
throw new Error('Not enough arguments');
|
1435
|
+
}
|
1436
|
+
|
1437
|
+
var event;
|
1438
|
+
// Shortcut if browser supports createEvent
|
1439
|
+
if ('createEvent' in document) {
|
1440
|
+
event = document.createEvent('Event');
|
1441
|
+
var bubbles = eventInitDict && eventInitDict.bubbles !== undefined ? eventInitDict.bubbles : false;
|
1442
|
+
var cancelable = eventInitDict && eventInitDict.cancelable !== undefined ? eventInitDict.cancelable : false;
|
1443
|
+
|
1444
|
+
event.initEvent(type, bubbles, cancelable);
|
1445
|
+
|
1446
|
+
return event;
|
1447
|
+
}
|
1448
|
+
|
1449
|
+
event = document.createEventObject();
|
1450
|
+
|
1451
|
+
event.type = type;
|
1452
|
+
event.bubbles = eventInitDict && eventInitDict.bubbles !== undefined ? eventInitDict.bubbles : false;
|
1453
|
+
event.cancelable = eventInitDict && eventInitDict.cancelable !== undefined ? eventInitDict.cancelable : false;
|
1454
|
+
|
1455
|
+
return event;
|
1456
|
+
};
|
1457
|
+
if (existingProto) {
|
1458
|
+
Object.defineProperty(window.Event, 'prototype', {
|
1459
|
+
configurable: false,
|
1460
|
+
enumerable: false,
|
1461
|
+
writable: true,
|
1462
|
+
value: existingProto
|
1463
|
+
});
|
1464
|
+
}
|
1465
|
+
|
1466
|
+
if (!('createEvent' in document)) {
|
1467
|
+
window.addEventListener = Window.prototype.addEventListener = Document.prototype.addEventListener = Element.prototype.addEventListener = function addEventListener() {
|
1468
|
+
var
|
1469
|
+
element = this,
|
1470
|
+
type = arguments[0],
|
1471
|
+
listener = arguments[1];
|
1472
|
+
|
1473
|
+
if (element === window && type in unlistenableWindowEvents) {
|
1474
|
+
throw new Error('In IE8 the event: ' + type + ' is not available on the window object. Please see https://github.com/Financial-Times/polyfill-service/issues/317 for more information.');
|
1475
|
+
}
|
1476
|
+
|
1477
|
+
if (!element._events) {
|
1478
|
+
element._events = {};
|
1479
|
+
}
|
1480
|
+
|
1481
|
+
if (!element._events[type]) {
|
1482
|
+
element._events[type] = function (event) {
|
1483
|
+
var
|
1484
|
+
list = element._events[event.type].list,
|
1485
|
+
events = list.slice(),
|
1486
|
+
index = -1,
|
1487
|
+
length = events.length,
|
1488
|
+
eventElement;
|
1489
|
+
|
1490
|
+
event.preventDefault = function preventDefault() {
|
1491
|
+
if (event.cancelable !== false) {
|
1492
|
+
event.returnValue = false;
|
1493
|
+
}
|
1494
|
+
};
|
1495
|
+
|
1496
|
+
event.stopPropagation = function stopPropagation() {
|
1497
|
+
event.cancelBubble = true;
|
1498
|
+
};
|
1499
|
+
|
1500
|
+
event.stopImmediatePropagation = function stopImmediatePropagation() {
|
1501
|
+
event.cancelBubble = true;
|
1502
|
+
event.cancelImmediate = true;
|
1503
|
+
};
|
1504
|
+
|
1505
|
+
event.currentTarget = element;
|
1506
|
+
event.relatedTarget = event.fromElement || null;
|
1507
|
+
event.target = event.target || event.srcElement || element;
|
1508
|
+
event.timeStamp = new Date().getTime();
|
1509
|
+
|
1510
|
+
if (event.clientX) {
|
1511
|
+
event.pageX = event.clientX + document.documentElement.scrollLeft;
|
1512
|
+
event.pageY = event.clientY + document.documentElement.scrollTop;
|
1513
|
+
}
|
1514
|
+
|
1515
|
+
while (++index < length && !event.cancelImmediate) {
|
1516
|
+
if (index in events) {
|
1517
|
+
eventElement = events[index];
|
1518
|
+
|
1519
|
+
if (indexOf(list, eventElement) !== -1 && typeof eventElement === 'function') {
|
1520
|
+
eventElement.call(element, event);
|
1521
|
+
}
|
1522
|
+
}
|
1523
|
+
}
|
1524
|
+
};
|
1525
|
+
|
1526
|
+
element._events[type].list = [];
|
1527
|
+
|
1528
|
+
if (element.attachEvent) {
|
1529
|
+
element.attachEvent('on' + type, element._events[type]);
|
1530
|
+
}
|
1531
|
+
}
|
1532
|
+
|
1533
|
+
element._events[type].list.push(listener);
|
1534
|
+
};
|
1535
|
+
|
1536
|
+
window.removeEventListener = Window.prototype.removeEventListener = Document.prototype.removeEventListener = Element.prototype.removeEventListener = function removeEventListener() {
|
1537
|
+
var
|
1538
|
+
element = this,
|
1539
|
+
type = arguments[0],
|
1540
|
+
listener = arguments[1],
|
1541
|
+
index;
|
1542
|
+
|
1543
|
+
if (element._events && element._events[type] && element._events[type].list) {
|
1544
|
+
index = indexOf(element._events[type].list, listener);
|
1545
|
+
|
1546
|
+
if (index !== -1) {
|
1547
|
+
element._events[type].list.splice(index, 1);
|
1548
|
+
|
1549
|
+
if (!element._events[type].list.length) {
|
1550
|
+
if (element.detachEvent) {
|
1551
|
+
element.detachEvent('on' + type, element._events[type]);
|
1552
|
+
}
|
1553
|
+
delete element._events[type];
|
1554
|
+
}
|
1555
|
+
}
|
1556
|
+
}
|
1557
|
+
};
|
1558
|
+
|
1559
|
+
window.dispatchEvent = Window.prototype.dispatchEvent = Document.prototype.dispatchEvent = Element.prototype.dispatchEvent = function dispatchEvent(event) {
|
1560
|
+
if (!arguments.length) {
|
1561
|
+
throw new Error('Not enough arguments');
|
1562
|
+
}
|
1563
|
+
|
1564
|
+
if (!event || typeof event.type !== 'string') {
|
1565
|
+
throw new Error('DOM Events Exception 0');
|
1566
|
+
}
|
1567
|
+
|
1568
|
+
var element = this, type = event.type;
|
1569
|
+
|
1570
|
+
try {
|
1571
|
+
if (!event.bubbles) {
|
1572
|
+
event.cancelBubble = true;
|
1573
|
+
|
1574
|
+
var cancelBubbleEvent = function (event) {
|
1575
|
+
event.cancelBubble = true;
|
1576
|
+
|
1577
|
+
(element || window).detachEvent('on' + type, cancelBubbleEvent);
|
1578
|
+
};
|
1579
|
+
|
1580
|
+
this.attachEvent('on' + type, cancelBubbleEvent);
|
1581
|
+
}
|
1582
|
+
|
1583
|
+
this.fireEvent('on' + type, event);
|
1584
|
+
} catch (error) {
|
1585
|
+
event.target = element;
|
1586
|
+
|
1587
|
+
do {
|
1588
|
+
event.currentTarget = element;
|
1589
|
+
|
1590
|
+
if ('_events' in element && typeof element._events[type] === 'function') {
|
1591
|
+
element._events[type].call(element, event);
|
1592
|
+
}
|
1593
|
+
|
1594
|
+
if (typeof element['on' + type] === 'function') {
|
1595
|
+
element['on' + type].call(element, event);
|
1596
|
+
}
|
1597
|
+
|
1598
|
+
element = element.nodeType === 9 ? element.parentWindow : element.parentNode;
|
1599
|
+
} while (element && !event.cancelBubble);
|
1600
|
+
}
|
1601
|
+
|
1602
|
+
return true;
|
1603
|
+
};
|
1604
|
+
|
1605
|
+
// Add the DOMContentLoaded Event
|
1606
|
+
document.attachEvent('onreadystatechange', function() {
|
1607
|
+
if (document.readyState === 'complete') {
|
1608
|
+
document.dispatchEvent(new Event('DOMContentLoaded', {
|
1609
|
+
bubbles: true
|
1610
|
+
}));
|
1611
|
+
}
|
1612
|
+
});
|
1613
|
+
}
|
1614
|
+
}());
|
1615
|
+
|
1616
|
+
})
|
1617
|
+
.call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
|
1618
|
+
|
1619
|
+
// @ts-nocheck
|
1620
|
+
|
1621
|
+
(function(undefined) {
|
1622
|
+
// Detection from https://github.com/Financial-Times/polyfill-service/blob/master/packages/polyfill-library/polyfills/Function/prototype/bind/detect.js
|
1623
|
+
var detect = 'bind' in Function.prototype;
|
1624
|
+
|
1625
|
+
if (detect) return
|
1626
|
+
|
1627
|
+
// Polyfill from https://cdn.polyfill.io/v2/polyfill.js?features=Function.prototype.bind&flags=always
|
1628
|
+
Object.defineProperty(Function.prototype, 'bind', {
|
1629
|
+
value: function bind(that) { // .length is 1
|
1630
|
+
// add necessary es5-shim utilities
|
1631
|
+
var $Array = Array;
|
1632
|
+
var $Object = Object;
|
1633
|
+
var ObjectPrototype = $Object.prototype;
|
1634
|
+
var ArrayPrototype = $Array.prototype;
|
1635
|
+
var Empty = function Empty() {};
|
1636
|
+
var to_string = ObjectPrototype.toString;
|
1637
|
+
var hasToStringTag = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol';
|
1638
|
+
var isCallable; /* inlined from https://npmjs.com/is-callable */ var fnToStr = Function.prototype.toString, tryFunctionObject = function tryFunctionObject(value) { try { fnToStr.call(value); return true; } catch (e) { return false; } }, fnClass = '[object Function]', genClass = '[object GeneratorFunction]'; isCallable = function isCallable(value) { if (typeof value !== 'function') { return false; } if (hasToStringTag) { return tryFunctionObject(value); } var strClass = to_string.call(value); return strClass === fnClass || strClass === genClass; };
|
1639
|
+
var array_slice = ArrayPrototype.slice;
|
1640
|
+
var array_concat = ArrayPrototype.concat;
|
1641
|
+
var array_push = ArrayPrototype.push;
|
1642
|
+
var max = Math.max;
|
1643
|
+
// /add necessary es5-shim utilities
|
1644
|
+
|
1645
|
+
// 1. Let Target be the this value.
|
1646
|
+
var target = this;
|
1647
|
+
// 2. If IsCallable(Target) is false, throw a TypeError exception.
|
1648
|
+
if (!isCallable(target)) {
|
1649
|
+
throw new TypeError('Function.prototype.bind called on incompatible ' + target);
|
1650
|
+
}
|
1651
|
+
// 3. Let A be a new (possibly empty) internal list of all of the
|
1652
|
+
// argument values provided after thisArg (arg1, arg2 etc), in order.
|
1653
|
+
// XXX slicedArgs will stand in for "A" if used
|
1654
|
+
var args = array_slice.call(arguments, 1); // for normal call
|
1655
|
+
// 4. Let F be a new native ECMAScript object.
|
1656
|
+
// 11. Set the [[Prototype]] internal property of F to the standard
|
1657
|
+
// built-in Function prototype object as specified in 15.3.3.1.
|
1658
|
+
// 12. Set the [[Call]] internal property of F as described in
|
1659
|
+
// 15.3.4.5.1.
|
1660
|
+
// 13. Set the [[Construct]] internal property of F as described in
|
1661
|
+
// 15.3.4.5.2.
|
1662
|
+
// 14. Set the [[HasInstance]] internal property of F as described in
|
1663
|
+
// 15.3.4.5.3.
|
1664
|
+
var bound;
|
1665
|
+
var binder = function () {
|
1666
|
+
|
1667
|
+
if (this instanceof bound) {
|
1668
|
+
// 15.3.4.5.2 [[Construct]]
|
1669
|
+
// When the [[Construct]] internal method of a function object,
|
1670
|
+
// F that was created using the bind function is called with a
|
1671
|
+
// list of arguments ExtraArgs, the following steps are taken:
|
1672
|
+
// 1. Let target be the value of F's [[TargetFunction]]
|
1673
|
+
// internal property.
|
1674
|
+
// 2. If target has no [[Construct]] internal method, a
|
1675
|
+
// TypeError exception is thrown.
|
1676
|
+
// 3. Let boundArgs be the value of F's [[BoundArgs]] internal
|
1677
|
+
// property.
|
1678
|
+
// 4. Let args be a new list containing the same values as the
|
1679
|
+
// list boundArgs in the same order followed by the same
|
1680
|
+
// values as the list ExtraArgs in the same order.
|
1681
|
+
// 5. Return the result of calling the [[Construct]] internal
|
1682
|
+
// method of target providing args as the arguments.
|
1683
|
+
|
1684
|
+
var result = target.apply(
|
1685
|
+
this,
|
1686
|
+
array_concat.call(args, array_slice.call(arguments))
|
1687
|
+
);
|
1688
|
+
if ($Object(result) === result) {
|
1689
|
+
return result;
|
1690
|
+
}
|
1691
|
+
return this;
|
1692
|
+
|
1693
|
+
} else {
|
1694
|
+
// 15.3.4.5.1 [[Call]]
|
1695
|
+
// When the [[Call]] internal method of a function object, F,
|
1696
|
+
// which was created using the bind function is called with a
|
1697
|
+
// this value and a list of arguments ExtraArgs, the following
|
1698
|
+
// steps are taken:
|
1699
|
+
// 1. Let boundArgs be the value of F's [[BoundArgs]] internal
|
1700
|
+
// property.
|
1701
|
+
// 2. Let boundThis be the value of F's [[BoundThis]] internal
|
1702
|
+
// property.
|
1703
|
+
// 3. Let target be the value of F's [[TargetFunction]] internal
|
1704
|
+
// property.
|
1705
|
+
// 4. Let args be a new list containing the same values as the
|
1706
|
+
// list boundArgs in the same order followed by the same
|
1707
|
+
// values as the list ExtraArgs in the same order.
|
1708
|
+
// 5. Return the result of calling the [[Call]] internal method
|
1709
|
+
// of target providing boundThis as the this value and
|
1710
|
+
// providing args as the arguments.
|
1711
|
+
|
1712
|
+
// equiv: target.call(this, ...boundArgs, ...args)
|
1713
|
+
return target.apply(
|
1714
|
+
that,
|
1715
|
+
array_concat.call(args, array_slice.call(arguments))
|
1716
|
+
);
|
1717
|
+
|
1718
|
+
}
|
1719
|
+
|
1720
|
+
};
|
1721
|
+
|
1722
|
+
// 15. If the [[Class]] internal property of Target is "Function", then
|
1723
|
+
// a. Let L be the length property of Target minus the length of A.
|
1724
|
+
// b. Set the length own property of F to either 0 or L, whichever is
|
1725
|
+
// larger.
|
1726
|
+
// 16. Else set the length own property of F to 0.
|
1727
|
+
|
1728
|
+
var boundLength = max(0, target.length - args.length);
|
1729
|
+
|
1730
|
+
// 17. Set the attributes of the length own property of F to the values
|
1731
|
+
// specified in 15.3.5.1.
|
1732
|
+
var boundArgs = [];
|
1733
|
+
for (var i = 0; i < boundLength; i++) {
|
1734
|
+
array_push.call(boundArgs, '$' + i);
|
1735
|
+
}
|
1736
|
+
|
1737
|
+
// XXX Build a dynamic function with desired amount of arguments is the only
|
1738
|
+
// way to set the length property of a function.
|
1739
|
+
// In environments where Content Security Policies enabled (Chrome extensions,
|
1740
|
+
// for ex.) all use of eval or Function costructor throws an exception.
|
1741
|
+
// However in all of these environments Function.prototype.bind exists
|
1742
|
+
// and so this code will never be executed.
|
1743
|
+
bound = Function('binder', 'return function (' + boundArgs.join(',') + '){ return binder.apply(this, arguments); }')(binder);
|
1744
|
+
|
1745
|
+
if (target.prototype) {
|
1746
|
+
Empty.prototype = target.prototype;
|
1747
|
+
bound.prototype = new Empty();
|
1748
|
+
// Clean up dangling references.
|
1749
|
+
Empty.prototype = null;
|
1750
|
+
}
|
1751
|
+
|
1752
|
+
// TODO
|
1753
|
+
// 18. Set the [[Extensible]] internal property of F to true.
|
1754
|
+
|
1755
|
+
// TODO
|
1756
|
+
// 19. Let thrower be the [[ThrowTypeError]] function Object (13.2.3).
|
1757
|
+
// 20. Call the [[DefineOwnProperty]] internal method of F with
|
1758
|
+
// arguments "caller", PropertyDescriptor {[[Get]]: thrower, [[Set]]:
|
1759
|
+
// thrower, [[Enumerable]]: false, [[Configurable]]: false}, and
|
1760
|
+
// false.
|
1761
|
+
// 21. Call the [[DefineOwnProperty]] internal method of F with
|
1762
|
+
// arguments "arguments", PropertyDescriptor {[[Get]]: thrower,
|
1763
|
+
// [[Set]]: thrower, [[Enumerable]]: false, [[Configurable]]: false},
|
1764
|
+
// and false.
|
1765
|
+
|
1766
|
+
// TODO
|
1767
|
+
// NOTE Function objects created using Function.prototype.bind do not
|
1768
|
+
// have a prototype property or the [[Code]], [[FormalParameters]], and
|
1769
|
+
// [[Scope]] internal properties.
|
1770
|
+
// XXX can't delete prototype in pure-js.
|
1771
|
+
|
1772
|
+
// 22. Return F.
|
1773
|
+
return bound;
|
1774
|
+
}
|
1775
|
+
});
|
1776
|
+
})
|
1777
|
+
.call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
|
1778
|
+
|
1779
|
+
/* eslint-disable es-x/no-date-now -- Polyfill imported */
|
1780
|
+
|
1781
|
+
/**
|
1782
|
+
* @constant
|
1783
|
+
* @type {CharacterCountTranslations}
|
1784
|
+
* @see Default value for {@link CharacterCountConfig.i18n}
|
1785
|
+
* @default
|
1786
|
+
*/
|
1787
|
+
var CHARACTER_COUNT_TRANSLATIONS = {
|
1788
|
+
// Characters
|
1789
|
+
charactersUnderLimit: {
|
1790
|
+
one: 'You have %{count} character remaining',
|
1791
|
+
other: 'You have %{count} characters remaining'
|
1792
|
+
},
|
1793
|
+
charactersAtLimit: 'You have 0 characters remaining',
|
1794
|
+
charactersOverLimit: {
|
1795
|
+
one: 'You have %{count} character too many',
|
1796
|
+
other: 'You have %{count} characters too many'
|
1797
|
+
},
|
1798
|
+
// Words
|
1799
|
+
wordsUnderLimit: {
|
1800
|
+
one: 'You have %{count} word remaining',
|
1801
|
+
other: 'You have %{count} words remaining'
|
1802
|
+
},
|
1803
|
+
wordsAtLimit: 'You have 0 words remaining',
|
1804
|
+
wordsOverLimit: {
|
1805
|
+
one: 'You have %{count} word too many',
|
1806
|
+
other: 'You have %{count} words too many'
|
1807
|
+
},
|
1808
|
+
textareaDescription: {
|
1809
|
+
other: ''
|
1810
|
+
}
|
1786
1811
|
};
|
1787
1812
|
|
1788
|
-
|
1789
|
-
|
1790
|
-
|
1791
|
-
|
1792
|
-
|
1793
|
-
|
1794
|
-
|
1795
|
-
|
1796
|
-
|
1797
|
-
|
1798
|
-
|
1799
|
-
|
1800
|
-
|
1801
|
-
|
1813
|
+
/**
|
1814
|
+
* JavaScript enhancements for the CharacterCount component
|
1815
|
+
*
|
1816
|
+
* Tracks the number of characters or words in the `.govuk-js-character-count`
|
1817
|
+
* `<textarea>` inside the element. Displays a message with the remaining number
|
1818
|
+
* of characters/words available, or the number of characters/words in excess.
|
1819
|
+
*
|
1820
|
+
* You can configure the message to only appear after a certain percentage
|
1821
|
+
* of the available characters/words has been entered.
|
1822
|
+
*
|
1823
|
+
* @class
|
1824
|
+
* @param {Element} $module - HTML element to use for character count
|
1825
|
+
* @param {CharacterCountConfig} [config] - Character count config
|
1826
|
+
*/
|
1827
|
+
function CharacterCount ($module, config) {
|
1828
|
+
if (!($module instanceof HTMLElement)) {
|
1829
|
+
return this
|
1830
|
+
}
|
1831
|
+
|
1832
|
+
var $textarea = $module.querySelector('.govuk-js-character-count');
|
1833
|
+
if (
|
1834
|
+
!(
|
1835
|
+
$textarea instanceof HTMLTextAreaElement ||
|
1836
|
+
$textarea instanceof HTMLInputElement
|
1837
|
+
)
|
1838
|
+
) {
|
1839
|
+
return this
|
1840
|
+
}
|
1841
|
+
|
1842
|
+
var defaultConfig = {
|
1843
|
+
threshold: 0,
|
1844
|
+
i18n: CHARACTER_COUNT_TRANSLATIONS
|
1802
1845
|
};
|
1803
|
-
}
|
1804
1846
|
|
1805
|
-
|
1806
|
-
|
1807
|
-
|
1808
|
-
|
1809
|
-
|
1810
|
-
|
1847
|
+
// Read config set using dataset ('data-' values)
|
1848
|
+
var datasetConfig = normaliseDataset($module.dataset);
|
1849
|
+
|
1850
|
+
// To ensure data-attributes take complete precedence, even if they change the
|
1851
|
+
// type of count, we need to reset the `maxlength` and `maxwords` from the
|
1852
|
+
// JavaScript config.
|
1853
|
+
//
|
1854
|
+
// We can't mutate `config`, though, as it may be shared across multiple
|
1855
|
+
// components inside `initAll`.
|
1856
|
+
var configOverrides = {};
|
1857
|
+
if ('maxwords' in datasetConfig || 'maxlength' in datasetConfig) {
|
1858
|
+
configOverrides = {
|
1859
|
+
maxlength: false,
|
1860
|
+
maxwords: false
|
1861
|
+
};
|
1862
|
+
}
|
1811
1863
|
|
1812
|
-
|
1813
|
-
|
1814
|
-
|
1815
|
-
|
1816
|
-
|
1817
|
-
|
1818
|
-
|
1819
|
-
|
1820
|
-
|
1821
|
-
|
1822
|
-
} else {
|
1823
|
-
return
|
1824
|
-
}
|
1864
|
+
/**
|
1865
|
+
* @deprecated Will be made private in v5.0
|
1866
|
+
* @type {CharacterCountConfig}
|
1867
|
+
*/
|
1868
|
+
this.config = mergeConfigs(
|
1869
|
+
defaultConfig,
|
1870
|
+
config || {},
|
1871
|
+
configOverrides,
|
1872
|
+
datasetConfig
|
1873
|
+
);
|
1825
1874
|
|
1826
|
-
|
1827
|
-
|
1828
|
-
|
1829
|
-
|
1830
|
-
|
1831
|
-
|
1832
|
-
|
1833
|
-
|
1834
|
-
|
1835
|
-
|
1836
|
-
|
1837
|
-
|
1838
|
-
|
1839
|
-
|
1840
|
-
|
1875
|
+
/** @deprecated Will be made private in v5.0 */
|
1876
|
+
this.i18n = new I18n(extractConfigByNamespace(this.config, 'i18n'), {
|
1877
|
+
// Read the fallback if necessary rather than have it set in the defaults
|
1878
|
+
locale: closestAttributeValue($module, 'lang')
|
1879
|
+
});
|
1880
|
+
|
1881
|
+
/** @deprecated Will be made private in v5.0 */
|
1882
|
+
this.maxLength = Infinity;
|
1883
|
+
// Determine the limit attribute (characters or words)
|
1884
|
+
if ('maxwords' in this.config && this.config.maxwords) {
|
1885
|
+
this.maxLength = this.config.maxwords;
|
1886
|
+
} else if ('maxlength' in this.config && this.config.maxlength) {
|
1887
|
+
this.maxLength = this.config.maxlength;
|
1888
|
+
} else {
|
1889
|
+
return
|
1890
|
+
}
|
1841
1891
|
|
1842
|
-
|
1843
|
-
|
1892
|
+
/** @deprecated Will be made private in v5.0 */
|
1893
|
+
this.$module = $module;
|
1844
1894
|
|
1845
|
-
|
1846
|
-
|
1847
|
-
|
1848
|
-
|
1849
|
-
|
1850
|
-
|
1895
|
+
/** @deprecated Will be made private in v5.0 */
|
1896
|
+
this.$textarea = $textarea;
|
1897
|
+
|
1898
|
+
/** @deprecated Will be made private in v5.0 */
|
1899
|
+
this.$visibleCountMessage = null;
|
1900
|
+
|
1901
|
+
/** @deprecated Will be made private in v5.0 */
|
1902
|
+
this.$screenReaderCountMessage = null;
|
1903
|
+
|
1904
|
+
/** @deprecated Will be made private in v5.0 */
|
1905
|
+
this.lastInputTimestamp = null;
|
1851
1906
|
|
1852
|
-
|
1853
|
-
|
1854
|
-
|
1855
|
-
|
1856
|
-
|
1857
|
-
// This doesn't need any styling classes, as it is never visible
|
1858
|
-
var $screenReaderCountMessage = document.createElement('div');
|
1859
|
-
$screenReaderCountMessage.className = 'govuk-character-count__sr-status govuk-visually-hidden';
|
1860
|
-
$screenReaderCountMessage.setAttribute('aria-live', 'polite');
|
1861
|
-
this.$screenReaderCountMessage = $screenReaderCountMessage;
|
1862
|
-
$textareaDescription.insertAdjacentElement('afterend', $screenReaderCountMessage);
|
1863
|
-
|
1864
|
-
// Create our live-updating counter element, copying the classes from the
|
1865
|
-
// textarea description for backwards compatibility as these may have been
|
1866
|
-
// configured
|
1867
|
-
var $visibleCountMessage = document.createElement('div');
|
1868
|
-
$visibleCountMessage.className = $textareaDescription.className;
|
1869
|
-
$visibleCountMessage.classList.add('govuk-character-count__status');
|
1870
|
-
$visibleCountMessage.setAttribute('aria-hidden', 'true');
|
1871
|
-
this.$visibleCountMessage = $visibleCountMessage;
|
1872
|
-
$textareaDescription.insertAdjacentElement('afterend', $visibleCountMessage);
|
1873
|
-
|
1874
|
-
// Hide the textarea description
|
1875
|
-
$textareaDescription.classList.add('govuk-visually-hidden');
|
1876
|
-
|
1877
|
-
// Remove hard limit if set
|
1878
|
-
$textarea.removeAttribute('maxlength');
|
1879
|
-
|
1880
|
-
this.bindChangeEvents();
|
1881
|
-
|
1882
|
-
// When the page is restored after navigating 'back' in some browsers the
|
1883
|
-
// state of the character count is not restored until *after* the
|
1884
|
-
// DOMContentLoaded event is fired, so we need to manually update it after the
|
1885
|
-
// pageshow event in browsers that support it.
|
1886
|
-
if ('onpageshow' in window) {
|
1887
|
-
window.addEventListener('pageshow', this.updateCountMessage.bind(this));
|
1888
|
-
} else {
|
1889
|
-
window.addEventListener('DOMContentLoaded', this.updateCountMessage.bind(this));
|
1907
|
+
/** @deprecated Will be made private in v5.0 */
|
1908
|
+
this.lastInputValue = '';
|
1909
|
+
|
1910
|
+
/** @deprecated Will be made private in v5.0 */
|
1911
|
+
this.valueChecker = null;
|
1890
1912
|
}
|
1891
|
-
|
1892
|
-
|
1893
|
-
|
1894
|
-
|
1895
|
-
|
1896
|
-
|
1897
|
-
|
1898
|
-
|
1899
|
-
|
1900
|
-
|
1901
|
-
|
1902
|
-
|
1903
|
-
|
1904
|
-
|
1905
|
-
|
1906
|
-
|
1907
|
-
|
1908
|
-
|
1909
|
-
|
1910
|
-
|
1911
|
-
|
1912
|
-
* Update the visible character counter and keep track of when the last update
|
1913
|
-
* happened for each keypress
|
1914
|
-
*/
|
1915
|
-
CharacterCount.prototype.handleKeyUp = function () {
|
1916
|
-
this.updateVisibleCountMessage();
|
1917
|
-
this.lastInputTimestamp = Date.now();
|
1918
|
-
};
|
1919
|
-
|
1920
|
-
/**
|
1921
|
-
* Handle focus event
|
1922
|
-
*
|
1923
|
-
* Speech recognition software such as Dragon NaturallySpeaking will modify the
|
1924
|
-
* fields by directly changing its `value`. These changes don't trigger events
|
1925
|
-
* in JavaScript, so we need to poll to handle when and if they occur.
|
1926
|
-
*
|
1927
|
-
* Once the keyup event hasn't been detected for at least 1000 ms (1s), check if
|
1928
|
-
* the textarea value has changed and update the count message if it has.
|
1929
|
-
*
|
1930
|
-
* This is so that the update triggered by the manual comparison doesn't
|
1931
|
-
* conflict with debounced KeyboardEvent updates.
|
1932
|
-
*/
|
1933
|
-
CharacterCount.prototype.handleFocus = function () {
|
1934
|
-
this.valueChecker = setInterval(function () {
|
1935
|
-
if (!this.lastInputTimestamp || (Date.now() - 500) >= this.lastInputTimestamp) {
|
1936
|
-
this.updateIfValueChanged();
|
1913
|
+
|
1914
|
+
/**
|
1915
|
+
* Initialise component
|
1916
|
+
*/
|
1917
|
+
CharacterCount.prototype.init = function () {
|
1918
|
+
// Check that required elements are present
|
1919
|
+
if (!this.$module || !this.$textarea) {
|
1920
|
+
return
|
1921
|
+
}
|
1922
|
+
|
1923
|
+
var $textarea = this.$textarea;
|
1924
|
+
var $textareaDescription = document.getElementById($textarea.id + '-info');
|
1925
|
+
if (!$textareaDescription) {
|
1926
|
+
return
|
1927
|
+
}
|
1928
|
+
|
1929
|
+
// Inject a description for the textarea if none is present already
|
1930
|
+
// for when the component was rendered with no maxlength, maxwords
|
1931
|
+
// nor custom textareaDescriptionText
|
1932
|
+
if ($textareaDescription.innerText.match(/^\s*$/)) {
|
1933
|
+
$textareaDescription.innerText = this.i18n.t('textareaDescription', { count: this.maxLength });
|
1937
1934
|
}
|
1938
|
-
|
1939
|
-
|
1940
|
-
|
1941
|
-
|
1942
|
-
|
1943
|
-
*
|
1944
|
-
|
1945
|
-
|
1946
|
-
|
1947
|
-
|
1948
|
-
|
1949
|
-
|
1950
|
-
|
1951
|
-
|
1952
|
-
|
1953
|
-
|
1954
|
-
|
1955
|
-
|
1956
|
-
|
1957
|
-
|
1935
|
+
|
1936
|
+
// Move the textarea description to be immediately after the textarea
|
1937
|
+
// Kept for backwards compatibility
|
1938
|
+
$textarea.insertAdjacentElement('afterend', $textareaDescription);
|
1939
|
+
|
1940
|
+
// Create the *screen reader* specific live-updating counter
|
1941
|
+
// This doesn't need any styling classes, as it is never visible
|
1942
|
+
var $screenReaderCountMessage = document.createElement('div');
|
1943
|
+
$screenReaderCountMessage.className = 'govuk-character-count__sr-status govuk-visually-hidden';
|
1944
|
+
$screenReaderCountMessage.setAttribute('aria-live', 'polite');
|
1945
|
+
this.$screenReaderCountMessage = $screenReaderCountMessage;
|
1946
|
+
$textareaDescription.insertAdjacentElement('afterend', $screenReaderCountMessage);
|
1947
|
+
|
1948
|
+
// Create our live-updating counter element, copying the classes from the
|
1949
|
+
// textarea description for backwards compatibility as these may have been
|
1950
|
+
// configured
|
1951
|
+
var $visibleCountMessage = document.createElement('div');
|
1952
|
+
$visibleCountMessage.className = $textareaDescription.className;
|
1953
|
+
$visibleCountMessage.classList.add('govuk-character-count__status');
|
1954
|
+
$visibleCountMessage.setAttribute('aria-hidden', 'true');
|
1955
|
+
this.$visibleCountMessage = $visibleCountMessage;
|
1956
|
+
$textareaDescription.insertAdjacentElement('afterend', $visibleCountMessage);
|
1957
|
+
|
1958
|
+
// Hide the textarea description
|
1959
|
+
$textareaDescription.classList.add('govuk-visually-hidden');
|
1960
|
+
|
1961
|
+
// Remove hard limit if set
|
1962
|
+
$textarea.removeAttribute('maxlength');
|
1963
|
+
|
1964
|
+
this.bindChangeEvents();
|
1965
|
+
|
1966
|
+
// When the page is restored after navigating 'back' in some browsers the
|
1967
|
+
// state of the character count is not restored until *after* the
|
1968
|
+
// DOMContentLoaded event is fired, so we need to manually update it after the
|
1969
|
+
// pageshow event in browsers that support it.
|
1970
|
+
window.addEventListener(
|
1971
|
+
'onpageshow' in window ? 'pageshow' : 'DOMContentLoaded',
|
1972
|
+
this.updateCountMessage.bind(this)
|
1973
|
+
);
|
1974
|
+
|
1958
1975
|
this.updateCountMessage();
|
1959
|
-
}
|
1960
|
-
};
|
1961
|
-
|
1962
|
-
/**
|
1963
|
-
* Update count message
|
1964
|
-
*
|
1965
|
-
* Helper function to update both the visible and screen reader-specific
|
1966
|
-
* counters simultaneously (e.g. on init)
|
1967
|
-
*/
|
1968
|
-
CharacterCount.prototype.updateCountMessage = function () {
|
1969
|
-
this.updateVisibleCountMessage();
|
1970
|
-
this.updateScreenReaderCountMessage();
|
1971
|
-
};
|
1972
|
-
|
1973
|
-
/**
|
1974
|
-
* Update visible count message
|
1975
|
-
*/
|
1976
|
-
CharacterCount.prototype.updateVisibleCountMessage = function () {
|
1977
|
-
var $textarea = this.$textarea;
|
1978
|
-
var $visibleCountMessage = this.$visibleCountMessage;
|
1979
|
-
var remainingNumber = this.maxLength - this.count($textarea.value);
|
1980
|
-
|
1981
|
-
// If input is over the threshold, remove the disabled class which renders the
|
1982
|
-
// counter invisible.
|
1983
|
-
if (this.isOverThreshold()) {
|
1984
|
-
$visibleCountMessage.classList.remove('govuk-character-count__message--disabled');
|
1985
|
-
} else {
|
1986
|
-
$visibleCountMessage.classList.add('govuk-character-count__message--disabled');
|
1987
|
-
}
|
1976
|
+
};
|
1988
1977
|
|
1989
|
-
|
1990
|
-
|
1991
|
-
|
1992
|
-
|
1993
|
-
|
1994
|
-
|
1995
|
-
|
1996
|
-
|
1997
|
-
|
1998
|
-
|
1978
|
+
/**
|
1979
|
+
* Bind change events
|
1980
|
+
*
|
1981
|
+
* Set up event listeners on the $textarea so that the count messages update
|
1982
|
+
* when the user types.
|
1983
|
+
*
|
1984
|
+
* @deprecated Will be made private in v5.0
|
1985
|
+
*/
|
1986
|
+
CharacterCount.prototype.bindChangeEvents = function () {
|
1987
|
+
var $textarea = this.$textarea;
|
1988
|
+
$textarea.addEventListener('keyup', this.handleKeyUp.bind(this));
|
1999
1989
|
|
2000
|
-
|
2001
|
-
|
2002
|
-
|
2003
|
-
|
2004
|
-
/**
|
2005
|
-
* Update screen reader count message
|
2006
|
-
*/
|
2007
|
-
CharacterCount.prototype.updateScreenReaderCountMessage = function () {
|
2008
|
-
var $screenReaderCountMessage = this.$screenReaderCountMessage;
|
2009
|
-
|
2010
|
-
// If over the threshold, remove the aria-hidden attribute, allowing screen
|
2011
|
-
// readers to announce the content of the element.
|
2012
|
-
if (this.isOverThreshold()) {
|
2013
|
-
$screenReaderCountMessage.removeAttribute('aria-hidden');
|
2014
|
-
} else {
|
2015
|
-
$screenReaderCountMessage.setAttribute('aria-hidden', true);
|
2016
|
-
}
|
1990
|
+
// Bind focus/blur events to start/stop polling
|
1991
|
+
$textarea.addEventListener('focus', this.handleFocus.bind(this));
|
1992
|
+
$textarea.addEventListener('blur', this.handleBlur.bind(this));
|
1993
|
+
};
|
2017
1994
|
|
2018
|
-
|
2019
|
-
|
2020
|
-
|
2021
|
-
|
2022
|
-
|
2023
|
-
|
2024
|
-
|
2025
|
-
|
2026
|
-
|
2027
|
-
|
2028
|
-
|
2029
|
-
|
2030
|
-
if (this.config.maxwords) {
|
2031
|
-
var tokens = text.match(/\S+/g) || []; // Matches consecutive non-whitespace chars
|
2032
|
-
return tokens.length
|
2033
|
-
} else {
|
2034
|
-
return text.length
|
2035
|
-
}
|
2036
|
-
};
|
2037
|
-
|
2038
|
-
/**
|
2039
|
-
* Get count message
|
2040
|
-
*
|
2041
|
-
* @returns {string} Status message
|
2042
|
-
*/
|
2043
|
-
CharacterCount.prototype.getCountMessage = function () {
|
2044
|
-
var remainingNumber = this.maxLength - this.count(this.$textarea.value);
|
2045
|
-
|
2046
|
-
var countType = this.config.maxwords ? 'words' : 'characters';
|
2047
|
-
return this.formatCountMessage(remainingNumber, countType)
|
2048
|
-
};
|
2049
|
-
|
2050
|
-
/**
|
2051
|
-
* Formats the message shown to users according to what's counted
|
2052
|
-
* and how many remain
|
2053
|
-
*
|
2054
|
-
* @param {number} remainingNumber - The number of words/characaters remaining
|
2055
|
-
* @param {string} countType - "words" or "characters"
|
2056
|
-
* @returns {string} Status message
|
2057
|
-
*/
|
2058
|
-
CharacterCount.prototype.formatCountMessage = function (remainingNumber, countType) {
|
2059
|
-
if (remainingNumber === 0) {
|
2060
|
-
return this.i18n.t(countType + 'AtLimit')
|
2061
|
-
}
|
1995
|
+
/**
|
1996
|
+
* Handle key up event
|
1997
|
+
*
|
1998
|
+
* Update the visible character counter and keep track of when the last update
|
1999
|
+
* happened for each keypress
|
2000
|
+
*
|
2001
|
+
* @deprecated Will be made private in v5.0
|
2002
|
+
*/
|
2003
|
+
CharacterCount.prototype.handleKeyUp = function () {
|
2004
|
+
this.updateVisibleCountMessage();
|
2005
|
+
this.lastInputTimestamp = Date.now();
|
2006
|
+
};
|
2062
2007
|
|
2063
|
-
|
2064
|
-
|
2065
|
-
|
2066
|
-
|
2067
|
-
|
2068
|
-
|
2069
|
-
|
2070
|
-
|
2071
|
-
|
2072
|
-
|
2073
|
-
|
2074
|
-
|
2075
|
-
|
2076
|
-
|
2077
|
-
|
2078
|
-
CharacterCount.prototype.
|
2079
|
-
|
2080
|
-
|
2081
|
-
|
2082
|
-
|
2008
|
+
/**
|
2009
|
+
* Handle focus event
|
2010
|
+
*
|
2011
|
+
* Speech recognition software such as Dragon NaturallySpeaking will modify the
|
2012
|
+
* fields by directly changing its `value`. These changes don't trigger events
|
2013
|
+
* in JavaScript, so we need to poll to handle when and if they occur.
|
2014
|
+
*
|
2015
|
+
* Once the keyup event hasn't been detected for at least 1000 ms (1s), check if
|
2016
|
+
* the textarea value has changed and update the count message if it has.
|
2017
|
+
*
|
2018
|
+
* This is so that the update triggered by the manual comparison doesn't
|
2019
|
+
* conflict with debounced KeyboardEvent updates.
|
2020
|
+
*
|
2021
|
+
* @deprecated Will be made private in v5.0
|
2022
|
+
*/
|
2023
|
+
CharacterCount.prototype.handleFocus = function () {
|
2024
|
+
this.valueChecker = setInterval(function () {
|
2025
|
+
if (!this.lastInputTimestamp || (Date.now() - 500) >= this.lastInputTimestamp) {
|
2026
|
+
this.updateIfValueChanged();
|
2027
|
+
}
|
2028
|
+
}.bind(this), 1000);
|
2029
|
+
};
|
2030
|
+
|
2031
|
+
/**
|
2032
|
+
* Handle blur event
|
2033
|
+
*
|
2034
|
+
* Stop checking the textarea value once the textarea no longer has focus
|
2035
|
+
*
|
2036
|
+
* @deprecated Will be made private in v5.0
|
2037
|
+
*/
|
2038
|
+
CharacterCount.prototype.handleBlur = function () {
|
2039
|
+
// Cancel value checking on blur
|
2040
|
+
clearInterval(this.valueChecker);
|
2041
|
+
};
|
2042
|
+
|
2043
|
+
/**
|
2044
|
+
* Update count message if textarea value has changed
|
2045
|
+
*
|
2046
|
+
* @deprecated Will be made private in v5.0
|
2047
|
+
*/
|
2048
|
+
CharacterCount.prototype.updateIfValueChanged = function () {
|
2049
|
+
if (this.$textarea.value !== this.lastInputValue) {
|
2050
|
+
this.lastInputValue = this.$textarea.value;
|
2051
|
+
this.updateCountMessage();
|
2052
|
+
}
|
2053
|
+
};
|
2054
|
+
|
2055
|
+
/**
|
2056
|
+
* Update count message
|
2057
|
+
*
|
2058
|
+
* Helper function to update both the visible and screen reader-specific
|
2059
|
+
* counters simultaneously (e.g. on init)
|
2060
|
+
*
|
2061
|
+
* @deprecated Will be made private in v5.0
|
2062
|
+
*/
|
2063
|
+
CharacterCount.prototype.updateCountMessage = function () {
|
2064
|
+
this.updateVisibleCountMessage();
|
2065
|
+
this.updateScreenReaderCountMessage();
|
2066
|
+
};
|
2067
|
+
|
2068
|
+
/**
|
2069
|
+
* Update visible count message
|
2070
|
+
*
|
2071
|
+
* @deprecated Will be made private in v5.0
|
2072
|
+
*/
|
2073
|
+
CharacterCount.prototype.updateVisibleCountMessage = function () {
|
2074
|
+
var $textarea = this.$textarea;
|
2075
|
+
var $visibleCountMessage = this.$visibleCountMessage;
|
2076
|
+
var remainingNumber = this.maxLength - this.count($textarea.value);
|
2077
|
+
|
2078
|
+
// If input is over the threshold, remove the disabled class which renders the
|
2079
|
+
// counter invisible.
|
2080
|
+
if (this.isOverThreshold()) {
|
2081
|
+
$visibleCountMessage.classList.remove('govuk-character-count__message--disabled');
|
2082
|
+
} else {
|
2083
|
+
$visibleCountMessage.classList.add('govuk-character-count__message--disabled');
|
2084
|
+
}
|
2085
|
+
|
2086
|
+
// Update styles
|
2087
|
+
if (remainingNumber < 0) {
|
2088
|
+
$textarea.classList.add('govuk-textarea--error');
|
2089
|
+
$visibleCountMessage.classList.remove('govuk-hint');
|
2090
|
+
$visibleCountMessage.classList.add('govuk-error-message');
|
2091
|
+
} else {
|
2092
|
+
$textarea.classList.remove('govuk-textarea--error');
|
2093
|
+
$visibleCountMessage.classList.remove('govuk-error-message');
|
2094
|
+
$visibleCountMessage.classList.add('govuk-hint');
|
2095
|
+
}
|
2096
|
+
|
2097
|
+
// Update message
|
2098
|
+
$visibleCountMessage.innerText = this.getCountMessage();
|
2099
|
+
};
|
2100
|
+
|
2101
|
+
/**
|
2102
|
+
* Update screen reader count message
|
2103
|
+
*
|
2104
|
+
* @deprecated Will be made private in v5.0
|
2105
|
+
*/
|
2106
|
+
CharacterCount.prototype.updateScreenReaderCountMessage = function () {
|
2107
|
+
var $screenReaderCountMessage = this.$screenReaderCountMessage;
|
2108
|
+
|
2109
|
+
// If over the threshold, remove the aria-hidden attribute, allowing screen
|
2110
|
+
// readers to announce the content of the element.
|
2111
|
+
if (this.isOverThreshold()) {
|
2112
|
+
$screenReaderCountMessage.removeAttribute('aria-hidden');
|
2113
|
+
} else {
|
2114
|
+
$screenReaderCountMessage.setAttribute('aria-hidden', 'true');
|
2115
|
+
}
|
2116
|
+
|
2117
|
+
// Update message
|
2118
|
+
$screenReaderCountMessage.innerText = this.getCountMessage();
|
2119
|
+
};
|
2120
|
+
|
2121
|
+
/**
|
2122
|
+
* Count the number of characters (or words, if `config.maxwords` is set)
|
2123
|
+
* in the given text
|
2124
|
+
*
|
2125
|
+
* @deprecated Will be made private in v5.0
|
2126
|
+
* @param {string} text - The text to count the characters of
|
2127
|
+
* @returns {number} the number of characters (or words) in the text
|
2128
|
+
*/
|
2129
|
+
CharacterCount.prototype.count = function (text) {
|
2130
|
+
if ('maxwords' in this.config && this.config.maxwords) {
|
2131
|
+
var tokens = text.match(/\S+/g) || []; // Matches consecutive non-whitespace chars
|
2132
|
+
return tokens.length
|
2133
|
+
} else {
|
2134
|
+
return text.length
|
2135
|
+
}
|
2136
|
+
};
|
2137
|
+
|
2138
|
+
/**
|
2139
|
+
* Get count message
|
2140
|
+
*
|
2141
|
+
* @deprecated Will be made private in v5.0
|
2142
|
+
* @returns {string} Status message
|
2143
|
+
*/
|
2144
|
+
CharacterCount.prototype.getCountMessage = function () {
|
2145
|
+
var remainingNumber = this.maxLength - this.count(this.$textarea.value);
|
2146
|
+
|
2147
|
+
var countType = 'maxwords' in this.config && this.config.maxwords ? 'words' : 'characters';
|
2148
|
+
return this.formatCountMessage(remainingNumber, countType)
|
2149
|
+
};
|
2150
|
+
|
2151
|
+
/**
|
2152
|
+
* Formats the message shown to users according to what's counted
|
2153
|
+
* and how many remain
|
2154
|
+
*
|
2155
|
+
* @deprecated Will be made private in v5.0
|
2156
|
+
* @param {number} remainingNumber - The number of words/characaters remaining
|
2157
|
+
* @param {string} countType - "words" or "characters"
|
2158
|
+
* @returns {string} Status message
|
2159
|
+
*/
|
2160
|
+
CharacterCount.prototype.formatCountMessage = function (remainingNumber, countType) {
|
2161
|
+
if (remainingNumber === 0) {
|
2162
|
+
return this.i18n.t(countType + 'AtLimit')
|
2163
|
+
}
|
2164
|
+
|
2165
|
+
var translationKeySuffix = remainingNumber < 0 ? 'OverLimit' : 'UnderLimit';
|
2166
|
+
|
2167
|
+
return this.i18n.t(countType + translationKeySuffix, { count: Math.abs(remainingNumber) })
|
2168
|
+
};
|
2169
|
+
|
2170
|
+
/**
|
2171
|
+
* Check if count is over threshold
|
2172
|
+
*
|
2173
|
+
* Checks whether the value is over the configured threshold for the input.
|
2174
|
+
* If there is no configured threshold, it is set to 0 and this function will
|
2175
|
+
* always return true.
|
2176
|
+
*
|
2177
|
+
* @deprecated Will be made private in v5.0
|
2178
|
+
* @returns {boolean} true if the current count is over the config.threshold
|
2179
|
+
* (or no threshold is set)
|
2180
|
+
*/
|
2181
|
+
CharacterCount.prototype.isOverThreshold = function () {
|
2182
|
+
// No threshold means we're always above threshold so save some computation
|
2183
|
+
if (!this.config.threshold) {
|
2184
|
+
return true
|
2185
|
+
}
|
2186
|
+
|
2187
|
+
var $textarea = this.$textarea;
|
2188
|
+
|
2189
|
+
// Determine the remaining number of characters/words
|
2190
|
+
var currentLength = this.count($textarea.value);
|
2191
|
+
var maxLength = this.maxLength;
|
2192
|
+
|
2193
|
+
var thresholdValue = maxLength * this.config.threshold / 100;
|
2194
|
+
|
2195
|
+
return (thresholdValue <= currentLength)
|
2196
|
+
};
|
2197
|
+
|
2198
|
+
/**
|
2199
|
+
* Character count config
|
2200
|
+
*
|
2201
|
+
* @typedef {CharacterCountConfigWithMaxLength | CharacterCountConfigWithMaxWords} CharacterCountConfig
|
2202
|
+
*/
|
2203
|
+
|
2204
|
+
/**
|
2205
|
+
* Character count config (with maximum number of characters)
|
2206
|
+
*
|
2207
|
+
* @typedef {object} CharacterCountConfigWithMaxLength
|
2208
|
+
* @property {number} [maxlength] - The maximum number of characters.
|
2209
|
+
* If maxwords is provided, the maxlength option will be ignored.
|
2210
|
+
* @property {number} [threshold = 0] - The percentage value of the limit at
|
2211
|
+
* which point the count message is displayed. If this attribute is set, the
|
2212
|
+
* count message will be hidden by default.
|
2213
|
+
* @property {CharacterCountTranslations} [i18n = CHARACTER_COUNT_TRANSLATIONS] - See constant {@link CHARACTER_COUNT_TRANSLATIONS}
|
2214
|
+
*/
|
2215
|
+
|
2216
|
+
/**
|
2217
|
+
* Character count config (with maximum number of words)
|
2218
|
+
*
|
2219
|
+
* @typedef {object} CharacterCountConfigWithMaxWords
|
2220
|
+
* @property {number} [maxwords] - The maximum number of words. If maxwords is
|
2221
|
+
* provided, the maxlength option will be ignored.
|
2222
|
+
* @property {number} [threshold = 0] - The percentage value of the limit at
|
2223
|
+
* which point the count message is displayed. If this attribute is set, the
|
2224
|
+
* count message will be hidden by default.
|
2225
|
+
* @property {CharacterCountTranslations} [i18n = CHARACTER_COUNT_TRANSLATIONS] - See constant {@link CHARACTER_COUNT_TRANSLATIONS}
|
2226
|
+
*/
|
2227
|
+
|
2228
|
+
/**
|
2229
|
+
* Character count translations
|
2230
|
+
*
|
2231
|
+
* @typedef {object} CharacterCountTranslations
|
2232
|
+
*
|
2233
|
+
* Messages shown to users as they type. It provides feedback on how many words
|
2234
|
+
* or characters they have remaining or if they are over the limit. This also
|
2235
|
+
* includes a message used as an accessible description for the textarea.
|
2236
|
+
* @property {TranslationPluralForms} [charactersUnderLimit] - Message displayed
|
2237
|
+
* when the number of characters is under the configured maximum, `maxlength`.
|
2238
|
+
* This message is displayed visually and through assistive technologies. The
|
2239
|
+
* component will replace the `%{count}` placeholder with the number of
|
2240
|
+
* remaining characters. This is a [pluralised list of
|
2241
|
+
* messages](https://frontend.design-system.service.gov.uk/localise-govuk-frontend).
|
2242
|
+
* @property {string} [charactersAtLimit] - Message displayed when the number of
|
2243
|
+
* characters reaches the configured maximum, `maxlength`. This message is
|
2244
|
+
* displayed visually and through assistive technologies.
|
2245
|
+
* @property {TranslationPluralForms} [charactersOverLimit] - Message displayed
|
2246
|
+
* when the number of characters is over the configured maximum, `maxlength`.
|
2247
|
+
* This message is displayed visually and through assistive technologies. The
|
2248
|
+
* component will replace the `%{count}` placeholder with the number of
|
2249
|
+
* remaining characters. This is a [pluralised list of
|
2250
|
+
* messages](https://frontend.design-system.service.gov.uk/localise-govuk-frontend).
|
2251
|
+
* @property {TranslationPluralForms} [wordsUnderLimit] - Message displayed when
|
2252
|
+
* the number of words is under the configured maximum, `maxlength`. This
|
2253
|
+
* message is displayed visually and through assistive technologies. The
|
2254
|
+
* component will replace the `%{count}` placeholder with the number of
|
2255
|
+
* remaining words. This is a [pluralised list of
|
2256
|
+
* messages](https://frontend.design-system.service.gov.uk/localise-govuk-frontend).
|
2257
|
+
* @property {string} [wordsAtLimit] - Message displayed when the number of
|
2258
|
+
* words reaches the configured maximum, `maxlength`. This message is
|
2259
|
+
* displayed visually and through assistive technologies.
|
2260
|
+
* @property {TranslationPluralForms} [wordsOverLimit] - Message displayed when
|
2261
|
+
* the number of words is over the configured maximum, `maxlength`. This
|
2262
|
+
* message is displayed visually and through assistive technologies. The
|
2263
|
+
* component will replace the `%{count}` placeholder with the number of
|
2264
|
+
* remaining words. This is a [pluralised list of
|
2265
|
+
* messages](https://frontend.design-system.service.gov.uk/localise-govuk-frontend).
|
2266
|
+
* @property {TranslationPluralForms} [textareaDescription] - Message made
|
2267
|
+
* available to assistive technologies, if none is already present in the
|
2268
|
+
* HTML, to describe that the component accepts only a limited amount of
|
2269
|
+
* content. It is visible on the page when JavaScript is unavailable. The
|
2270
|
+
* component will replace the `%{count}` placeholder with the value of the
|
2271
|
+
* `maxlength` or `maxwords` parameter.
|
2272
|
+
*/
|
2273
|
+
|
2274
|
+
/**
|
2275
|
+
* @typedef {import('../../i18n.mjs').TranslationPluralForms} TranslationPluralForms
|
2276
|
+
*/
|
2083
2277
|
|
2084
|
-
|
2085
|
-
|
2086
|
-
// Determine the remaining number of characters/words
|
2087
|
-
var currentLength = this.count($textarea.value);
|
2088
|
-
var maxLength = this.maxLength;
|
2089
|
-
|
2090
|
-
var thresholdValue = maxLength * this.config.threshold / 100;
|
2091
|
-
|
2092
|
-
return (thresholdValue <= currentLength)
|
2093
|
-
};
|
2094
|
-
|
2095
|
-
/**
|
2096
|
-
* Character count config
|
2097
|
-
*
|
2098
|
-
* @typedef {CharacterCountConfigWithMaxLength | CharacterCountConfigWithMaxWords} CharacterCountConfig
|
2099
|
-
*/
|
2100
|
-
|
2101
|
-
/**
|
2102
|
-
* Character count config (with maximum number of characters)
|
2103
|
-
*
|
2104
|
-
* @typedef {object} CharacterCountConfigWithMaxLength
|
2105
|
-
* @property {number} [maxlength] - The maximum number of characters.
|
2106
|
-
* If maxwords is provided, the maxlength option will be ignored.
|
2107
|
-
* @property {number} [threshold = 0] - The percentage value of the limit at
|
2108
|
-
* which point the count message is displayed. If this attribute is set, the
|
2109
|
-
* count message will be hidden by default.
|
2110
|
-
* @property {CharacterCountTranslations} [i18n = CHARACTER_COUNT_TRANSLATIONS] - See constant {@link CHARACTER_COUNT_TRANSLATIONS}
|
2111
|
-
*/
|
2112
|
-
|
2113
|
-
/**
|
2114
|
-
* Character count config (with maximum number of words)
|
2115
|
-
*
|
2116
|
-
* @typedef {object} CharacterCountConfigWithMaxWords
|
2117
|
-
* @property {number} [maxwords] - The maximum number of words. If maxwords is
|
2118
|
-
* provided, the maxlength option will be ignored.
|
2119
|
-
* @property {number} [threshold = 0] - The percentage value of the limit at
|
2120
|
-
* which point the count message is displayed. If this attribute is set, the
|
2121
|
-
* count message will be hidden by default.
|
2122
|
-
* @property {CharacterCountTranslations} [i18n = CHARACTER_COUNT_TRANSLATIONS] - See constant {@link CHARACTER_COUNT_TRANSLATIONS}
|
2123
|
-
*/
|
2124
|
-
|
2125
|
-
/**
|
2126
|
-
* Character count translations
|
2127
|
-
*
|
2128
|
-
* @typedef {object} CharacterCountTranslations
|
2129
|
-
*
|
2130
|
-
* Messages shown to users as they type. It provides feedback on how many words
|
2131
|
-
* or characters they have remaining or if they are over the limit. This also
|
2132
|
-
* includes a message used as an accessible description for the textarea.
|
2133
|
-
* @property {TranslationPluralForms} [charactersUnderLimit] - Message displayed
|
2134
|
-
* when the number of characters is under the configured maximum, `maxlength`.
|
2135
|
-
* This message is displayed visually and through assistive technologies. The
|
2136
|
-
* component will replace the `%{count}` placeholder with the number of
|
2137
|
-
* remaining characters. This is a [pluralised list of
|
2138
|
-
* messages](https://frontend.design-system.service.gov.uk/localise-govuk-frontend).
|
2139
|
-
* @property {string} [charactersAtLimit] - Message displayed when the number of
|
2140
|
-
* characters reaches the configured maximum, `maxlength`. This message is
|
2141
|
-
* displayed visually and through assistive technologies.
|
2142
|
-
* @property {TranslationPluralForms} [charactersOverLimit] - Message displayed
|
2143
|
-
* when the number of characters is over the configured maximum, `maxlength`.
|
2144
|
-
* This message is displayed visually and through assistive technologies. The
|
2145
|
-
* component will replace the `%{count}` placeholder with the number of
|
2146
|
-
* remaining characters. This is a [pluralised list of
|
2147
|
-
* messages](https://frontend.design-system.service.gov.uk/localise-govuk-frontend).
|
2148
|
-
* @property {TranslationPluralForms} [wordsUnderLimit] - Message displayed when
|
2149
|
-
* the number of words is under the configured maximum, `maxlength`. This
|
2150
|
-
* message is displayed visually and through assistive technologies. The
|
2151
|
-
* component will replace the `%{count}` placeholder with the number of
|
2152
|
-
* remaining words. This is a [pluralised list of
|
2153
|
-
* messages](https://frontend.design-system.service.gov.uk/localise-govuk-frontend).
|
2154
|
-
* @property {string} [wordsAtLimit] - Message displayed when the number of
|
2155
|
-
* words reaches the configured maximum, `maxlength`. This message is
|
2156
|
-
* displayed visually and through assistive technologies.
|
2157
|
-
* @property {TranslationPluralForms} [wordsOverLimit] - Message displayed when
|
2158
|
-
* the number of words is over the configured maximum, `maxlength`. This
|
2159
|
-
* message is displayed visually and through assistive technologies. The
|
2160
|
-
* component will replace the `%{count}` placeholder with the number of
|
2161
|
-
* remaining words. This is a [pluralised list of
|
2162
|
-
* messages](https://frontend.design-system.service.gov.uk/localise-govuk-frontend).
|
2163
|
-
* @property {TranslationPluralForms} [textareaDescription] - Message made
|
2164
|
-
* available to assistive technologies, if none is already present in the
|
2165
|
-
* HTML, to describe that the component accepts only a limited amount of
|
2166
|
-
* content. It is visible on the page when JavaScript is unavailable. The
|
2167
|
-
* component will replace the `%{count}` placeholder with the value of the
|
2168
|
-
* `maxlength` or `maxwords` parameter.
|
2169
|
-
*/
|
2170
|
-
|
2171
|
-
/**
|
2172
|
-
* @typedef {import('../../i18n.mjs').TranslationPluralForms} TranslationPluralForms
|
2173
|
-
*/
|
2174
|
-
|
2175
|
-
return CharacterCount;
|
2278
|
+
return CharacterCount;
|
2176
2279
|
|
2177
2280
|
})));
|
2281
|
+
//# sourceMappingURL=character-count.js.map
|