clipboard.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import Clipboard from '../src/clipboard';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import ClipboardObserver from '../src/clipboardobserver';
  9. import { stringify as stringifyView } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  10. import {
  11. stringify as stringifyModel,
  12. setData as setModelData,
  13. getData as getModelData
  14. } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  15. import ViewDocumentFragment from '@ckeditor/ckeditor5-engine/src/view/documentfragment';
  16. import ViewText from '@ckeditor/ckeditor5-engine/src/view/text';
  17. describe( 'Clipboard feature', () => {
  18. let editor, editingView, clipboardPlugin;
  19. beforeEach( () => {
  20. return VirtualTestEditor.create( {
  21. plugins: [ Clipboard, Paragraph ]
  22. } )
  23. .then( ( newEditor ) => {
  24. editor = newEditor;
  25. editingView = editor.editing.view;
  26. clipboardPlugin = editor.plugins.get( 'clipboard/clipboard' );
  27. } );
  28. } );
  29. describe( 'constructor()', () => {
  30. it( 'registers ClipboardObserver', () => {
  31. expect( editingView.getObserver( ClipboardObserver ) ).to.be.instanceOf( ClipboardObserver );
  32. } );
  33. } );
  34. describe( 'clipboard paste pipeline', () => {
  35. describe( 'takes HTML data from the dataTransfer', () => {
  36. it( 'and fires the input event on the editingView', ( done ) => {
  37. const dataTransferMock = createDataTransfer( { 'text/html': '<p>x</p>', 'text/plain': 'y' } );
  38. const preventDefaultSpy = sinon.spy();
  39. editingView.on( 'input', ( evt, data ) => {
  40. expect( preventDefaultSpy.calledOnce ).to.be.true;
  41. expect( data.dataTransfer ).to.equal( dataTransferMock );
  42. done();
  43. } );
  44. editingView.fire( 'paste', {
  45. dataTransfer: dataTransferMock,
  46. preventDefault: preventDefaultSpy
  47. } );
  48. } );
  49. it( 'and fires the inputTransformation event on the clipboardPlugin', ( done ) => {
  50. const dataTransferMock = createDataTransfer( { 'text/html': '<p>x</p>', 'text/plain': 'y' } );
  51. const preventDefaultSpy = sinon.spy();
  52. clipboardPlugin.on( 'inputTransformation', ( evt, data ) => {
  53. expect( data.content ).is.instanceOf( ViewDocumentFragment );
  54. expect( stringifyView( data.content ) ).to.equal( '<p>x</p>' );
  55. done();
  56. } );
  57. editingView.fire( 'paste', {
  58. dataTransfer: dataTransferMock,
  59. preventDefault: preventDefaultSpy
  60. } );
  61. } );
  62. } );
  63. describe( 'takes plain text data from the dataTransfer if there is no HTML', () => {
  64. it( 'and fires the input event on the editingView', ( done ) => {
  65. const dataTransferMock = createDataTransfer( { 'text/plain': 'x\n\ny z' } );
  66. const preventDefaultSpy = sinon.spy();
  67. editingView.on( 'input', ( evt, data ) => {
  68. expect( preventDefaultSpy.calledOnce ).to.be.true;
  69. expect( data.dataTransfer ).to.equal( dataTransferMock );
  70. done();
  71. } );
  72. editingView.fire( 'paste', {
  73. dataTransfer: dataTransferMock,
  74. preventDefault: preventDefaultSpy
  75. } );
  76. } );
  77. it( 'and fires the inputTransformation event on the clipboardPlugin', ( done ) => {
  78. const dataTransferMock = createDataTransfer( { 'text/plain': 'x\n\ny z' } );
  79. const preventDefaultSpy = sinon.spy();
  80. clipboardPlugin.on( 'inputTransformation', ( evt, data ) => {
  81. expect( data.content ).is.instanceOf( ViewDocumentFragment );
  82. expect( stringifyView( data.content ) ).to.equal( '<p>x</p><p>y z</p>' );
  83. done();
  84. } );
  85. editingView.fire( 'paste', {
  86. dataTransfer: dataTransferMock,
  87. preventDefault: preventDefaultSpy
  88. } );
  89. } );
  90. } );
  91. it( 'fires events with empty data if there is no HTML nor plain text', ( done ) => {
  92. const dataTransferMock = createDataTransfer( {} );
  93. const preventDefaultSpy = sinon.spy();
  94. const editorViewCalled = sinon.spy();
  95. editingView.on( 'input', ( evt, data ) => {
  96. expect( preventDefaultSpy.calledOnce ).to.be.true;
  97. expect( data.dataTransfer ).to.equal( dataTransferMock );
  98. editorViewCalled();
  99. } );
  100. clipboardPlugin.on( 'inputTransformation', ( evt, data ) => {
  101. expect( data.content ).is.instanceOf( ViewDocumentFragment );
  102. expect( stringifyView( data.content ) ).to.equal( '' );
  103. expect( editorViewCalled.calledOnce ).to.be.true;
  104. done();
  105. } );
  106. editingView.fire( 'paste', {
  107. dataTransfer: dataTransferMock,
  108. preventDefault: preventDefaultSpy
  109. } );
  110. } );
  111. it( 'uses low priority observer for the paste event', () => {
  112. const dataTransferMock = createDataTransfer( { 'text/html': 'x' } );
  113. const spy = sinon.spy();
  114. editingView.on( 'paste', ( evt ) => {
  115. evt.stop();
  116. } );
  117. editingView.on( 'input', spy );
  118. editingView.fire( 'paste', {
  119. dataTransfer: dataTransferMock,
  120. preventDefault() {}
  121. } );
  122. expect( spy.callCount ).to.equal( 0 );
  123. } );
  124. it( 'inserts content to the editor', () => {
  125. const dataTransferMock = createDataTransfer( { 'text/html': '<p>x</p>', 'text/plain': 'y' } );
  126. const spy = sinon.stub( editor.data, 'insertContent' );
  127. editingView.fire( 'paste', {
  128. dataTransfer: dataTransferMock,
  129. preventDefault() {}
  130. } );
  131. expect( spy.calledOnce ).to.be.true;
  132. expect( stringifyModel( spy.args[ 0 ][ 0 ] ) ).to.equal( '<paragraph>x</paragraph>' );
  133. } );
  134. it( 'converts content in an "all allowed" context', () => {
  135. // It's enough if we check this here with a text node and paragraph because if the conversion was made
  136. // in a normal root, then text or paragraph wouldn't be allowed here.
  137. const dataTransferMock = createDataTransfer( { 'text/html': 'x<p>y</p>', 'text/plain': 'z' } );
  138. const spy = sinon.stub( editor.data, 'insertContent' );
  139. editingView.fire( 'paste', {
  140. dataTransfer: dataTransferMock,
  141. preventDefault() {}
  142. } );
  143. expect( spy.calledOnce ).to.be.true;
  144. expect( stringifyModel( spy.args[ 0 ][ 0 ] ) ).to.equal( 'x<paragraph>y</paragraph>' );
  145. } );
  146. it( 'does nothing when pasted content is empty', () => {
  147. const dataTransferMock = createDataTransfer( { 'text/plain': '' } );
  148. const spy = sinon.stub( editor.data, 'insertContent' );
  149. editingView.fire( 'input', {
  150. dataTransfer: dataTransferMock,
  151. content: new ViewDocumentFragment()
  152. } );
  153. expect( spy.callCount ).to.equal( 0 );
  154. } );
  155. it( 'uses low priority observer for the input event', () => {
  156. const dataTransferMock = createDataTransfer( { 'text/html': 'x' } );
  157. const spy = sinon.stub( editor.data, 'insertContent' );
  158. editingView.on( 'input', ( evt ) => {
  159. evt.stop();
  160. } );
  161. editingView.fire( 'paste', {
  162. dataTransfer: dataTransferMock,
  163. preventDefault() {}
  164. } );
  165. expect( spy.callCount ).to.equal( 0 );
  166. } );
  167. function createDataTransfer( data ) {
  168. return {
  169. getData( type ) {
  170. return data[ type ];
  171. }
  172. };
  173. }
  174. } );
  175. describe( 'clipboard copy/cut pipeline', () => {
  176. it( 'fires clipboardOutput for copy with the selected content and correct method', ( done ) => {
  177. const dataTransferMock = createDataTransfer();
  178. const preventDefaultSpy = sinon.spy();
  179. setModelData( editor.document, '<paragraph>a[bc</paragraph><paragraph>de]f</paragraph>' );
  180. editingView.on( 'clipboardOutput', ( evt, data ) => {
  181. expect( preventDefaultSpy.calledOnce ).to.be.true;
  182. expect( data.method ).to.equal( 'copy' );
  183. expect( data.dataTransfer ).to.equal( dataTransferMock );
  184. expect( data.content ).is.instanceOf( ViewDocumentFragment );
  185. expect( stringifyView( data.content ) ).to.equal( '<p>bc</p><p>de</p>' );
  186. done();
  187. } );
  188. editingView.fire( 'copy', {
  189. dataTransfer: dataTransferMock,
  190. preventDefault: preventDefaultSpy
  191. } );
  192. } );
  193. it( 'fires clipboardOutput for cut with the selected content and correct method', ( done ) => {
  194. const dataTransferMock = createDataTransfer();
  195. const preventDefaultSpy = sinon.spy();
  196. setModelData( editor.document, '<paragraph>a[bc</paragraph><paragraph>de]f</paragraph>' );
  197. editingView.on( 'clipboardOutput', ( evt, data ) => {
  198. expect( data.method ).to.equal( 'cut' );
  199. done();
  200. } );
  201. editingView.fire( 'cut', {
  202. dataTransfer: dataTransferMock,
  203. preventDefault: preventDefaultSpy
  204. } );
  205. } );
  206. it( 'uses low priority observer for the copy event', () => {
  207. const dataTransferMock = createDataTransfer();
  208. const spy = sinon.spy();
  209. editingView.on( 'copy', ( evt ) => {
  210. evt.stop();
  211. } );
  212. editingView.on( 'clipboardOutput', spy );
  213. editingView.fire( 'copy', {
  214. dataTransfer: dataTransferMock,
  215. preventDefault() {}
  216. } );
  217. expect( spy.callCount ).to.equal( 0 );
  218. } );
  219. it( 'sets clipboard HTML data', () => {
  220. const dataTransferMock = createDataTransfer();
  221. setModelData( editor.document, '<paragraph>f[o]o</paragraph>' );
  222. editingView.fire( 'clipboardOutput', {
  223. dataTransfer: dataTransferMock,
  224. content: new ViewDocumentFragment( [ new ViewText( 'abc' ) ] ),
  225. method: 'copy'
  226. } );
  227. expect( dataTransferMock.getData( 'text/html' ) ).to.equal( 'abc' );
  228. expect( getModelData( editor.document ) ).to.equal( '<paragraph>f[o]o</paragraph>' );
  229. } );
  230. it( 'does not set clipboard HTML data if content is empty', () => {
  231. const dataTransferMock = createDataTransfer();
  232. editingView.fire( 'clipboardOutput', {
  233. dataTransfer: dataTransferMock,
  234. content: new ViewDocumentFragment(),
  235. method: 'copy'
  236. } );
  237. expect( dataTransferMock.getData( 'text/html' ) ).to.be.undefined;
  238. } );
  239. it( 'deletes selected content in case of cut', () => {
  240. const dataTransferMock = createDataTransfer();
  241. setModelData( editor.document, '<paragraph>f[o</paragraph><paragraph>x]o</paragraph>' );
  242. editingView.fire( 'clipboardOutput', {
  243. dataTransfer: dataTransferMock,
  244. content: new ViewDocumentFragment(),
  245. method: 'cut'
  246. } );
  247. expect( getModelData( editor.document ) ).to.equal( '<paragraph>f[]o</paragraph>' );
  248. } );
  249. it( 'uses low priority observer for the clipboardOutput event', () => {
  250. const dataTransferMock = createDataTransfer();
  251. editingView.on( 'clipboardOutput', ( evt ) => {
  252. evt.stop();
  253. } );
  254. editingView.fire( 'copy', {
  255. dataTransfer: dataTransferMock,
  256. content: new ViewDocumentFragment( [ new ViewText( 'abc' ) ] ),
  257. preventDefault() {}
  258. } );
  259. expect( dataTransferMock.getData( 'text/html' ) ).to.be.undefined;
  260. } );
  261. function createDataTransfer() {
  262. const store = new Map();
  263. return {
  264. setData( type, data ) {
  265. store.set( type, data );
  266. },
  267. getData( type ) {
  268. return store.get( type );
  269. }
  270. };
  271. }
  272. } );
  273. } );