govuk_tech_docs 3.4.0 → 3.4.5

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