Skip to content

[anchor-has-content] account for v-html and v-text on child elements #313

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

Merged
merged 2 commits into from
Oct 21, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

- Deprecate the accessible-emoji rule. See https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/issues/627 for details.
- Fix the `alt-text` handling of different element case names.
- Fix the `anchor-has-content` handling for content directives (v-html, v-text) on child elements.

## [1.1.0] - 2021-10-14

Expand Down
2 changes: 2 additions & 0 deletions src/rules/__tests__/anchor-has-content.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ makeRuleTester("anchor-has-content", rule, {
"<VAnchor />",
"<a aria-label='This is my label' />",
"<a><img alt='foo' /></a>",
"<a><span v-html='msg' /></a>",
"<a><span v-text='msg' /></a>",
{
code: "<a v-accessibleDirective='msg' />",
options: [{ accessibleDirectives: ["accessibleDirective"] }]
Expand Down
14 changes: 14 additions & 0 deletions src/utils/hasContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ function hasDirective(node: AST.VElement, name: string) {
);
}

function hasChildWithDirective(node: AST.VElement, name: string): boolean {
return node.children.some((child) => {
if (child.type !== "VElement") return false;

if (hasDirective(child, name)) {
return true;
}

return hasChildWithDirective(child, name)
});
}

function hasChildImageWithAlt(node: AST.VElement): boolean {
return node.children.some((child) => {
if (child.type === "VElement") {
Expand Down Expand Up @@ -46,6 +58,8 @@ function hasContent(
hasAccessibleDirective(node, accessibleDirectives) ||
hasDirective(node, "text") ||
hasDirective(node, "html") ||
hasChildWithDirective(node, "text") ||
hasChildWithDirective(node, "html") ||
hasChildImageWithAlt(node)
);
}
Expand Down