Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(commons/aria): deprecate getRole({ noImplicit }) for getExplicitRole() #2294

Merged
merged 5 commits into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/checks/lists/dlitem-evaluate.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { getComposedParent } from '../../commons/dom';
import { getRole } from '../../commons/aria';
import { getExplicitRole } from '../../commons/aria';

function dlitemEvaluate(node) {
let parent = getComposedParent(node);
let parentTagName = parent.nodeName.toUpperCase();
let parentRole = getRole(parent, { noImplicit: true });
let parentRole = getExplicitRole(parent);

if (
parentTagName === 'DIV' &&
['presentation', 'none', null].includes(parentRole)
) {
parent = getComposedParent(parent);
parentTagName = parent.nodeName.toUpperCase();
parentRole = getRole(parent, { noImplicit: true });
parentRole = getExplicitRole(parent);
}

// Unlike with UL|OL+LI, DT|DD must be in a DL
Expand Down
4 changes: 2 additions & 2 deletions lib/checks/lists/only-dlitems-evaluate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isVisible } from '../../commons/dom';
import { getRole } from '../../commons/aria';
import { getRole, getExplicitRole } from '../../commons/aria';

function onlyDlitemsEvaluate(node, options, virtualNode) {
const ALLOWED_ROLES = ['definition', 'term', 'list'];
Expand All @@ -24,7 +24,7 @@ function onlyDlitemsEvaluate(node, options, virtualNode) {
const tagName = actualNode.nodeName.toUpperCase();

if (actualNode.nodeType === 1 && isVisible(actualNode, true, false)) {
const explicitRole = getRole(actualNode, { noImplicit: true });
const explicitRole = getExplicitRole(actualNode);

if ((tagName !== 'DT' && tagName !== 'DD') || explicitRole) {
if (!ALLOWED_ROLES.includes(explicitRole)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/checks/navigation/region-evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const implicitLandmarks = landmarkRoles
// Check if the current element is a landmark
function isRegion(virtualNode, options) {
const node = virtualNode.actualNode;
const explicitRole = aria.getRole(node, { noImplicit: true });
const explicitRole = aria.getExplicitRole(node);
const ariaLive = (node.getAttribute('aria-live') || '').toLowerCase().trim();

// Ignore content inside of aria-live
Expand Down
26 changes: 26 additions & 0 deletions lib/commons/aria/get-explicit-role.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import isValidRole from './is-valid-role';
import { getNodeFromTree, tokenList } from '../../core/utils';
import AbstractVirtuaNode from '../../core/base/virtual-node/abstract-virtual-node';

function getExplicitRole(vNode, { fallback, abstracts, dpub } = {}) {
vNode = vNode instanceof AbstractVirtuaNode ? vNode : getNodeFromTree(vNode);

if (vNode.props.nodeType !== 1) {
return null;
}

const roleAttr = (vNode.attr('role') || '').trim().toLowerCase();
const roleList = fallback ? tokenList(roleAttr) : [roleAttr];

// Get the first valid role:
const firstValidRole = roleList.find(role => {
if (!dpub && role.substr(0, 4) === 'doc-') {
return false;
}
return isValidRole(role, { allowAbstract: abstracts });
});

return firstValidRole || null;
}

export default getExplicitRole;
33 changes: 13 additions & 20 deletions lib/commons/aria/get-role.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,35 @@
import isValidRole from './is-valid-role';
import getExplicitRole from './get-explicit-role';
import getImplicitRole from './implicit-role';
import { getNodeFromTree } from '../../core/utils';
import AbstractVirtuaNode from '../../core/base/virtual-node/abstract-virtual-node';

/**
* Return the accessible role of an element
* Return the semantic role of an element
*
* @method getRole
* @memberof axe.commons.aria
* @instance
* @param {Element} node
* @param {Element|VirtualNode} node
* @param {Object} options
* @param {boolean} options.noImplicit Do not return the implicit role
* @param {boolean} options.noImplicit Do not return the implicit role // @deprecated
* @param {boolean} options.fallback Allow fallback roles
* @param {boolean} options.abstracts Allow role to be abstract
* @param {boolean} options.dpub Allow role to be any (valid) doc-* roles
* @returns {string|null} Role or null
*
* @deprecated noImplicit option is deprecated. Use aria.getExplicitRole instead.
*/
function getRole(node, { noImplicit, fallback, abstracts, dpub } = {}) {
node = node.actualNode || node;
if (node.nodeType !== 1) {
const vNode =
node instanceof AbstractVirtuaNode ? node : getNodeFromTree(node);
if (vNode.props.nodeType !== 1) {
return null;
}
const roleAttr = (node.getAttribute('role') || '').trim().toLowerCase();
// TODO: es-module-utils.tokenList
const roleList = fallback ? axe.utils.tokenList(roleAttr) : [roleAttr];

// Get the first valid role:
const validRoles = roleList.filter(role => {
if (!dpub && role.substr(0, 4) === 'doc-') {
return false;
}
return isValidRole(role, { allowAbstract: abstracts });
});

const explicitRole = validRoles[0];
const explicitRole = getExplicitRole(vNode, { fallback, abstracts, dpub });

// Get the implicit role, if permitted
if (!explicitRole && !noImplicit) {
return getImplicitRole(node);
return getImplicitRole(vNode);
}

return explicitRole || null;
Expand Down
9 changes: 6 additions & 3 deletions lib/commons/aria/implicit-role.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import lookupTable from './lookup-table';
import { getNodeFromTree } from '../../core/utils';
import AbstractVirtuaNode from '../../core/base/virtual-node/abstract-virtual-node';

/**
* Get the implicit role for a given node
* @method implicitRole
* @memberof axe.commons.aria
* @instance
* @param {HTMLElement} node The node to test
* @param {HTMLElement|VirtualNode} node The node to test
* @return {Mixed} Either the role or `null` if there is none
*/
function implicitRole(node) {
const vNode = getNodeFromTree(node);
const vNode =
node instanceof AbstractVirtuaNode ? node : getNodeFromTree(node);
node = vNode.actualNode;

// this error is only thrown if the virtual tree is not a
// complete tree, which only happens in linting and if a
Expand All @@ -24,7 +27,7 @@ function implicitRole(node) {

// until we have proper implicit role lookups for svgs we will
// avoid giving them one
if (node.namespaceURI === 'http://www.w3.org/2000/svg') {
if (node && node.namespaceURI === 'http://www.w3.org/2000/svg') {
return null;
}

Expand Down
1 change: 1 addition & 0 deletions lib/commons/aria/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export { default as allowedAttr } from './allowed-attr';
export { default as arialabelText } from './arialabel-text';
export { default as arialabelledbyText } from './arialabelledby-text';
export { default as getElementUnallowedRoles } from './get-element-unallowed-roles';
export { default as getExplicitRole } from './get-explicit-role';
export { default as getOwnedVirtual } from './get-owned-virtual';
export { default as getRoleType } from './get-role-type';
export { default as getRole } from './get-role';
Expand Down
4 changes: 2 additions & 2 deletions lib/commons/forms/is-aria-combobox.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getRole from '../aria/get-role';
import getExplicitRole from '../aria/get-explicit-role';

/**
* Determines if an element is an aria combobox element
Expand All @@ -8,7 +8,7 @@ import getRole from '../aria/get-role';
* @returns {Bool}
*/
function isAriaCombobox(node) {
const role = getRole(node, { noImplicit: true });
const role = getExplicitRole(node);
return role === 'combobox';
}

Expand Down
4 changes: 2 additions & 2 deletions lib/commons/forms/is-aria-listbox.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getRole from '../aria/get-role';
import getExplicitRole from '../aria/get-explicit-role';

/**
* Determines if an element is an aria listbox element
Expand All @@ -8,7 +8,7 @@ import getRole from '../aria/get-role';
* @returns {Bool}
*/
function isAriaListbox(node) {
const role = getRole(node, { noImplicit: true });
const role = getExplicitRole(node);
return role === 'listbox';
}

Expand Down
4 changes: 2 additions & 2 deletions lib/commons/forms/is-aria-range.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getRole from '../aria/get-role';
import getExplicitRole from '../aria/get-explicit-role';

const rangeRoles = ['progressbar', 'scrollbar', 'slider', 'spinbutton'];

Expand All @@ -10,7 +10,7 @@ const rangeRoles = ['progressbar', 'scrollbar', 'slider', 'spinbutton'];
* @returns {Bool}
*/
function isAriaRange(node) {
const role = getRole(node, { noImplicit: true });
const role = getExplicitRole(node);
return rangeRoles.includes(role);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/commons/forms/is-aria-textbox.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getRole from '../aria/get-role';
import getExplicitRole from '../aria/get-explicit-role';

/**
* Determines if an element is an aria textbox element
Expand All @@ -8,7 +8,7 @@ import getRole from '../aria/get-role';
* @returns {Bool}
*/
function isAriaTextbox(node) {
const role = getRole(node, { noImplicit: true });
const role = getExplicitRole(node);
return role === 'textbox';
}

Expand Down
4 changes: 2 additions & 2 deletions lib/commons/matches/explicit-role.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fromPrimative from './from-primative';
import getRole from '../aria/get-role';
import getExplicitRole from '../aria/get-explicit-role';

/**
* Check if a virtual node matches an explicit role(s)
Expand All @@ -18,7 +18,7 @@ import getRole from '../aria/get-role';
* @returns {Boolean}
*/
function explicitRole(vNode, matcher) {
return fromPrimative(getRole(vNode, { noImplicit: true }), matcher);
return fromPrimative(getExplicitRole(vNode), matcher);
}

export default explicitRole;
2 changes: 1 addition & 1 deletion lib/commons/matches/implicit-role.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import getImplicitRole from '../aria/implicit-role';
* @returns {Boolean}
*/
function implicitRole(vNode, matcher) {
return fromPrimative(getImplicitRole(vNode.actualNode), matcher);
return fromPrimative(getImplicitRole(vNode), matcher);
}

export default implicitRole;
2 changes: 1 addition & 1 deletion lib/commons/text/native-text-alternative.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function nativeTextAlternative(virtualNode, context = {}) {
const { actualNode } = virtualNode;
if (
actualNode.nodeType !== 1 ||
['presentation', 'none'].includes(getRole(actualNode))
['presentation', 'none'].includes(getRole(virtualNode))
) {
return '';
}
Expand Down
5 changes: 2 additions & 3 deletions lib/rules/aria-allowed-role-matches.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { getRole } from '../commons/aria';
import { getExplicitRole } from '../commons/aria';

function ariaAllowedRoleMatches(node) {
return (
getRole(node, {
noImplicit: true,
getExplicitRole(node, {
dpub: true,
fallback: true
}) !== null
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/aria-form-field-name-matches.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getRole } from '../commons/aria';
import { getExplicitRole } from '../commons/aria';
import { querySelectorAll } from '../core/utils';

function ariaFormFieldNameMatches(node, virtualNode) {
Expand All @@ -8,7 +8,7 @@ function ariaFormFieldNameMatches(node, virtualNode) {
* see relevant rule spec for details of 'role(s)' being filtered.
*/
const nodeName = node.nodeName.toUpperCase();
const role = getRole(node, { noImplicit: true });
const role = getExplicitRole(node);

/**
* Ignore elements from rule -> 'area-alt'
Expand Down
Loading