clipboard.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. preventDefault() {}
  152. } );
  153. sinon.assert.notCalled( spy );
  154. } );
  155. it( 'stops `clipboardInput` event on highest priority when editor is read-only', () => {
  156. const dataTransferMock = createDataTransfer( { 'text/html': '<p>x</p>', 'text/plain': 'y' } );
  157. const spy = sinon.spy();
  158. viewDocument.on( 'clipboardInput', spy, { priority: 'high' } );
  159. editor.isReadOnly = true;
  160. viewDocument.fire( 'clipboardInput', {
  161. dataTransfer: dataTransferMock
  162. } );
  163. sinon.assert.notCalled( spy );
  164. editor.isReadOnly = false;
  165. viewDocument.fire( 'clipboardInput', {
  166. dataTransfer: dataTransferMock
  167. } );
  168. sinon.assert.calledOnce( spy );
  169. } );
  170. it( 'does not insert content if the whole content was invalid', () => {
  171. // Whole content is invalid. Even though there is "view" content, the "model" content would be empty.
  172. // Do not insert content in this case.
  173. const dataTransferMock = createDataTransfer( { 'text/html': '<unknownTag></unknownTag>', 'text/plain': '' } );
  174. const spy = sinon.stub( editor.model, 'insertContent' );
  175. viewDocument.fire( 'paste', {
  176. dataTransfer: dataTransferMock,
  177. preventDefault() {}
  178. } );
  179. sinon.assert.notCalled( spy );
  180. } );
  181. it( 'converts content in an "all allowed" context', () => {
  182. // It's enough if we check this here with a text node and paragraph because if the conversion was made
  183. // in a normal root, then text or paragraph wouldn't be allowed here.
  184. const dataTransferMock = createDataTransfer( { 'text/html': 'x<p>y</p>', 'text/plain': 'z' } );
  185. const spy = sinon.stub( editor.model, 'insertContent' );
  186. viewDocument.fire( 'paste', {
  187. dataTransfer: dataTransferMock,
  188. preventDefault() {}
  189. } );
  190. expect( spy.calledOnce ).to.be.true;
  191. expect( stringifyModel( spy.args[ 0 ][ 0 ] ) ).to.equal( 'x<paragraph>y</paragraph>' );
  192. } );
  193. it( 'does nothing when pasted content is empty', () => {
  194. const dataTransferMock = createDataTransfer( { 'text/plain': '' } );
  195. const spy = sinon.stub( editor.model, 'insertContent' );
  196. viewDocument.fire( 'clipboardInput', {
  197. dataTransfer: dataTransferMock,
  198. content: new ViewDocumentFragment()
  199. } );
  200. expect( spy.callCount ).to.equal( 0 );
  201. } );
  202. it( 'scrolls the editing document to the selection after the pasted content is inserted', () => {
  203. const dataTransferMock = createDataTransfer( { 'text/html': '<p>x</p>', 'text/plain': 'y' } );
  204. const inputTransformationSpy = sinon.spy();
  205. clipboardPlugin.on( 'inputTransformation', inputTransformationSpy );
  206. viewDocument.fire( 'clipboardInput', {
  207. dataTransfer: dataTransferMock,
  208. content: new ViewDocumentFragment()
  209. } );
  210. sinon.assert.calledOnce( scrollSpy );
  211. sinon.assert.callOrder( inputTransformationSpy, scrollSpy );
  212. } );
  213. it( 'uses low priority observer for the clipboardInput event', () => {
  214. const dataTransferMock = createDataTransfer( { 'text/html': 'x' } );
  215. const spy = sinon.stub( editor.model, 'insertContent' );
  216. viewDocument.on( 'clipboardInput', evt => {
  217. evt.stop();
  218. } );
  219. viewDocument.fire( 'paste', {
  220. dataTransfer: dataTransferMock,
  221. preventDefault() {}
  222. } );
  223. expect( spy.callCount ).to.equal( 0 );
  224. } );
  225. function createDataTransfer( data ) {
  226. return {
  227. getData( type ) {
  228. return data[ type ];
  229. }
  230. };
  231. }
  232. } );
  233. describe( 'clipboard copy/cut pipeline', () => {
  234. it( 'fires clipboardOutput for copy with the selected content and correct method', done => {
  235. const dataTransferMock = createDataTransfer();
  236. const preventDefaultSpy = sinon.spy();
  237. setModelData( editor.model, '<paragraph>a[bc</paragraph><paragraph>de]f</paragraph>' );
  238. viewDocument.on( 'clipboardOutput', ( evt, data ) => {
  239. expect( preventDefaultSpy.calledOnce ).to.be.true;
  240. expect( data.method ).to.equal( 'copy' );
  241. expect( data.dataTransfer ).to.equal( dataTransferMock );
  242. expect( data.content ).is.instanceOf( ViewDocumentFragment );
  243. expect( stringifyView( data.content ) ).to.equal( '<p>bc</p><p>de</p>' );
  244. done();
  245. } );
  246. viewDocument.fire( 'copy', {
  247. dataTransfer: dataTransferMock,
  248. preventDefault: preventDefaultSpy
  249. } );
  250. } );
  251. it( 'fires clipboardOutput for cut with the selected content and correct method', done => {
  252. const dataTransferMock = createDataTransfer();
  253. const preventDefaultSpy = sinon.spy();
  254. setModelData( editor.model, '<paragraph>a[bc</paragraph><paragraph>de]f</paragraph>' );
  255. viewDocument.on( 'clipboardOutput', ( evt, data ) => {
  256. expect( data.method ).to.equal( 'cut' );
  257. done();
  258. } );
  259. viewDocument.fire( 'cut', {
  260. dataTransfer: dataTransferMock,
  261. preventDefault: preventDefaultSpy
  262. } );
  263. } );
  264. it( 'not fires clipboardOutput and preventDefault event for cut when editor is read-only', () => {
  265. const dataTransferMock = createDataTransfer();
  266. const preventDefaultSpy = sinon.spy();
  267. const spy = sinon.spy();
  268. setModelData( editor.model, '<paragraph>a[bc</paragraph><paragraph>de]f</paragraph>' );
  269. editor.isReadOnly = true;
  270. viewDocument.on( 'clipboardOutput', spy );
  271. viewDocument.fire( 'cut', {
  272. dataTransfer: dataTransferMock,
  273. preventDefault: preventDefaultSpy
  274. } );
  275. sinon.assert.notCalled( spy );
  276. sinon.assert.calledOnce( preventDefaultSpy );
  277. } );
  278. it( 'uses low priority observer for the copy event', () => {
  279. const dataTransferMock = createDataTransfer();
  280. const spy = sinon.spy();
  281. viewDocument.on( 'copy', evt => {
  282. evt.stop();
  283. } );
  284. viewDocument.on( 'clipboardOutput', spy );
  285. viewDocument.fire( 'copy', {
  286. dataTransfer: dataTransferMock,
  287. preventDefault() {}
  288. } );
  289. expect( spy.callCount ).to.equal( 0 );
  290. } );
  291. it( 'sets clipboard HTML data', () => {
  292. const dataTransferMock = createDataTransfer();
  293. const input =
  294. '<blockquote>' +
  295. '<p>foo</p>' +
  296. '<p>bar</p>' +
  297. '</blockquote>' +
  298. '<ul>' +
  299. '<li>u<strong>l ite</strong>m</li>' +
  300. '<li>ul item</li>' +
  301. '</ul>' +
  302. '<p>foobar</p>' +
  303. '<ol>' +
  304. '<li>o<a href="foo">l ite</a>m</li>' +
  305. '<li>ol item</li>' +
  306. '</ol>' +
  307. '<figure>' +
  308. '<img src="foo.jpg" alt="image foo" />' +
  309. '<figcaption>caption</figcaption>' +
  310. '</figure>';
  311. const output =
  312. '<blockquote>' +
  313. '<p>foo</p>' +
  314. '<p>bar</p>' +
  315. '</blockquote>' +
  316. '<ul>' +
  317. '<li>u<strong>l ite</strong>m</li>' +
  318. '<li>ul item</li>' +
  319. '</ul>' +
  320. '<p>foobar</p>' +
  321. '<ol>' +
  322. '<li>o<a href="foo">l ite</a>m</li>' +
  323. '<li>ol item</li>' +
  324. '</ol>' +
  325. '<figure>' +
  326. '<img alt="image foo" src="foo.jpg">' + // Weird attributes ordering behavior + no closing "/>".
  327. '<figcaption>caption</figcaption>' +
  328. '</figure>';
  329. viewDocument.fire( 'clipboardOutput', {
  330. dataTransfer: dataTransferMock,
  331. content: parseView( input ),
  332. method: 'copy'
  333. } );
  334. expect( dataTransferMock.getData( 'text/html' ) ).to.equal( output );
  335. } );
  336. it( 'sets clipboard plain text data', () => {
  337. const dataTransferMock = createDataTransfer();
  338. const input =
  339. '<container:blockquote>' +
  340. '<container:p>foo</container:p>' +
  341. '<container:p>bar</container:p>' +
  342. '</container:blockquote>' +
  343. '<container:ul>' +
  344. '<container:li>u<strong>l ite</strong>m</container:li>' +
  345. '<container:li>ul item</container:li>' +
  346. '</container:ul>' +
  347. '<container:p>foobar</container:p>' +
  348. '<container:ol>' +
  349. '<container:li>o<a href="foo">l ite</a>m</container:li>' +
  350. '<container:li>ol item</container:li>' +
  351. '</container:ol>' +
  352. '<container:figure>' +
  353. '<img alt="image foo" src="foo.jpg" />' +
  354. '<container:figcaption>caption</container:figcaption>' +
  355. '</container:figure>';
  356. const output =
  357. 'foo\n' +
  358. '\n' +
  359. 'bar\n' +
  360. '\n' +
  361. 'ul item\n' +
  362. 'ul item\n' +
  363. '\n' +
  364. 'foobar\n' +
  365. '\n' +
  366. 'ol item\n' +
  367. 'ol item\n' +
  368. '\n' +
  369. 'image foo\n' +
  370. 'caption';
  371. viewDocument.fire( 'clipboardOutput', {
  372. dataTransfer: dataTransferMock,
  373. content: parseView( input ),
  374. method: 'copy'
  375. } );
  376. expect( dataTransferMock.getData( 'text/plain' ) ).to.equal( output );
  377. } );
  378. it( 'does not set clipboard HTML data if content is empty', () => {
  379. const dataTransferMock = createDataTransfer();
  380. viewDocument.fire( 'clipboardOutput', {
  381. dataTransfer: dataTransferMock,
  382. content: new ViewDocumentFragment(),
  383. method: 'copy'
  384. } );
  385. expect( dataTransferMock.getData( 'text/html' ) ).to.be.undefined;
  386. } );
  387. it( 'deletes selected content in case of cut', () => {
  388. const dataTransferMock = createDataTransfer();
  389. setModelData( editor.model, '<paragraph>f[o</paragraph><paragraph>x]o</paragraph>' );
  390. // Change block is only to get writer instance.
  391. // Writer should not be passed along this event.
  392. editor.model.change( writer => {
  393. viewDocument.fire( 'clipboardOutput', {
  394. dataTransfer: dataTransferMock,
  395. content: new ViewDocumentFragment(),
  396. method: 'cut',
  397. writer
  398. } );
  399. } );
  400. expect( getModelData( editor.model ) ).to.equal( '<paragraph>f[]o</paragraph>' );
  401. } );
  402. it( 'uses low priority observer for the clipboardOutput event', () => {
  403. const dataTransferMock = createDataTransfer();
  404. viewDocument.on( 'clipboardOutput', evt => {
  405. evt.stop();
  406. } );
  407. viewDocument.fire( 'copy', {
  408. dataTransfer: dataTransferMock,
  409. content: new ViewDocumentFragment( [ new ViewText( 'abc' ) ] ),
  410. preventDefault() {}
  411. } );
  412. expect( dataTransferMock.getData( 'text/html' ) ).to.be.undefined;
  413. } );
  414. function createDataTransfer() {
  415. const store = new Map();
  416. return {
  417. setData( type, data ) {
  418. store.set( type, data );
  419. },
  420. getData( type ) {
  421. return store.get( type );
  422. }
  423. };
  424. }
  425. } );
  426. } );