Skip to content

Commit 8af435a

Browse files
authored
Merge pull request #26 from github/shadowroot
Add support for Shadow DOM follow-up
2 parents 9955d09 + 356470a commit 8af435a

File tree

4 files changed

+64
-15
lines changed

4 files changed

+64
-15
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"chai": "^4.2.0",
3131
"eslint": "^6.5.1",
3232
"eslint-plugin-github": "^3.1.3",
33-
"flow-bin": "^0.109.0",
33+
"flow-bin": "^0.111.1",
3434
"karma": "^4.3.0",
3535
"karma-chai": "^0.1.0",
3636
"karma-chrome-launcher": "^3.1.0",

src/clipboard-copy-element.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ function copy(button: HTMLElement) {
1313
if (text) {
1414
copyText(text).then(trigger)
1515
} else if (id) {
16-
const node = button.ownerDocument.getElementById(id)
16+
const root = 'getRootNode' in Element.prototype ? button.getRootNode() : button.ownerDocument
17+
if (!(root instanceof Document || ('ShadowRoot' in window && root instanceof ShadowRoot))) return
18+
const node = root.getElementById(id)
1719
if (node) copyTarget(node).then(trigger)
1820
}
1921
}

test/test.js

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)