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 a11y-svg-has-accessible-name considering whitespace JSXText #508

Merged
merged 3 commits into from
Feb 29, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/rules/a11y-svg-has-accessible-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ module.exports = {
const elementType = getElementType(context, node)
if (elementType !== 'svg') return

// Check if there is a nested title element that is the first child of the `<svg>`
// Check if there is a nested title element that is the first non-whitespace child of the `<svg>`
const childrenWithoutWhitespace = node.parent.children
?.filter(({type, value}) => (type === 'JSXText' && value.trim() !== '') || type !== 'JSXText')
nnmrts marked this conversation as resolved.
Show resolved Hide resolved

const hasNestedTitleAsFirstChild =
node.parent.children?.[0]?.type === 'JSXElement' &&
node.parent.children?.[0]?.openingElement?.name?.name === 'title'
childrenWithoutWhitespace?.[0]?.type === 'JSXElement' &&
childrenWithoutWhitespace?.[0]?.openingElement?.name?.name === 'title'

// Check if `aria-label` or `aria-labelledby` is set
const hasAccessibleName = hasProp(node.attributes, 'aria-label') || hasProp(node.attributes, 'aria-labelledby')
Expand Down