clipboard.js 24 KB

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