Skip to content

Add a11y-accessible-emoji #4411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
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
14 changes: 11 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"code-red": "0.1.1",
"codecov": "^3.5.0",
"css-tree": "1.0.0-alpha22",
"emoji-regex": "^8.0.0",
"eslint": "^6.3.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-svelte3": "^2.7.3",
Expand Down
32 changes: 32 additions & 0 deletions src/compiler/compile/nodes/Element.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import emojiRegex from 'emoji-regex';
import { is_void } from '../../utils/names';
import Node from './shared/Node';
import Attribute from './Attribute';
Expand Down Expand Up @@ -274,6 +275,7 @@ export default class Element extends Node {
this.validate_attributes();
this.validate_bindings();
this.validate_content();
this.validate_emoji();
this.validate_event_handlers();
}

Expand Down Expand Up @@ -671,6 +673,36 @@ export default class Element extends Node {
}
}

validate_emoji() {
if (this.children.length === 1) {
const child = this.children[0];
if (child.type === 'Text' && emojiRegex().test(child.data)) {

const isHidden = this.attributes.find(
(attribute: Attribute) => attribute.name === 'aria-hidden'
);
if (isHidden && (isHidden.is_true || isHidden.get_static_value() === "true")) {
return; // emoji is decorative
}

const hasLabel = this.attributes.find(
(attribute: Attribute) => attribute.name === 'aria-label' || attribute.name === 'aria-labelledby'
);
const role = this.attributes.find(
(attribute: Attribute) => attribute.name === 'role'
);
const isSpan = this.name === 'span';

if (!hasLabel || role.get_static_value() !== 'img' || isSpan === false) {
this.component.warn(this, {
code: `a11y-accessible-emoji`,
message: `A11y: Emojis should be wrapped in <span>, have role="img", and have an accessible description with aria-label or aria-labelledby. `
});
}
}
}
}

validate_event_handlers() {
const { component } = this;

Expand Down
18 changes: 18 additions & 0 deletions test/validator/samples/a11y-accessible-emoji/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div />
<span />
<span>No emoji here!</span>
<span role="img" aria-label="Panda face">🐼</span>
<span role="img" aria-label="Snowman">&#9731;</span>
<span role="img" aria-labelledby="id1">🐼</span>
<span role="img" aria-labelledby="id1">&#9731;</span>
<span role="img" aria-labelledby="id1" aria-label="Snowman">&#9731;</span>
<span aria-hidden="true">🐼</span>
<span aria-hidden>🐼</span>
<div aria-hidden="true">🐼</div>

<span>🐼</span>
<span>foo🐼bar</span>
<span>foo 🐼 bar</span>
<i role="img" aria-label="Panda face">🐼</i>
<i role="img" aria-labelledby="id1">🐼</i>
<span aria-hidden="false">🐼</span>
92 changes: 92 additions & 0 deletions test/validator/samples/a11y-accessible-emoji/warnings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
[
{
"code": "a11y-accessible-emoji",
"message": "A11y: Emojis should be wrapped in <span>, have role=\"img\", and have an accessible description with aria-label or aria-labelledby. ",
"start": {
"line": 13,
"column": 0,
"character": 424
},
"end": {
"line": 13,
"column": 15,
"character": 439
},
"pos": 424
},
{
"code": "a11y-accessible-emoji",
"message": "A11y: Emojis should be wrapped in <span>, have role=\"img\", and have an accessible description with aria-label or aria-labelledby. ",
"start": {
"line": 14,
"column": 0,
"character": 440
},
"end": {
"line": 14,
"column": 21,
"character": 461
},
"pos": 440
},
{
"code": "a11y-accessible-emoji",
"message": "A11y: Emojis should be wrapped in <span>, have role=\"img\", and have an accessible description with aria-label or aria-labelledby. ",
"start": {
"line": 15,
"column": 0,
"character": 462
},
"end": {
"line": 15,
"column": 23,
"character": 485
},
"pos": 462
},
{
"code": "a11y-accessible-emoji",
"message": "A11y: Emojis should be wrapped in <span>, have role=\"img\", and have an accessible description with aria-label or aria-labelledby. ",
"start": {
"line": 16,
"column": 0,
"character": 486
},
"end": {
"line": 16,
"column": 44,
"character": 530
},
"pos": 486
},
{
"code": "a11y-accessible-emoji",
"message": "A11y: Emojis should be wrapped in <span>, have role=\"img\", and have an accessible description with aria-label or aria-labelledby. ",
"start": {
"line": 17,
"column": 0,
"character": 531
},
"end": {
"line": 17,
"column": 42,
"character": 573
},
"pos": 531
},
{
"code": "a11y-accessible-emoji",
"message": "A11y: Emojis should be wrapped in <span>, have role=\"img\", and have an accessible description with aria-label or aria-labelledby. ",
"start": {
"line": 18,
"column": 0,
"character": 574
},
"end": {
"line": 18,
"column": 35,
"character": 609
},
"pos": 574
}
]