clipboard.js 15 KB

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