@@ -160,4 +160,61 @@ describe('clipboard-copy element', function() {
160
160
} )
161
161
} )
162
162
} )
163
+
164
+ describe ( 'shadow DOM context' , function ( ) {
165
+ const nativeClipboard = navigator . clipboard
166
+ function defineClipboard ( customClipboard ) {
167
+ Object . defineProperty ( navigator , 'clipboard' , {
168
+ enumerable : false ,
169
+ configurable : true ,
170
+ get ( ) {
171
+ return customClipboard
172
+ }
173
+ } )
174
+ }
175
+
176
+ let whenCopied
177
+ beforeEach ( function ( ) {
178
+ const container = document . createElement ( 'div' )
179
+ container . id = 'shadow'
180
+ const elementInDocument = document . createElement ( 'div' )
181
+ elementInDocument . id = 'copy-target'
182
+ elementInDocument . textContent = 'Target in Document'
183
+ const shadowRoot = container . attachShadow ( { mode : 'open' } )
184
+ shadowRoot . innerHTML = `
185
+ <clipboard-copy for="copy-target">
186
+ Copy
187
+ </clipboard-copy>
188
+ <div id="copy-target">Target in shadowRoot</div>`
189
+ document . body . append ( container )
190
+ document . body . append ( elementInDocument )
191
+ container . click ( )
192
+
193
+ let copiedText = null
194
+ defineClipboard ( {
195
+ writeText ( text ) {
196
+ copiedText = text
197
+ return Promise . resolve ( )
198
+ }
199
+ } )
200
+
201
+ whenCopied = new Promise ( resolve => {
202
+ shadowRoot . addEventListener ( 'clipboard-copy' , ( ) => resolve ( copiedText ) , { once : true } )
203
+ } )
204
+ } )
205
+
206
+ afterEach ( function ( ) {
207
+ document . body . innerHTML = ''
208
+ defineClipboard ( nativeClipboard )
209
+ } )
210
+
211
+ it ( 'node' , function ( ) {
212
+ const shadow = document . querySelector ( '#shadow' )
213
+ shadow . shadowRoot . querySelector ( 'clipboard-copy' ) . click ( )
214
+
215
+ return whenCopied . then ( text => {
216
+ assert . equal ( text , 'Target in shadowRoot' )
217
+ } )
218
+ } )
219
+ } )
163
220
} )
0 commit comments