Skip to content

Instantly share code, notes, and snippets.

@AutoSponge
Last active March 21, 2020 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AutoSponge/07265e4bc9eaa48c7424e46751fe2807 to your computer and use it in GitHub Desktop.
Save AutoSponge/07265e4bc9eaa48c7424e46751fe2807 to your computer and use it in GitHub Desktop.
list headings
$$('h1,h2,h3,h4,h5,h6,[role="heading"][aria-level]').map(el => [
`${el.tagName.replace(/\D/, '')}${el.getAttribute('aria-level') || ''}`,
el.textContent.trim(),
]);
(function (roles, tags) {
const cssRoles = roles.map(r => `[role="${r}"]`).join(',');
const cssTags = tags.join(',');
const landmarks = $$(`${cssRoles},${cssTags}`);
return landmarks.map(l => {
const role = l.getAttribute('role') || roles[tags.indexOf(l.tagName.toLowerCase())];
let label = l.getAttribute('aria-label') || '';
const labelledby = l.getAttribute('aria-labelledby');
const labels = labelledby ? labelledby.split(' ') : []
label += labels.flatMap(
ll => $$(`#${ll}`).map(el => el ? el.textContent.trim() : [])
).join(' ');
return [`${role}${ label ? ` (${label})` : ''}`, l];
})
}('banner,complementary,contentinfo,form,main,navigation,region,search'.split(','),
'header,aside,footer,form,main,nav,section'.split(',')));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment