Skip to content

Commit 9bcca51

Browse files
committed
feat: Support all tags for alt-text
1 parent b28586e commit 9bcca51

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

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: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
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'
512

613
const queryAllByAltText: AllByBoundAttribute = (
714
container,
815
alt,
9-
{exact = true, collapseWhitespace, trim, normalizer} = {},
16+
options: MatcherOptions = {},
1017
) => {
1118
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),
18-
)
19+
return queryAllByAttribute('alt', container, alt, options)
1920
}
2021

2122
const getMultipleError: GetErrorFunction<[unknown]> = (c, alt) =>

0 commit comments

Comments
 (0)