Skip to content

Commit

Permalink
Fix S6853: directive condition and small refactor (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilia-kebets-sonarsource authored Feb 19, 2024
1 parent b913dbc commit e781a9e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> CONTROL_TAGS = List.of("INPUT", "METER", "OUTPUT", "PROGRESS", "SELECT", "TEXTAREA");
private static final Set<String> CONTROL_TAGS = Set.of("INPUT", "METER", "OUTPUT", "PROGRESS", "SELECT", "TEXTAREA");
private boolean foundControl;
private boolean foundAccessibleLabel;
private TagNode label;
Expand Down Expand Up @@ -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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<label for="foo">
bar
<span><?= $block->escapeHtml(__('bar')) ?></span>
</label>
<input id="foo"/>

0 comments on commit e781a9e

Please sign in to comment.