clipboard.js 15 KB

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