Skip to content
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
67 changes: 67 additions & 0 deletions lib/rules/template-no-redundant-role.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,67 @@ function getSelectImplicitRole(node) {
return 'combobox';
}

const INPUT_TYPE_ROLES = new Map([
['button', 'button'],
['checkbox', 'checkbox'],
['email', 'textbox'],
['image', 'button'],
['number', 'spinbutton'],
['radio', 'radio'],
['range', 'slider'],
['reset', 'button'],
['search', 'searchbox'],
['submit', 'button'],
['tel', 'textbox'],
['text', 'textbox'],
['url', 'textbox'],
// Types with no implicit role.
['color', null],
['date', null],
['datetime-local', null],
['file', null],
['hidden', null],
['month', null],
['password', null],
['time', null],
['week', null],
]);

// Per HTML-AAM, the implicit role of <input> depends on its `type` (and, for
// text-like types, on the presence of a `list` attribute, which makes it a
// "combobox").
//
// Spec:
// - https://www.w3.org/TR/html-aam-1.0/#html-element-role-mappings (one row per input type)
// - https://www.w3.org/TR/html-aria/#el-input-text-list (a missing or invalid
// `type` is the text state; with `list` it is a combobox)
// - https://www.w3.org/TR/html-aria/#el-input-number (example of a type-specific
// row; the other types follow the same pattern)
//
// Returns the implicit role, or 'unknown' when a dynamic `type` or `list`
// blocks a decision. Types mapped to null above (e.g. "password") have no
// implicit role. Callers should skip flagging on 'unknown'.
function getInputImplicitRole(node) {
const attrs = node.attributes || [];
const typeAttr = attrs.find((a) => a.name === 'type');
let type = 'text';
if (typeAttr) {
if (typeAttr.value?.type !== 'GlimmerTextNode') {
return 'unknown';
}
type = typeAttr.value.chars.trim().toLowerCase();
}
// Missing or invalid `type` values fall back to the "text" state.
const role = INPUT_TYPE_ROLES.has(type) ? INPUT_TYPE_ROLES.get(type) : 'textbox';
if (role === 'textbox' || role === 'searchbox') {
const listAttr = attrs.find((a) => a.name === 'list');
if (listAttr) {
return listAttr.value && listAttr.value.type !== 'GlimmerTextNode' ? 'unknown' : 'combobox';
}
}
return role;
}

// Mapping of roles to their corresponding HTML elements
// From https://www.w3.org/TR/html-aria/
const ROLE_TO_ELEMENTS = {
Expand Down Expand Up @@ -212,6 +273,12 @@ module.exports = {
}
}

// <input>'s implicit role depends on its `type`; a role attribute is
// only redundant when it matches the computed implicit role.
if (node.tag === 'input' && getInputImplicitRole(node) !== roleValue) {
return;
}

const isRedundant =
elementsWithRole.includes(node.tag) &&
!ALLOWED_ELEMENT_ROLES.some((e) => e.name === node.tag && e.role === roleValue);
Expand Down
24 changes: 24 additions & 0 deletions tests/lib/rules/template-no-redundant-role.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ ruleTester.run('template-no-redundant-role', rule, {
options: [{ checkAllHTMLElements: false }],
},
'<template><input role="combobox"></template>',
// <input>'s implicit role depends on `type`; roles differing from it are not redundant.
'<template><input type="text" role="spinbutton" aria-valuenow="2" aria-valuemin="0" aria-valuemax="9" /></template>',
'<template><input type="text" role="searchbox" /></template>',
'<template><input type="number" role="slider" /></template>',
'<template><input role="checkbox" /></template>',
'<template><input type={{this.type}} role="spinbutton" /></template>',
'<template><input type="text" list="opts" role="textbox" /></template>',
// <select multiple> has implicit role listbox, so combobox is not redundant.
'<template><select role="combobox" multiple></select></template>',
// <select size="5"> (size > 1) has implicit role listbox.
Expand Down Expand Up @@ -209,6 +216,13 @@ hbsRuleTester.run('template-no-redundant-role', rule, {
'<ol role="list"></ol>',
'<ul role="list"></ul>',
'<input role="combobox">',
// <input>'s implicit role depends on `type`; roles differing from it are not redundant.
'<input type="text" role="spinbutton" aria-valuenow="2" aria-valuemin="0" aria-valuemax="9" />',
'<input type="text" role="searchbox" />',
'<input type="number" role="slider" />',
'<input role="checkbox" />',
'<input type={{this.type}} role="spinbutton" />',
'<input type="text" list="opts" role="textbox" />',
'<footer role={{this.bar}}></footer>',
'<nav class="navigation" role="navigation"></nav>',
'<button role="link"></button>',
Expand Down Expand Up @@ -315,6 +329,16 @@ hbsRuleTester.run('template-no-redundant-role', rule, {
output: '<input type="checkbox" name="agree" value="checkbox1" />',
errors: [{ message: 'Use of redundant or invalid role: checkbox on <input> detected.' }],
},
{
code: '<input type="number" role="spinbutton" />',
output: '<input type="number" />',
errors: [{ message: 'Use of redundant or invalid role: spinbutton on <input> detected.' }],
},
{
code: '<input role="textbox" />',
output: '<input />',
errors: [{ message: 'Use of redundant or invalid role: textbox on <input> detected.' }],
},
{
code: '<table><th role="columnheader">Some heading</th><td>cell1</td></table>',
output: '<table><th>Some heading</th><td>cell1</td></table>',
Expand Down
Loading