Skip to content

Commit

Permalink
[fix] - Purely decorative emojis do not need descriptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
beefancohen committed Dec 30, 2018
1 parent 4186f91 commit 33a1f94
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions __tests__/src/rules/accessible-emoji-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ ruleTester.run('accessible-emoji', rule, {
{ code: '<span role="img" aria-labelledby="id1">&#9731;</span>' },
{ code: '<span role="img" aria-labelledby="id1" aria-label="Snowman">&#9731;</span>' },
{ code: '<span>{props.emoji}</span>' },
{ code: '<span aria-hidden>{props.emoji}</span>' },
{ code: '<span aria-hidden="true">🐼</span>' },
{ code: '<span aria-hidden>🐼</span>' },
{ code: '<div aria-hidden="true">🐼</div>' },
].map(parserOptionsMapper),
invalid: [
{ code: '<span>🐼</span>', errors: [expectedError] },
Expand All @@ -42,5 +46,6 @@ ruleTester.run('accessible-emoji', rule, {
{ code: '<i role="img" aria-label="Panda face">🐼</i>', errors: [expectedError] },
{ code: '<i role="img" aria-labelledby="id1">🐼</i>', errors: [expectedError] },
{ code: '<Foo>🐼</Foo>', errors: [expectedError] },
{ code: '<span aria-hidden="false">🐼</span>', errors: [expectedError] },
].map(parserOptionsMapper),
});
6 changes: 6 additions & 0 deletions src/rules/accessible-emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import emojiRegex from 'emoji-regex';
import { getProp, getLiteralPropValue, elementType } from 'jsx-ast-utils';
import { generateObjSchema } from '../util/schemas';
import isHiddenFromScreenReader from '../util/isHiddenFromScreenReader';

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

Expand All @@ -28,6 +29,11 @@ module.exports = {
const literalChildValue = node.parent.children.find(child => child.type === 'Literal' || child.type === 'JSXText');

if (literalChildValue && emojiRegex().test(literalChildValue.value)) {
const elementIsHidden = isHiddenFromScreenReader(elementType(node), node.attributes);
if (elementIsHidden) {
return; // emoji is decorative
}

const rolePropValue = getLiteralPropValue(getProp(node.attributes, 'role'));
const ariaLabelProp = getProp(node.attributes, 'aria-label');
const arialLabelledByProp = getProp(node.attributes, 'aria-labelledby');
Expand Down

0 comments on commit 33a1f94

Please sign in to comment.