aria-api

access ARIA information from JavaScript
git clone https://git.ce9e.org/aria-api.git

NameSize
.github/workflows/main.yml369B
.gitignore36B
CHANGES.md6586B
LICENSE1077B
Makefile575B
README.md6311B
dist/aria.js28761B
index.js260B
lib/atree.js2094B
lib/attrs.js3426B
lib/constants.js14386B
lib/name.js6381B
lib/query.js1290B
package.json717B
test/index.html744B
test/test-conflict.js1494B
test/test-hidden.js2989B
test/test-loop.js565B
test/test-name.js816B
test/test-role.js5024B
test/test-wpt.js7060B
test/wpt.js336070B
wpt.py2460B

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 rollup. 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:

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:

Implemented standards

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

Notes

Related projects