diff --git a/js/common.js b/js/common.js index 5ad72afd..346e1423 100644 --- a/js/common.js +++ b/js/common.js @@ -152,3 +152,22 @@ exports.urlToString = function (url) { encodeURI(url.query.join('').replace(/^&+|&+$/gm,'')) : '') + (url.fragment ? '#' + encodeURI(url.fragment) : ''); }; + +exports.enableClipboard = function () { + navigator.clipboard = (function () { + let savedText = ""; + + async function writeText(text) { + savedText = text; + }; + + async function readText() { + return savedText; + }; + + return { + writeText, + readText, + }; + })(); +}; diff --git a/js/privatebin.js b/js/privatebin.js index 77afc8b7..b67de8ad 100644 --- a/js/privatebin.js +++ b/js/privatebin.js @@ -5454,11 +5454,11 @@ jQuery.PrivateBin = (function($, RawDeflate) { const CopyToClipboard = (function () { const me = {}; - let copyButton = $('#prettyMessageCopyBtn'), - copyLinkButton = $('#copyLink'), - copyIcon = $('#copyIcon'), - successIcon = $('#copySuccessIcon'), - shortcutHint = $('#copyShortcutHintText'), + let copyButton, + copyLinkButton, + copyIcon, + successIcon, + shortcutHint, url; /** @@ -5613,6 +5613,12 @@ jQuery.PrivateBin = (function($, RawDeflate) { * @function */ me.init = function() { + copyButton = $('#prettyMessageCopyBtn'); + copyLinkButton = $('#copyLink'); + copyIcon = $('#copyIcon'); + successIcon = $('#copySuccessIcon'); + shortcutHint = $('#copyShortcutHintText'); + handleCopyButtonClick(); handleCopyLinkButtonClick(); handleKeyboardShortcut(); diff --git a/js/test/CopyToClipboard.js b/js/test/CopyToClipboard.js new file mode 100644 index 00000000..9d2df2f1 --- /dev/null +++ b/js/test/CopyToClipboard.js @@ -0,0 +1,123 @@ +'use strict'; +const common = require('../common'); + +describe('CopyToClipboard', function() { + this.timeout(30000); + + describe ('Copy paste co clipboard', function () { + jsc.property('Copy with button click', common.jscFormats(), 'nestring', async function (format, text) { + var clean = jsdom(); + common.enableClipboard(); + + $('body').html( + '' + ); + + $.PrivateBin.PasteViewer.init(); + $.PrivateBin.PasteViewer.setFormat(format); + $.PrivateBin.PasteViewer.setText(text); + $.PrivateBin.PasteViewer.run(); + + $.PrivateBin.CopyToClipboard.init(); + + $('#prettyMessageCopyBtn').trigger('click'); + + const savedToClipboardText = await navigator.clipboard.readText(); + + clean(); + + return text === savedToClipboardText; + }); + + /** + * Unfortunately in JSVerify impossible to check if copy with shortcut when user selected some text on the page + * (the copy paste to clipboard should not work in this case) due to lucking window.getSelection() in jsdom. + */ + jsc.property('Copy with keyboard shortcut', common.jscFormats(), 'nestring', async function (format, text) { + var clean = jsdom(); + common.enableClipboard(); + + $('body').html( + '
+++ no paste text ' + + '+++
' + ); + + $.PrivateBin.PasteViewer.init(); + $.PrivateBin.PasteViewer.setFormat(format); + $.PrivateBin.PasteViewer.setText(text); + $.PrivateBin.PasteViewer.run(); + + $.PrivateBin.CopyToClipboard.init(); + + $('body').trigger('copy'); + + const copiedTextWithoutSelectedText = await navigator.clipboard.readText(); + + clean(); + + return copiedTextWithoutSelectedText === text; + }); + }); + + + jsc.property('Copy link to clipboard', 'nestring', async function (text) { + var clean = jsdom(); + common.enableClipboard(); + + $('body').html(''); + + $.PrivateBin.CopyToClipboard.init(); + $.PrivateBin.CopyToClipboard.setUrl(text); + + $('#copyLink').trigger('click'); + + const copiedText = await navigator.clipboard.readText(); + + clean(); + + return text === copiedText; + }); + + + describe('Keyboard shortcut hint', function () { + jsc.property('Show hint', 'nestring', function (text) { + var clean = jsdom(); + + $('body').html(''); + + $.PrivateBin.CopyToClipboard.init(); + $.PrivateBin.CopyToClipboard.showKeyboardShortcutHint(); + + const keyboardShortcutHint = $('#copyShortcutHintText').text(); + + clean(); + + return keyboardShortcutHint.length > 0; + }); + + jsc.property('Hide hint', 'nestring', function (text) { + var clean = jsdom(); + + $('body').html('' + text + ''); + + $.PrivateBin.CopyToClipboard.init(); + $.PrivateBin.CopyToClipboard.hideKeyboardShortcutHint(); + + const keyboardShortcutHint = $('#copyShortcutHintText').text(); + + clean(); + + return keyboardShortcutHint.length === 0; + }); + }); + +}); \ No newline at end of file diff --git a/lib/Configuration.php b/lib/Configuration.php index e3a8069c..d181cf99 100644 --- a/lib/Configuration.php +++ b/lib/Configuration.php @@ -108,7 +108,7 @@ class Configuration 'js/kjua-0.9.0.js' => 'sha512-CVn7af+vTMBd9RjoS4QM5fpLFEOtBCoB0zPtaqIDC7sF4F8qgUSRFQQpIyEDGsr6yrjbuOLzdf20tkHHmpaqwQ==', 'js/legacy.js' => 'sha512-UxW/TOZKon83n6dk/09GsYKIyeO5LeBHokxyIq+r7KFS5KMBeIB/EM7NrkVYIezwZBaovnyNtY2d9tKFicRlXg==', 'js/prettify.js' => 'sha512-puO0Ogy++IoA2Pb9IjSxV1n4+kQkKXYAEUtVzfZpQepyDPyXk8hokiYDS7ybMogYlyyEIwMLpZqVhCkARQWLMg==', - 'js/privatebin.js' => 'sha512-/hDE31uyN+PB84PJ+C7fCFcdbfPy3TeV4slm6eSFrvqNhVMn/FPh/wsjyKLdQGW7fqBdxlGfq5+CDwUfF8fY0w==', + 'js/privatebin.js' => 'sha512-POa+8KNXFFwJFsqp7r9APmR5Rc1w2l363y+OScSzLCySrHN7UhOOgt1VH/o8mVddFvvUozj3FZVmdkTxRlrS5g==', 'js/purify-3.2.3.js' => 'sha512-m8Wa/I//YoYMiIahBxDDwYfTnycl+i2DwH58nR8ps1o4KWqXzF8k1K4qHDgAz2HSQFNCNNKH/Qcbfu/jLOuhuQ==', 'js/rawinflate-0.3.js' => 'sha512-g8uelGgJW9A/Z1tB6Izxab++oj5kdD7B4qC7DHwZkB6DGMXKyzx7v5mvap2HXueI2IIn08YlRYM56jwWdm2ucQ==', 'js/showdown-2.1.0.js' => 'sha512-WYXZgkTR0u/Y9SVIA4nTTOih0kXMEd8RRV6MLFdL6YU8ymhR528NLlYQt1nlJQbYz4EW+ZsS0fx1awhiQJme1Q==',