-
Notifications
You must be signed in to change notification settings - Fork 776
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add allowEmpty option for aria-valid-attr-value (#1154)
Allow specifying per ARIA attribute which ones are allowed to be empty. Changes include: - aria-haspopup: Now allowed to be empty - aria-invalid: Now allowed to be empty - aria-current: Now allowed to be empty - aria-valuetext: No longer allowed to be empty Closes #994 ## Reviewer checks **Required fields, to be filled out by PR reviewer(s)** - [x] Follows the commit message policy, appropriate for next version - [x] Has documentation updated, a DU ticket, or requires no documentation change - [x] Includes new tests, or was unnecessary - [x] Code is reviewed for security by: Jey
- Loading branch information
1 parent
bdff141
commit 89d18d0
Showing
9 changed files
with
578 additions
and
380 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
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,63 @@ | ||
/* global aria, dom */ | ||
|
||
/** | ||
* Validate the value of an ARIA attribute | ||
* @method validateAttrValue | ||
* @memberof axe.commons.aria | ||
* @instance | ||
* @param {HTMLElement} node The element to check | ||
* @param {String} attr The name of the attribute | ||
* @return {Boolean} | ||
*/ | ||
aria.validateAttrValue = function validateAttrValue(node, attr) { | ||
/*eslint complexity: ["error",17]*/ | ||
'use strict'; | ||
var matches, | ||
list, | ||
value = node.getAttribute(attr), | ||
attrInfo = aria.lookupTable.attributes[attr]; | ||
|
||
var doc = dom.getRootNode(node); | ||
|
||
if (!attrInfo) { | ||
return true; | ||
} | ||
if (attrInfo.allowEmpty && (!value || value.trim() === '')) { | ||
return true; | ||
} | ||
|
||
switch (attrInfo.type) { | ||
case 'boolean': | ||
case 'nmtoken': | ||
return ( | ||
typeof value === 'string' && | ||
attrInfo.values.includes(value.toLowerCase()) | ||
); | ||
|
||
case 'nmtokens': | ||
list = axe.utils.tokenList(value); | ||
// Check if any value isn't in the list of values | ||
return list.reduce(function(result, token) { | ||
return result && attrInfo.values.includes(token); | ||
// Initial state, fail if the list is empty | ||
}, list.length !== 0); | ||
|
||
case 'idref': | ||
return !!(value && doc.getElementById(value)); | ||
|
||
case 'idrefs': | ||
list = axe.utils.tokenList(value); | ||
return list.some(token => doc.getElementById(token)); | ||
|
||
case 'string': | ||
// Not allowed empty except with allowEmpty: true | ||
return value.trim() !== ''; | ||
|
||
case 'decimal': | ||
matches = value.match(/^[-+]?([0-9]*)\.?([0-9]*)$/); | ||
return !!(matches && (matches[1] || matches[2])); | ||
|
||
case 'int': | ||
return /^[-+]?[0-9]+$/.test(value); | ||
} | ||
}; |
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.