clipboard.js 12 KB

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