From 8f60cfb0a6e55eb0bd50b139307622dc04116bb2 Mon Sep 17 00:00:00 2001 From: Andrew Hosgood Date: Thu, 2 Jan 2025 17:30:01 +0000 Subject: [PATCH] Use dataset rather than getAttribute("data-...") --- CHANGELOG.md | 6 +++++ src/nationalarchives/all.mjs | 12 +++++----- src/nationalarchives/analytics.mjs | 2 +- .../cookie-banner/cookie-banner.mjs | 14 +++++------- .../components/cookie-banner/fixtures.json | 22 +++++++++---------- .../components/cookie-banner/template.njk | 2 +- .../error-summary/error-summary.mjs | 2 +- .../components/error-summary/fixtures.json | 2 +- .../components/error-summary/template.njk | 2 +- .../components/footer/analytics.js | 2 +- .../components/picture/fixtures.json | 2 +- .../components/picture/picture.mjs | 6 ++--- .../components/picture/picture.stories.js | 4 ++-- .../components/picture/template.njk | 2 +- src/nationalarchives/lib/cookies.mjs | 6 ++--- src/nationalarchives/templates/fixtures.json | 10 ++++----- 16 files changed, 48 insertions(+), 48 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d15cd6a7..ed0499a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - More tracking added to accordions, breadcrumbs, footers, galleries, headers and sidebars ### Changed + +- Optional `data-tnacookiesdomain` and `data-tnacookiespath` attributes on the `` element are now `data-tna-cookies-domain` and `data-tna-cookies-path` +- The `data-preferenceskey` and `data-policieskey` attributes on the cookies component are now `data-preferences-key` and `data-policies-key` +- The `data-disableautofocus` attribute on the error summary component is now `data-disable-autofocus` +- The `data-informationlabelopen` and `data-informationlabelclose` attributes on the picture component are now `data-information-label-open` and `data-information-label-close` + ### Deprecated ### Removed ### Fixed diff --git a/src/nationalarchives/all.mjs b/src/nationalarchives/all.mjs index f8c073fc..6bbc1d09 100644 --- a/src/nationalarchives/all.mjs +++ b/src/nationalarchives/all.mjs @@ -129,7 +129,7 @@ const initAll = (options) => { .querySelectorAll(".tna-details__details:not([open])") .forEach((e) => { e.setAttribute("open", ""); - e.dataset.wasclosed = ""; + e.dataset.wasClosed = ""; }); document .querySelectorAll( @@ -137,22 +137,22 @@ const initAll = (options) => { ) .forEach((e) => { e.removeAttribute("hidden"); - e.dataset.wasclosed = ""; + e.dataset.wasClosed = ""; }); } else { document - .querySelectorAll(".tna-details__details[data-wasclosed]") + .querySelectorAll(".tna-details__details[data-was-closed]") .forEach((e) => { e.removeAttribute("open"); - delete e.dataset.wasclosed; + delete e.dataset.wasClosed; }); document .querySelectorAll( - ".tna-accordion__content[data-wasclosed], .tna-picture__transcript[data-wasclosed]", + ".tna-accordion__content[data-was-closed], .tna-picture__transcript[data-was-closed]", ) .forEach((e) => { e.setAttribute("hidden", "until-closed"); - e.dataset.wasclosed = ""; + e.dataset.wasClosed = ""; }); } }); diff --git a/src/nationalarchives/analytics.mjs b/src/nationalarchives/analytics.mjs index 39c2dcd9..8a436e52 100644 --- a/src/nationalarchives/analytics.mjs +++ b/src/nationalarchives/analytics.mjs @@ -402,7 +402,7 @@ class GA4 extends EventTracker { } } -const ga4Id = document.documentElement.getAttribute("data-ga4"); +const ga4Id = document.documentElement.dataset.ga4; if (ga4Id) { new GA4({ id: ga4Id }); } diff --git a/src/nationalarchives/components/cookie-banner/cookie-banner.mjs b/src/nationalarchives/components/cookie-banner/cookie-banner.mjs index 36c2596c..eade7e9c 100644 --- a/src/nationalarchives/components/cookie-banner/cookie-banner.mjs +++ b/src/nationalarchives/components/cookie-banner/cookie-banner.mjs @@ -27,16 +27,15 @@ export class CookieBanner { return; } - const policies = this.$module.getAttribute("data-policies") || ""; + const policies = this.$module.dataset.policies || ""; const extraPolicies = policies .split(",") .filter((x) => x) .map((policy) => policy.trim()); - const domain = this.$module.getAttribute("data-domain") || undefined; - const path = this.$module.getAttribute("data-path") || undefined; - const secure = this.$module.getAttribute("data-secure") || undefined; - const policiesKey = - this.$module.getAttribute("data-policieskey") || undefined; + const domain = this.$module.dataset.domain || undefined; + const path = this.$module.dataset.path || undefined; + const secure = this.$module.dataset.secure || undefined; + const policiesKey = this.$module.dataset.policiesKey || undefined; this.cookies = new Cookies({ extraPolicies, @@ -48,8 +47,7 @@ export class CookieBanner { }); this.cookiePreferencesSet = - this.$module.getAttribute("data-preferenceskey") || - "cookie_preferences_set"; + this.$module.dataset.preferencesKey || "cookie_preferences_set"; const cookiePreferencesSet = this.cookies.hasValue( this.cookiePreferencesSet, "true", diff --git a/src/nationalarchives/components/cookie-banner/fixtures.json b/src/nationalarchives/components/cookie-banner/fixtures.json index 6cbd299a..2e078e52 100644 --- a/src/nationalarchives/components/cookie-banner/fixtures.json +++ b/src/nationalarchives/components/cookie-banner/fixtures.json @@ -7,7 +7,7 @@ "serviceName": "My service", "cookiesUrl": "/cookies" }, - "html": "" + "html": "" }, { "name": "custom preferences set key", @@ -16,7 +16,7 @@ "cookiesUrl": "/cookies", "cookiesPreferencesSetKey": "custom" }, - "html": "" + "html": "" }, { "name": "custom policies", @@ -25,7 +25,7 @@ "cookiesUrl": "/cookies", "policies": "custom" }, - "html": "" + "html": "" }, { "name": "custom policies key", @@ -34,7 +34,7 @@ "cookiesUrl": "/cookies", "policiesKey": "custom_key" }, - "html": "" + "html": "" }, { "name": "with domain", @@ -43,7 +43,7 @@ "cookiesUrl": "/cookies", "cookiesDomain": "nationalarchives.gov.uk" }, - "html": "" + "html": "" }, { "name": "with path", @@ -52,7 +52,7 @@ "cookiesUrl": "/cookies", "cookiesPath": "/my-service" }, - "html": "" + "html": "" }, { "name": "with preferences set key", @@ -61,7 +61,7 @@ "cookiesUrl": "/cookies", "preferencesSetKey": "custom_preferences_set_key" }, - "html": "" + "html": "" }, { "name": "insecure", @@ -70,7 +70,7 @@ "cookiesUrl": "/cookies", "allowInsecure": true }, - "html": "" + "html": "" }, { "name": "custom content", @@ -82,7 +82,7 @@ "acceptedBody": "

Lorem ipsum 2

", "rejectedBody": "

Lorem ipsum 3

" }, - "html": "" + "html": "" }, { "name": "with classes", @@ -91,7 +91,7 @@ "cookiesUrl": "/cookies", "classes": "tna-cookie-banner--fixture" }, - "html": "" + "html": "" }, { "name": "with attributes", @@ -102,7 +102,7 @@ "data-fixturetest": "pass" } }, - "html": "" + "html": "" } ] } diff --git a/src/nationalarchives/components/cookie-banner/template.njk b/src/nationalarchives/components/cookie-banner/template.njk index 5a505ec6..3ffd92d3 100644 --- a/src/nationalarchives/components/cookie-banner/template.njk +++ b/src/nationalarchives/components/cookie-banner/template.njk @@ -9,7 +9,7 @@ {%- set containerClasses = containerClasses.concat('tna-background-tint') -%} {%- endif -%} {%- set classes = containerClasses | join(' ') -%} -