clipboard.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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 DataTransfer from '../src/datatransfer';
  10. import {
  11. stringify as stringifyView,
  12. parse as parseView
  13. } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  14. import {
  15. stringify as stringifyModel,
  16. setData as setModelData,
  17. getData as getModelData
  18. } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  19. import ViewDocumentFragment from '@ckeditor/ckeditor5-engine/src/view/documentfragment';
  20. import ViewText from '@ckeditor/ckeditor5-engine/src/view/text';
  21. describe( 'Clipboard feature', () => {
  22. let editor, view, viewDocument, clipboardPlugin, scrollSpy;
  23. beforeEach( () => {
  24. return VirtualTestEditor
  25. .create( {
  26. plugins: [ Clipboard, Paragraph ]
  27. } )
  28. .then( newEditor => {
  29. editor = newEditor;
  30. view = editor.editing.view;
  31. viewDocument = editor.editing.view.document;
  32. clipboardPlugin = editor.plugins.get( 'Clipboard' );
  33. // VirtualTestEditor has no DOM, so this method must be stubbed for all tests.
  34. // Otherwise it will throw as it accesses the DOM to do its job.
  35. scrollSpy = sinon.stub( view, 'scrollToTheSelection' );
  36. } );
  37. } );
  38. describe( 'constructor()', () => {
  39. it( 'registers ClipboardObserver', () => {
  40. expect( view.getObserver( ClipboardObserver ) ).to.be.instanceOf( ClipboardObserver );
  41. } );
  42. } );
  43. describe( 'clipboard paste pipeline', () => {
  44. describe( 'takes HTML data from the dataTransfer', () => {
  45. it( 'and fires the clipboardInput event on the editingView', done => {
  46. const dataTransferMock = createDataTransfer( { 'text/html': '<p>x</p>', 'text/plain': 'y' } );
  47. const preventDefaultSpy = sinon.spy();
  48. viewDocument.on( 'clipboardInput', ( evt, data ) => {
  49. expect( preventDefaultSpy.calledOnce ).to.be.true;
  50. expect( data.dataTransfer ).to.equal( dataTransferMock );
  51. done();
  52. } );
  53. viewDocument.fire( 'paste', {
  54. dataTransfer: dataTransferMock,
  55. preventDefault: preventDefaultSpy
  56. } );
  57. } );
  58. it( 'and fires the inputTransformation event on the clipboardPlugin', done => {
  59. const dataTransferMock = createDataTransfer( { 'text/html': '<p>x</p>', 'text/plain': 'y' } );
  60. const preventDefaultSpy = sinon.spy();
  61. clipboardPlugin.on( 'inputTransformation', ( evt, data ) => {
  62. expect( data.content ).is.instanceOf( ViewDocumentFragment );
  63. expect( data.dataTransfer ).to.equal( dataTransferMock );
  64. expect( stringifyView( data.content ) ).to.equal( '<p>x</p>' );
  65. done();
  66. } );
  67. viewDocument.fire( 'paste', {
  68. dataTransfer: dataTransferMock,
  69. preventDefault: preventDefaultSpy
  70. } );
  71. } );
  72. } );
  73. describe( 'takes plain text data from the dataTransfer if there is no HTML', () => {
  74. it( 'and fires the clipboardInput event on the editingView', done => {
  75. const dataTransferMock = createDataTransfer( { 'text/plain': 'x\n\ny z' } );
  76. const preventDefaultSpy = sinon.spy();
  77. viewDocument.on( 'clipboardInput', ( evt, data ) => {
  78. expect( preventDefaultSpy.calledOnce ).to.be.true;
  79. expect( data.dataTransfer ).to.equal( dataTransferMock );
  80. done();
  81. } );
  82. viewDocument.fire( 'paste', {
  83. dataTransfer: dataTransferMock,
  84. preventDefault: preventDefaultSpy
  85. } );
  86. } );
  87. it( 'and fires the inputTransformation event on the clipboardPlugin', done => {
  88. const dataTransferMock = createDataTransfer( { 'text/plain': 'x\n\ny z' } );
  89. const preventDefaultSpy = sinon.spy();
  90. clipboardPlugin.on( 'inputTransformation', ( evt, data ) => {
  91. expect( data.content ).is.instanceOf( ViewDocumentFragment );
  92. expect( data.dataTransfer ).to.equal( dataTransferMock );
  93. expect( stringifyView( data.content ) ).to.equal( '<p>x</p><p></p><p>y z</p>' );
  94. done();
  95. } );
  96. viewDocument.fire( 'paste', {
  97. dataTransfer: dataTransferMock,
  98. preventDefault: preventDefaultSpy
  99. } );
  100. } );
  101. } );
  102. it( 'fires events with empty data if there is no HTML nor plain text', done => {
  103. const dataTransferMock = createDataTransfer( {} );
  104. const preventDefaultSpy = sinon.spy();
  105. const editorViewCalled = sinon.spy();
  106. viewDocument.on( 'clipboardInput', ( evt, data ) => {
  107. expect( preventDefaultSpy.calledOnce ).to.be.true;
  108. expect( data.dataTransfer ).to.equal( dataTransferMock );
  109. editorViewCalled();
  110. } );
  111. clipboardPlugin.on( 'inputTransformation', ( evt, data ) => {
  112. expect( data.content ).is.instanceOf( ViewDocumentFragment );
  113. expect( data.dataTransfer ).to.equal( dataTransferMock );
  114. expect( stringifyView( data.content ) ).to.equal( '' );
  115. expect( editorViewCalled.calledOnce ).to.be.true;
  116. done();
  117. } );
  118. viewDocument.fire( 'paste', {
  119. dataTransfer: dataTransferMock,
  120. preventDefault: preventDefaultSpy
  121. } );
  122. } );
  123. it( 'uses low priority observer for the paste event', () => {
  124. const dataTransferMock = createDataTransfer( { 'text/html': 'x' } );
  125. const spy = sinon.spy();
  126. viewDocument.on( 'paste', evt => {
  127. evt.stop();
  128. } );
  129. viewDocument.on( 'clipboardInput', spy );
  130. viewDocument.fire( 'paste', {
  131. dataTransfer: dataTransferMock,
  132. preventDefault() {}
  133. } );
  134. expect( spy.callCount ).to.equal( 0 );
  135. } );
  136. it( 'inserts content to the editor', () => {
  137. const dataTransferMock = createDataTransfer( { 'text/html': '<p>x</p>', 'text/plain': 'y' } );
  138. const spy = sinon.stub( editor.model, 'insertContent' );
  139. viewDocument.fire( 'paste', {
  140. dataTransfer: dataTransferMock,
  141. stopPropagation() {},
  142. preventDefault() {}
  143. } );
  144. expect( spy.calledOnce ).to.be.true;
  145. expect( stringifyModel( spy.args[ 0 ][ 0 ] ) ).to.equal( '<paragraph>x</paragraph>' );
  146. } );
  147. it( 'does not insert content when editor is read-only', () => {
  148. const dataTransferMock = createDataTransfer( { 'text/html': '<p>x</p>', 'text/plain': 'y' } );
  149. const spy = sinon.stub( editor.model, 'insertContent' );
  150. editor.isReadOnly = true;
  151. viewDocument.fire( 'paste', {
  152. dataTransfer: dataTransferMock,
  153. stopPropagation() {},
  154. preventDefault() {}
  155. } );
  156. sinon.assert.notCalled( spy );
  157. } );
  158. it( 'stops `clipboardInput` event on highest priority when editor is read-only', () => {
  159. const dataTransferMock = createDataTransfer( { 'text/html': '<p>x</p>', 'text/plain': 'y' } );
  160. const spy = sinon.spy();
  161. viewDocument.on( 'clipboardInput', spy, { priority: 'high' } );
  162. editor.isReadOnly = true;
  163. viewDocument.fire( 'clipboardInput', {
  164. dataTransfer: dataTransferMock
  165. } );
  166. sinon.assert.notCalled( spy );
  167. editor.isReadOnly = false;
  168. viewDocument.fire( 'clipboardInput', {
  169. dataTransfer: dataTransferMock
  170. } );
  171. sinon.assert.calledOnce( spy );
  172. } );
  173. it( 'does not insert content if the whole content was invalid', () => {
  174. // Whole content is invalid. Even though there is "view" content, the "model" content would be empty.
  175. // Do not insert content in this case.
  176. const dataTransferMock = createDataTransfer( { 'text/html': '<unknownTag></unknownTag>', 'text/plain': '' } );
  177. const spy = sinon.stub( editor.model, 'insertContent' );
  178. viewDocument.fire( 'paste', {
  179. dataTransfer: dataTransferMock,
  180. preventDefault() {}
  181. } );
  182. sinon.assert.notCalled( spy );
  183. } );
  184. it( 'converts content in an "all allowed" context', () => {
  185. // It's enough if we check this here with a text node and paragraph because if the conversion was made
  186. // in a normal root, then text or paragraph wouldn't be allowed here.
  187. const dataTransferMock = createDataTransfer( { 'text/html': 'x<p>y</p>', 'text/plain': 'z' } );
  188. const spy = sinon.stub( editor.model, 'insertContent' );
  189. viewDocument.fire( 'paste', {
  190. dataTransfer: dataTransferMock,
  191. stopPropagation() {},
  192. preventDefault() {}
  193. } );
  194. expect( spy.calledOnce ).to.be.true;
  195. expect( stringifyModel( spy.args[ 0 ][ 0 ] ) ).to.equal( 'x<paragraph>y</paragraph>' );
  196. } );
  197. it( 'does nothing when pasted content is empty', () => {
  198. const dataTransferMock = createDataTransfer( { 'text/plain': '' } );
  199. const spy = sinon.stub( editor.model, 'insertContent' );
  200. viewDocument.fire( 'clipboardInput', {
  201. dataTransfer: dataTransferMock,
  202. content: new ViewDocumentFragment()
  203. } );
  204. expect( spy.callCount ).to.equal( 0 );
  205. } );
  206. it( 'scrolls the editing document to the selection after the pasted content is inserted', () => {
  207. const dataTransferMock = createDataTransfer( { 'text/html': '<p>x</p>', 'text/plain': 'y' } );
  208. const inputTransformationSpy = sinon.spy();
  209. clipboardPlugin.on( 'inputTransformation', inputTransformationSpy );
  210. viewDocument.fire( 'clipboardInput', {
  211. dataTransfer: dataTransferMock,
  212. content: new ViewDocumentFragment()
  213. } );
  214. sinon.assert.calledOnce( scrollSpy );
  215. sinon.assert.callOrder( inputTransformationSpy, scrollSpy );
  216. } );
  217. it( 'uses low priority observer for the clipboardInput event', () => {
  218. const dataTransferMock = createDataTransfer( { 'text/html': 'x' } );
  219. const spy = sinon.stub( editor.model, 'insertContent' );
  220. viewDocument.on( 'clipboardInput', evt => {
  221. evt.stop();
  222. } );
  223. viewDocument.fire( 'paste', {
  224. dataTransfer: dataTransferMock,
  225. stopPropagation() {},
  226. preventDefault() {}
  227. } );
  228. expect( spy.callCount ).to.equal( 0 );
  229. } );
  230. // https://github.com/ckeditor/ckeditor5-upload/issues/92
  231. // https://github.com/ckeditor/ckeditor5/issues/6464
  232. it( 'should stop propagation of the original event if CKEditor handled the input', () => {
  233. const dataTransferMock = createDataTransfer( { 'text/html': 'x' } );
  234. const spy = sinon.spy();
  235. viewDocument.fire( 'paste', {
  236. dataTransfer: dataTransferMock,
  237. stopPropagation: spy,
  238. preventDefault() {}
  239. } );
  240. expect( spy.callCount ).to.equal( 1 );
  241. } );
  242. // https://github.com/ckeditor/ckeditor5-upload/issues/92
  243. // https://github.com/ckeditor/ckeditor5/issues/6464
  244. it( 'should stop propagation of the original event if inputTransformation listener called stop (for file drop)', () => {
  245. const fileMock = {
  246. type: 'application/zip',
  247. size: 1024
  248. };
  249. const dataTransferMock = new DataTransfer( { files: [ fileMock ], types: [ 'Files' ], getData: () => {} } );
  250. const spy = sinon.spy();
  251. viewDocument.fire( 'drop', {
  252. dataTransfer: dataTransferMock,
  253. stopPropagation: spy,
  254. preventDefault() {}
  255. } );
  256. expect( spy.callCount ).to.equal( 0 );
  257. clipboardPlugin.on( 'inputTransformation', evt => {
  258. evt.stop();
  259. } );
  260. viewDocument.fire( 'paste', {
  261. dataTransfer: dataTransferMock,
  262. stopPropagation: spy,
  263. preventDefault() {}
  264. } );
  265. expect( spy.callCount ).to.equal( 1 );
  266. } );
  267. function createDataTransfer( data ) {
  268. return {
  269. getData( type ) {
  270. return data[ type ];
  271. }
  272. };
  273. }
  274. } );
  275. describe( 'clipboard copy/cut pipeline', () => {
  276. it( 'fires clipboardOutput for copy with the selected content and correct method', done => {
  277. const dataTransferMock = createDataTransfer();
  278. const preventDefaultSpy = sinon.spy();
  279. setModelData( editor.model, '<paragraph>a[bc</paragraph><paragraph>de]f</paragraph>' );
  280. viewDocument.on( 'clipboardOutput', ( evt, data ) => {
  281. expect( preventDefaultSpy.calledOnce ).to.be.true;
  282. expect( data.method ).to.equal( 'copy' );
  283. expect( data.dataTransfer ).to.equal( dataTransferMock );
  284. expect( data.content ).is.instanceOf( ViewDocumentFragment );
  285. expect( stringifyView( data.content ) ).to.equal( '<p>bc</p><p>de</p>' );
  286. done();
  287. } );
  288. viewDocument.fire( 'copy', {
  289. dataTransfer: dataTransferMock,
  290. preventDefault: preventDefaultSpy
  291. } );
  292. } );
  293. it( 'fires clipboardOutput for cut with the selected content and correct method', done => {
  294. const dataTransferMock = createDataTransfer();
  295. const preventDefaultSpy = sinon.spy();
  296. setModelData( editor.model, '<paragraph>a[bc</paragraph><paragraph>de]f</paragraph>' );
  297. viewDocument.on( 'clipboardOutput', ( evt, data ) => {
  298. expect( data.method ).to.equal( 'cut' );
  299. done();
  300. } );
  301. viewDocument.fire( 'cut', {
  302. dataTransfer: dataTransferMock,
  303. preventDefault: preventDefaultSpy
  304. } );
  305. } );
  306. it( 'not fires clipboardOutput and preventDefault event for cut when editor is read-only', () => {
  307. const dataTransferMock = createDataTransfer();
  308. const preventDefaultSpy = sinon.spy();
  309. const spy = sinon.spy();
  310. setModelData( editor.model, '<paragraph>a[bc</paragraph><paragraph>de]f</paragraph>' );
  311. editor.isReadOnly = true;
  312. viewDocument.on( 'clipboardOutput', spy );
  313. viewDocument.fire( 'cut', {
  314. dataTransfer: dataTransferMock,
  315. preventDefault: preventDefaultSpy
  316. } );
  317. sinon.assert.notCalled( spy );
  318. sinon.assert.calledOnce( preventDefaultSpy );
  319. } );
  320. it( 'uses low priority observer for the copy event', () => {
  321. const dataTransferMock = createDataTransfer();
  322. const spy = sinon.spy();
  323. viewDocument.on( 'copy', evt => {
  324. evt.stop();
  325. } );
  326. viewDocument.on( 'clipboardOutput', spy );
  327. viewDocument.fire( 'copy', {
  328. dataTransfer: dataTransferMock,
  329. preventDefault() {}
  330. } );
  331. expect( spy.callCount ).to.equal( 0 );
  332. } );
  333. it( 'sets clipboard HTML data', () => {
  334. const dataTransferMock = createDataTransfer();
  335. const input =
  336. '<blockquote>' +
  337. '<p>foo</p>' +
  338. '<p>bar</p>' +
  339. '</blockquote>' +
  340. '<ul>' +
  341. '<li>u<strong>l ite</strong>m</li>' +
  342. '<li>ul item</li>' +
  343. '</ul>' +
  344. '<p>foobar</p>' +
  345. '<ol>' +
  346. '<li>o<a href="foo">l ite</a>m</li>' +
  347. '<li>ol item</li>' +
  348. '</ol>' +
  349. '<figure>' +
  350. '<img src="foo.jpg" alt="image foo" />' +
  351. '<figcaption>caption</figcaption>' +
  352. '</figure>';
  353. const output =
  354. '<blockquote>' +
  355. '<p>foo</p>' +
  356. '<p>bar</p>' +
  357. '</blockquote>' +
  358. '<ul>' +
  359. '<li>u<strong>l ite</strong>m</li>' +
  360. '<li>ul item</li>' +
  361. '</ul>' +
  362. '<p>foobar</p>' +
  363. '<ol>' +
  364. '<li>o<a href="foo">l ite</a>m</li>' +
  365. '<li>ol item</li>' +
  366. '</ol>' +
  367. '<figure>' +
  368. '<img alt="image foo" src="foo.jpg">' + // Weird attributes ordering behavior + no closing "/>".
  369. '<figcaption>caption</figcaption>' +
  370. '</figure>';
  371. viewDocument.fire( 'clipboardOutput', {
  372. dataTransfer: dataTransferMock,
  373. content: parseView( input ),
  374. method: 'copy'
  375. } );
  376. expect( dataTransferMock.getData( 'text/html' ) ).to.equal( output );
  377. } );
  378. it( 'sets clipboard plain text data', () => {
  379. const dataTransferMock = createDataTransfer();
  380. const input =
  381. '<container:blockquote>' +
  382. '<container:p>foo</container:p>' +
  383. '<container:p>bar</container:p>' +
  384. '</container:blockquote>' +
  385. '<container:ul>' +
  386. '<container:li>u<strong>l ite</strong>m</container:li>' +
  387. '<container:li>ul item</container:li>' +
  388. '</container:ul>' +
  389. '<container:p>foobar</container:p>' +
  390. '<container:ol>' +
  391. '<container:li>o<a href="foo">l ite</a>m</container:li>' +
  392. '<container:li>ol item</container:li>' +
  393. '</container:ol>' +
  394. '<container:figure>' +
  395. '<img alt="image foo" src="foo.jpg" />' +
  396. '<container:figcaption>caption</container:figcaption>' +
  397. '</container:figure>';
  398. const output =
  399. 'foo\n' +
  400. '\n' +
  401. 'bar\n' +
  402. '\n' +
  403. 'ul item\n' +
  404. 'ul item\n' +
  405. '\n' +
  406. 'foobar\n' +
  407. '\n' +
  408. 'ol item\n' +
  409. 'ol item\n' +
  410. '\n' +
  411. 'image foo\n' +
  412. 'caption';
  413. viewDocument.fire( 'clipboardOutput', {
  414. dataTransfer: dataTransferMock,
  415. content: parseView( input ),
  416. method: 'copy'
  417. } );
  418. expect( dataTransferMock.getData( 'text/plain' ) ).to.equal( output );
  419. } );
  420. it( 'does not set clipboard HTML data if content is empty', () => {
  421. const dataTransferMock = createDataTransfer();
  422. viewDocument.fire( 'clipboardOutput', {
  423. dataTransfer: dataTransferMock,
  424. content: new ViewDocumentFragment(),
  425. method: 'copy'
  426. } );
  427. expect( dataTransferMock.getData( 'text/html' ) ).to.be.undefined;
  428. } );
  429. it( 'deletes selected content in case of cut', () => {
  430. const dataTransferMock = createDataTransfer();
  431. setModelData( editor.model, '<paragraph>f[o</paragraph><paragraph>x]o</paragraph>' );
  432. // Change block is only to get writer instance.
  433. // Writer should not be passed along this event.
  434. editor.model.change( writer => {
  435. viewDocument.fire( 'clipboardOutput', {
  436. dataTransfer: dataTransferMock,
  437. content: new ViewDocumentFragment(),
  438. method: 'cut',
  439. writer
  440. } );
  441. } );
  442. expect( getModelData( editor.model ) ).to.equal( '<paragraph>f[]o</paragraph>' );
  443. } );
  444. it( 'uses low priority observer for the clipboardOutput event', () => {
  445. const dataTransferMock = createDataTransfer();
  446. viewDocument.on( 'clipboardOutput', evt => {
  447. evt.stop();
  448. } );
  449. viewDocument.fire( 'copy', {
  450. dataTransfer: dataTransferMock,
  451. content: new ViewDocumentFragment( [ new ViewText( 'abc' ) ] ),
  452. preventDefault() {}
  453. } );
  454. expect( dataTransferMock.getData( 'text/html' ) ).to.be.undefined;
  455. } );
  456. function createDataTransfer() {
  457. const store = new Map();
  458. return {
  459. setData( type, data ) {
  460. store.set( type, data );
  461. },
  462. getData( type ) {
  463. return store.get( type );
  464. }
  465. };
  466. }
  467. } );
  468. } );