-
-
Notifications
You must be signed in to change notification settings - Fork 629
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[484] Fix role-has-required-aria-props for semantic elements like inp…
…ut[checkbox]
- Loading branch information
1 parent
46e9abd
commit 56d3b9a
Showing
5 changed files
with
102 additions
and
4 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,30 @@ | ||
/* eslint-env mocha */ | ||
import expect from 'expect'; | ||
import isSemanticRoleElement from '../../../src/util/isSemanticRoleElement'; | ||
import JSXAttributeMock from '../../../__mocks__/JSXAttributeMock'; | ||
|
||
describe('isSemanticRoleElement', () => { | ||
it('should identify semantic role elements', () => { | ||
expect(isSemanticRoleElement('input', [ | ||
JSXAttributeMock('type', 'checkbox'), | ||
JSXAttributeMock('role', 'switch'), | ||
])).toBe(true); | ||
}); | ||
it('should reject non-semantic role elements', () => { | ||
expect(isSemanticRoleElement('input', [ | ||
JSXAttributeMock('type', 'radio'), | ||
JSXAttributeMock('role', 'switch'), | ||
])).toBe(false); | ||
expect(isSemanticRoleElement('input', [ | ||
JSXAttributeMock('type', 'text'), | ||
JSXAttributeMock('role', 'combobox'), | ||
])).toBe(false); | ||
expect(isSemanticRoleElement('button', [ | ||
JSXAttributeMock('role', 'switch'), | ||
JSXAttributeMock('aria-pressed', 'true'), | ||
])).toBe(false); | ||
expect(isSemanticRoleElement('input', [ | ||
JSXAttributeMock('role', 'switch'), | ||
])).toBe(false); | ||
}); | ||
}); |
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,59 @@ | ||
/** | ||
* @flow | ||
*/ | ||
|
||
import type { JSXAttribute } from 'ast-types-flow'; | ||
import { AXObjectRoles, elementAXObjects } from 'axobject-query'; | ||
import { getLiteralPropValue, getProp, propName } from 'jsx-ast-utils'; | ||
|
||
const isSemanticRoleElement = ( | ||
elementType: string, | ||
attributes: Array<JSXAttribute>, | ||
): boolean => { | ||
const roleAttr = getProp(attributes, 'role'); | ||
let res = false; | ||
const roleAttrValue = getLiteralPropValue(roleAttr); | ||
elementAXObjects.forEach((axObjects, concept) => { | ||
if (res) { | ||
return; | ||
} | ||
if ( | ||
concept.name === elementType | ||
&& (concept.attributes || []).every( | ||
cAttr => attributes.some( | ||
(attr) => { | ||
const namesMatch = cAttr.name === propName(attr); | ||
let valuesMatch; | ||
if (cAttr.value !== undefined) { | ||
valuesMatch = cAttr.value === getLiteralPropValue(attr); | ||
} | ||
if (!namesMatch) { | ||
return false; | ||
} | ||
return namesMatch && (valuesMatch !== undefined) ? valuesMatch : true; | ||
}, | ||
), | ||
) | ||
) { | ||
axObjects.forEach((name) => { | ||
if (res) { | ||
return; | ||
} | ||
const roles = AXObjectRoles.get(name); | ||
if (roles) { | ||
roles.forEach((role) => { | ||
if (res === true) { | ||
return; | ||
} | ||
if (role.name === roleAttrValue) { | ||
res = true; | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
return res; | ||
}; | ||
|
||
export default isSemanticRoleElement; |