CopyToClipboard.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. 'use strict';
  2. const common = require('../common');
  3. describe('CopyToClipboard', function () {
  4. this.timeout(30000);
  5. describe('Copy document to clipboard', function () {
  6. jsc.property('Copy with button click',
  7. common.jscFormats(),
  8. 'nestring',
  9. async function (format, text) {
  10. var clean = globalThis.cleanup();
  11. common.enableClipboard();
  12. document.body.innerHTML = (
  13. '<div id="placeholder" class="hidden">+++ no document text ' +
  14. '+++</div><div id="prettymessage" class="hidden">' +
  15. '<button type="button" id="prettyMessageCopyBtn"><svg id="copyIcon"></svg>' +
  16. '<svg id="copySuccessIcon"></svg></button><pre ' +
  17. 'id="prettyprint" class="prettyprint linenums:1"></pre>' +
  18. '</div><div id="plaintext" class="hidden"></div>'
  19. );
  20. PrivateBin.PasteViewer.init();
  21. PrivateBin.PasteViewer.setFormat(format);
  22. PrivateBin.PasteViewer.setText(text);
  23. PrivateBin.PasteViewer.run();
  24. PrivateBin.CopyToClipboard.init();
  25. document.getElementById('prettyMessageCopyBtn').click();
  26. const savedToClipboardText = await navigator.clipboard.readText();
  27. clean();
  28. return text === savedToClipboardText;
  29. }
  30. );
  31. /**
  32. * Unfortunately in JSVerify impossible to check if copy with shortcut when user selected some text on the page
  33. * (the copy document to clipboard should not work in this case) due to lacking window.getSelection() in jsdom.
  34. */
  35. jsc.property('Copy with keyboard shortcut',
  36. common.jscFormats(),
  37. 'nestring',
  38. async function (format, text) {
  39. var clean = globalThis.cleanup();
  40. common.enableClipboard();
  41. document.body.innerHTML = (
  42. '<div id="placeholder">+++ no document text ' +
  43. '+++</div><div id="prettymessage" class="hidden">' +
  44. '<button type="button" id="prettyMessageCopyBtn"><svg id="copyIcon"></svg>' +
  45. '<svg id="copySuccessIcon"></svg></button><pre ' +
  46. 'id="prettyprint" class="prettyprint linenums:1"></pre>' +
  47. '</div><div id="plaintext" class="hidden"></div>'
  48. );
  49. PrivateBin.PasteViewer.init();
  50. PrivateBin.PasteViewer.setFormat(format);
  51. PrivateBin.PasteViewer.setText(text);
  52. PrivateBin.PasteViewer.run();
  53. PrivateBin.CopyToClipboard.init();
  54. document.body.dispatchEvent(getClipboardEvent());
  55. const copiedTextWithoutSelectedText = await navigator.clipboard.readText();
  56. clean();
  57. return copiedTextWithoutSelectedText === text;
  58. }
  59. );
  60. /**
  61. * ClipboardEvent is not supported in jsdom yet, so this creates a mock event to trigger the copy event listener.
  62. *
  63. * See https://github.com/jsdom/jsdom/issues/1568
  64. *
  65. * @returns {ClipboardEvent}
  66. */
  67. function getClipboardEvent() {
  68. /** {@type ClipboardEvent} */
  69. const clipboardEvent = new Event('copy', {
  70. bubbles: true,
  71. cancelable: true,
  72. composed: true
  73. });
  74. clipboardEvent['clipboardData'] = {
  75. getData: function () {
  76. return '';
  77. }
  78. }
  79. return clipboardEvent;
  80. }
  81. });
  82. jsc.property('Copy link to clipboard',
  83. 'nestring',
  84. async function (text) {
  85. var clean = globalThis.cleanup();
  86. common.enableClipboard();
  87. document.body.innerHTML = '<button id="copyLink"></button>';
  88. PrivateBin.CopyToClipboard.init();
  89. PrivateBin.CopyToClipboard.setUrl(text);
  90. document.getElementById('copyLink').click();
  91. const copiedText = await navigator.clipboard.readText();
  92. clean();
  93. return text === copiedText;
  94. }
  95. );
  96. describe('Keyboard shortcut hint', function () {
  97. jsc.property('Show hint',
  98. 'nestring',
  99. function (text) {
  100. var clean = globalThis.cleanup();
  101. document.body.innerHTML = '<small id="copyShortcutHintText"></small>';
  102. PrivateBin.CopyToClipboard.init();
  103. PrivateBin.CopyToClipboard.showKeyboardShortcutHint();
  104. const keyboardShortcutHint = document.getElementById('copyShortcutHintText').textContent;
  105. clean();
  106. return keyboardShortcutHint.length > 0;
  107. }
  108. );
  109. jsc.property('Hide hint',
  110. 'nestring',
  111. function (text) {
  112. var clean = globalThis.cleanup();
  113. document.body.innerHTML = '<small id="copyShortcutHintText">' + text + '</small>';
  114. PrivateBin.CopyToClipboard.init();
  115. PrivateBin.CopyToClipboard.hideKeyboardShortcutHint();
  116. const keyboardShortcutHint = document.getElementById('copyShortcutHintText').textContent;
  117. clean();
  118. return keyboardShortcutHint.length === 0;
  119. }
  120. );
  121. });
  122. });