Skip to content

Commit 30f5d80

Browse files
authored
fix a11y-svg-has-accessible-name considering whitespace JSXText (#508)
1 parent adea305 commit 30f5d80

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

lib/rules/a11y-svg-has-accessible-name.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ module.exports = {
1616
const elementType = getElementType(context, node)
1717
if (elementType !== 'svg') return
1818

19-
// Check if there is a nested title element that is the first child of the `<svg>`
19+
// Check if there is a nested title element that is the first non-whitespace child of the `<svg>`
20+
const childrenWithoutWhitespace = node.parent.children?.filter(({type, value}) =>
21+
type === 'JSXText' ? value.trim() !== '' : type !== 'JSXText',
22+
)
23+
2024
const hasNestedTitleAsFirstChild =
21-
node.parent.children?.[0]?.type === 'JSXElement' &&
22-
node.parent.children?.[0]?.openingElement?.name?.name === 'title'
25+
childrenWithoutWhitespace?.[0]?.type === 'JSXElement' &&
26+
childrenWithoutWhitespace?.[0]?.openingElement?.name?.name === 'title'
2327

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

tests/a11y-svg-has-accessible-name.js

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ ruleTester.run('a11y-svg-has-accessible-name', rule, {
1919
{
2020
code: "<svg height='100' width='100'><title>Circle with a black outline and red fill</title><circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/></svg>",
2121
},
22+
{
23+
code: `<svg height='100' width='100'>
24+
<title>Circle with a black outline and red fill</title>
25+
<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/>
26+
</svg>`,
27+
},
2228
{
2329
code: "<svg aria-label='Circle with a black outline and red fill' height='100' width='100'><circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/></svg>",
2430
},

0 commit comments

Comments
 (0)