npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

aria-api

v0.6.0

Published

Access ARIA information from JavaScript

Downloads

491

Readme

aria-api

WAI-ARIA allows websites to provide additional semantics to assistive technologies. Roles and attributes can be set either explicitly (e.g. <span role="link">click me</span>) or implicitly (<a href="//example.com">click me</a> implicitly has the role "link").

While the implicit mappings make authoring accessible websites simpler, it makes the task of calculating an element's role and attributes more complicated. This library takes care of exactly that.

Install

npm install aria-api

This installation method works best if you use tools like webpack or browserify. There is also an UMD build included as dist/aria.js.

Usage

var aria = require('aria-api'):

aria.querySelector('landmark').forEach(landmark => {
    if (!aria.matches(landmark, ':hidden')) {
        var role = aria.getRole(landmark);
        var name = aria.getName(landmark);
        console.log(role, name);
    }
});

getRole(element)

Calculate an element's role.

Note that this will return only the most specific role. If you want to know whether an element has a role, use matches() instead.

getAttribute(element, attribute)

Calculate the value of an element's attribute (state or property). The "aria-" prefix is not included in the attribute name.

getName(element)

Calculate an element's name according to the Accessible Name and Description Computation.

getDescription(element)

Calculate an element's description according to the Accessible Name and Description Computation.

matches(element, selector)

Similar to Element.matches(), this allows to check whether an element matches a selector. A selector can be any of the following:

  • role: Matches if the element has the specified role. This also works for hierarchical roles such as "landmark".
  • :attribute: Matches if the attribute is truthy. The "aria-" prefix is not included in the attribute name.
  • [attribute="value"]: Matches if the value of the attribute converted to string equals the specified value.

Note that combinations of selectors are not supported (e.g. main link, link:hidden, :not(:hidden)). The single exception to this rule are comma-separated lists of roles, e.g. link,button.

querySelector(element, selector)

Similar to Element.querySelector(). See matches() for details.

querySelectorAll(element, selector)

Similar to Element.querySelectorAll(). See matches() for details.

closest(element, selector)

Similar to Element.closest(). See matches() for details.

getParentNode(node)

Similar to Node.parentNode, but takes aria-owns into account.

getChildNodes(node)

Similar to Node.childNodes, but takes aria-owns into account.

What is this for?

First of all, I thought that something like this should exist. I currently use it for a11y-outline, a web extension that generates outlines based on WAI-ARIA roles.

That said, this is what I think it could also be used for:

  • Providing features based on the additional information provided by ARIA, e.g. landmark navigation.
  • Tools helping developers with improving accessibility.

Implemented standards

I try to update the code whenever a new version of these specs becomes a recommendation.

Notes

  • This is a pet project. I do not have the time to do extensive testing and may skip some details now and then. I am happy to receive bug reports and pull requests though.
  • The standards are still in a very rough state. Many things are unclear/undecided and therefore no browser really implements them. So naturally, this library cannot really implement the standards either.
  • This library does not do any validity checks. Invalid attributes or roles will not produce any warnings.
  • In order to calculate the "hidden" attribute, Window.getComputedStyle() is called. This only seems to return reliable values if the element is attached to document.
  • Due to security restrictions it is not generally possible to inspect the content of iframes, so they are ignored.

Related projects