-
Notifications
You must be signed in to change notification settings - Fork 45
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
SONARHTML-198 Create rule S6852: Elements with an interactive role should support focus #284
Merged
+265
−1
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"project:custom/S6852.html": [ | ||
1 | ||
] | ||
} |
Submodule sources
updated
from a35edb to 49de9f
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...n/java/org/sonar/plugins/html/checks/accessibility/FocusableInteractiveElementsCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* SonarSource HTML analyzer :: Sonar Plugin | ||
* Copyright (c) 2010-2024 SonarSource SA and Matthijs Galesloot | ||
* [email protected] | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.sonar.plugins.html.checks.accessibility; | ||
|
||
import static org.sonar.plugins.html.api.HtmlConstants.INTERACTIVE_ELEMENTS; | ||
import static org.sonar.plugins.html.api.HtmlConstants.INTERACTIVE_ROLES; | ||
import static org.sonar.plugins.html.api.HtmlConstants.KNOWN_HTML_TAGS; | ||
import static org.sonar.plugins.html.api.HtmlConstants.NON_INTERACTIVE_ELEMENTS; | ||
import static org.sonar.plugins.html.api.HtmlConstants.NON_INTERACTIVE_ROLES; | ||
import static org.sonar.plugins.html.api.HtmlConstants.PRESENTATION_ROLES; | ||
import static org.sonar.plugins.html.checks.accessibility.AccessibilityUtils.isDisabledElement; | ||
import static org.sonar.plugins.html.checks.accessibility.AccessibilityUtils.isHiddenFromScreenReader; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import org.sonar.check.Rule; | ||
import org.sonar.plugins.html.checks.AbstractPageCheck; | ||
import org.sonar.plugins.html.node.TagNode; | ||
|
||
@Rule(key = "S6852") | ||
public class FocusableInteractiveElementsCheck extends AbstractPageCheck { | ||
|
||
private static final String MESSAGE_TEMPLATE = "Elements with the \"%s\" interactive role must be focusable."; | ||
|
||
private static final Set<String> INTERACTIVE_PROPS = new HashSet<>(); | ||
static { | ||
INTERACTIVE_PROPS.addAll(EventHandlers.EVENT_HANDLERS_BY_TYPE.get("keyboard")); | ||
INTERACTIVE_PROPS.addAll(EventHandlers.EVENT_HANDLERS_BY_TYPE.get("mouse")); | ||
} | ||
|
||
@Override | ||
public void startElement(TagNode element) { | ||
var role = element.getAttribute("role"); | ||
if (role == null) { | ||
return; | ||
} | ||
|
||
if (!isHTMLTag(element) | ||
|| !hasInteractiveProps(element) | ||
|| !hasInteractiveRole(element) | ||
|| isDisabledElement(element) | ||
|| isHiddenFromScreenReader(element) | ||
|| isInteractiveElement(element) | ||
|| isNoninteractiveElement(element) | ||
|| hasPresentationRole(element) | ||
|| hasNoninteractiveRole(element) | ||
|| element.hasProperty("tabindex") | ||
) { | ||
return; | ||
} | ||
|
||
var message = String.format(MESSAGE_TEMPLATE, role); | ||
createViolation(element.getStartLinePosition(), message); | ||
} | ||
|
||
private static boolean isHTMLTag(TagNode element) { | ||
return KNOWN_HTML_TAGS.stream().anyMatch(tag -> tag.equalsIgnoreCase(element.getNodeName())); | ||
} | ||
|
||
private static boolean hasInteractiveProps(TagNode element) { | ||
return INTERACTIVE_PROPS.stream().anyMatch(prop -> { | ||
var attr = element.getAttribute(prop); | ||
return attr != null && !attr.isEmpty(); | ||
}); | ||
} | ||
|
||
private static boolean hasInteractiveRole(TagNode element) { | ||
return INTERACTIVE_ROLES.stream().anyMatch(role -> role.equalsIgnoreCase(element.getAttribute("role"))); | ||
} | ||
|
||
private static boolean isInteractiveElement(TagNode element) { | ||
return INTERACTIVE_ELEMENTS.stream().anyMatch(tag -> tag.equalsIgnoreCase(element.getNodeName())); | ||
} | ||
|
||
private static boolean isNoninteractiveElement(TagNode element) { | ||
return NON_INTERACTIVE_ELEMENTS.stream().anyMatch(tag -> tag.equalsIgnoreCase(element.getNodeName())); | ||
} | ||
|
||
private static boolean hasPresentationRole(TagNode element) { | ||
return PRESENTATION_ROLES.stream().anyMatch(role -> role.equalsIgnoreCase(element.getAttribute("role"))); | ||
} | ||
|
||
private static boolean hasNoninteractiveRole(TagNode element) { | ||
return NON_INTERACTIVE_ROLES.stream().anyMatch(role -> role.equalsIgnoreCase(element.getAttribute("role"))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
sonar-html-plugin/src/main/resources/org/sonar/l10n/web/rules/Web/S6852.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<p>Interactive elements being focusable is vital for website accessibility. It enables users, including those using assistive technologies, to | ||
interact effectively with the website. Without this, some users may be unable to access certain features, leading to a poor user experience and | ||
potential non-compliance with accessibility standards.</p> | ||
<h2>Why is this an issue?</h2> | ||
<p>Lack of focusability can hinder navigation and interaction with the website, resulting in an exclusionary user experience and possible violation of | ||
accessibility guidelines.</p> | ||
<h2>How to fix it</h2> | ||
<p>Ensure that all interactive elements on your website can receive focus. This can be achieved by using standard HTML interactive elements, or by | ||
assigning a <code>tabindex</code> attribute of "0" to custom interactive components.</p> | ||
<h3>Code examples</h3> | ||
<h4>Noncompliant code example</h4> | ||
<pre data-diff-id="1" data-diff-type="noncompliant"> | ||
<!-- Element with mouse/keyboard handler has no tabindex --> | ||
<span onclick="submitForm();" role="button">Submit</span> | ||
|
||
<!-- Anchor element without href is not focusable --> | ||
<a onclick="showNextPage();" role="button">Next page</a> | ||
</pre> | ||
<h4>Compliant solution</h4> | ||
<pre data-diff-id="1" data-diff-type="compliant"> | ||
<!-- Element with mouse handler has tabIndex --> | ||
<span onClick="doSomething();" tabIndex="0" role="button">Submit</span> | ||
|
||
<!-- Focusable anchor with mouse handler --> | ||
<a href="javascript:void(0);" onClick="doSomething();"> Next page </a> | ||
</pre> | ||
<h2>Resources</h2> | ||
<h3>Documentation</h3> | ||
<ul> | ||
<li> W3C - <a href="https://www.w3.org/TR/WCAG20-TECHS/H4.html">Creating a logical tab order through links, form controls, and objects</a> </li> | ||
<li> W3C - <a href="https://www.w3.org/TR/wai-aria-practices-1.1/#kbd_generalnav">Fundamental Keyboard Navigation Conventions</a> </li> | ||
<li> MDN - <a href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/button_role#accessibility_concerns">ARIA: button role - | ||
Accessibility concerns</a> </li> | ||
</ul> | ||
|
24 changes: 24 additions & 0 deletions
24
sonar-html-plugin/src/main/resources/org/sonar/l10n/web/rules/Web/S6852.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"title": "Elements with an interactive role should support focus", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
"accessibility" | ||
], | ||
"defaultSeverity": "Minor", | ||
"ruleSpecification": "RSPEC-6852", | ||
"sqKey": "S6852", | ||
"scope": "All", | ||
"quickfix": "infeasible", | ||
"code": { | ||
"impacts": { | ||
"MAINTAINABILITY": "LOW", | ||
"RELIABILITY": "MEDIUM" | ||
}, | ||
"attribute": "CONVENTIONAL" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...va/org/sonar/plugins/html/checks/accessibility/FocusableInteractiveElementsCheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* SonarSource HTML analyzer :: Sonar Plugin | ||
* Copyright (c) 2010-2024 SonarSource SA and Matthijs Galesloot | ||
* [email protected] | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.sonar.plugins.html.checks.accessibility; | ||
|
||
import java.io.File; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.sonar.plugins.html.checks.CheckMessagesVerifierRule; | ||
import org.sonar.plugins.html.checks.TestHelper; | ||
import org.sonar.plugins.html.visitor.HtmlSourceCode; | ||
|
||
class FocusableInteractiveElementsCheckTest { | ||
|
||
@RegisterExtension | ||
public CheckMessagesVerifierRule checkMessagesVerifier = new CheckMessagesVerifierRule(); | ||
|
||
@Test | ||
void test() throws Exception { | ||
HtmlSourceCode sourceCode = TestHelper.scan( | ||
new File("src/test/resources/checks/FocusableInteractiveElementsCheck.html"), | ||
new FocusableInteractiveElementsCheck()); | ||
|
||
checkMessagesVerifier.verify(sourceCode.getIssues()) | ||
.next().atLine(36).withMessage("Elements with the \"button\" interactive role must be focusable.") | ||
.noMore(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
sonar-html-plugin/src/test/resources/checks/FocusableInteractiveElementsCheck.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<!-- Ignored: Missing role --> | ||
<Foo /> | ||
|
||
<!-- Ignored: Custom --> | ||
<Foo role="bar" /> | ||
|
||
<!-- Ignored: No interactive props --> | ||
<div role="foo" onBar="" /> | ||
<div role="foo" onBar /> | ||
<div role="foo" onClick /> | ||
<div role="foo" onClick="" /> | ||
|
||
<!-- Ignored: Disabled --> | ||
<div role="button" onClick="bar()" disabled /> | ||
<div role="button" onClick="bar()" aria-disabled="true" /> | ||
|
||
<!-- Ignored: Hidden --> | ||
<div role="button" onClick="bar()" aria-hidden="true" /> | ||
|
||
<!-- Ignored: Interactive element --> | ||
<audio role="button" onClick="bar()" /> | ||
|
||
<!-- Ignored: Noninteractive element --> | ||
<dialog role="button" onClick="bar()" /> | ||
|
||
<!-- Ignored: Presentation role --> | ||
<div role="presentation" onClick="bar()" /> | ||
|
||
<!-- Ignored: Noninteractive role --> | ||
<div role="alert" onClick="bar()" /> | ||
|
||
<!-- Ignored: tabindex --> | ||
<div role="button" onClick="bar()" tabindex="-1" /> | ||
|
||
<!-- Noncompliant --> | ||
<div role="button" onClick="foo" /> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you refactor this to use the methods from
HtmlConstants.java
, and move the new ones there?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍