clipboard.js 14 KB

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