diff --git a/lib/rules/a11y-svg-has-accessible-name.js b/lib/rules/a11y-svg-has-accessible-name.js index e8c92a8f..45e675fd 100644 --- a/lib/rules/a11y-svg-has-accessible-name.js +++ b/lib/rules/a11y-svg-has-accessible-name.js @@ -16,10 +16,14 @@ 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 `` + // Check if there is a nested title element that is the first non-whitespace child of the `` + const childrenWithoutWhitespace = node.parent.children?.filter(({type, value}) => + type === 'JSXText' ? value.trim() !== '' : type !== 'JSXText', + ) + 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') diff --git a/tests/a11y-svg-has-accessible-name.js b/tests/a11y-svg-has-accessible-name.js index c708ee9a..62442132 100644 --- a/tests/a11y-svg-has-accessible-name.js +++ b/tests/a11y-svg-has-accessible-name.js @@ -19,6 +19,12 @@ ruleTester.run('a11y-svg-has-accessible-name', rule, { { code: "Circle with a black outline and red fill", }, + { + code: ` + Circle with a black outline and red fill + + `, + }, { code: "", },