GeezerVison is showing github.com — fix `a11y-svg-has-accessible-name` considering whitespace JSXText (#508) · github/eslint-plugin-github@30f5d80
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: lib/rules/a11y-svg-has-accessible-name.js +7-3Lines changed: 7 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,10 +16,14 @@ module.exports = { |
16 | 16 | const elementType = getElementType(context, node) |
17 | 17 | if (elementType !== 'svg') return |
18 | 18 |
|
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 | + |
20 | 24 | 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' |
23 | 27 |
|
24 | 28 | // Check if `aria-label` or `aria-labelledby` is set |
25 | 29 | const hasAccessibleName = hasProp(node.attributes, 'aria-label') || hasProp(node.attributes, 'aria-labelledby') |
|
Collapse file
Copy file name to clipboardExpand all lines: tests/a11y-svg-has-accessible-name.js +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,6 +19,12 @@ ruleTester.run('a11y-svg-has-accessible-name', rule, { |
19 | 19 | { |
20 | 20 | 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>", |
21 | 21 | }, |
| 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 | + }, |
22 | 28 | { |
23 | 29 | 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>", |
24 | 30 | }, |
|