a11y-img-redudant-alt

pull/4750/head
André Lins 6 years ago
parent 29a34d15ed
commit f0959bafb8

@ -272,6 +272,7 @@ export default class Element extends Node {
} }
this.validate_attributes(); this.validate_attributes();
this.validate_special_cases();
this.validate_bindings(); this.validate_bindings();
this.validate_content(); this.validate_content();
this.validate_event_handlers(); this.validate_event_handlers();
@ -420,8 +421,16 @@ export default class Element extends Node {
attribute_map.set(attribute.name, attribute); attribute_map.set(attribute.name, attribute);
}); });
}
validate_special_cases() {
const { component,attributes } = this;
let attribute_map = new Map();
attributes.forEach(attribute => (
attribute_map.set(attribute.name, attribute)
));
// handle special cases
if (this.name === 'a') { if (this.name === 'a') {
const href_attribute = attribute_map.get('href') || attribute_map.get('xlink:href'); const href_attribute = attribute_map.get('href') || attribute_map.get('xlink:href');
const id_attribute = attribute_map.get('id'); const id_attribute = attribute_map.get('id');
@ -447,9 +456,7 @@ export default class Element extends Node {
}); });
} }
} }
} } else {
else {
const required_attributes = a11y_required_attributes[this.name]; const required_attributes = a11y_required_attributes[this.name];
if (required_attributes) { if (required_attributes) {
const has_attribute = required_attributes.some(name => attribute_map.has(name)); const has_attribute = required_attributes.some(name => attribute_map.has(name));
@ -458,6 +465,7 @@ export default class Element extends Node {
should_have_attribute(this, required_attributes); should_have_attribute(this, required_attributes);
} }
} }
}
if (this.name === 'input') { if (this.name === 'input') {
const type = attribute_map.get('type'); const type = attribute_map.get('type');
@ -470,6 +478,28 @@ export default class Element extends Node {
} }
} }
} }
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)
);
if (contain_invalid_word) {
component.warn(this, {
code: `a11y-img-redundant-alt`,
message: `A11y: Screenreaders already announce <img> elements as an image.`
});
}
}
} }
} }

@ -3,7 +3,7 @@
</script> </script>
<figure> <figure>
<img src='foo.jpg' alt='a picture of a foo'> <img src='foo.jpg' alt='a foo'>
{#if caption} {#if caption}
<figcaption>{caption}</figcaption> <figcaption>{caption}</figcaption>
{/if} {/if}

@ -1,5 +1,5 @@
<figure> <figure>
<img src='foo.jpg' alt='a picture of a foo'> <img src='foo.jpg' alt='a foo'>
<figcaption> <figcaption>
a foo in its natural habitat a foo in its natural habitat

@ -1,5 +1,5 @@
<figure> <figure>
<img src='foo.jpg' alt='a picture of a foo'> <img src='foo.jpg' alt='a foo'>
<figcaption> <figcaption>
a foo in its natural habitat a foo in its natural habitat
@ -9,7 +9,7 @@
</figure> </figure>
<figure> <figure>
<img src='foo.jpg' alt='a picture of a foo'> <img src='foo.jpg' alt='a foo'>
<div class='markup-for-styling'> <div class='markup-for-styling'>
<figcaption> <figcaption>

@ -5,14 +5,14 @@
"start": { "start": {
"line": 4, "line": 4,
"column": 1, "column": 1,
"character": 57 "character": 44
}, },
"end": { "end": {
"line": 6, "line": 6,
"column": 14, "column": 14,
"character": 115 "character": 102
}, },
"pos": 57 "pos": 44
}, },
{ {
"code": "a11y-structure", "code": "a11y-structure",
@ -20,13 +20,13 @@
"start": { "start": {
"line": 15, "line": 15,
"column": 2, "column": 2,
"character": 252 "character": 226
}, },
"end": { "end": {
"line": 17, "line": 17,
"column": 15, "column": 15,
"character": 328 "character": 302
}, },
"pos": 252 "pos": 226
} }
] ]

@ -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." />

@ -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
}
]
Loading…
Cancel
Save