Skip to content

Add accessible-emoji a11y check #4805

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
32 changes: 32 additions & 0 deletions src/compiler/compile/nodes/Text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default class Text extends Node {
super(component, parent, scope, info);
this.data = info.data;
this.synthetic = info.synthetic || false;
this.validate();
}

should_skip() {
Expand All @@ -42,4 +43,35 @@ export default class Text extends Node {

return parent_element.namespace || elements_without_text.has(parent_element.name);
}

validate() {
const { data, component, parent } = this;

// https://github.com/mathiasbynens/emoji-regex/blob/master/src/index.js
const emoji_regex =/<% emojiSequence %>|\p{Emoji_Presentation}|\p{Emoji}\uFE0F|\p{Emoji_Modifier_Base}/gu;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though it's now EOL, Node 8 is still officially supported by Svelte 3, which means we can't use regex features like this - and this is failing CI which is running on Node 8, 10, 12, and 14.

There's also a previous PR #4411 for this that's been languishing for a little bit, but which is compatible with Node 8.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops - completely forgot to check for existing PRs. In that case I'll close this one. Thanks :)


if (!emoji_regex.test(data)) return;

let is_accessible = false;

if (parent.type === 'Element' && parent.name === 'span') {
const { attributes } = parent;
const role = parent.get_static_attribute_value('role');
const aria_label_index = attributes.findIndex(attr =>
attr.type === 'Attribute' &&
(attr.name === 'aria-label' || attr.name === 'aria-labelledby')
);

if (role === 'img' && aria_label_index > -1) {
is_accessible = true;
}
}

if (!is_accessible) {
component.warn(this, {
code: 'a11y-accessible-emoji',
message: 'A11y: emojis should be wrapped in a <span>, with role="img", and have a description using aria-label or aria-labelledby'
});
}
}
}
5 changes: 5 additions & 0 deletions test/validator/samples/a11y-accessible-emoji/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<span>🐼</span>
<i role="img" aria-label="Panda">🐼</i>

<!-- this one won't have a warning -->
<span role="img" aria-label="Panda">🐼</span>
33 changes: 33 additions & 0 deletions test/validator/samples/a11y-accessible-emoji/warnings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[
{
"code": "a11y-accessible-emoji",
"message": "A11y: emojis should be wrapped in a <span>, with role=\"img\", and have a description using aria-label or aria-labelledby",
"start": {
"line": 1,
"column": 6,
"character": 6
},
"end": {
"line": 1,
"column": 8,
"character": 8
},
"pos": 6
},

{
"code": "a11y-accessible-emoji",
"message": "A11y: emojis should be wrapped in a <span>, with role=\"img\", and have a description using aria-label or aria-labelledby",
"start": {
"line": 2,
"column": 33,
"character": 49
},
"end": {
"line": 2,
"column": 35,
"character": 51
},
"pos": 49
}
]