Skip to content

Commit

Permalink
change how aliases work
Browse files Browse the repository at this point in the history
  • Loading branch information
xi committed Jan 30, 2024
1 parent d07b2af commit e21f6d6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
9 changes: 7 additions & 2 deletions lib/attrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ var flatten = function(arr) {
return [].concat.apply([], arr);
};

var normalizeRoles = function(roles) {
return unique(roles.map(r => constants.aliases[r] || r));
};

// candidates can be passed for performance optimization
const getRole = function(el, candidates) {
if (el.hasAttribute('role')) {
const roles = el.getAttribute('role').toLowerCase().split(/\s+/);
let roles = el.getAttribute('role').toLowerCase().split(/\s+/);
roles = normalizeRoles(roles);
if (roles.length > 1 && candidates) {
return [roles, candidates];
}
Expand All @@ -35,7 +40,7 @@ const getRole = function(el, candidates) {
};

const hasRole = function(el, roles) {
const subRoles = roles.map(role => {
const subRoles = normalizeRoles(roles).map(role => {
return (constants.roles[role] || {}).subRoles || [role];
});
return !!getRole(el, unique(flatten(subRoles)));
Expand Down
15 changes: 7 additions & 8 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,9 @@ exports.roles = {
selectors: ['nav'],
childRoles: ['doc-index', 'doc-pagelist', 'doc-toc'],
},
none: {
selectors: ['img[alt=""]'],
},
note: {
childRoles: ['doc-notice', 'doc-tip'],
},
Expand All @@ -362,9 +365,6 @@ exports.roles = {
paragraph: {
selectors: ['p'],
},
presentation: {
selectors: ['img[alt=""]'],
},
progressbar: {
selectors: ['progress'],
defaults: {
Expand Down Expand Up @@ -512,7 +512,6 @@ exports.roles = {
'document',
'none',
'generic',
'presentation',
'range',
'rowgroup',
'section',
Expand Down Expand Up @@ -631,10 +630,10 @@ for (const role in exports.roles) {
}
}
}
exports.roles['none'] = exports.roles['none'] || {};
exports.roles['none'].subRoles = ['none', 'presentation'];
exports.roles['presentation'] = exports.roles['presentation'] || {};
exports.roles['presentation'].subRoles = ['presentation', 'none'];

exports.aliases = {
'presentation': 'none',
};

exports.nameFromDescendant = {
'figure': 'figcaption',
Expand Down
6 changes: 6 additions & 0 deletions test/test-role.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ describe('query', () => {
var actual = aria.getRole(testbed.children[0]);
expect(actual).toEqual('link');
});

it('does treat presentation as alias of none', () => {
testbed.innerHTML = '<a role="presentation">';
var actual = aria.getRole(testbed.children[0]);
expect(actual).toEqual('none');
});
});

describe('closest', () => {
Expand Down

0 comments on commit e21f6d6

Please sign in to comment.