diff --git a/.eslintrc.js b/.eslintrc.js index 3c91159d8b..4a1f015502 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -30,7 +30,7 @@ module.exports = { excludedFiles: ['**/*.test.mjs'], parser: '@typescript-eslint/parser', parserOptions: { - // Note: Allow ES6 for import/export syntax (although our code is ES3 for legacy browsers) + // Note: Allow ES6 for import/export syntax ecmaVersion: '2015', project: [resolve(__dirname, 'tsconfig.json')] }, @@ -41,7 +41,7 @@ module.exports = { extends: [ 'plugin:@typescript-eslint/recommended', 'plugin:@typescript-eslint/recommended-requiring-type-checking', - 'plugin:es-x/restrict-to-es3' + 'plugin:es-x/restrict-to-es5' ], rules: { // Allow unknown `.prototype` members until ES6 classes diff --git a/src/govuk/all.mjs b/src/govuk/all.mjs index 5f91941c8b..9590472ac6 100644 --- a/src/govuk/all.mjs +++ b/src/govuk/all.mjs @@ -1,5 +1,4 @@ import { version } from './common/govuk-frontend-version.mjs' -import { nodeListForEach } from './common/index.mjs' import Accordion from './components/accordion/accordion.mjs' import Button from './components/button/button.mjs' import CharacterCount from './components/character-count/character-count.mjs' @@ -28,27 +27,27 @@ function initAll (config) { var $scope = config.scope instanceof HTMLElement ? config.scope : document var $accordions = $scope.querySelectorAll('[data-module="govuk-accordion"]') - nodeListForEach($accordions, function ($accordion) { + $accordions.forEach(function ($accordion) { new Accordion($accordion, config.accordion).init() }) var $buttons = $scope.querySelectorAll('[data-module="govuk-button"]') - nodeListForEach($buttons, function ($button) { + $buttons.forEach(function ($button) { new Button($button, config.button).init() }) var $characterCounts = $scope.querySelectorAll('[data-module="govuk-character-count"]') - nodeListForEach($characterCounts, function ($characterCount) { + $characterCounts.forEach(function ($characterCount) { new CharacterCount($characterCount, config.characterCount).init() }) var $checkboxes = $scope.querySelectorAll('[data-module="govuk-checkboxes"]') - nodeListForEach($checkboxes, function ($checkbox) { + $checkboxes.forEach(function ($checkbox) { new Checkboxes($checkbox).init() }) var $details = $scope.querySelectorAll('[data-module="govuk-details"]') - nodeListForEach($details, function ($detail) { + $details.forEach(function ($detail) { new Details($detail).init() }) @@ -65,12 +64,12 @@ function initAll (config) { } var $notificationBanners = $scope.querySelectorAll('[data-module="govuk-notification-banner"]') - nodeListForEach($notificationBanners, function ($notificationBanner) { + $notificationBanners.forEach(function ($notificationBanner) { new NotificationBanner($notificationBanner, config.notificationBanner).init() }) var $radios = $scope.querySelectorAll('[data-module="govuk-radios"]') - nodeListForEach($radios, function ($radio) { + $radios.forEach(function ($radio) { new Radios($radio).init() }) @@ -81,7 +80,7 @@ function initAll (config) { } var $tabs = $scope.querySelectorAll('[data-module="govuk-tabs"]') - nodeListForEach($tabs, function ($tabs) { + $tabs.forEach(function ($tabs) { new Tabs($tabs).init() }) } diff --git a/src/govuk/common/index.mjs b/src/govuk/common/index.mjs index 3986c29381..cea3bb194e 100644 --- a/src/govuk/common/index.mjs +++ b/src/govuk/common/index.mjs @@ -8,26 +8,6 @@ * @module common/index */ -/** - * TODO: Ideally this would be a NodeList.prototype.forEach polyfill - * This seems to fail in IE8, requires more investigation. - * See: https://github.com/imagitama/nodelist-foreach-polyfill - * - * @deprecated Will be made private in v5.0 - * @template {Node} ElementType - * @param {NodeListOf} nodes - NodeList from querySelectorAll() - * @param {(value: ElementType, index: number, nodes: NodeListOf) => void} callback - Callback function to run for each node - * @returns {void} - */ -export function nodeListForEach (nodes, callback) { - if (window.NodeList.prototype.forEach) { - return nodes.forEach(callback) - } - for (var i = 0; i < nodes.length; i++) { - callback.call(window, nodes[i], i, nodes) - } -} - /** * Used to generate a unique string, allows multiple instances of the component * without them conflicting with each other. diff --git a/src/govuk/common/index.unit.test.mjs b/src/govuk/common/index.unit.test.mjs index bedbcfa5bd..2395929db5 100644 --- a/src/govuk/common/index.unit.test.mjs +++ b/src/govuk/common/index.unit.test.mjs @@ -1,6 +1,6 @@ import { mergeConfigs, extractConfigByNamespace } from './index.mjs' -// TODO: Write unit tests for `nodeListForEach` and `generateUniqueID` +// TODO: Write unit tests for `generateUniqueID` describe('Common JS utilities', () => { describe('mergeConfigs', () => { diff --git a/src/govuk/common/normalise-dataset.mjs b/src/govuk/common/normalise-dataset.mjs index f02effacbc..e172e62432 100644 --- a/src/govuk/common/normalise-dataset.mjs +++ b/src/govuk/common/normalise-dataset.mjs @@ -1,8 +1,3 @@ -/* eslint-disable es-x/no-string-prototype-trim -- Polyfill imported */ - -import '../vendor/polyfills/Element/prototype/dataset.mjs' -import '../vendor/polyfills/String/prototype/trim.mjs' - /** * Normalise string * diff --git a/src/govuk/components/accordion/accordion.mjs b/src/govuk/components/accordion/accordion.mjs index deb1ef2077..f9c849852d 100644 --- a/src/govuk/components/accordion/accordion.mjs +++ b/src/govuk/components/accordion/accordion.mjs @@ -1,14 +1,8 @@ -/* eslint-disable es-x/no-function-prototype-bind -- Polyfill imported */ -/* eslint-disable es-x/no-string-prototype-trim -- Polyfill imported */ - -import { nodeListForEach, mergeConfigs, extractConfigByNamespace } from '../../common/index.mjs' +import { mergeConfigs, extractConfigByNamespace } from '../../common/index.mjs' import { normaliseDataset } from '../../common/normalise-dataset.mjs' import { I18n } from '../../i18n.mjs' import '../../vendor/polyfills/Element/prototype/classList.mjs' import '../../vendor/polyfills/Element/prototype/closest.mjs' -import '../../vendor/polyfills/Event.mjs' // addEventListener, event.target normalization and DOMContentLoaded -import '../../vendor/polyfills/Function/prototype/bind.mjs' -import '../../vendor/polyfills/String/prototype/trim.mjs' /** * Accordion translation defaults @@ -212,7 +206,7 @@ Accordion.prototype.initSectionHeaders = function () { var $sections = this.$sections // Loop through sections - nodeListForEach($sections, function ($section, i) { + $sections.forEach(function ($section, i) { var $header = $section.querySelector('.' + $component.sectionHeaderClass) if (!$header) { return @@ -385,7 +379,7 @@ Accordion.prototype.onShowOrHideAllToggle = function () { var nowExpanded = !this.checkIfAllSectionsOpen() // Loop through sections - nodeListForEach($sections, function ($section) { + $sections.forEach(function ($section) { $component.setExpanded(nowExpanded, $section) // Store the state in sessionStorage when a change is triggered $component.storeState($section) diff --git a/src/govuk/components/button/button.mjs b/src/govuk/components/button/button.mjs index f0c8d09982..35c86c874c 100644 --- a/src/govuk/components/button/button.mjs +++ b/src/govuk/components/button/button.mjs @@ -1,9 +1,5 @@ -/* eslint-disable es-x/no-function-prototype-bind -- Polyfill imported */ - import { mergeConfigs } from '../../common/index.mjs' import { normaliseDataset } from '../../common/normalise-dataset.mjs' -import '../../vendor/polyfills/Event.mjs' // addEventListener, event.target normalization and DOMContentLoaded -import '../../vendor/polyfills/Function/prototype/bind.mjs' var KEY_SPACE = 32 var DEBOUNCE_TIMEOUT_IN_SECONDS = 1 diff --git a/src/govuk/components/character-count/character-count.mjs b/src/govuk/components/character-count/character-count.mjs index c4197185f6..cf195f1f2e 100644 --- a/src/govuk/components/character-count/character-count.mjs +++ b/src/govuk/components/character-count/character-count.mjs @@ -1,14 +1,8 @@ -/* eslint-disable es-x/no-date-now -- Polyfill imported */ -/* eslint-disable es-x/no-function-prototype-bind -- Polyfill imported */ - import { closestAttributeValue } from '../../common/closest-attribute-value.mjs' import { extractConfigByNamespace, mergeConfigs } from '../../common/index.mjs' import { normaliseDataset } from '../../common/normalise-dataset.mjs' import { I18n } from '../../i18n.mjs' -import '../../vendor/polyfills/Date/now.mjs' import '../../vendor/polyfills/Element/prototype/classList.mjs' -import '../../vendor/polyfills/Event.mjs' // addEventListener, event.target normalization and DOMContentLoaded -import '../../vendor/polyfills/Function/prototype/bind.mjs' /** * Character count translation defaults diff --git a/src/govuk/components/checkboxes/checkboxes.mjs b/src/govuk/components/checkboxes/checkboxes.mjs index f1895119aa..881f1e7d61 100644 --- a/src/govuk/components/checkboxes/checkboxes.mjs +++ b/src/govuk/components/checkboxes/checkboxes.mjs @@ -1,9 +1,4 @@ -/* eslint-disable es-x/no-function-prototype-bind -- Polyfill imported */ - -import { nodeListForEach } from '../../common/index.mjs' import '../../vendor/polyfills/Element/prototype/classList.mjs' -import '../../vendor/polyfills/Event.mjs' // addEventListener, event.target normalization and DOMContentLoaded -import '../../vendor/polyfills/Function/prototype/bind.mjs' /** * Checkboxes component @@ -52,7 +47,7 @@ Checkboxes.prototype.init = function () { var $module = this.$module var $inputs = this.$inputs - nodeListForEach($inputs, function ($input) { + $inputs.forEach(function ($input) { var targetId = $input.getAttribute('data-aria-controls') // Skip checkboxes without data-aria-controls attributes, or where the @@ -91,7 +86,7 @@ Checkboxes.prototype.init = function () { * @deprecated Will be made private in v5.0 */ Checkboxes.prototype.syncAllConditionalReveals = function () { - nodeListForEach(this.$inputs, this.syncConditionalRevealWithInputState.bind(this)) + this.$inputs.forEach(this.syncConditionalRevealWithInputState.bind(this)) } /** @@ -135,7 +130,7 @@ Checkboxes.prototype.unCheckAllInputsExcept = function ($input) { 'input[type="checkbox"][name="' + $input.name + '"]' ) - nodeListForEach(allInputsWithSameName, function ($inputWithSameName) { + allInputsWithSameName.forEach(function ($inputWithSameName) { var hasSameFormOwner = ($input.form === $inputWithSameName.form) if (hasSameFormOwner && $inputWithSameName !== $input) { $inputWithSameName.checked = false @@ -162,7 +157,7 @@ Checkboxes.prototype.unCheckExclusiveInputs = function ($input) { 'input[data-behaviour="exclusive"][type="checkbox"][name="' + $input.name + '"]' ) - nodeListForEach(allInputsWithSameNameAndExclusiveBehaviour, function ($exclusiveInput) { + allInputsWithSameNameAndExclusiveBehaviour.forEach(function ($exclusiveInput) { var hasSameFormOwner = ($input.form === $exclusiveInput.form) if (hasSameFormOwner) { $exclusiveInput.checked = false diff --git a/src/govuk/components/details/details.mjs b/src/govuk/components/details/details.mjs index 5bd0390fdc..bca51c127b 100644 --- a/src/govuk/components/details/details.mjs +++ b/src/govuk/components/details/details.mjs @@ -1,5 +1,3 @@ -/* eslint-disable es-x/no-function-prototype-bind -- Polyfill imported */ - /** * JavaScript 'polyfill' for HTML5's
and elements * and 'shim' to add accessiblity enhancements for all browsers @@ -7,8 +5,6 @@ * http://caniuse.com/#feat=details */ import { generateUniqueID } from '../../common/index.mjs' -import '../../vendor/polyfills/Event.mjs' // addEventListener, event.target normalization and DOMContentLoaded -import '../../vendor/polyfills/Function/prototype/bind.mjs' var KEY_ENTER = 13 var KEY_SPACE = 32 diff --git a/src/govuk/components/error-summary/error-summary.mjs b/src/govuk/components/error-summary/error-summary.mjs index a0b9b34882..2b0629d6e8 100644 --- a/src/govuk/components/error-summary/error-summary.mjs +++ b/src/govuk/components/error-summary/error-summary.mjs @@ -1,10 +1,6 @@ -/* eslint-disable es-x/no-function-prototype-bind -- Polyfill imported */ - import { mergeConfigs } from '../../common/index.mjs' import { normaliseDataset } from '../../common/normalise-dataset.mjs' import '../../vendor/polyfills/Element/prototype/closest.mjs' -import '../../vendor/polyfills/Event.mjs' // addEventListener, event.target normalization and DOMContentLoaded -import '../../vendor/polyfills/Function/prototype/bind.mjs' /** * JavaScript enhancements for the ErrorSummary diff --git a/src/govuk/components/header/header.mjs b/src/govuk/components/header/header.mjs index e0e8d4a2ba..b3ad44149d 100644 --- a/src/govuk/components/header/header.mjs +++ b/src/govuk/components/header/header.mjs @@ -1,8 +1,3 @@ -/* eslint-disable es-x/no-function-prototype-bind -- Polyfill imported */ - -import '../../vendor/polyfills/Event.mjs' -import '../../vendor/polyfills/Function/prototype/bind.mjs' - /** * Header component * diff --git a/src/govuk/components/notification-banner/notification-banner.mjs b/src/govuk/components/notification-banner/notification-banner.mjs index b82e9c3b66..c8822f5e84 100644 --- a/src/govuk/components/notification-banner/notification-banner.mjs +++ b/src/govuk/components/notification-banner/notification-banner.mjs @@ -1,6 +1,5 @@ import { mergeConfigs } from '../../common/index.mjs' import { normaliseDataset } from '../../common/normalise-dataset.mjs' -import '../../vendor/polyfills/Event.mjs' // addEventListener, event.target normalization and DOMContentLoaded /** * Notification Banner component diff --git a/src/govuk/components/radios/radios.mjs b/src/govuk/components/radios/radios.mjs index 9a48f20917..2712554ad7 100644 --- a/src/govuk/components/radios/radios.mjs +++ b/src/govuk/components/radios/radios.mjs @@ -1,9 +1,4 @@ -/* eslint-disable es-x/no-function-prototype-bind -- Polyfill imported */ - -import { nodeListForEach } from '../../common/index.mjs' import '../../vendor/polyfills/Element/prototype/classList.mjs' -import '../../vendor/polyfills/Event.mjs' // addEventListener, event.target normalization and DOMContentLoaded -import '../../vendor/polyfills/Function/prototype/bind.mjs' /** * Radios component @@ -52,7 +47,7 @@ Radios.prototype.init = function () { var $module = this.$module var $inputs = this.$inputs - nodeListForEach($inputs, function ($input) { + $inputs.forEach(function ($input) { var targetId = $input.getAttribute('data-aria-controls') // Skip radios without data-aria-controls attributes, or where the @@ -91,7 +86,7 @@ Radios.prototype.init = function () { * @deprecated Will be made private in v5.0 */ Radios.prototype.syncAllConditionalReveals = function () { - nodeListForEach(this.$inputs, this.syncConditionalRevealWithInputState.bind(this)) + this.$inputs.forEach(this.syncConditionalRevealWithInputState.bind(this)) } /** @@ -146,7 +141,7 @@ Radios.prototype.handleClick = function (event) { var $clickedInputForm = $clickedInput.form var $clickedInputName = $clickedInput.name - nodeListForEach($allInputs, function ($input) { + $allInputs.forEach(function ($input) { var hasSameFormOwner = $input.form === $clickedInputForm var hasSameName = $input.name === $clickedInputName diff --git a/src/govuk/components/skip-link/skip-link.mjs b/src/govuk/components/skip-link/skip-link.mjs index 5a767890b6..1a7f5bbfb5 100644 --- a/src/govuk/components/skip-link/skip-link.mjs +++ b/src/govuk/components/skip-link/skip-link.mjs @@ -1,8 +1,4 @@ -/* eslint-disable es-x/no-function-prototype-bind -- Polyfill imported */ - import '../../vendor/polyfills/Element/prototype/classList.mjs' -import '../../vendor/polyfills/Event.mjs' // addEventListener, event.target normalization and DOMContentLoaded -import '../../vendor/polyfills/Function/prototype/bind.mjs' /** * Skip link component diff --git a/src/govuk/components/tabs/tabs.mjs b/src/govuk/components/tabs/tabs.mjs index 9e4de19fa1..1417365a98 100644 --- a/src/govuk/components/tabs/tabs.mjs +++ b/src/govuk/components/tabs/tabs.mjs @@ -1,11 +1,4 @@ -/* eslint-disable es-x/no-function-prototype-bind -- Polyfill imported */ - -import { nodeListForEach } from '../../common/index.mjs' import '../../vendor/polyfills/Element/prototype/classList.mjs' -import '../../vendor/polyfills/Element/prototype/nextElementSibling.mjs' -import '../../vendor/polyfills/Element/prototype/previousElementSibling.mjs' -import '../../vendor/polyfills/Event.mjs' // addEventListener, event.target normalization and DOMContentLoaded -import '../../vendor/polyfills/Function/prototype/bind.mjs' /** * Tabs component @@ -110,11 +103,11 @@ Tabs.prototype.setup = function () { $tabList.setAttribute('role', 'tablist') - nodeListForEach($tabListItems, function ($item) { + $tabListItems.forEach(function ($item) { $item.setAttribute('role', 'presentation') }) - nodeListForEach($tabs, function ($tab) { + $tabs.forEach(function ($tab) { // Set HTML attributes $component.setAttributes($tab) @@ -156,11 +149,11 @@ Tabs.prototype.teardown = function () { $tabList.removeAttribute('role') - nodeListForEach($tabListItems, function ($item) { + $tabListItems.forEach(function ($item) { $item.removeAttribute('role') }) - nodeListForEach($tabs, function ($tab) { + $tabs.forEach(function ($tab) { // Remove events $tab.removeEventListener('click', $component.boundTabClick, true) $tab.removeEventListener('keydown', $component.boundTabKeydown, true) diff --git a/src/govuk/vendor/polyfills/Date/now.mjs b/src/govuk/vendor/polyfills/Date/now.mjs deleted file mode 100644 index f1c756b868..0000000000 --- a/src/govuk/vendor/polyfills/Date/now.mjs +++ /dev/null @@ -1,13 +0,0 @@ -(function (undefined) { - - // Detection from https://github.com/Financial-Times/polyfill-library/blob/v3.111.0/polyfills/Date/now/detect.js - var detect = ('Date' in self && 'now' in self.Date && 'getTime' in self.Date.prototype) - - if (detect) return - - // Polyfill from https://polyfill.io/v3/polyfill.js?version=3.111.0&features=Date.now&flags=always - Date.now = function () { - return new Date().getTime(); - }; - -}).call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {}); diff --git a/src/govuk/vendor/polyfills/Document.mjs b/src/govuk/vendor/polyfills/Document.mjs deleted file mode 100644 index 3ef2c2e186..0000000000 --- a/src/govuk/vendor/polyfills/Document.mjs +++ /dev/null @@ -1,27 +0,0 @@ -(function (undefined) { - -// Detection from https://github.com/Financial-Times/polyfill-service/blob/master/packages/polyfill-library/polyfills/Document/detect.js -var detect = ("Document" in this) - -if (detect) return - -// Polyfill from https://cdn.polyfill.io/v2/polyfill.js?features=Document&flags=always -// @ts-expect-error Ignore unknown globals from Web Workers API -if ((typeof WorkerGlobalScope === "undefined") && (typeof importScripts !== "function")) { - - if (this.HTMLDocument) { // IE8 - - // HTMLDocument is an extension of Document. If the browser has HTMLDocument but not Document, the former will suffice as an alias for the latter. - this.Document = this.HTMLDocument; - - } else { - - // 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. - this.Document = this.HTMLDocument = document.constructor = (new Function('return function Document() {}')()); - this.Document.prototype = document; - } -} - - -}) -.call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {}); diff --git a/src/govuk/vendor/polyfills/Element.mjs b/src/govuk/vendor/polyfills/Element.mjs deleted file mode 100644 index 043cccd5f0..0000000000 --- a/src/govuk/vendor/polyfills/Element.mjs +++ /dev/null @@ -1,119 +0,0 @@ -import './Document.mjs' - -(function(undefined) { - -// Detection from https://github.com/Financial-Times/polyfill-service/blob/master/packages/polyfill-library/polyfills/Element/detect.js -var detect = ('Element' in this && 'HTMLElement' in this) - -if (detect) return - -// Polyfill from https://cdn.polyfill.io/v2/polyfill.js?features=Element&flags=always -(function () { - - // IE8 - if (window.Element && !window.HTMLElement) { - // @ts-expect-error Ignore window global override - window.HTMLElement = window.Element; - return; - } - - // create Element constructor - window.Element = window.HTMLElement = new Function('return function Element() {}')(); - - // generate sandboxed iframe - var vbody = document.appendChild(document.createElement('body')); - var frame = vbody.appendChild(document.createElement('iframe')); - - // use sandboxed iframe to replicate Element functionality - var frameDocument = frame.contentWindow.document; - var prototype = Element.prototype = frameDocument.appendChild(frameDocument.createElement('*')); - var cache = {}; - - // polyfill Element.prototype on an element - var shiv = function (element, deep) { - var - childNodes = element.childNodes || [], - index = -1, - key, value, childNode; - - if (element.nodeType === 1 && element.constructor !== Element) { - element.constructor = Element; - - for (key in cache) { - value = cache[key]; - element[key] = value; - } - } - - while (childNode = deep && childNodes[++index]) { - shiv(childNode, deep); - } - - return element; - }; - - var elements = document.getElementsByTagName('*'); - var nativeCreateElement = document.createElement; - var interval; - var loopLimit = 100; - - // @ts-expect-error Ignore 'attachEvent()' for Internet Explorer - prototype.attachEvent('onpropertychange', function (event) { - var - propertyName = event.propertyName, - nonValue = !cache.hasOwnProperty(propertyName), - newValue = prototype[propertyName], - oldValue = cache[propertyName], - index = -1, - element; - - while (element = elements[++index]) { - if (element.nodeType === 1) { - if (nonValue || element[propertyName] === oldValue) { - element[propertyName] = newValue; - } - } - } - - cache[propertyName] = newValue; - }); - - prototype.constructor = Element; - - if (!prototype.hasAttribute) { - // .hasAttribute - prototype.hasAttribute = function hasAttribute(name) { - return this.getAttribute(name) !== null; - }; - } - - // Apply Element prototype to the pre-existing DOM as soon as the body element appears. - function bodyCheck() { - if (!(loopLimit--)) clearTimeout(interval); - // @ts-expect-error Ignore unknown 'prototype' on HTMLElement - if (document.body && !document.body.prototype && /(complete|interactive)/.test(document.readyState)) { - shiv(document, true); - // @ts-ignore - if (interval && document.body.prototype) clearTimeout(interval); - // @ts-ignore - return (!!document.body.prototype); - } - return false; - } - if (!bodyCheck()) { - document.onreadystatechange = bodyCheck; - interval = setInterval(bodyCheck, 25); - } - - // Apply to any new elements created after load - document.createElement = function createElement(nodeName) { - var element = nativeCreateElement(String(nodeName).toLowerCase()); - return shiv(element); - }; - - // remove sandboxed iframe - document.removeChild(vbody); -}()); - -}) -.call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {}); diff --git a/src/govuk/vendor/polyfills/Element/prototype/classList.mjs b/src/govuk/vendor/polyfills/Element/prototype/classList.mjs index 46e38521a2..37d5fd8650 100644 --- a/src/govuk/vendor/polyfills/Element/prototype/classList.mjs +++ b/src/govuk/vendor/polyfills/Element/prototype/classList.mjs @@ -1,6 +1,4 @@ -import '../../Object/defineProperty.mjs' import '../../DOMTokenList.mjs' -import '../../Element.mjs' (function(undefined) { diff --git a/src/govuk/vendor/polyfills/Element/prototype/dataset.mjs b/src/govuk/vendor/polyfills/Element/prototype/dataset.mjs deleted file mode 100644 index df323516e3..0000000000 --- a/src/govuk/vendor/polyfills/Element/prototype/dataset.mjs +++ /dev/null @@ -1,68 +0,0 @@ -import '../../Object/defineProperty.mjs' -import '../../Element.mjs' - -(function(undefined) { - - // Detection from https://raw.githubusercontent.com/Financial-Times/polyfill-library/13cf7c340974d128d557580b5e2dafcd1b1192d1/polyfills/Element/prototype/dataset/detect.js - var detect = (function(){ - if (!document.documentElement.dataset) { - return false; - } - var el = document.createElement('div'); - el.setAttribute("data-a-b", "c"); - return el.dataset && el.dataset.aB == "c"; - }()) - - if (detect) return - - // Polyfill derived from https://raw.githubusercontent.com/Financial-Times/polyfill-library/13cf7c340974d128d557580b5e2dafcd1b1192d1/polyfills/Element/prototype/dataset/polyfill.js - Object.defineProperty(Element.prototype, 'dataset', { - get: function() { - var element = this; - var attributes = this.attributes; - var map = {}; - - for (var i = 0; i < attributes.length; i++) { - var attribute = attributes[i]; - - // This regex has been edited from the original polyfill, to add - // support for period (.) separators in data-* attribute names. These - // are allowed in the HTML spec, but were not covered by the original - // polyfill's regex. We use periods in our i18n implementation. - if (attribute && attribute.name && (/^data-\w[.\w-]*$/).test(attribute.name)) { - var name = attribute.name; - var value = attribute.value; - - var propName = name.substr(5).replace(/-./g, function (prop) { - return prop.charAt(1).toUpperCase(); - }); - - // If this browser supports __defineGetter__ and __defineSetter__, - // continue using defineProperty. If not (like IE 8 and below), we use - // a hacky fallback which at least gives an object in the right format - if ('__defineGetter__' in Object.prototype && '__defineSetter__' in Object.prototype) { - Object.defineProperty(map, propName, { - enumerable: true, - get: function() { - return this.value; - }.bind({value: value || ''}), - set: function setter(name, value) { - if (typeof value !== 'undefined') { - this.setAttribute(name, value); - } else { - this.removeAttribute(name); - } - }.bind(element, name) - }); - } else { - map[propName] = value - } - - } - } - - return map; - } - }); - -}).call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {}); \ No newline at end of file diff --git a/src/govuk/vendor/polyfills/Element/prototype/nextElementSibling.mjs b/src/govuk/vendor/polyfills/Element/prototype/nextElementSibling.mjs deleted file mode 100644 index 993491da7a..0000000000 --- a/src/govuk/vendor/polyfills/Element/prototype/nextElementSibling.mjs +++ /dev/null @@ -1,22 +0,0 @@ -import '../../Object/defineProperty.mjs' -import '../../Element.mjs' - -(function(undefined) { - - // Detection from https://raw.githubusercontent.com/Financial-Times/polyfill-library/master/polyfills/Element/prototype/nextElementSibling/detect.js - var detect = ( - 'document' in this && "nextElementSibling" in document.documentElement - ) - - if (detect) return - - // Polyfill from https://raw.githubusercontent.com/Financial-Times/polyfill-library/master/polyfills/Element/prototype/nextElementSibling/polyfill.js - Object.defineProperty(Element.prototype, "nextElementSibling", { - get: function(){ - var el = this.nextSibling; - while (el && el.nodeType !== 1) { el = el.nextSibling; } - return el; - } - }); - -}).call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {}); diff --git a/src/govuk/vendor/polyfills/Element/prototype/previousElementSibling.mjs b/src/govuk/vendor/polyfills/Element/prototype/previousElementSibling.mjs deleted file mode 100644 index a7731bb14f..0000000000 --- a/src/govuk/vendor/polyfills/Element/prototype/previousElementSibling.mjs +++ /dev/null @@ -1,22 +0,0 @@ -import '../../Object/defineProperty.mjs' -import '../../Element.mjs' - -(function(undefined) { - - // Detection from https://raw.githubusercontent.com/Financial-Times/polyfill-library/master/polyfills/Element/prototype/previousElementSibling/detect.js - var detect = ( - 'document' in this && "previousElementSibling" in document.documentElement - ) - - if (detect) return - - // Polyfill from https://raw.githubusercontent.com/Financial-Times/polyfill-library/master/polyfills/Element/prototype/previousElementSibling/polyfill.js - Object.defineProperty(Element.prototype, 'previousElementSibling', { - get: function(){ - var el = this.previousSibling; - while (el && el.nodeType !== 1) { el = el.previousSibling; } - return el; - } - }); - -}).call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {}); diff --git a/src/govuk/vendor/polyfills/Event.mjs b/src/govuk/vendor/polyfills/Event.mjs deleted file mode 100644 index 79beaf06c3..0000000000 --- a/src/govuk/vendor/polyfills/Event.mjs +++ /dev/null @@ -1,276 +0,0 @@ -import './Window.mjs' -import './Element.mjs' -import './Object/defineProperty.mjs' - -(function(undefined) { - -// Detection from https://github.com/Financial-Times/polyfill-service/blob/master/packages/polyfill-library/polyfills/Event/detect.js -var detect = ( - (function(global) { - - if (!('Event' in global)) return false; - if (typeof global.Event === 'function') return true; - - try { - - // In IE 9-11, the Event object exists but cannot be instantiated - new Event('click'); - return true; - } catch(e) { - return false; - } - }(this)) -) - -if (detect) return - -// Polyfill from https://cdn.polyfill.io/v2/polyfill.js?features=Event&flags=always -(function () { - var unlistenableWindowEvents = { - click: 1, - dblclick: 1, - keyup: 1, - keypress: 1, - keydown: 1, - mousedown: 1, - mouseup: 1, - mousemove: 1, - mouseover: 1, - mouseenter: 1, - mouseleave: 1, - mouseout: 1, - storage: 1, - storagecommit: 1, - textinput: 1 - }; - - // This polyfill depends on availability of `document` so will not run in a worker - // However, we asssume there are no browsers with worker support that lack proper - // support for `Event` within the worker - if (typeof document === 'undefined' || typeof window === 'undefined') return; - - function indexOf(array, element) { - var - index = -1, - length = array.length; - - while (++index < length) { - if (index in array && array[index] === element) { - return index; - } - } - - return -1; - } - - var existingProto = (window.Event && window.Event.prototype) || null; - // @ts-expect-error Ignore window global override - window.Event = Window.prototype.Event = function Event(type, eventInitDict) { - if (!type) { - throw new Error('Not enough arguments'); - } - - var event; - // Shortcut if browser supports createEvent - if ('createEvent' in document) { - event = document.createEvent('Event'); - var bubbles = eventInitDict && eventInitDict.bubbles !== undefined ? eventInitDict.bubbles : false; - var cancelable = eventInitDict && eventInitDict.cancelable !== undefined ? eventInitDict.cancelable : false; - - event.initEvent(type, bubbles, cancelable); - - return event; - } - - // @ts-expect-error Ignore 'createEventObject()' for Internet Explorer - event = document.createEventObject(); - - event.type = type; - event.bubbles = eventInitDict && eventInitDict.bubbles !== undefined ? eventInitDict.bubbles : false; - event.cancelable = eventInitDict && eventInitDict.cancelable !== undefined ? eventInitDict.cancelable : false; - - return event; - }; - if (existingProto) { - Object.defineProperty(window.Event, 'prototype', { - configurable: false, - enumerable: false, - writable: true, - value: existingProto - }); - } - - if (!('createEvent' in document)) { - window.addEventListener = Window.prototype.addEventListener = Document.prototype.addEventListener = Element.prototype.addEventListener = function addEventListener() { - var - element = this, - type = arguments[0], - listener = arguments[1]; - - // @ts-expect-error Ignore mismatch between window types - if (element === window && type in unlistenableWindowEvents) { - 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.'); - } - - // @ts-expect-error Ignore unknown '_events' property on Element - if (!element._events) { - // @ts-ignore - element._events = {}; - } - - // @ts-expect-error Ignore unknown '_events' property on Element - if (!element._events[type]) { - // @ts-ignore - element._events[type] = function (event) { - var - // @ts-ignore - list = element._events[event.type].list, - events = list.slice(), - index = -1, - length = events.length, - eventElement; - - event.preventDefault = function preventDefault() { - if (event.cancelable !== false) { - event.returnValue = false; - } - }; - - event.stopPropagation = function stopPropagation() { - event.cancelBubble = true; - }; - - event.stopImmediatePropagation = function stopImmediatePropagation() { - event.cancelBubble = true; - event.cancelImmediate = true; - }; - - event.currentTarget = element; - event.relatedTarget = event.fromElement || null; - event.target = event.target || event.srcElement || element; - event.timeStamp = new Date().getTime(); - - if (event.clientX) { - event.pageX = event.clientX + document.documentElement.scrollLeft; - event.pageY = event.clientY + document.documentElement.scrollTop; - } - - while (++index < length && !event.cancelImmediate) { - if (index in events) { - eventElement = events[index]; - - if (indexOf(list, eventElement) !== -1 && typeof eventElement === 'function') { - eventElement.call(element, event); - } - } - } - }; - - // @ts-expect-error Ignore unknown '_events' property on Element - element._events[type].list = []; - - // @ts-expect-error Ignore 'attachEvent()' for Internet Explorer - if (element.attachEvent) { - // @ts-ignore - element.attachEvent('on' + type, element._events[type]); - } - } - - // @ts-expect-error Ignore unknown '_events' property on Element - element._events[type].list.push(listener); - }; - - window.removeEventListener = Window.prototype.removeEventListener = Document.prototype.removeEventListener = Element.prototype.removeEventListener = function removeEventListener() { - var - element = this, - type = arguments[0], - listener = arguments[1], - index; - - // @ts-expect-error Ignore unknown '_events' property on Element - if (element._events && element._events[type] && element._events[type].list) { - // @ts-ignore - index = indexOf(element._events[type].list, listener); - - if (index !== -1) { - // @ts-ignore - element._events[type].list.splice(index, 1); - - // @ts-ignore - if (!element._events[type].list.length) { - // @ts-ignore - if (element.detachEvent) { - // @ts-ignore - element.detachEvent('on' + type, element._events[type]); - } - // @ts-ignore - delete element._events[type]; - } - } - } - }; - - window.dispatchEvent = Window.prototype.dispatchEvent = Document.prototype.dispatchEvent = Element.prototype.dispatchEvent = function dispatchEvent(event) { - if (!arguments.length) { - throw new Error('Not enough arguments'); - } - - if (!event || typeof event.type !== 'string') { - throw new Error('DOM Events Exception 0'); - } - - var element = this, type = event.type; - - try { - if (!event.bubbles) { - event.cancelBubble = true; - - var cancelBubbleEvent = function (event) { - event.cancelBubble = true; - - // @ts-expect-error Ignore 'detachEvent()' for Internet Explorer - (element || window).detachEvent('on' + type, cancelBubbleEvent); - }; - - // @ts-expect-error Ignore 'attachEvent()' for Internet Explorer - this.attachEvent('on' + type, cancelBubbleEvent); - } - - // @ts-expect-error Ignore 'fireEvent()' for Internet Explorer - this.fireEvent('on' + type, event); - } catch (error) { - event.target = element; - - do { - event.currentTarget = element; - - if ('_events' in element && typeof element._events[type] === 'function') { - element._events[type].call(element, event); - } - - if (typeof element['on' + type] === 'function') { - element['on' + type].call(element, event); - } - - // @ts-expect-error Ignore unknown 'parentWindow' on Element - element = element.nodeType === 9 ? element.parentWindow : element.parentNode; - } while (element && !event.cancelBubble); - } - - return true; - }; - - // Add the DOMContentLoaded Event - // @ts-expect-error Ignore 'attachEvent()' for Internet Explorer - document.attachEvent('onreadystatechange', function() { - if (document.readyState === 'complete') { - document.dispatchEvent(new Event('DOMContentLoaded', { - bubbles: true - })); - } - }); - } -}()); - -}) -.call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {}); diff --git a/src/govuk/vendor/polyfills/Function/prototype/bind.mjs b/src/govuk/vendor/polyfills/Function/prototype/bind.mjs deleted file mode 100644 index 51530f6e1e..0000000000 --- a/src/govuk/vendor/polyfills/Function/prototype/bind.mjs +++ /dev/null @@ -1,159 +0,0 @@ -import '../../Object/defineProperty.mjs' - -(function(undefined) { - // Detection from https://github.com/Financial-Times/polyfill-service/blob/master/packages/polyfill-library/polyfills/Function/prototype/bind/detect.js - var detect = 'bind' in Function.prototype - - if (detect) return - - // Polyfill from https://cdn.polyfill.io/v2/polyfill.js?features=Function.prototype.bind&flags=always - Object.defineProperty(Function.prototype, 'bind', { - value: function bind(that) { // .length is 1 - // add necessary es5-shim utilities - var $Array = Array; - var $Object = Object; - var ObjectPrototype = $Object.prototype; - var ArrayPrototype = $Array.prototype; - var Empty = function Empty() {}; - var to_string = ObjectPrototype.toString; - var hasToStringTag = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol'; - 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; }; - var array_slice = ArrayPrototype.slice; - var array_concat = ArrayPrototype.concat; - var array_push = ArrayPrototype.push; - var max = Math.max; - // /add necessary es5-shim utilities - - // 1. Let Target be the this value. - var target = this; - // 2. If IsCallable(Target) is false, throw a TypeError exception. - if (!isCallable(target)) { - throw new TypeError('Function.prototype.bind called on incompatible ' + target); - } - // 3. Let A be a new (possibly empty) internal list of all of the - // argument values provided after thisArg (arg1, arg2 etc), in order. - // XXX slicedArgs will stand in for "A" if used - var args = array_slice.call(arguments, 1); // for normal call - // 4. Let F be a new native ECMAScript object. - // 11. Set the [[Prototype]] internal property of F to the standard - // built-in Function prototype object as specified in 15.3.3.1. - // 12. Set the [[Call]] internal property of F as described in - // 15.3.4.5.1. - // 13. Set the [[Construct]] internal property of F as described in - // 15.3.4.5.2. - // 14. Set the [[HasInstance]] internal property of F as described in - // 15.3.4.5.3. - var bound; - var binder = function () { - - if (this instanceof bound) { - // 15.3.4.5.2 [[Construct]] - // When the [[Construct]] internal method of a function object, - // F that was created using the bind function is called with a - // list of arguments ExtraArgs, the following steps are taken: - // 1. Let target be the value of F's [[TargetFunction]] - // internal property. - // 2. If target has no [[Construct]] internal method, a - // TypeError exception is thrown. - // 3. Let boundArgs be the value of F's [[BoundArgs]] internal - // property. - // 4. Let args be a new list containing the same values as the - // list boundArgs in the same order followed by the same - // values as the list ExtraArgs in the same order. - // 5. Return the result of calling the [[Construct]] internal - // method of target providing args as the arguments. - - var result = target.apply( - this, - array_concat.call(args, array_slice.call(arguments)) - ); - if ($Object(result) === result) { - return result; - } - return this; - - } else { - // 15.3.4.5.1 [[Call]] - // When the [[Call]] internal method of a function object, F, - // which was created using the bind function is called with a - // this value and a list of arguments ExtraArgs, the following - // steps are taken: - // 1. Let boundArgs be the value of F's [[BoundArgs]] internal - // property. - // 2. Let boundThis be the value of F's [[BoundThis]] internal - // property. - // 3. Let target be the value of F's [[TargetFunction]] internal - // property. - // 4. Let args be a new list containing the same values as the - // list boundArgs in the same order followed by the same - // values as the list ExtraArgs in the same order. - // 5. Return the result of calling the [[Call]] internal method - // of target providing boundThis as the this value and - // providing args as the arguments. - - // equiv: target.call(this, ...boundArgs, ...args) - return target.apply( - that, - array_concat.call(args, array_slice.call(arguments)) - ); - - } - - }; - - // 15. If the [[Class]] internal property of Target is "Function", then - // a. Let L be the length property of Target minus the length of A. - // b. Set the length own property of F to either 0 or L, whichever is - // larger. - // 16. Else set the length own property of F to 0. - - var boundLength = max(0, target.length - args.length); - - // 17. Set the attributes of the length own property of F to the values - // specified in 15.3.5.1. - var boundArgs = []; - for (var i = 0; i < boundLength; i++) { - array_push.call(boundArgs, '$' + i); - } - - // XXX Build a dynamic function with desired amount of arguments is the only - // way to set the length property of a function. - // In environments where Content Security Policies enabled (Chrome extensions, - // for ex.) all use of eval or Function costructor throws an exception. - // However in all of these environments Function.prototype.bind exists - // and so this code will never be executed. - bound = Function('binder', 'return function (' + boundArgs.join(',') + '){ return binder.apply(this, arguments); }')(binder); - - if (target.prototype) { - Empty.prototype = target.prototype; - bound.prototype = new Empty(); - // Clean up dangling references. - Empty.prototype = null; - } - - // TODO - // 18. Set the [[Extensible]] internal property of F to true. - - // TODO - // 19. Let thrower be the [[ThrowTypeError]] function Object (13.2.3). - // 20. Call the [[DefineOwnProperty]] internal method of F with - // arguments "caller", PropertyDescriptor {[[Get]]: thrower, [[Set]]: - // thrower, [[Enumerable]]: false, [[Configurable]]: false}, and - // false. - // 21. Call the [[DefineOwnProperty]] internal method of F with - // arguments "arguments", PropertyDescriptor {[[Get]]: thrower, - // [[Set]]: thrower, [[Enumerable]]: false, [[Configurable]]: false}, - // and false. - - // TODO - // NOTE Function objects created using Function.prototype.bind do not - // have a prototype property or the [[Code]], [[FormalParameters]], and - // [[Scope]] internal properties. - // XXX can't delete prototype in pure-js. - - // 22. Return F. - return bound; - } - }); -}) -.call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {}); diff --git a/src/govuk/vendor/polyfills/Object/defineProperty.mjs b/src/govuk/vendor/polyfills/Object/defineProperty.mjs deleted file mode 100644 index d83cd11550..0000000000 --- a/src/govuk/vendor/polyfills/Object/defineProperty.mjs +++ /dev/null @@ -1,88 +0,0 @@ -(function (undefined) { - -// Detection from https://github.com/Financial-Times/polyfill-service/blob/master/packages/polyfill-library/polyfills/Object/defineProperty/detect.js -var detect = ( - // In IE8, defineProperty could only act on DOM elements, so full support - // for the feature requires the ability to set a property on an arbitrary object - 'defineProperty' in Object && (function() { - try { - var a = {}; - Object.defineProperty(a, 'test', {value:42}); - return true; - } catch(e) { - return false - } - }()) -) - -if (detect) return - -// Polyfill from https://cdn.polyfill.io/v2/polyfill.js?features=Object.defineProperty&flags=always -(function (nativeDefineProperty) { - - var supportsAccessors = Object.prototype.hasOwnProperty('__defineGetter__'); - var ERR_ACCESSORS_NOT_SUPPORTED = 'Getters & setters cannot be defined on this javascript engine'; - var ERR_VALUE_ACCESSORS = 'A property cannot both have accessors and be writable or have a value'; - - Object.defineProperty = function defineProperty(object, property, descriptor) { - - // Where native support exists, assume it - if (nativeDefineProperty && (object === window || object === document || object === Element.prototype || object instanceof Element)) { - return nativeDefineProperty(object, property, descriptor); - } - - if (object === null || !(object instanceof Object || typeof object === 'object')) { - throw new TypeError('Object.defineProperty called on non-object'); - } - - if (!(descriptor instanceof Object)) { - throw new TypeError('Property description must be an object'); - } - - var propertyString = String(property); - var hasValueOrWritable = 'value' in descriptor || 'writable' in descriptor; - var getterType = 'get' in descriptor && typeof descriptor.get; - var setterType = 'set' in descriptor && typeof descriptor.set; - - // handle descriptor.get - if (getterType) { - if (getterType !== 'function') { - throw new TypeError('Getter must be a function'); - } - if (!supportsAccessors) { - throw new TypeError(ERR_ACCESSORS_NOT_SUPPORTED); - } - if (hasValueOrWritable) { - throw new TypeError(ERR_VALUE_ACCESSORS); - } - // @ts-expect-error Ignore unknown '__defineGetter__' on ObjectConstructor - Object.__defineGetter__.call(object, propertyString, descriptor.get); - } else { - object[propertyString] = descriptor.value; - } - - // handle descriptor.set - if (setterType) { - if (setterType !== 'function') { - throw new TypeError('Setter must be a function'); - } - if (!supportsAccessors) { - throw new TypeError(ERR_ACCESSORS_NOT_SUPPORTED); - } - if (hasValueOrWritable) { - throw new TypeError(ERR_VALUE_ACCESSORS); - } - // @ts-expect-error Ignore unknown '__defineSetter__' on ObjectConstructor - Object.__defineSetter__.call(object, propertyString, descriptor.set); - } - - // OK to define value unconditionally - if a getter has been specified as well, an error would be thrown above - if ('value' in descriptor) { - object[propertyString] = descriptor.value; - } - - return object; - }; -}(Object.defineProperty)); -}) -.call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {}); diff --git a/src/govuk/vendor/polyfills/String/prototype/trim.mjs b/src/govuk/vendor/polyfills/String/prototype/trim.mjs deleted file mode 100644 index aed3a6f03f..0000000000 --- a/src/govuk/vendor/polyfills/String/prototype/trim.mjs +++ /dev/null @@ -1,13 +0,0 @@ -(function (undefined) { - - // Detection from https://github.com/mdn/content/blob/cf607d68522cd35ee7670782d3ee3a361eaef2e4/files/en-us/web/javascript/reference/global_objects/string/trim/index.md#polyfill - var detect = ('trim' in String.prototype) - - if (detect) return - - // Polyfill from https://github.com/mdn/content/blob/cf607d68522cd35ee7670782d3ee3a361eaef2e4/files/en-us/web/javascript/reference/global_objects/string/trim/index.md#polyfill - String.prototype.trim = function () { - return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ''); - }; - -}).call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {}); diff --git a/src/govuk/vendor/polyfills/Window.mjs b/src/govuk/vendor/polyfills/Window.mjs deleted file mode 100644 index d9d3f492d5..0000000000 --- a/src/govuk/vendor/polyfills/Window.mjs +++ /dev/null @@ -1,21 +0,0 @@ -(function (undefined) { - -// Detection from https://github.com/Financial-Times/polyfill-service/blob/master/packages/polyfill-library/polyfills/Window/detect.js -var detect = ('Window' in this) - -if (detect) return - -// Polyfill from https://cdn.polyfill.io/v2/polyfill.js?features=Window&flags=always -// @ts-expect-error Ignore unknown globals from Web Workers API -if ((typeof WorkerGlobalScope === "undefined") && (typeof importScripts !== "function")) { - (function (global) { - if (global.constructor) { - global.Window = global.constructor; - } else { - (global.Window = global.constructor = new Function('return function Window() {}')()).prototype = this; - } - }(this)); -} - -}) -.call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});