@@ -299,4 +299,50 @@ describe( 'Unique Selector Tests', () =>
299
299
expect ( uniqueSelector ) . to . equal ( 'span' ) ;
300
300
} )
301
301
} )
302
+
303
+ describe ( 'shadow dom' , ( ) => {
304
+ it ( 'builds expected selector inside and outside shadow context' , ( ) => {
305
+ $ ( 'body' ) . append ( '<div id="shadow-host" class="shadow-host-class"></div>' ) ;
306
+
307
+ const hostNode = $ ( '#shadow-host' ) . get ( 0 ) ;
308
+
309
+ const shadowRoot = hostNode . attachShadow ( { mode : "open" } )
310
+ const shadowElement = hostNode . ownerDocument . createElement ( 'div' )
311
+ shadowElement . innerHTML = `
312
+ <div id="inner-shadow-container">
313
+ <button id="shadow-button" class="shadow-button-class">Click Me</button>
314
+ </div>
315
+ `
316
+ shadowRoot . appendChild ( shadowElement ) ;
317
+
318
+ const uniqueSelectorForHost = unique ( hostNode ) ;
319
+ expect ( uniqueSelectorForHost ) . to . equal ( '#shadow-host' ) ;
320
+
321
+ const uniqueSelectorForShadowContent = unique ( shadowElement . querySelector ( '#shadow-button' ) )
322
+ expect ( uniqueSelectorForShadowContent ) . to . equal ( '#shadow-button' ) ;
323
+ } )
324
+
325
+ it ( 'builds unique selector scoped to shadow root' , ( ) => {
326
+ $ ( 'body' ) . append ( '<div id="shadow-host" class="shadow-host-class"></div>' ) ;
327
+ $ ( 'body' ) . append ( '<button class="shadow-button-class">Click Me Third</button>' ) ;
328
+
329
+ const hostNode = $ ( '#shadow-host' ) . get ( 0 ) ;
330
+
331
+ const shadowRoot = hostNode . attachShadow ( { mode : "open" } )
332
+ const shadowElement = hostNode . ownerDocument . createElement ( 'div' )
333
+ shadowElement . innerHTML = `
334
+ <div id="inner-shadow-container">
335
+ <button class="shadow-button-class">Click Me First</button>
336
+ <button class="shadow-button-class">Click Me Second</button>
337
+ </div>
338
+ `
339
+ shadowRoot . appendChild ( shadowElement ) ;
340
+
341
+ const uniqueSelectorInRootDocument = unique ( $ ( 'body' ) . find ( '.shadow-button-class' ) . get ( 0 ) ) ;
342
+ expect ( uniqueSelectorInRootDocument ) . to . equal ( '.shadow-button-class' ) ;
343
+
344
+ const uniqueSelectorForShadowContent = unique ( shadowElement . querySelectorAll ( '.shadow-button-class' ) [ 0 ] )
345
+ expect ( uniqueSelectorForShadowContent ) . to . equal ( '#inner-shadow-container > :nth-child(1)' ) ;
346
+ } )
347
+ } )
302
348
} ) ;
0 commit comments