Skip to content

Commit ce1f311

Browse files
fix: correctly handle img with empty alt
1 parent b21e7d4 commit ce1f311

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,32 @@ Name "Tertiary Heading":
4444
data-testid="a-h3"
4545
/>
4646
47+
--------------------------------------------------
48+
img:
49+
50+
Name "":
51+
<img
52+
data-testid="a-img-1"
53+
src="http://example.com/image.png"
54+
/>
55+
56+
Name "a meaningful description":
57+
<img
58+
alt="a meaningful description"
59+
data-testid="a-img-3"
60+
src="http://example.com/image.png"
61+
/>
62+
63+
--------------------------------------------------
64+
presentation:
65+
66+
Name "":
67+
<img
68+
alt=""
69+
data-testid="a-img-2"
70+
src="http://example.com/image.png"
71+
/>
72+
4773
--------------------------------------------------
4874
article:
4975

src/__tests__/role-helpers.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ function setup() {
2727
<h2 data-testid='a-h2'>Sub Heading</h2>
2828
<h3 data-testid='a-h3'>Tertiary Heading</h3>
2929
30+
<img src="http://example.com/image.png" data-testid='a-img-1'/>
31+
<img alt="" src="http://example.com/image.png" data-testid='a-img-2'/>
32+
<img alt="a meaningful description" src="http://example.com/image.png" data-testid='a-img-3'/>
33+
3034
<article data-testid='a-article'>
3135
<ul data-testid='a-list'>
3236
<li data-testid='a-list-item-1'>Item 1</li>
@@ -74,6 +78,9 @@ function setup() {
7478
h1: getByTestId('a-h1'),
7579
h2: getByTestId('a-h2'),
7680
h3: getByTestId('a-h3'),
81+
unnamedImg: getByTestId('a-img-1'),
82+
presentationImg: getByTestId('a-img-2'),
83+
namedImg: getByTestId('a-img-3'),
7784
nav: getByTestId('a-nav'),
7885
article: getByTestId('a-article'),
7986
aUl: getByTestId('a-list'),
@@ -109,6 +116,9 @@ test('getRoles returns expected roles for various dom nodes', () => {
109116
h1,
110117
h2,
111118
h3,
119+
unnamedImg,
120+
presentationImg,
121+
namedImg,
112122
nav,
113123
article,
114124
aUl,
@@ -139,6 +149,7 @@ test('getRoles returns expected roles for various dom nodes', () => {
139149
link: [anchor],
140150
generic: [invalidAnchor, unnamedSection],
141151
heading: [h1, h2, h3],
152+
img: [unnamedImg, namedImg],
142153
navigation: [nav],
143154
radio: [radio, radio2],
144155
article: [article],
@@ -153,6 +164,7 @@ test('getRoles returns expected roles for various dom nodes', () => {
153164
region: [namedSection],
154165
term: [dt],
155166
definition: [dd],
167+
presentation: [presentationImg],
156168
})
157169
expect(getRoles(header)).toEqual({
158170
banner: [header],

src/role-helpers.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,10 @@ function buildElementRoleList(elementRolesMap) {
8282
return `${name}${attributes
8383
.map(({name: attributeName, value, constraints = []}) => {
8484
const shouldNotExist = constraints.indexOf('undefined') !== -1
85+
const hasValue = typeof value !== 'undefined'
8586
if (shouldNotExist) {
8687
return `:not([${attributeName}])`
87-
} else if (value) {
88+
} else if (hasValue) {
8889
return `[${attributeName}="${value}"]`
8990
} else {
9091
return `[${attributeName}]`
@@ -94,14 +95,32 @@ function buildElementRoleList(elementRolesMap) {
9495
}
9596

9697
function getSelectorSpecificity({attributes = []}) {
97-
return attributes.length
98+
return {
99+
primaryKey: attributes.length,
100+
secondaryKey: attributes.reduce((acc, {value, constraints = []}) => {
101+
const shouldNotExist = constraints.indexOf('undefined') !== -1
102+
const hasValue = typeof value !== 'undefined'
103+
104+
if (shouldNotExist) {
105+
return acc + 2
106+
} else if (hasValue) {
107+
return acc + 1
108+
}
109+
110+
return acc
111+
}, 0),
112+
}
98113
}
99114

100115
function bySelectorSpecificity(
101116
{specificity: leftSpecificity},
102117
{specificity: rightSpecificity},
103118
) {
104-
return rightSpecificity - leftSpecificity
119+
if (rightSpecificity.primaryKey !== leftSpecificity.primaryKey) {
120+
return rightSpecificity.primaryKey - leftSpecificity.primaryKey
121+
}
122+
123+
return rightSpecificity.secondaryKey - leftSpecificity.secondaryKey
105124
}
106125

107126
function match(element) {

0 commit comments

Comments
 (0)