-
Notifications
You must be signed in to change notification settings - Fork 470
/
Copy pathtitle.ts
63 lines (57 loc) · 1.85 KB
/
title.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import {wrapAllByQueryWithSuggestion} from '../query-helpers'
import {checkContainerType} from '../helpers'
import {
AllByBoundAttribute,
GetErrorFunction,
Matcher,
MatcherOptions,
} from '../../types'
import {
fuzzyMatches,
matches,
getConfig,
makeNormalizer,
getNodeText,
buildQueries,
} from './all-utils'
const isSvgTitle = (node: HTMLElement) =>
node.tagName.toLowerCase() === 'title' &&
node.parentElement?.tagName.toLowerCase() === 'svg'
const queryAllByTitle: AllByBoundAttribute = (
container,
text,
{exact = true, collapseWhitespace, trim, normalizer} = {},
) => {
checkContainerType(container)
const matcher = exact ? matches : fuzzyMatches
const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
return Array.from(
getConfig().queryAllElements<HTMLElement, HTMLElement>(
container,
'[title], svg > title',
),
).filter(
node =>
matcher(node.getAttribute('title'), node, text, matchNormalizer) ||
(isSvgTitle(node) &&
matcher(getNodeText(node), node, text, matchNormalizer)),
)
}
const getMultipleError: GetErrorFunction<[unknown]> = (c, title) =>
`Found multiple elements with the title: ${title}.`
const getMissingError: GetErrorFunction<[unknown]> = (c, title) =>
`Unable to find an element with the title: ${title}.`
const queryAllByTitleWithSuggestions = wrapAllByQueryWithSuggestion<
// @ts-expect-error -- See `wrapAllByQueryWithSuggestion` Argument constraint comment
[title: Matcher, options?: MatcherOptions]
>(queryAllByTitle, queryAllByTitle.name, 'queryAll')
const [queryByTitle, getAllByTitle, getByTitle, findAllByTitle, findByTitle] =
buildQueries(queryAllByTitle, getMultipleError, getMissingError)
export {
queryByTitle,
queryAllByTitleWithSuggestions as queryAllByTitle,
getByTitle,
getAllByTitle,
findAllByTitle,
findByTitle,
}