Skip to content

Commit

Permalink
Create rule S6847 (#3436)
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Nov 16, 2023
1 parent f1048d2 commit 82cc53e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
26 changes: 26 additions & 0 deletions rules/S6847/javascript/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"title": "Non-interactive elements shouldn't have event handlers",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"accessibility",
"react"
],
"defaultSeverity": "Minor",
"ruleSpecification": "RSPEC-6847",
"sqKey": "S6847",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "infeasible",
"code": {
"impacts": {
"MAINTAINABILITY": "MEDIUM",
"RELIABILITY": "LOW"
},
"attribute": "CONVENTIONAL"
}
}
37 changes: 37 additions & 0 deletions rules/S6847/javascript/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Non-interactive HTML elements, such as ``++<div>++`` and ``++<span>++``, are not designed to have event handlers. When these elements are given event handlers, it can lead to accessibility issues.

== Why is this an issue?

Attaching event handlers to non-interactive HTML elements can lead to significant accessibility issues. These elements, such as ``++<div>++`` and ``++<span>++``, are not designed to interact with assistive technologies like screen readers, making it difficult for users with disabilities to navigate and interact with the website. Additionally, these elements may not be focusable or provide visual feedback when interacted with, resulting in a confusing and potentially frustrating user experience. Therefore, to maintain an accessible and user-friendly website, event handlers should be used exclusively with interactive elements.

== How to fix it

To fix this issue, remove the event handler from the non-interactive element and attach it to an interactive element instead. If the element is not interactive, it should not have an event handler.

=== Code examples

==== Noncompliant code example

[source,text,diff-id=1,diff-type=noncompliant]
----
<li onClick={() => void 0} />
<div onClick={() => void 0} role="listitem" />
----

==== Compliant solution

[source,text,diff-id=1,diff-type=compliant]
----
<div onClick={() => void 0} role="button" />
<div onClick={() => void 0} role="presentation" />
<input type="text" onClick={() => void 0} /> // Interactive element does not require role.
<button onClick={() => void 0} className="foo" /> // button is interactive.
<div onClick={() => void 0} role="button" aria-hidden /> // This is hidden from the screenreader.
----


== Resources
=== Documentation

* WCAG - https://www.w3.org/TR/wai-aria-practices-1.1/#aria_ex[WAI-ARIA Authoring Practices Guide - Design Patterns and Widgets]
* MDN - https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_button_role#Keyboard_and_focus[ARIA Techniques]
2 changes: 2 additions & 0 deletions rules/S6847/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}

0 comments on commit 82cc53e

Please sign in to comment.