From e781a9e09f207461afc9b7a8ab2946ccb3e240a9 Mon Sep 17 00:00:00 2001 From: Ilia Kebets <104737176+ilia-kebets-sonarsource@users.noreply.github.com> Date: Mon, 19 Feb 2024 15:54:46 +0100 Subject: [PATCH] Fix S6853: directive condition and small refactor (#275) --- .../accessibility/LabelHasAssociatedControlCheck.java | 5 +++-- .../LabelHasAssociatedControlCheckTest.java | 9 +++++++++ .../checks/LabelHasAssociatedControlCheck/file.phtml | 5 +++++ 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 sonar-html-plugin/src/test/resources/checks/LabelHasAssociatedControlCheck/file.phtml diff --git a/sonar-html-plugin/src/main/java/org/sonar/plugins/html/checks/accessibility/LabelHasAssociatedControlCheck.java b/sonar-html-plugin/src/main/java/org/sonar/plugins/html/checks/accessibility/LabelHasAssociatedControlCheck.java index 759ee6680..2f3c3d064 100644 --- a/sonar-html-plugin/src/main/java/org/sonar/plugins/html/checks/accessibility/LabelHasAssociatedControlCheck.java +++ b/sonar-html-plugin/src/main/java/org/sonar/plugins/html/checks/accessibility/LabelHasAssociatedControlCheck.java @@ -19,6 +19,7 @@ import java.util.List; import java.util.Locale; +import java.util.Set; import org.sonar.check.Rule; import org.sonar.plugins.html.checks.AbstractPageCheck; import org.sonar.plugins.html.node.DirectiveNode; @@ -30,7 +31,7 @@ @Rule(key = "S6853") public class LabelHasAssociatedControlCheck extends AbstractPageCheck { private static final String MESSAGE = "A form label must be associated with a control."; - private static final List CONTROL_TAGS = List.of("INPUT", "METER", "OUTPUT", "PROGRESS", "SELECT", "TEXTAREA"); + private static final Set CONTROL_TAGS = Set.of("INPUT", "METER", "OUTPUT", "PROGRESS", "SELECT", "TEXTAREA"); private boolean foundControl; private boolean foundAccessibleLabel; private TagNode label; @@ -88,7 +89,7 @@ public void characters(TextNode textNode) { @Override public void directive(DirectiveNode node) { if (label != null) { - foundAccessibleLabel = "?php".equalsIgnoreCase(node.getNodeName()); + foundAccessibleLabel = true; } } diff --git a/sonar-html-plugin/src/test/java/org/sonar/plugins/html/checks/accessibility/LabelHasAssociatedControlCheckTest.java b/sonar-html-plugin/src/test/java/org/sonar/plugins/html/checks/accessibility/LabelHasAssociatedControlCheckTest.java index a803f18c6..94899f4c1 100644 --- a/sonar-html-plugin/src/test/java/org/sonar/plugins/html/checks/accessibility/LabelHasAssociatedControlCheckTest.java +++ b/sonar-html-plugin/src/test/java/org/sonar/plugins/html/checks/accessibility/LabelHasAssociatedControlCheckTest.java @@ -82,4 +82,13 @@ void jsp() throws Exception { .next().atLine(1).withMessage("A form label must be associated with a control.") .noMore(); } + + @Test + void phtml() throws Exception { + HtmlSourceCode sourceCode = TestHelper.scan( + new File("src/test/resources/checks/LabelHasAssociatedControlCheck/file.phtml"), + new LabelHasAssociatedControlCheck()); + checkMessagesVerifier.verify(sourceCode.getIssues()) + .noMore(); + } } diff --git a/sonar-html-plugin/src/test/resources/checks/LabelHasAssociatedControlCheck/file.phtml b/sonar-html-plugin/src/test/resources/checks/LabelHasAssociatedControlCheck/file.phtml new file mode 100644 index 000000000..6fcfa27b0 --- /dev/null +++ b/sonar-html-plugin/src/test/resources/checks/LabelHasAssociatedControlCheck/file.phtml @@ -0,0 +1,5 @@ + +