Skip to content

Commit bd02aa4

Browse files
committed
Merge pull request #2 from cannona/master
Accepts images with non-empty alt attributes as labels for clickable elements
2 parents 9cdbeb2 + 7d82b21 commit bd02aa4

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

lib/__tests__/index-test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ describe('props', () => {
5858
<div onClick={k}><span><span>foo</span></span></div>;
5959
});
6060
});
61+
62+
it('does not warn if there is an image with an alt attribute', () => {
63+
doNotExpectWarning(assertions.props.onClick.NO_LABEL.msg, () => {
64+
<div onClick={k}><img src="#" alt="Foo"/></div>;
65+
});
66+
});
67+
68+
it('warns if there is an image with an empty alt attribute', () => {
69+
expectWarning(assertions.props.onClick.NO_LABEL.msg, () => {
70+
<div onClick={k}><img src="#" alt=""/></div>;
71+
});
72+
});
6173
});
6274

6375
describe('when role="button"', () => {

lib/assertions.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ var hasChildTextNode = (props, children) => {
2525
return;
2626
else if (typeof child === 'string')
2727
hasText = true;
28+
else if (child.type === 'img' && child.props.alt)
29+
hasText = true;
2830
else if (child.props.children)
2931
hasText = hasChildTextNode(child.props, child.props.children);
3032
});
@@ -56,6 +58,7 @@ exports.props = {
5658
return (
5759
props['aria-label'] ||
5860
props['aria-labelled-by'] ||
61+
(tagName === 'img' && props.alt) ||
5962
hasChildTextNode(props, children)
6063
);
6164
}

0 commit comments

Comments
 (0)