Skip to content

Commit

Permalink
Create rule S6841: "tabIndex" values should be 0 or -1 (#3650)
Browse files Browse the repository at this point in the history
* Add html to rule S6841

* Share JS description with HTML

---------

Co-authored-by: yassin-kammoun-sonarsource <yassin-kammoun-sonarsource@users.noreply.github.com>
Co-authored-by: yassin-kammoun-sonarsource <yassin.kammoun@sonarsource.com>
  • Loading branch information
3 people authored Feb 19, 2024
1 parent 4f796b7 commit e4fce14
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 37 deletions.
3 changes: 3 additions & 0 deletions rules/S6841/how.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
== How to fix it

If you need to make an element focusable that isn't by default (like a ``++<div>++`` or ``++<span>++``), you can use ``++tabIndex="0"++``. This will add the element to the natural tab order based on its position in the HTML. Otherwise, either remove the ``++tabIndex++`` value or use ``++tabIndex="-1"++`` to remove the element from the tab order.
2 changes: 2 additions & 0 deletions rules/S6841/html/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
30 changes: 30 additions & 0 deletions rules/S6841/html/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
include::../why.adoc[]

include::../how.adoc[]

=== Code examples

==== Noncompliant code example

[source,html,diff-id=1,diff-type=noncompliant]
----
<div>
<span tabIndex="5">foo</span> <!-- Noncompliant -->
<span tabIndex="3">bar</span> <!-- Noncompliant -->
<span tabIndex="1">baz</span> <!-- Noncompliant -->
<span tabIndex="2">qux</span> <!-- Noncompliant -->
</div>
----

==== Compliant solution

[source,html,diff-id=1,diff-type=compliant]
----
<div>
<span tabIndex="0">foo</span>
<span tabIndex="-1">bar</span>
<span>qux</span>
</div>
----

include::../resources.adoc[]
28 changes: 4 additions & 24 deletions rules/S6841/javascript/metadata.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
{
"title": "\"tabIndex\" values should be 0 or -1",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"accessibility",
"react"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6841",
"sqKey": "S6841",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "infeasible",
"code": {
"impacts": {
"MAINTAINABILITY": "LOW",
"RELIABILITY": "MEDIUM"
},
"attribute": "CONVENTIONAL"
}
"tags": [
"accessibility",
"react"
]
}
16 changes: 3 additions & 13 deletions rules/S6841/javascript/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
== Why is this an issue?
include::../why.adoc[]

Positive ``++tabIndex++`` values can disrupt the natural tab order of the webpage. This can be confusing for screen reader users who rely on a logical tab order to navigate through the content. If the tab order doesn't match the visual or logical order of elements, users may struggle to understand the page structure.

Therefore, it's recommended to avoid using positive ``++tabIndex++`` values.

== How to fix it

If you need to make an element focusable that isn't by default (like a ``++<div>++`` or ``++<span>++``), you can use ``++tabIndex="0"++``. This will add the element to the natural tab order based on its position in the HTML. Otherwise, either remove the ``++tabIndex++`` value or use ``++tabIndex="-1"++`` to remove the element from the tab order.
include::../how.adoc[]

=== Code examples

Expand Down Expand Up @@ -42,8 +36,4 @@ function MyDiv() {
}
----

== Resources
=== Documentation

* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex[tabindex]
* WCAG - https://www.w3.org/WAI/WCAG21/Understanding/focus-order[Focus Order]
include::../resources.adoc[]
23 changes: 23 additions & 0 deletions rules/S6841/metadata.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,25 @@
{
"title": "\"tabIndex\" values should be 0 or -1",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"accessibility"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6841",
"sqKey": "S6841",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "infeasible",
"code": {
"impacts": {
"MAINTAINABILITY": "LOW",
"RELIABILITY": "MEDIUM"
},
"attribute": "CONVENTIONAL"
}
}
5 changes: 5 additions & 0 deletions rules/S6841/resources.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
== Resources
=== Documentation

* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex[tabindex]
* WCAG - https://www.w3.org/WAI/WCAG21/Understanding/focus-order[Focus Order]
5 changes: 5 additions & 0 deletions rules/S6841/why.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
== Why is this an issue?

Positive ``++tabIndex++`` values can disrupt the natural tab order of the webpage. This can be confusing for screen reader users who rely on a logical tab order to navigate through the content. If the tab order doesn't match the visual or logical order of elements, users may struggle to understand the page structure.

Therefore, it's recommended to avoid using positive ``++tabIndex++`` values.

0 comments on commit e4fce14

Please sign in to comment.