@@ -37,16 +37,6 @@ describe('clipboard-copy element', function() {
3737
3838 describe ( 'target element' , function ( ) {
3939 const nativeClipboard = navigator . clipboard
40- function defineClipboard ( customClipboard ) {
41- Object . defineProperty ( navigator , 'clipboard' , {
42- enumerable : false ,
43- configurable : true ,
44- get ( ) {
45- return customClipboard
46- }
47- } )
48- }
49-
5040 let whenCopied
5141 beforeEach ( function ( ) {
5242 const container = document . createElement ( 'div' )
@@ -160,4 +150,61 @@ describe('clipboard-copy element', function() {
160150 } )
161151 } )
162152 } )
153+
154+ describe ( 'shadow DOM context' , function ( ) {
155+ const nativeClipboard = navigator . clipboard
156+ let whenCopied
157+ beforeEach ( function ( ) {
158+ const container = document . createElement ( 'div' )
159+ container . id = 'shadow'
160+ const elementInDocument = document . createElement ( 'div' )
161+ elementInDocument . id = 'copy-target'
162+ elementInDocument . textContent = 'Target in Document'
163+ const shadowRoot = container . attachShadow ( { mode : 'open' } )
164+ shadowRoot . innerHTML = `
165+ <clipboard-copy for="copy-target">
166+ Copy
167+ </clipboard-copy>
168+ <div id="copy-target">Target in shadowRoot</div>`
169+ document . body . append ( container )
170+ document . body . append ( elementInDocument )
171+ container . click ( )
172+
173+ let copiedText = null
174+ defineClipboard ( {
175+ writeText ( text ) {
176+ copiedText = text
177+ return Promise . resolve ( )
178+ }
179+ } )
180+
181+ whenCopied = new Promise ( resolve => {
182+ shadowRoot . addEventListener ( 'clipboard-copy' , ( ) => resolve ( copiedText ) , { once : true } )
183+ } )
184+ } )
185+
186+ afterEach ( function ( ) {
187+ document . body . innerHTML = ''
188+ defineClipboard ( nativeClipboard )
189+ } )
190+
191+ it ( 'copies from within its shadow root' , function ( ) {
192+ const shadow = document . querySelector ( '#shadow' )
193+ shadow . shadowRoot . querySelector ( 'clipboard-copy' ) . click ( )
194+
195+ return whenCopied . then ( text => {
196+ assert . equal ( text , 'Target in shadowRoot' )
197+ } )
198+ } )
199+ } )
163200} )
201+
202+ function defineClipboard ( customClipboard ) {
203+ Object . defineProperty ( navigator , 'clipboard' , {
204+ enumerable : false ,
205+ configurable : true ,
206+ get ( ) {
207+ return customClipboard
208+ }
209+ } )
210+ }
0 commit comments