@@ -33,6 +33,39 @@ function escapeCSSSelector(str) {
33
33
. replace ( / [ ! " # $ % & ' ( ) * + , . / : ; < = > ? @ [ \\ \] ^ ` { | } ~ ] / g, '\\$&' )
34
34
. replace ( / ^ \d / , '\\3$& ' ) ;
35
35
}
36
+ function generateSelectorWithShadow ( elm ) {
37
+ const selectors = getShadowSelector ( elm ) ;
38
+ if ( typeof selectors === 'string' ) {
39
+ return selectors ;
40
+ } else {
41
+ // merge selectors of an array with ,
42
+ return selectors . join ( ',' ) . replace ( / , $ / , '' ) ;
43
+ }
44
+ }
45
+
46
+ function getShadowSelector ( elm ) {
47
+ if ( ! elm ) {
48
+ return '' ;
49
+ }
50
+ let doc = ( elm . getRootNode && elm . getRootNode ( ) ) || document ;
51
+ // Not a DOCUMENT_FRAGMENT - shadow DOM
52
+ if ( doc . nodeType !== 11 ) {
53
+ return getFullPathSelector ( elm ) ;
54
+ }
55
+
56
+ const stack = [ ] ;
57
+ while ( doc . nodeType === 11 ) {
58
+ if ( ! doc . host ) {
59
+ return '' ;
60
+ }
61
+ stack . unshift ( { elm, doc } ) ;
62
+ elm = doc . host ;
63
+ doc = elm . getRootNode ( ) ;
64
+ }
65
+
66
+ stack . unshift ( { elm, doc } ) ;
67
+ return stack . map ( item => getFullPathSelector ( item . elm ) ) ;
68
+ }
36
69
37
70
function getFullPathSelector ( elm ) {
38
71
if ( elm . nodeName === 'HTML' || elm . nodeName === 'BODY' ) {
@@ -116,8 +149,12 @@ function getSourceOpt(element) {
116
149
. filter ( attr => ! attr . name . startsWith ( 'data-percy-' ) )
117
150
. map ( attr => `${ attr . name } ="${ attr . value } "` )
118
151
. join ( ' ' ) ;
119
-
120
- result = attributes ? `<${ tagName } ${ attributes } >` : `<${ tagName } >` ;
152
+ const closingTag = element . children . length ? false : true ;
153
+ if ( closingTag ) {
154
+ result = `<${ tagName } ${ attributes } >${ element . textContent } </${ tagName } >` ;
155
+ } else {
156
+ result = attributes ? `<${ tagName } ${ attributes } >` : `<${ tagName } >` ;
157
+ }
121
158
result = truncate ( result , 300 ) ; // Truncate to 300 characters
122
159
// Store in cache
123
160
sourceCache . set ( element , result ) ;
@@ -210,9 +247,7 @@ DqElement.prototype = {
210
247
*/
211
248
get selector ( ) {
212
249
if ( axe . _cache . get ( 'runTypeAOpt' ) ) {
213
- return (
214
- this . spec . selector || [ getFullPathSelector ( this . element , this . _options ) ]
215
- ) ;
250
+ return this . spec . selector || [ generateSelectorWithShadow ( this . element ) ] ;
216
251
}
217
252
return this . spec . selector || [ getSelector ( this . element , this . _options ) ] ;
218
253
} ,
0 commit comments