Skip to content

Commit d89d507

Browse files
fix: correctly handle img with empty alt
1 parent 8d7c09e commit d89d507

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

src/__tests__/__snapshots__/role-helpers.js.snap

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,5 +190,31 @@ Name "":
190190
data-testid="a-dd"
191191
/>
192192
193+
--------------------------------------------------
194+
img:
195+
196+
Name "":
197+
<img
198+
data-testid="a-img-1"
199+
src="http://example.com/image.png"
200+
/>
201+
202+
Name "a meaningful description":
203+
<img
204+
alt="a meaningful description"
205+
data-testid="a-img-3"
206+
src="http://example.com/image.png"
207+
/>
208+
209+
--------------------------------------------------
210+
presentation:
211+
212+
Name "":
213+
<img
214+
alt=""
215+
data-testid="a-img-2"
216+
src="http://example.com/image.png"
217+
/>
218+
193219
--------------------------------------------------
194220
`;

src/__tests__/role-helpers.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,22 @@ function setup() {
5858
5959
<form data-testid="a-form" />
6060
<section data-testid="a-section" />
61-
</article>
62-
<dl>
61+
</article>
62+
<dl>
6363
<dt data-testid="a-dt">Term</dt>
6464
<dd data-testid="a-dd">Definition</dd>
65-
</dl>
65+
</dl>
66+
67+
<img src="http://example.com/image.png" data-testid='a-img-1'/>
68+
<img alt="" src="http://example.com/image.png" data-testid='a-img-2'/>
69+
<img alt="a meaningful description" src="http://example.com/image.png" data-testid='a-img-3'/>
6670
</section>
6771
`)
6872

6973
return {
7074
unnamedSection: getByTestId('a-section'),
7175
namedSection: getByTestId('named-section'),
7276
anchor: getByTestId('a-link'),
73-
invalidAnchor: getByTestId('invalid-link'),
7477
h1: getByTestId('a-h1'),
7578
h2: getByTestId('a-h2'),
7679
h3: getByTestId('a-h3'),
@@ -98,14 +101,16 @@ function setup() {
98101
dt: getByTestId('a-dt'),
99102
dd: getByTestId('a-dd'),
100103
header: getByTestId('a-header'),
104+
invalidAnchor: getByTestId('invalid-link'),
105+
unnamedImg: getByTestId('a-img-1'),
106+
presentationImg: getByTestId('a-img-2'),
107+
namedImg: getByTestId('a-img-3'),
101108
}
102109
}
103110

104111
test('getRoles returns expected roles for various dom nodes', () => {
105112
const {
106-
unnamedSection,
107113
anchor,
108-
invalidAnchor,
109114
h1,
110115
h2,
111116
h3,
@@ -133,11 +138,15 @@ test('getRoles returns expected roles for various dom nodes', () => {
133138
dd,
134139
dt,
135140
header,
141+
invalidAnchor,
142+
unnamedSection,
143+
unnamedImg,
144+
presentationImg,
145+
namedImg,
136146
} = setup()
137147

138148
expect(getRoles(namedSection)).toEqual({
139149
link: [anchor],
140-
generic: [invalidAnchor, unnamedSection],
141150
heading: [h1, h2, h3],
142151
navigation: [nav],
143152
radio: [radio, radio2],
@@ -153,6 +162,9 @@ test('getRoles returns expected roles for various dom nodes', () => {
153162
region: [namedSection],
154163
term: [dt],
155164
definition: [dd],
165+
generic: [invalidAnchor, unnamedSection],
166+
img: [unnamedImg, namedImg],
167+
presentation: [presentationImg],
156168
})
157169
expect(getRoles(header)).toEqual({
158170
banner: [header],

src/role-helpers.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,18 @@ function buildElementRoleList(elementRolesMap) {
8282
return `${name}${attributes
8383
.map(({name: attributeName, value, constraints = []}) => {
8484
const shouldNotExist = constraints.indexOf('undefined') !== -1
85-
if (shouldNotExist) {
86-
return `:not([${attributeName}])`
87-
} else if (value) {
85+
const shouldBeNonEmpty = constraints.indexOf('set') !== -1
86+
const hasExplicitValue = typeof value !== 'undefined'
87+
88+
if (hasExplicitValue) {
8889
return `[${attributeName}="${value}"]`
89-
} else {
90-
return `[${attributeName}]`
90+
} else if (shouldNotExist) {
91+
return `:not([${attributeName}])`
92+
} else if (shouldBeNonEmpty) {
93+
return `[${attributeName}]:not([${attributeName}=""])`
9194
}
95+
96+
return `[${attributeName}]`
9297
})
9398
.join('')}`
9499
}

0 commit comments

Comments
 (0)