Skip to content

Commit b056134

Browse files
Ethan Cohenjessebeach
authored andcommitted
Add aria-labelledby as possible way to add description to emoji.
1 parent 37db5a3 commit b056134

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

__tests__/src/rules/accessible-emoji-test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const parserOptions = {
2424
const ruleTester = new RuleTester();
2525

2626
const expectedError = {
27-
message: 'Emojis should be wrapped in <span>, have role="img", and have aria-label="Description of emoji".',
27+
message: 'Emojis should be wrapped in <span>, have role="img", and have an accessible description with aria-label or aria-labelledby.',
2828
type: 'JSXOpeningElement',
2929
};
3030

@@ -35,13 +35,17 @@ ruleTester.run('accessible-emoji', rule, {
3535
{ code: '<span>No emoji here!</span>', parserOptions },
3636
{ code: '<span role="img" aria-label="Panda face">🐼</span>', parserOptions },
3737
{ code: '<span role="img" aria-label="Snowman">&#9731;</span>', parserOptions },
38+
{ code: '<span role="img" aria-labelledby="id1">🐼</span>', parserOptions },
39+
{ code: '<span role="img" aria-labelledby="id1">&#9731;</span>', parserOptions },
40+
{ code: '<span role="img" aria-labelledby="id1" aria-label="Snowman">&#9731;</span>', parserOptions },
3841
{ code: '<span>{props.emoji}</span>', parserOptions },
3942
],
4043
invalid: [
4144
{ code: '<span>🐼</span>', errors: [expectedError], parserOptions },
4245
{ code: '<span>foo🐼bar</span>', errors: [expectedError], parserOptions },
4346
{ code: '<span>foo 🐼 bar</span>', errors: [expectedError], parserOptions },
4447
{ code: '<i role="img" aria-label="Panda face">🐼</i>', errors: [expectedError], parserOptions },
48+
{ code: '<i role="img" aria-labelledby="id1">🐼</i>', errors: [expectedError], parserOptions },
4549
{ code: '<Foo>🐼</Foo>', errors: [expectedError], parserOptions },
4650
],
4751
});

docs/rules/accessible-emoji.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This rule takes no arguments.
1313
```jsx
1414
<span role="img" aria-label="Snowman">&#9731;</span>
1515
<span role="img" aria-label="Panda">🐼</span>
16+
<span role="img" aria-labelledby="panda1">🐼</span>
1617
```
1718

1819
### Fail

src/rules/accessible-emoji.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { getProp, getLiteralPropValue, elementType } from 'jsx-ast-utils';
1212
import { generateObjSchema } from '../util/schemas';
1313

1414
const errorMessage =
15-
'Emojis should be wrapped in <span>, have role="img", and have aria-label="Description of emoji".';
15+
'Emojis should be wrapped in <span>, have role="img", and have an accessible description with aria-label or aria-labelledby.';
1616

1717
const schema = generateObjSchema();
1818

@@ -34,9 +34,11 @@ module.exports = {
3434
if (literalChildValue && emojiRegex().test(literalChildValue.value)) {
3535
const rolePropValue = getLiteralPropValue(getProp(node.attributes, 'role'));
3636
const ariaLabelProp = getProp(node.attributes, 'aria-label');
37+
const arialLabelledByProp = getProp(node.attributes, 'aria-labelledby');
38+
const hasLabel = ariaLabelProp !== undefined || arialLabelledByProp !== undefined;
3739
const isSpan = elementType(node) === 'span';
3840

39-
if (ariaLabelProp === 'undefined' || rolePropValue !== 'img' || isSpan === false) {
41+
if (hasLabel === false || rolePropValue !== 'img' || isSpan === false) {
4042
context.report({
4143
node,
4244
message: errorMessage,

0 commit comments

Comments
 (0)