govuk_publishing_components 34.7.0 → 34.7.1

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