Skip to content

Commit

Permalink
Create rule S6851: Images should have a non-redundant alternate descr…
Browse files Browse the repository at this point in the history
…iption (#3441)

* Create rule S6851

* Update metadata and description

* Update rules/S6851/javascript/metadata.json

Co-authored-by: Ilia Kebets <104737176+ilia-kebets-sonarsource@users.noreply.github.com>

* Update rules/S6851/javascript/rule.adoc

Co-authored-by: Ilia Kebets <104737176+ilia-kebets-sonarsource@users.noreply.github.com>

* Update rules/S6851/javascript/rule.adoc

Co-authored-by: Ilia Kebets <104737176+ilia-kebets-sonarsource@users.noreply.github.com>

---------

Co-authored-by: yassin-kammoun-sonarsource <yassin-kammoun-sonarsource@users.noreply.github.com>
Co-authored-by: yassin-kammoun-sonarsource <yassin.kammoun@sonarsource.com>
Co-authored-by: Yassin Kammoun <52890329+yassin-kammoun-sonarsource@users.noreply.github.com>
Co-authored-by: Ilia Kebets <104737176+ilia-kebets-sonarsource@users.noreply.github.com>
  • Loading branch information
5 people authored Nov 17, 2023
1 parent fe00bf4 commit 03512e6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
26 changes: 26 additions & 0 deletions rules/S6851/javascript/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"title": "Images should have a non-redundant alternate description",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"accessibility",
"react"
],
"defaultSeverity": "Minor",
"ruleSpecification": "RSPEC-6851",
"sqKey": "S6851",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "infeasible",
"code": {
"impacts": {
"MAINTAINABILITY": "LOW",
"RELIABILITY": "LOW"
},
"attribute": "CONVENTIONAL"
}
}
38 changes: 38 additions & 0 deletions rules/S6851/javascript/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
== Why is this an issue?

``++alt++`` attributes, also known as "alt tags" or "alt descriptions," are used to specify alternative text that is rendered when an image cannot be displayed. They are crucial for improving web accessibility, as they provide a text description of images for users who rely on screen readers.

Screen readers announce the presence of an ``++<img>++`` element and read its ``++alt++`` attribute aloud to describe the image. If the ``++alt++`` attribute includes words like "image", "picture", or "photo", it leads to redundancy as the screen reader would repeat "image". For instance, an ``++alt++`` attribute like "image of a sunrise" would be read as "Image, image of a sunrise", unnecessarily repeating "image".

Instead, the ``++alt++`` attribute should focus on describing the content of the image, not the fact that it is an image. This makes the browsing experience more efficient and enjoyable for users of screen readers, as they receive a concise and meaningful description of the image without unnecessary repetition.

== How to fix it

To fix this issue, you should revise the ``++alt++`` attribute of your ``++<img>++`` elements to remove any instances of the words "image", "picture", or "photo". Instead, provide a concise and accurate description of the image content that adds value for users who cannot see the image.

=== Code examples

==== Noncompliant code example

[source,javascript,diff-id=1,diff-type=noncompliant]
----
function MyImage() {
return <img src="sunrise.jpg" alt="image of a sunrise" />; // Noncompliant: "Image, image of a sunrise"
}
----

==== Compliant solution

[source,javascript,diff-id=1,diff-type=compliant]
----
function MyImage() {
return <img src="sunrise.jpg" alt="a sunrise over a mountain range" />;
}
----

== Resources
=== Documentation

* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img[img element]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt[alt property]
* WebAIM - https://webaim.org/techniques/alttext/[Alternative Text]
2 changes: 2 additions & 0 deletions rules/S6851/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}

0 comments on commit 03512e6

Please sign in to comment.