Skip to content

Commit 87d2682

Browse files
authored
feat(ByAltText): Always include custom elements (#1049)
1 parent b28586e commit 87d2682

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

src/__tests__/queries.find.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ test('find asynchronously finds elements', async () => {
3131
<select><option>display value</option></select>
3232
<input placeholder="placeholder" />
3333
<img alt="test alt text" src="/lucy-ricardo.png" />
34+
<div alt="test alt text" />
3435
<span title="test title" />
3536
<div role="dialog"></div>
3637
<div role="meter progressbar"></div>

src/__tests__/text-matchers.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ cases(
3434
query: `Finding Nemo poster`,
3535
queryFn: `queryAllByAltText`,
3636
},
37+
'queryAllByAltText (for amp-img)': {
38+
dom: `
39+
<amp-img
40+
alt="Finding Nemo poster"
41+
src="/finding-nemo.png"
42+
/>`,
43+
query: `Finding Nemo poster`,
44+
queryFn: `queryAllByAltText`,
45+
},
3746
queryAllByPlaceholderText: {
3847
dom: `<input placeholder="Dwayne 'The Rock' Johnson" />`,
3948
query: `Dwayne 'The Rock' Johnson`,
@@ -93,6 +102,16 @@ cases(
93102
query: /^Finding Nemo poster$/,
94103
queryFn: `queryAllByAltText`,
95104
},
105+
'queryAllByAltText (for amp-img)': {
106+
dom: `
107+
<amp-img
108+
alt="
109+
Finding Nemo poster "
110+
src="/finding-nemo.png"
111+
/>`,
112+
query: /^Finding Nemo poster$/,
113+
queryFn: `queryAllByAltText`,
114+
},
96115
queryAllByPlaceholderText: {
97116
dom: `
98117
<input placeholder=" Dwayne 'The Rock' Johnson " />`,
@@ -198,6 +217,15 @@ cases(
198217
query: `Finding Nemo poster`,
199218
queryFn: `queryAllByAltText`,
200219
},
220+
'queryAllByAltText (for amp-img)': {
221+
dom: `
222+
<amp-img
223+
alt="Finding Nemo poster"
224+
src="/finding-nemo.png"
225+
/>`,
226+
query: `Finding Nemo poster`,
227+
queryFn: `queryAllByAltText`,
228+
},
201229
},
202230
)
203231

@@ -251,6 +279,10 @@ cases(
251279
dom: `<img alt="User ${LRM}name" src="username.jpg" />`,
252280
queryFn: 'queryAllByAltText',
253281
},
282+
'queryAllByAltText (for amp-img)': {
283+
dom: `<amp-img alt="User ${LRM}name" src="username.jpg" />`,
284+
queryFn: 'queryAllByAltText',
285+
},
254286
queryAllByTitle: {
255287
dom: `<div title="User ${LRM}name" />`,
256288
queryFn: 'queryAllByTitle',

src/queries/alt-text.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
import {wrapAllByQueryWithSuggestion} from '../query-helpers'
1+
import {
2+
queryAllByAttribute,
3+
wrapAllByQueryWithSuggestion,
4+
} from '../query-helpers'
25
import {checkContainerType} from '../helpers'
3-
import {AllByBoundAttribute, GetErrorFunction} from '../../types'
4-
import {matches, fuzzyMatches, makeNormalizer, buildQueries} from './all-utils'
6+
import {
7+
AllByBoundAttribute,
8+
GetErrorFunction,
9+
MatcherOptions,
10+
} from '../../types'
11+
import {buildQueries} from './all-utils'
12+
13+
// Valid tags are img, input, area and custom elements
14+
const VALID_TAG_REGEXP = /^(img|input|area|.+-.+)$/i
515

616
const queryAllByAltText: AllByBoundAttribute = (
717
container,
818
alt,
9-
{exact = true, collapseWhitespace, trim, normalizer} = {},
19+
options: MatcherOptions = {},
1020
) => {
1121
checkContainerType(container)
12-
const matcher = exact ? matches : fuzzyMatches
13-
const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
14-
return Array.from(
15-
container.querySelectorAll<HTMLElement>('img,input,area'),
16-
).filter(node =>
17-
matcher(node.getAttribute('alt'), node, alt, matchNormalizer),
22+
return queryAllByAttribute('alt', container, alt, options).filter(node =>
23+
VALID_TAG_REGEXP.test(node.tagName),
1824
)
1925
}
2026

0 commit comments

Comments
 (0)