@@ -14,9 +14,21 @@ import {
1414 buildQueries ,
1515} from './all-utils'
1616
17- const isSvgTitle = ( node : HTMLElement ) =>
18- node . tagName . toLowerCase ( ) === 'title' &&
19- node . parentElement ?. tagName . toLowerCase ( ) === 'svg'
17+ const getSvgTitleOwner = ( node : HTMLElement ) => {
18+ if (
19+ node . tagName . toLowerCase ( ) !== 'title' ||
20+ node . namespaceURI !== 'http://www.w3.org/2000/svg'
21+ ) {
22+ return null
23+ }
24+
25+ const parent = node . parentElement
26+ if ( ! parent || parent . tagName . toLowerCase ( ) === 'svg' ) {
27+ return node
28+ }
29+
30+ return parent
31+ }
2032
2133const queryAllByTitle : AllByBoundAttribute = (
2234 container ,
@@ -27,13 +39,28 @@ const queryAllByTitle: AllByBoundAttribute = (
2739 const matcher = exact ? matches : fuzzyMatches
2840 const matchNormalizer = makeNormalizer ( { collapseWhitespace, trim, normalizer} )
2941 return Array . from (
30- container . querySelectorAll < HTMLElement > ( '[title], svg > title' ) ,
31- ) . filter (
32- node =>
33- matcher ( node . getAttribute ( 'title' ) , node , text , matchNormalizer ) ||
34- ( isSvgTitle ( node ) &&
35- matcher ( getNodeText ( node ) , node , text , matchNormalizer ) ) ,
36- )
42+ container . querySelectorAll < HTMLElement > ( '[title], svg title' ) ,
43+ ) . reduce < HTMLElement [ ] > ( ( elements , node ) => {
44+ const addElement = ( element : HTMLElement ) => {
45+ if ( ! elements . includes ( element ) ) {
46+ elements . push ( element )
47+ }
48+ }
49+
50+ if ( matcher ( node . getAttribute ( 'title' ) , node , text , matchNormalizer ) ) {
51+ addElement ( node )
52+ }
53+
54+ const svgTitleOwner = getSvgTitleOwner ( node )
55+ if (
56+ svgTitleOwner &&
57+ matcher ( getNodeText ( node ) , svgTitleOwner , text , matchNormalizer )
58+ ) {
59+ addElement ( svgTitleOwner )
60+ }
61+
62+ return elements
63+ } , [ ] )
3764}
3865
3966const getMultipleError : GetErrorFunction < [ unknown ] > = ( c , title ) =>
0 commit comments