clipboard.js 16 KB

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