Skip to content
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

Rule a11y img-redundant-alt #4750

Merged
merged 10 commits into from
May 3, 2020
Merged
Show file tree
Hide file tree
Changes from 7 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
54 changes: 42 additions & 12 deletions src/compiler/compile/nodes/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ export default class Element extends Node {
}

this.validate_attributes();
this.validate_special_cases();
this.validate_bindings();
this.validate_content();
this.validate_event_handlers();
Expand Down Expand Up @@ -420,8 +421,16 @@ export default class Element extends Node {

attribute_map.set(attribute.name, attribute);
});
}

validate_special_cases() {
const { component,attributes } = this;
const attribute_map = new Map();

attributes.forEach(attribute => (
attribute_map.set(attribute.name, attribute)
));

// handle special cases
if (this.name === 'a') {
const href_attribute = attribute_map.get('href') || attribute_map.get('xlink:href');
const id_attribute = attribute_map.get('id');
Expand All @@ -447,9 +456,7 @@ export default class Element extends Node {
});
}
}
}

else {
} else {
const required_attributes = a11y_required_attributes[this.name];
if (required_attributes) {
const has_attribute = required_attributes.some(name => attribute_map.has(name));
Expand All @@ -458,16 +465,39 @@ export default class Element extends Node {
should_have_attribute(this, required_attributes);
}
}
}

if (this.name === 'input') {
const type = attribute_map.get('type');
if (type && type.get_static_value() === 'image') {
const required_attributes = ['alt', 'aria-label', 'aria-labelledby'];
const has_attribute = required_attributes.some(name => attribute_map.has(name));
if (this.name === 'input') {
const type = attribute_map.get('type');
if (type && type.get_static_value() === 'image') {
const required_attributes = ['alt', 'aria-label', 'aria-labelledby'];
const has_attribute = required_attributes.some(name => attribute_map.has(name));

if (!has_attribute) {
should_have_attribute(this, required_attributes, 'input type="image"');
}
if (!has_attribute) {
should_have_attribute(this, required_attributes, 'input type="image"');
}
}
}

if (this.name === 'img') {
const alt_attribute = attribute_map.get('alt');
const aria_hidden_attribute = attribute_map.get('aria-hidden');

const aria_hidden_exist = aria_hidden_attribute && aria_hidden_attribute.get_static_value();

if (alt_attribute && !aria_hidden_exist) {
const invalid_words = ['image', 'photo', 'picture'];
const alt_value = alt_attribute.get_static_value();

const contain_invalid_word = invalid_words.some(word =>
alt_value.toLowerCase().includes(word)
andrelmlins marked this conversation as resolved.
Show resolved Hide resolved
);

if (contain_invalid_word) {
component.warn(this, {
code: `a11y-img-redundant-alt`,
message: `A11y: Screenreaders already announce <img> elements as an image.`
});
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
</script>

<figure>
<img src='foo.jpg' alt='a picture of a foo'>
<img src='foo.jpg' alt='a foo'>
{#if caption}
<figcaption>{caption}</figcaption>
{/if}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<figure>
<img src='foo.jpg' alt='a picture of a foo'>
<img src='foo.jpg' alt='a foo'>

<figcaption>
a foo in its natural habitat
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<figure>
<img src='foo.jpg' alt='a picture of a foo'>
<img src='foo.jpg' alt='a foo'>

<figcaption>
a foo in its natural habitat
Expand All @@ -9,7 +9,7 @@
</figure>

<figure>
<img src='foo.jpg' alt='a picture of a foo'>
<img src='foo.jpg' alt='a foo'>

<div class='markup-for-styling'>
<figcaption>
Expand Down
12 changes: 6 additions & 6 deletions test/validator/samples/a11y-figcaption-wrong-place/warnings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@
"start": {
"line": 4,
"column": 1,
"character": 57
"character": 44
},
"end": {
"line": 6,
"column": 14,
"character": 115
"character": 102
},
"pos": 57
"pos": 44
},
{
"code": "a11y-structure",
"message": "A11y: <figcaption> must be an immediate child of <figure>",
"start": {
"line": 15,
"column": 2,
"character": 252
"character": 226
},
"end": {
"line": 17,
"column": 15,
"character": 328
"character": 302
},
"pos": 252
"pos": 226
}
]
5 changes: 5 additions & 0 deletions test/validator/samples/a11y-img-redundant-alt/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<img src="foo" alt="Foo eating a sandwich." />
<img src="bar" aria-hidden alt="Picture of me taking a photo of an image" />
<img src="foo" alt="Photo of foo being weird." />
<img src="bar" alt="Image of me at a bar!" />
<img src="baz" alt="Picture of baz fixing a bug." />
47 changes: 47 additions & 0 deletions test/validator/samples/a11y-img-redundant-alt/warnings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[
{
"code": "a11y-img-redundant-alt",
"message": "A11y: Screenreaders already announce <img> elements as an image.",
"end": {
"character": 173,
"column": 49,
"line": 3
},
"start": {
"character": 124,
"column": 0,
"line": 3
},
"pos": 124
},
{
"code": "a11y-img-redundant-alt",
"message": "A11y: Screenreaders already announce <img> elements as an image.",
"end": {
"character": 219,
"column": 45,
"line": 4
},
"start": {
"character": 174,
"column": 0,
"line": 4
},
"pos": 174
},
{
"code": "a11y-img-redundant-alt",
"message": "A11y: Screenreaders already announce <img> elements as an image.",
"end": {
"character": 272,
"column": 52,
"line": 5
},
"start": {
"character": 220,
"column": 0,
"line": 5
},
"pos": 220
}
]