clipboard-copy
Advanced tools
Comparing version 1.1.0 to 1.2.0
20
index.js
@@ -7,3 +7,3 @@ module.exports = clipboardCopy | ||
span.textContent = text | ||
span.style.whiteSpace = 'pre' // Preserve spaces and newlines | ||
span.style.whiteSpace = 'pre' // Preserve consecutive spaces and newlines | ||
@@ -13,4 +13,4 @@ // An <iframe> isolates the <span> from the page's styles | ||
iframe.sandbox = 'allow-same-origin' | ||
document.body.appendChild(iframe) | ||
document.body.appendChild(iframe) | ||
var win = iframe.contentWindow | ||
@@ -20,10 +20,17 @@ win.document.body.appendChild(span) | ||
var selection = win.getSelection() | ||
// Firefox fails to get a selection from <iframe> window, so fallback | ||
if (!selection) { | ||
win = window | ||
selection = win.getSelection() | ||
document.body.appendChild(span) | ||
} | ||
var range = win.document.createRange() | ||
selection.removeAllRanges() | ||
range.selectNode(span) | ||
selection.addRange(range) | ||
var success = false | ||
try { | ||
selection.removeAllRanges() | ||
range.selectNode(span) | ||
selection.addRange(range) | ||
success = win.document.execCommand('copy') | ||
@@ -33,2 +40,3 @@ } catch (err) {} | ||
selection.removeAllRanges() | ||
span.remove() | ||
iframe.remove() | ||
@@ -35,0 +43,0 @@ |
{ | ||
"name": "clipboard-copy", | ||
"description": "Lightweight copy to clipboard for the web", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Feross Aboukhadijeh", |
@@ -1,2 +0,2 @@ | ||
# clipboard-copy [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] | ||
# clipboard-copy [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url] | ||
@@ -9,2 +9,4 @@ [travis-image]: https://img.shields.io/travis/feross/clipboard-copy/master.svg | ||
[downloads-url]: https://npmjs.org/package/clipboard-copy | ||
[standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg | ||
[standard-url]: https://standardjs.com | ||
@@ -50,3 +52,3 @@ ### Lightweight copy to clipboard for the web | ||
- **`clipboard-copy` (this package): 453 bytes** | ||
- **`clipboard-copy` (this package): 542 bytes** | ||
- [`clipboard-js`](https://www.npmjs.com/package/clipboard-js): 1.83 kB | ||
@@ -53,0 +55,0 @@ - [`clipboard`](https://www.npmjs.com/package/clipboard): 3.19 kB |
5145
42
58