-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2276 from judithhinlung/no-invalid-aria-attributes
- Loading branch information
Showing
8 changed files
with
581 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# no-invalid-aria-attributes | ||
|
||
ARIA attributes are used to provide an element with specific accessibility functions. An ARIA attribute is invalid if its name or values are either misspelled or do not currently exist in the [WAI-ARIA States and Properties spec](https://www.w3.org/WAI/PF/aria-1.1/states_and_properties). | ||
|
||
This rule disallows the use of invalid ARIA attributes. | ||
|
||
## Examples | ||
|
||
This rule **forbids** the following: | ||
|
||
```hbs | ||
<input type="text" aria-not-real="true" /> | ||
<div role="region" aria-live="bogus">Inaccessible live region</div>', | ||
<button type="submit" aria-invalid={{if this.foo "true" "woosh"}}>Submit</button> | ||
``` | ||
|
||
This rule **allows** the following: | ||
|
||
```hbs | ||
<input type="text" aria-required="true" /> | ||
<div role="region" aria-live="polite">Accessible live region</div>', | ||
<button type="submit" aria-invalid={{if this.hasNoSpellingErrors "false" "spelling"}}>Send now</button> | ||
``` | ||
|
||
## References | ||
|
||
- [Using ARIA, Roles, States, and Properties](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import { aria } from 'aria-query'; | ||
|
||
import Rule from './_base.js'; | ||
|
||
const VALID_ARIA_ATTRIBUTES = aria; | ||
|
||
function createInvalidAriaAttributeMessage(name) { | ||
return `${name} is an unrecognized ARIA attribute.`; | ||
} | ||
|
||
function createInvalidAttributeTypeErrorMessage(name, type, permittedValues) { | ||
switch (type) { | ||
case 'tristate': | ||
return `The value for ${name} must be a boolean or the string "mixed".`; | ||
case 'token': | ||
return `The value for ${name} must be a single token from the following: ${permittedValues.join( | ||
', ' | ||
)}.`; | ||
case 'tokenlist': | ||
return `The value for ${name} must be a list of one or more tokens from the following: ${permittedValues.join( | ||
', ' | ||
)}.`; | ||
case 'idlist': | ||
return `The value for ${name} must be a list of strings that represent DOM element IDs (idlist)`; | ||
case 'id': | ||
return `The value for ${name} must be a string that represents a DOM element ID`; | ||
case 'integer': | ||
return `The value for ${name} must be an integer.`; | ||
default: | ||
return `The value for ${name} must be a ${type}.`; | ||
} | ||
} | ||
|
||
function isBoolean(value) { | ||
return typeof value === 'boolean' || value === 'true' || value === 'false'; | ||
} | ||
|
||
function isNumeric(value) { | ||
if (typeof value === 'string') { | ||
value = Number.parseInt(value, 10); | ||
} | ||
return !isBoolean(value) && !Number.isNaN(value); | ||
} | ||
|
||
function validityCheck(expectedType, permittedValues, allowUndefined, value) { | ||
if (value === 'undefined') { | ||
return allowUndefined; | ||
} | ||
switch (expectedType) { | ||
case 'boolean': | ||
return isBoolean(value); | ||
case 'string': | ||
case 'id': | ||
return typeof value === 'string' && !isBoolean(value); | ||
case 'idlist': | ||
return ( | ||
typeof value === 'string' && | ||
value.split(' ').every((token) => validityCheck('id', [], false, token)) | ||
); | ||
case 'tristate': | ||
return isBoolean(value) || value === 'mixed'; | ||
case 'integer': | ||
case 'number': | ||
return isNumeric(value); | ||
case 'token': | ||
return typeof value === 'string' && permittedValues.includes(value); | ||
case 'tokenlist': | ||
return ( | ||
typeof value === 'string' && | ||
value.split(' ').every((token) => permittedValues.includes(token.toLowerCase())) | ||
); | ||
} | ||
} | ||
|
||
function getValuesFromMustache(mustacheNode) { | ||
let valuesList = []; | ||
if (['BooleanLiteral', 'NumberLiteral', 'StringLiteral'].includes(mustacheNode.path.type)) { | ||
valuesList.push(mustacheNode.path.original); | ||
} else if (mustacheNode.path.type === 'PathExpression') { | ||
if (mustacheNode.path.original === 'if' || mustacheNode.path.original === 'unless') { | ||
if (mustacheNode.params.length === 2 || mustacheNode.params.length === 3) { | ||
valuesList.push(mustacheNode.params[1].value); | ||
} | ||
if (mustacheNode.params.length === 3) { | ||
valuesList.push(mustacheNode.params[2].value); | ||
} | ||
} | ||
} | ||
return valuesList; | ||
} | ||
|
||
export default class NoInvalidAriaAttributes extends Rule { | ||
visitor() { | ||
return { | ||
ElementNode(node) { | ||
let foundAriaAttributes = []; | ||
for (const attribute of node.attributes) { | ||
if (attribute.name.startsWith('aria-')) { | ||
if (!VALID_ARIA_ATTRIBUTES.has(attribute.name)) { | ||
this.log({ | ||
message: createInvalidAriaAttributeMessage(attribute.name), | ||
node, | ||
}); | ||
return; | ||
} else { | ||
foundAriaAttributes.push(attribute); | ||
} | ||
} | ||
} | ||
for (let attribute of foundAriaAttributes) { | ||
let validAriaAttribute = VALID_ARIA_ATTRIBUTES.get(attribute.name); | ||
let expectedType = validAriaAttribute.type; | ||
let permittedValues = validAriaAttribute.values; | ||
let allowUndefined = validAriaAttribute.allowundefined || false; | ||
let isValidValue; | ||
if (attribute.value.type === 'MustacheStatement') { | ||
if (attribute.value.path) { | ||
let valuesList = getValuesFromMustache(attribute.value); | ||
if (valuesList.length === 0) { | ||
isValidValue = true; | ||
} else { | ||
for (let value of valuesList) { | ||
isValidValue = validityCheck( | ||
expectedType, | ||
permittedValues, | ||
allowUndefined, | ||
value | ||
); | ||
} | ||
} | ||
} | ||
} else { | ||
isValidValue = validityCheck( | ||
expectedType, | ||
permittedValues, | ||
allowUndefined, | ||
attribute.value.chars | ||
); | ||
} | ||
if (!isValidValue) { | ||
this.log({ | ||
message: createInvalidAttributeTypeErrorMessage( | ||
attribute.name, | ||
expectedType, | ||
permittedValues | ||
), | ||
node, | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.