Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ByAltText): Always include custom elements #1049

Merged
merged 3 commits into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/__tests__/queries.find.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ test('find asynchronously finds elements', async () => {
<select><option>display value</option></select>
<input placeholder="placeholder" />
<img alt="test alt text" src="/lucy-ricardo.png" />
<div alt="test alt text" />
<span title="test title" />
<div role="dialog"></div>
<div role="meter progressbar"></div>
Expand Down
32 changes: 32 additions & 0 deletions src/__tests__/text-matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ cases(
query: `Finding Nemo poster`,
queryFn: `queryAllByAltText`,
},
'queryAllByAltText (for amp-img)': {
dom: `
<amp-img
alt="Finding Nemo poster"
src="/finding-nemo.png"
/>`,
query: `Finding Nemo poster`,
queryFn: `queryAllByAltText`,
},
queryAllByPlaceholderText: {
dom: `<input placeholder="Dwayne 'The Rock' Johnson" />`,
query: `Dwayne 'The Rock' Johnson`,
Expand Down Expand Up @@ -93,6 +102,16 @@ cases(
query: /^Finding Nemo poster$/,
queryFn: `queryAllByAltText`,
},
'queryAllByAltText (for amp-img)': {
dom: `
<amp-img
alt="
Finding Nemo poster "
src="/finding-nemo.png"
/>`,
query: /^Finding Nemo poster$/,
queryFn: `queryAllByAltText`,
},
queryAllByPlaceholderText: {
dom: `
<input placeholder=" Dwayne 'The Rock' Johnson " />`,
Expand Down Expand Up @@ -198,6 +217,15 @@ cases(
query: `Finding Nemo poster`,
queryFn: `queryAllByAltText`,
},
'queryAllByAltText (for amp-img)': {
dom: `
<amp-img
alt="Finding Nemo poster"
src="/finding-nemo.png"
/>`,
query: `Finding Nemo poster`,
queryFn: `queryAllByAltText`,
},
},
)

Expand Down Expand Up @@ -251,6 +279,10 @@ cases(
dom: `<img alt="User ${LRM}name" src="username.jpg" />`,
queryFn: 'queryAllByAltText',
},
'queryAllByAltText (for amp-img)': {
dom: `<amp-img alt="User ${LRM}name" src="username.jpg" />`,
queryFn: 'queryAllByAltText',
},
queryAllByTitle: {
dom: `<div title="User ${LRM}name" />`,
queryFn: 'queryAllByTitle',
Expand Down
26 changes: 16 additions & 10 deletions src/queries/alt-text.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import {wrapAllByQueryWithSuggestion} from '../query-helpers'
import {
queryAllByAttribute,
wrapAllByQueryWithSuggestion,
} from '../query-helpers'
import {checkContainerType} from '../helpers'
import {AllByBoundAttribute, GetErrorFunction} from '../../types'
import {matches, fuzzyMatches, makeNormalizer, buildQueries} from './all-utils'
import {
AllByBoundAttribute,
GetErrorFunction,
MatcherOptions,
} from '../../types'
import {buildQueries} from './all-utils'

// Valid tags are img, input, area and custom elements
SantoJambit marked this conversation as resolved.
Show resolved Hide resolved
const VALID_TAG_REGEXP = /^(img|input|area|.+-.+)$/i

const queryAllByAltText: AllByBoundAttribute = (
container,
alt,
{exact = true, collapseWhitespace, trim, normalizer} = {},
options: MatcherOptions = {},
) => {
checkContainerType(container)
const matcher = exact ? matches : fuzzyMatches
const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
return Array.from(
container.querySelectorAll<HTMLElement>('img,input,area'),
).filter(node =>
matcher(node.getAttribute('alt'), node, alt, matchNormalizer),
return queryAllByAttribute('alt', container, alt, options).filter(node =>
VALID_TAG_REGEXP.test(node.tagName),
)
}

Expand Down