Skip to content
Closed
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
37 changes: 27 additions & 10 deletions lib/rules/template-no-invalid-aria-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,7 @@ function isNumeric(value) {
return !Number.isNaN(Number(value));
}

function isValidAriaValue(attrName, value) {
const attrDef = aria.get(attrName);
if (!attrDef) {
return true;
}

if (value === 'undefined') {
return Boolean(attrDef.allowundefined);
}

function validateByType(attrDef, value) {
switch (attrDef.type) {
case 'boolean': {
return isBoolean(value);
Expand Down Expand Up @@ -60,6 +51,32 @@ function isValidAriaValue(attrName, value) {
}
}

function isValidAriaValue(attrName, value) {
const attrDef = aria.get(attrName);
if (!attrDef) {
return true;
}

// Check whether 'undefined' is an explicit token in the attribute's value list
// (e.g. aria-orientation allows 'undefined' per aria-query) BEFORE applying the
// generic allowundefined shortcut. Otherwise valid tokens get rejected for
// attributes where allowundefined is false.
if (
value === 'undefined' &&
(attrDef.type === 'token' || attrDef.type === 'tokenlist') &&
Array.isArray(attrDef.values) &&
attrDef.values.includes('undefined')
) {
return true;
}

if (value === 'undefined') {
return Boolean(attrDef.allowundefined);
}

return validateByType(attrDef, value);
}

function getExpectedTypeDescription(attrName) {
const attrDef = aria.get(attrName);
if (!attrDef) {
Expand Down
14 changes: 14 additions & 0 deletions tests/lib/rules/template-no-invalid-aria-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ ruleTester.run('template-no-invalid-aria-attributes', rule, {
'<template><button type="submit" aria-disabled={{this.isDisabled}}>Submit</button></template>',
'<template><div role="textbox" aria-sort={{if this.hasCustomSort "other" "ascending"}}></div></template>',
'<template><div role="combobox" aria-expanded="undefined"></div></template>',
'<template><div role="slider" aria-orientation="undefined"></div></template>',
'<template><div role="slider" aria-orientation="horizontal"></div></template>',
'<template><button aria-label={{if @isNew (t "actions.add") (t "actions.edit")}}></button></template>',
],
invalid: [
Expand Down Expand Up @@ -121,6 +123,11 @@ ruleTester.run('template-no-invalid-aria-attributes', rule, {
output: null,
errors: [{ messageId: 'invalidAriaAttributeValue' }],
},
{
code: '<template><div role="slider" aria-orientation="sideways"></div></template>',
output: null,
errors: [{ messageId: 'invalidAriaAttributeValue' }],
},
],
});

Expand Down Expand Up @@ -150,6 +157,8 @@ hbsRuleTester.run('template-no-invalid-aria-attributes', rule, {
'<button type="submit" aria-disabled={{this.isDisabled}}>Submit</button>',
'<div role="textbox" aria-sort={{if this.hasCustomSort "other" "ascending"}}></div>',
'<div role="combobox" aria-expanded="undefined"></div>',
'<div role="slider" aria-orientation="undefined"></div>',
'<div role="slider" aria-orientation="horizontal"></div>',
'<button aria-label={{if @isNew (t "actions.add") (t "actions.edit")}}></button>',
],
invalid: [
Expand Down Expand Up @@ -223,5 +232,10 @@ hbsRuleTester.run('template-no-invalid-aria-attributes', rule, {
output: null,
errors: [{ messageId: 'invalidAriaAttributeValue' }],
},
{
code: '<div role="slider" aria-orientation="sideways"></div>',
output: null,
errors: [{ messageId: 'invalidAriaAttributeValue' }],
},
],
});
Loading