From 8b471cd2be9c1a51889233f3bf69e6b78a77f449 Mon Sep 17 00:00:00 2001 From: Wesley B <62723358+wesleyboar@users.noreply.github.com> Date: Mon, 8 Jun 2026 14:52:26 -0500 Subject: [PATCH] feat(generic): add promote-data-aria-attributes for WYSIWYG HTML Editors that strip aria-* on publish can keep data-aria-*; callers run this once on a scope root before scripts that depend on real ARIA attributes. Relates-to TACC/Core-Styles#645 Co-authored-by: Cursor --- .../js/promote-data-aria-attributes.js | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 generic_assets/js/promote-data-aria-attributes.js diff --git a/generic_assets/js/promote-data-aria-attributes.js b/generic_assets/js/promote-data-aria-attributes.js new file mode 100644 index 00000000..ace70b37 --- /dev/null +++ b/generic_assets/js/promote-data-aria-attributes.js @@ -0,0 +1,33 @@ +/** + * Copy `data-aria-*` attributes to matching `aria-*` on the same nodes. + * For HTML saved through WYSIWYG editors that strip unprefixed ARIA on publish. + * + * @param {ParentNode | null | undefined} [root=document] + */ +export default function promoteDataAriaAttributes(root = document) { + if (!root) { + return; + } + + const elements = + root instanceof Document + ? root.querySelectorAll('*') + : root instanceof Element + ? [ root, ...root.querySelectorAll('*') ] + : []; + + for (const el of elements) { + if (!(el instanceof Element)) { + continue; + } + for (const attr of [ ...el.attributes ]) { + if (!attr.name.startsWith('data-aria-')) { + continue; + } + el.setAttribute( + 'aria-' + attr.name.slice('data-aria-'.length), + attr.value + ); + } + } +}