diff --git a/src/clipboard-action.js b/src/clipboard-action.js index c7d3cf4..ab6a5f6 100644 --- a/src/clipboard-action.js +++ b/src/clipboard-action.js @@ -42,18 +42,10 @@ class ClipboardAction { /** * Creates a fake textarea element, sets its value from `text` property, - * and makes a selection on it. */ - selectFake() { + createFakeElement() { const isRTL = document.documentElement.getAttribute('dir') === 'rtl'; - this.removeFake(); - - this.fakeHandlerCallback = () => this.removeFake(); - this.fakeHandler = - this.container.addEventListener('click', this.fakeHandlerCallback) || - true; - this.fakeElem = document.createElement('textarea'); // Prevent zooming on iOS this.fakeElem.style.fontSize = '12pt'; @@ -71,10 +63,29 @@ class ClipboardAction { this.fakeElem.setAttribute('readonly', ''); this.fakeElem.value = this.text; - this.container.appendChild(this.fakeElem); + return this.fakeElem; + } + + /** + * Get's the value of fakeElem, + * and makes a selection on it. + */ + selectFake() { + const fakeElem = this.createFakeElement(); + + this.fakeHandlerCallback = () => this.removeFake(); + + this.fakeHandler = + this.container.addEventListener('click', this.fakeHandlerCallback) || + true; + + this.container.appendChild(fakeElem); + + this.selectedText = select(fakeElem); - this.selectedText = select(this.fakeElem); this.copyText(); + + this.removeFake(); } /** diff --git a/test/clipboard-action.js b/test/clipboard-action.js index 8d5cc62..b589b4c 100644 --- a/test/clipboard-action.js +++ b/test/clipboard-action.js @@ -47,7 +47,9 @@ describe('ClipboardAction', () => { text: 'foo', }); - assert.equal(clip.fakeElem.style.right, '-9999px'); + const el = clip.createFakeElement(); + + assert.equal(el.style.right, '-9999px'); done(); }); }); @@ -90,7 +92,9 @@ describe('ClipboardAction', () => { text: 'blah', }); - assert.equal(clip.selectedText, clip.fakeElem.value); + const el = clip.createFakeElement(); + + assert.equal(clip.selectedText, el.value); }); });