Skip to content
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

Resolve SONARHTML-217 #310

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
"project:Silverpeas-Core-master/war-core/src/main/webapp/agenda/jsp/agenda.jsp": [
323
],
"project:Silverpeas-Core-master/war-core/src/main/webapp/attachment/jsp/displayAttachedFiles.jsp": [
259,
262
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.sonar.api.internal.apachecommons.lang.StringUtils;
import org.sonar.check.Rule;
import org.sonar.plugins.html.api.accessibility.Aria;
import org.sonar.plugins.html.api.accessibility.Aria.RoleProperties;
import org.sonar.plugins.html.api.accessibility.Aria.RoleDefinition;
import org.sonar.plugins.html.api.accessibility.AriaProperty;
import org.sonar.plugins.html.api.accessibility.AriaRole;
import org.sonar.plugins.html.checks.AbstractPageCheck;
Expand All @@ -44,7 +44,7 @@ public void startElement(TagNode element) {
roles = Arrays.stream(roleAttr.split("\\s+")).map(String::trim).map(AriaRole::of).toArray(AriaRole[]::new);
}

var rolesProperties = Arrays.stream(roles).map(Aria::getRole).filter(Objects::nonNull).toArray(RoleProperties[]::new);
var rolesProperties = Arrays.stream(roles).map(Aria::getRole).filter(Objects::nonNull).toArray(RoleDefinition[]::new);

if (rolesProperties.length == 0) {
return;
Expand All @@ -62,7 +62,7 @@ public void startElement(TagNode element) {
String.format(
"The attribute %s is not supported by the role %s.",
attr.getName(),
StringUtils.join(Arrays.stream(rolesProperties).map(RoleProperties::getName).map(AriaRole::toString).toArray(String[]::new), " or ")
StringUtils.join(Arrays.stream(rolesProperties).map(RoleDefinition::getName).map(AriaRole::toString).toArray(String[]::new), " or ")
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
*/
package org.sonar.plugins.html.checks.sonar;

import java.util.Arrays;
import java.util.regex.Pattern;
import org.sonar.check.Rule;
import org.sonar.plugins.html.api.accessibility.Aria;
import org.sonar.plugins.html.checks.AbstractPageCheck;
import org.sonar.plugins.html.node.TagNode;

Expand All @@ -39,6 +41,25 @@ public void startElement(TagNode node) {
return;
}

var roleAttributeValue = node.getAttribute("role");
String[] roles = new String[]{};

if (roleAttributeValue != null) {
roles = roleAttributeValue.split(" ");
} else {
var role = Aria.getImplicitRole(node);

if (role != null) {
roles = new String[]{
role.toString()
};
}
}

if (Arrays.stream(roles).anyMatch(MouseEventWithoutKeyboardEquivalentCheck::isAnInteractiveRole)) {
return;
}

if ((hasOnClick(node) || hasButtonRole(node)) && !(hasOnKeyPress(node) || hasOnKeyDown(node) || hasOnKeyUp(node))) {
attribute = "onKeyPress|onKeyDown|onKeyUp";
} else if (hasOnMouseover(node) && !hasOnFocus(node)) {
Expand All @@ -53,6 +74,10 @@ public void startElement(TagNode node) {
}
}

private static boolean isAnInteractiveRole(String role) {
return "textbox".equalsIgnoreCase(role);
}

private static boolean isException(TagNode node) {
return isClickableLightningButton(node) || ((isInput(node) || isButton(node) || isHyperlink(node) || isSummary(node)) && hasOnClick(node) && !hasButtonRole(node));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public void detected() throws Exception {
.next().atLine(65)
.next().atLine(68)
.next().atLine(70)
.next().atLine(71);
.next().atLine(71)
.next().atLine(79).withMessage("Add a 'onKeyPress|onKeyDown|onKeyUp' attribute to this <div> tag.")
;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,10 @@
<lightning-button-icon onclick={doSomething}></lightning-button-icon> <!-- Compliant -->
<lightning-button-menu onclick={doSomething}></lightning-button-menu> <!-- Compliant -->

<summary onclick="foo"></summary> <!-- Compliant -->
<summary onclick="foo"></summary> <!-- Compliant -->

<div onclick="console.log(this)">Div with a button role</div> <!-- Non-Compliant - Add a 'onKeyPress|onKeyDown|onKeyUp' attribute to this <div> tag. -->
<input onclick="console.log(this)"/> <!-- Compliant -->
<details>
<summary onclick="console.log(this.target)">Summary</summary> <!-- Compliant -->
</details>