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

Fix S6853: directive condition and small refactor #275

Merged
merged 3 commits into from
Feb 19, 2024
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
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"/>