Skip to content

Commit 93f7885

Browse files
committed
[Fix] control-has-associated-label: don't accept whitespace as an accessible label
Fixes #918
1 parent 20b082a commit 93f7885

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

__tests__/src/util/mayHaveAccessibleLabel-test.js

+16
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ describe('mayHaveAccessibleLabel', () => {
4141
JSXAttributeMock('aria-label', ''),
4242
], []))).toBe(false);
4343
});
44+
it('aria-label with only whitespace, should return false', () => {
45+
expect(mayHaveAccessibleLabel(JSXElementMock('div', [
46+
JSXAttributeMock('aria-label', ' '),
47+
], []))).toBe(false);
48+
expect(mayHaveAccessibleLabel(JSXElementMock('div', [
49+
JSXAttributeMock('aria-label', '\n'),
50+
], []))).toBe(false);
51+
});
4452
it('aria-labelledby, should return true', () => {
4553
expect(mayHaveAccessibleLabel(JSXElementMock('div', [
4654
JSXAttributeMock('aria-labelledby', 'elementId'),
@@ -78,6 +86,14 @@ describe('mayHaveAccessibleLabel', () => {
7886
LiteralMock('A fancy label'),
7987
]))).toBe(true);
8088
});
89+
it('Literal whitespace, should return false', () => {
90+
expect(mayHaveAccessibleLabel(JSXElementMock('div', [], [
91+
LiteralMock(' '),
92+
]))).toBe(false);
93+
expect(mayHaveAccessibleLabel(JSXElementMock('div', [], [
94+
LiteralMock('\n'),
95+
]))).toBe(false);
96+
});
8197
it('JSXText, should return true', () => {
8298
expect(mayHaveAccessibleLabel(JSXElementMock('div', [], [
8399
JSXTextMock('A fancy label'),

src/util/mayHaveAccessibleLabel.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import includes from 'array-includes';
1212
import { getPropValue, propName } from 'jsx-ast-utils';
1313
import type { JSXOpeningElement, Node } from 'ast-types-flow';
1414

15+
function tryTrim(value: any) {
16+
return typeof value === 'string' ? value.trim() : value;
17+
}
18+
1519
function hasLabellingProp(
1620
openingElement: JSXOpeningElement,
1721
additionalLabellingProps?: Array<string> = [],
@@ -30,7 +34,7 @@ function hasLabellingProp(
3034
// Attribute matches.
3135
if (
3236
includes(labellingProps, propName(attribute))
33-
&& !!getPropValue(attribute)
37+
&& !!tryTrim(getPropValue(attribute))
3438
) {
3539
return true;
3640
}
@@ -52,7 +56,7 @@ export default function mayHaveAccessibleLabel(
5256
return false;
5357
}
5458
// Check for literal text.
55-
if (node.type === 'Literal' && !!node.value) {
59+
if (node.type === 'Literal' && !!tryTrim(node.value)) {
5660
return true;
5761
}
5862
// Assume an expression container renders a label. It is the best we can
@@ -62,7 +66,7 @@ export default function mayHaveAccessibleLabel(
6266
}
6367
// Check for JSXText.
6468
// $FlowFixMe Remove after updating ast-types-flow
65-
if (node.type === 'JSXText' && !!node.value) {
69+
if (node.type === 'JSXText' && !!tryTrim(node.value)) {
6670
return true;
6771
}
6872
// Check for labelling props.

0 commit comments

Comments
 (0)