clipboard.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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. // https://github.com/ckeditor/ckeditor5/issues/1006
  268. describe( 'pasting plain text', () => {
  269. let model;
  270. beforeEach( () => {
  271. model = editor.model;
  272. model.schema.extend( '$text', { allowAttributes: 'bold' } );
  273. model.schema.register( 'softBreak', {
  274. allowWhere: '$text',
  275. isInline: true
  276. } );
  277. editor.conversion.for( 'upcast' )
  278. .elementToElement( {
  279. model: 'softBreak',
  280. view: 'br'
  281. } );
  282. } );
  283. it( 'should inherit selection attributes (collapsed selection)', () => {
  284. const insertContent = model.insertContent.bind( model );
  285. let insertedNode;
  286. sinon.stub( model, 'insertContent' ).callsFake( documentFragment => {
  287. insertedNode = documentFragment.getChild( 0 );
  288. return insertContent( documentFragment );
  289. } );
  290. setModelData( model, '<paragraph><$text bold="true">Bolded []text.</$text></paragraph>' );
  291. const dataTransferMock = createDataTransfer( { 'text/plain': 'foo' } );
  292. viewDocument.fire( 'paste', {
  293. dataTransfer: dataTransferMock,
  294. stopPropagation() {},
  295. preventDefault() {}
  296. } );
  297. expect( getModelData( model ) ).to.equal( '<paragraph><$text bold="true">Bolded foo[]text.</$text></paragraph>' );
  298. expect( insertedNode.getAttribute( 'bold' ) ).to.equal( true );
  299. } );
  300. it( 'should inherit selection attributes (non-collapsed selection)', () => {
  301. const insertContent = model.insertContent.bind( model );
  302. let insertedNode;
  303. sinon.stub( model, 'insertContent' ).callsFake( documentFragment => {
  304. insertedNode = documentFragment.getChild( 0 );
  305. return insertContent( documentFragment );
  306. } );
  307. setModelData( model, '<paragraph><$text bold="true">Bolded [text.]</$text></paragraph>' );
  308. const dataTransferMock = createDataTransfer( { 'text/plain': 'foo' } );
  309. viewDocument.fire( 'paste', {
  310. dataTransfer: dataTransferMock,
  311. stopPropagation() {},
  312. preventDefault() {}
  313. } );
  314. expect( getModelData( model ) ).to.equal( '<paragraph><$text bold="true">Bolded foo[]</$text></paragraph>' );
  315. expect( insertedNode.getAttribute( 'bold' ) ).to.equal( true );
  316. } );
  317. it( 'should inherit selection attributes while pasting a plain text as text/html', () => {
  318. setModelData( model, '<paragraph><$text bold="true">Bolded []text.</$text></paragraph>' );
  319. const dataTransferMock = createDataTransfer( {
  320. 'text/html': 'foo',
  321. 'text/plain': 'foo'
  322. } );
  323. viewDocument.fire( 'paste', {
  324. dataTransfer: dataTransferMock,
  325. stopPropagation() {},
  326. preventDefault() {}
  327. } );
  328. expect( getModelData( model ) ).to.equal( '<paragraph><$text bold="true">Bolded foo[]text.</$text></paragraph>' );
  329. } );
  330. it( 'should inherit selection attributes while pasting a plain text as text/html (Chrome style)', () => {
  331. setModelData( model, '<paragraph><$text bold="true">Bolded []text.</$text></paragraph>' );
  332. const dataTransferMock = createDataTransfer( {
  333. 'text/html': '<meta http-equiv="content-type" content="text/html; charset=utf-8">foo',
  334. 'text/plain': 'foo'
  335. } );
  336. viewDocument.fire( 'paste', {
  337. dataTransfer: dataTransferMock,
  338. stopPropagation() {},
  339. preventDefault() {}
  340. } );
  341. expect( getModelData( model ) ).to.equal( '<paragraph><$text bold="true">Bolded foo[]text.</$text></paragraph>' );
  342. } );
  343. it( 'should inherit selection attributes while pasting HTML with unsupported attributes', () => {
  344. setModelData( model, '<paragraph><$text bold="true">Bolded []text.</$text></paragraph>' );
  345. const dataTransferMock = createDataTransfer( {
  346. 'text/html': '<i>foo</i>',
  347. 'text/plain': 'foo'
  348. } );
  349. viewDocument.fire( 'paste', {
  350. dataTransfer: dataTransferMock,
  351. stopPropagation() {},
  352. preventDefault() {}
  353. } );
  354. expect( getModelData( model ) ).to.equal( '<paragraph><$text bold="true">Bolded foo[]text.</$text></paragraph>' );
  355. } );
  356. it( 'should inherit selection attributes with data.asPlainText switch set', () => {
  357. setModelData( model, '<paragraph><$text bold="true">Bolded []text.</$text></paragraph>' );
  358. const dataTransferMock = createDataTransfer( {
  359. 'text/html': 'foo',
  360. 'text/plain': 'foo'
  361. } );
  362. viewDocument.fire( 'clipboardInput', {
  363. dataTransfer: dataTransferMock,
  364. asPlainText: true,
  365. stopPropagation() {},
  366. preventDefault() {}
  367. } );
  368. expect( getModelData( model ) ).to.equal( '<paragraph><$text bold="true">Bolded foo[]text.</$text></paragraph>' );
  369. } );
  370. it( 'should discard selection attributes with data.asPlainText switch set to false', () => {
  371. setModelData( model, '<paragraph><$text bold="true">Bolded []text.</$text></paragraph>' );
  372. const dataTransferMock = createDataTransfer( {
  373. 'text/html': 'foo<br>bar',
  374. 'text/plain': 'foo\nbar'
  375. } );
  376. viewDocument.fire( 'clipboardInput', {
  377. dataTransfer: dataTransferMock,
  378. asPlainText: false,
  379. stopPropagation() {},
  380. preventDefault() {}
  381. } );
  382. expect( getModelData( model ) ).to.equal( '<paragraph><$text bold="true">Bolded </$text>' +
  383. 'foo<softBreak></softBreak>bar[]' +
  384. '<$text bold="true">text.</$text></paragraph>' );
  385. } );
  386. it( 'should work if the insertContent event is cancelled', () => {
  387. // (#7887).
  388. setModelData( model, '<paragraph><$text bold="true">Bolded []text.</$text></paragraph>' );
  389. const dataTransferMock = createDataTransfer( {
  390. 'text/html': 'foo',
  391. 'text/plain': 'foo'
  392. } );
  393. model.on( 'insertContent', event => {
  394. event.stop();
  395. }, { priority: 'high' } );
  396. viewDocument.fire( 'clipboardInput', {
  397. dataTransfer: dataTransferMock,
  398. asPlainText: false,
  399. stopPropagation() {},
  400. preventDefault() {}
  401. } );
  402. expect( getModelData( model ) ).to.equal( '<paragraph><$text bold="true">Bolded []text.</$text></paragraph>' );
  403. } );
  404. } );
  405. function createDataTransfer( data ) {
  406. return {
  407. getData( type ) {
  408. return data[ type ];
  409. }
  410. };
  411. }
  412. } );
  413. describe( 'clipboard copy/cut pipeline', () => {
  414. it( 'fires clipboardOutput for copy with the selected content and correct method', done => {
  415. const dataTransferMock = createDataTransfer();
  416. const preventDefaultSpy = sinon.spy();
  417. setModelData( editor.model, '<paragraph>a[bc</paragraph><paragraph>de]f</paragraph>' );
  418. viewDocument.on( 'clipboardOutput', ( evt, data ) => {
  419. expect( preventDefaultSpy.calledOnce ).to.be.true;
  420. expect( data.method ).to.equal( 'copy' );
  421. expect( data.dataTransfer ).to.equal( dataTransferMock );
  422. expect( data.content ).is.instanceOf( ViewDocumentFragment );
  423. expect( stringifyView( data.content ) ).to.equal( '<p>bc</p><p>de</p>' );
  424. done();
  425. } );
  426. viewDocument.fire( 'copy', {
  427. dataTransfer: dataTransferMock,
  428. preventDefault: preventDefaultSpy
  429. } );
  430. } );
  431. it( 'fires clipboardOutput for cut with the selected content and correct method', done => {
  432. const dataTransferMock = createDataTransfer();
  433. const preventDefaultSpy = sinon.spy();
  434. setModelData( editor.model, '<paragraph>a[bc</paragraph><paragraph>de]f</paragraph>' );
  435. viewDocument.on( 'clipboardOutput', ( evt, data ) => {
  436. expect( data.method ).to.equal( 'cut' );
  437. done();
  438. } );
  439. viewDocument.fire( 'cut', {
  440. dataTransfer: dataTransferMock,
  441. preventDefault: preventDefaultSpy
  442. } );
  443. } );
  444. it( 'not fires clipboardOutput and preventDefault event for cut when editor is read-only', () => {
  445. const dataTransferMock = createDataTransfer();
  446. const preventDefaultSpy = sinon.spy();
  447. const spy = sinon.spy();
  448. setModelData( editor.model, '<paragraph>a[bc</paragraph><paragraph>de]f</paragraph>' );
  449. editor.isReadOnly = true;
  450. viewDocument.on( 'clipboardOutput', spy );
  451. viewDocument.fire( 'cut', {
  452. dataTransfer: dataTransferMock,
  453. preventDefault: preventDefaultSpy
  454. } );
  455. sinon.assert.notCalled( spy );
  456. sinon.assert.calledOnce( preventDefaultSpy );
  457. } );
  458. it( 'uses low priority observer for the copy event', () => {
  459. const dataTransferMock = createDataTransfer();
  460. const spy = sinon.spy();
  461. viewDocument.on( 'copy', evt => {
  462. evt.stop();
  463. } );
  464. viewDocument.on( 'clipboardOutput', spy );
  465. viewDocument.fire( 'copy', {
  466. dataTransfer: dataTransferMock,
  467. preventDefault() {}
  468. } );
  469. expect( spy.callCount ).to.equal( 0 );
  470. } );
  471. it( 'sets clipboard HTML data', () => {
  472. const dataTransferMock = createDataTransfer();
  473. const input =
  474. '<blockquote>' +
  475. '<p>foo</p>' +
  476. '<p>bar</p>' +
  477. '</blockquote>' +
  478. '<ul>' +
  479. '<li>u<strong>l ite</strong>m</li>' +
  480. '<li>ul item</li>' +
  481. '</ul>' +
  482. '<p>foobar</p>' +
  483. '<ol>' +
  484. '<li>o<a href="foo">l ite</a>m</li>' +
  485. '<li>ol item</li>' +
  486. '</ol>' +
  487. '<figure>' +
  488. '<img src="foo.jpg" alt="image foo" />' +
  489. '<figcaption>caption</figcaption>' +
  490. '</figure>';
  491. const output =
  492. '<blockquote>' +
  493. '<p>foo</p>' +
  494. '<p>bar</p>' +
  495. '</blockquote>' +
  496. '<ul>' +
  497. '<li>u<strong>l ite</strong>m</li>' +
  498. '<li>ul item</li>' +
  499. '</ul>' +
  500. '<p>foobar</p>' +
  501. '<ol>' +
  502. '<li>o<a href="foo">l ite</a>m</li>' +
  503. '<li>ol item</li>' +
  504. '</ol>' +
  505. '<figure>' +
  506. '<img alt="image foo" src="foo.jpg">' + // Weird attributes ordering behavior + no closing "/>".
  507. '<figcaption>caption</figcaption>' +
  508. '</figure>';
  509. viewDocument.fire( 'clipboardOutput', {
  510. dataTransfer: dataTransferMock,
  511. content: parseView( input ),
  512. method: 'copy'
  513. } );
  514. expect( dataTransferMock.getData( 'text/html' ) ).to.equal( output );
  515. } );
  516. it( 'sets clipboard plain text data', () => {
  517. const dataTransferMock = createDataTransfer();
  518. const input =
  519. '<container:blockquote>' +
  520. '<container:p>foo</container:p>' +
  521. '<container:p>bar</container:p>' +
  522. '</container:blockquote>' +
  523. '<container:ul>' +
  524. '<container:li>u<strong>l ite</strong>m</container:li>' +
  525. '<container:li>ul item</container:li>' +
  526. '</container:ul>' +
  527. '<container:p>foobar</container:p>' +
  528. '<container:ol>' +
  529. '<container:li>o<a href="foo">l ite</a>m</container:li>' +
  530. '<container:li>ol item</container:li>' +
  531. '</container:ol>' +
  532. '<container:figure>' +
  533. '<img alt="image foo" src="foo.jpg" />' +
  534. '<container:figcaption>caption</container:figcaption>' +
  535. '</container:figure>';
  536. const output =
  537. 'foo\n' +
  538. '\n' +
  539. 'bar\n' +
  540. '\n' +
  541. 'ul item\n' +
  542. 'ul item\n' +
  543. '\n' +
  544. 'foobar\n' +
  545. '\n' +
  546. 'ol item\n' +
  547. 'ol item\n' +
  548. '\n' +
  549. 'image foo\n' +
  550. 'caption';
  551. viewDocument.fire( 'clipboardOutput', {
  552. dataTransfer: dataTransferMock,
  553. content: parseView( input ),
  554. method: 'copy'
  555. } );
  556. expect( dataTransferMock.getData( 'text/plain' ) ).to.equal( output );
  557. } );
  558. it( 'does not set clipboard HTML data if content is empty', () => {
  559. const dataTransferMock = createDataTransfer();
  560. viewDocument.fire( 'clipboardOutput', {
  561. dataTransfer: dataTransferMock,
  562. content: new ViewDocumentFragment(),
  563. method: 'copy'
  564. } );
  565. expect( dataTransferMock.getData( 'text/html' ) ).to.be.undefined;
  566. } );
  567. it( 'deletes selected content in case of cut', () => {
  568. const dataTransferMock = createDataTransfer();
  569. setModelData( editor.model, '<paragraph>f[o</paragraph><paragraph>x]o</paragraph>' );
  570. // Change block is only to get writer instance.
  571. // Writer should not be passed along this event.
  572. editor.model.change( writer => {
  573. viewDocument.fire( 'clipboardOutput', {
  574. dataTransfer: dataTransferMock,
  575. content: new ViewDocumentFragment(),
  576. method: 'cut',
  577. writer
  578. } );
  579. } );
  580. expect( getModelData( editor.model ) ).to.equal( '<paragraph>f[]o</paragraph>' );
  581. } );
  582. it( 'uses low priority observer for the clipboardOutput event', () => {
  583. const dataTransferMock = createDataTransfer();
  584. viewDocument.on( 'clipboardOutput', evt => {
  585. evt.stop();
  586. } );
  587. viewDocument.fire( 'copy', {
  588. dataTransfer: dataTransferMock,
  589. content: new ViewDocumentFragment( [ new ViewText( 'abc' ) ] ),
  590. preventDefault() {}
  591. } );
  592. expect( dataTransferMock.getData( 'text/html' ) ).to.be.undefined;
  593. } );
  594. function createDataTransfer() {
  595. const store = new Map();
  596. return {
  597. setData( type, data ) {
  598. store.set( type, data );
  599. },
  600. getData( type ) {
  601. return store.get( type );
  602. }
  603. };
  604. }
  605. } );
  606. } );