tableclipboard.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  6. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import TableEditing from '../src/tableediting';
  9. import { modelTable, viewTable } from './_utils/utils';
  10. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  11. import TableClipboard from '../src/tableclipboard';
  12. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  13. describe( 'table clipboard', () => {
  14. let editor, model, modelRoot, tableSelection, viewDocument;
  15. beforeEach( async () => {
  16. editor = await VirtualTestEditor.create( {
  17. plugins: [ TableEditing, TableClipboard, Paragraph, Clipboard ]
  18. } );
  19. model = editor.model;
  20. modelRoot = model.document.getRoot();
  21. viewDocument = editor.editing.view.document;
  22. tableSelection = editor.plugins.get( 'TableSelection' );
  23. setModelData( model, modelTable( [
  24. [ '00[]', '01', '02' ],
  25. [ '10', '11', '12' ],
  26. [ '20', '21', '22' ]
  27. ] ) );
  28. } );
  29. afterEach( async () => {
  30. await editor.destroy();
  31. } );
  32. describe( 'Clipboard integration', () => {
  33. describe( 'copy', () => {
  34. it( 'should do nothing for normal selection in table', () => {
  35. const dataTransferMock = createDataTransfer();
  36. const spy = sinon.spy();
  37. viewDocument.on( 'clipboardOutput', spy );
  38. viewDocument.fire( 'copy', {
  39. dataTransfer: dataTransferMock,
  40. preventDefault: sinon.spy()
  41. } );
  42. sinon.assert.calledOnce( spy );
  43. } );
  44. it( 'should copy selected table cells as a standalone table', () => {
  45. const preventDefaultSpy = sinon.spy();
  46. tableSelection._setCellSelection(
  47. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  48. modelRoot.getNodeByPath( [ 0, 1, 2 ] )
  49. );
  50. const data = {
  51. dataTransfer: createDataTransfer(),
  52. preventDefault: preventDefaultSpy
  53. };
  54. viewDocument.fire( 'copy', data );
  55. sinon.assert.calledOnce( preventDefaultSpy );
  56. expect( data.dataTransfer.getData( 'text/html' ) ).to.equal( viewTable( [
  57. [ '01', '02' ],
  58. [ '11', '12' ]
  59. ] ) );
  60. } );
  61. it( 'should trim selected table to a selection rectangle (inner cell with colspan, no colspan after trim)', () => {
  62. setModelData( model, modelTable( [
  63. [ '00[]', '01', '02' ],
  64. [ '10', { contents: '11', colspan: 2 } ],
  65. [ '20', '21', '22' ]
  66. ] ) );
  67. tableSelection._setCellSelection(
  68. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  69. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  70. );
  71. assertClipboardContentOnMethod( 'copy', viewTable( [
  72. [ '00', '01' ],
  73. [ '10', '11' ],
  74. [ '20', '21' ]
  75. ] ) );
  76. } );
  77. it( 'should trim selected table to a selection rectangle (inner cell with colspan, has colspan after trim)', () => {
  78. setModelData( model, modelTable( [
  79. [ '00[]', '01', '02' ],
  80. [ { contents: '10', colspan: 3 } ],
  81. [ '20', '21', '22' ]
  82. ] ) );
  83. tableSelection._setCellSelection(
  84. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  85. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  86. );
  87. assertClipboardContentOnMethod( 'copy', viewTable( [
  88. [ '00', '01' ],
  89. [ { contents: '10', colspan: 2 } ],
  90. [ '20', '21' ]
  91. ] ) );
  92. } );
  93. it( 'should trim selected table to a selection rectangle (inner cell with rowspan, no colspan after trim)', () => {
  94. setModelData( model, modelTable( [
  95. [ '00[]', '01', '02' ],
  96. [ '10', { contents: '11', rowspan: 2 }, '12' ],
  97. [ '20', '21', '22' ]
  98. ] ) );
  99. tableSelection._setCellSelection(
  100. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  101. modelRoot.getNodeByPath( [ 0, 1, 2 ] )
  102. );
  103. assertClipboardContentOnMethod( 'copy', viewTable( [
  104. [ '00', '01', '02' ],
  105. [ '10', '11', '12' ]
  106. ] ) );
  107. } );
  108. it( 'should trim selected table to a selection rectangle (inner cell with rowspan, has rowspan after trim)', () => {
  109. setModelData( model, modelTable( [
  110. [ '00[]', { contents: '01', rowspan: 3 }, '02' ],
  111. [ '10', '12' ],
  112. [ '20', '22' ]
  113. ] ) );
  114. tableSelection._setCellSelection(
  115. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  116. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  117. );
  118. assertClipboardContentOnMethod( 'copy', viewTable( [
  119. [ '00', { contents: '01', rowspan: 2 }, '02' ],
  120. [ '10', '12' ]
  121. ] ) );
  122. } );
  123. it( 'should prepend spanned columns with empty cells (outside cell with colspan)', () => {
  124. setModelData( model, modelTable( [
  125. [ '00[]', '01', '02' ],
  126. [ { contents: '10', colspan: 2 }, '12' ],
  127. [ '20', '21', '22' ]
  128. ] ) );
  129. tableSelection._setCellSelection(
  130. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  131. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  132. );
  133. assertClipboardContentOnMethod( 'copy', viewTable( [
  134. [ '01', '02' ],
  135. [ ' ', '12' ],
  136. [ '21', '22' ]
  137. ] ) );
  138. } );
  139. it( 'should prepend spanned columns with empty cells (outside cell with rowspan)', () => {
  140. setModelData( model, modelTable( [
  141. [ '00[]', { contents: '01', rowspan: 2 }, '02' ],
  142. [ '10', '12' ],
  143. [ '20', '21', '22' ]
  144. ] ) );
  145. tableSelection._setCellSelection(
  146. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  147. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  148. );
  149. assertClipboardContentOnMethod( 'copy', viewTable( [
  150. [ '10', ' ', '12' ],
  151. [ '20', '21', '22' ]
  152. ] ) );
  153. } );
  154. it( 'should fix selected table to a selection rectangle (hardcore case)', () => {
  155. // This test check how previous simple rules run together (mixed prepending and trimming).
  156. // In the example below a selection is set from cell "32" to "88"
  157. //
  158. // Input table: Copied table:
  159. //
  160. // +----+----+----+----+----+----+----+----+----+
  161. // | 00 | 01 | 02 | 03 | 04 | 06 | 07 | 08 |
  162. // +----+----+ +----+ +----+----+----+
  163. // | 10 | 11 | | 13 | | 16 | 17 | 18 |
  164. // +----+----+ +----+ +----+----+----+ +----+----+----+---------+----+----+
  165. // | 20 | 21 | | 23 | | 26 | | 21 | | 23 | | | 26 | |
  166. // +----+----+ +----+ +----+----+----+ +----+----+----+----+----+----+----+
  167. // | 30 | 31 | | 33 | | 36 | 37 | | 31 | | 33 | | | 36 | 37 |
  168. // +----+----+----+----+ +----+----+----+ +----+----+----+----+----+----+----+
  169. // | 40 | | 46 | 47 | 48 | | | | | | | 46 | 47 |
  170. // +----+----+----+----+ +----+----+----+ ==> +----+----+----+----+----+----+----+
  171. // | 50 | 51 | 52 | 53 | | 56 | 57 | 58 | | 51 | 52 | 53 | | | 56 | 57 |
  172. // +----+----+----+----+----+----+ +----+----+ +----+----+----+----+----+----+----+
  173. // | 60 | 61 | 64 | 65 | | 67 | 68 | | 61 | | | 64 | 65 | | 67 |
  174. // +----+----+----+----+----+----+ +----+----+ +----+----+----+----+----+----+----+
  175. // | 70 | 71 | 72 | 73 | 74 | 75 | | 77 | 78 | | 71 | 72 | 73 | 74 | 75 | | 77 |
  176. // +----+ +----+----+----+----+ +----+----+ +----+----+----+----+----+----+----+
  177. // | 80 | | 82 | 83 | 84 | 85 | | 87 | 88 |
  178. // +----+----+----+----+----+----+----+----+----+
  179. //
  180. setModelData( model, modelTable( [
  181. [ '00', '01', { contents: '02', rowspan: 4 }, '03', { contents: '04', colspan: 2, rowspan: 7 }, '07', '07', '08' ],
  182. [ '10', '11', '13', '17', '17', '18' ],
  183. [ '20', '21', '23', { contents: '27', colspan: 3 } ],
  184. [ '30', '31', '33', '37', { contents: '37', colspan: 2 } ],
  185. [ { contents: '40', colspan: 4 }, '47', '47', '48' ],
  186. [ '50', '51', '52', '53', { contents: '57', rowspan: 4 }, '57', '58' ],
  187. [ '60', { contents: '61', colspan: 3 }, '67', '68' ],
  188. [ '70', { contents: '71', rowspan: 2 }, '72', '73', '74', '75', '77', '78' ],
  189. [ '80', '82', '83', '84', '85', '87', '88' ]
  190. ] ) );
  191. tableSelection._setCellSelection(
  192. modelRoot.getNodeByPath( [ 0, 2, 1 ] ),
  193. modelRoot.getNodeByPath( [ 0, 7, 6 ] )
  194. );
  195. assertClipboardContentOnMethod( 'copy', viewTable( [
  196. [ '21', ' ', '23', ' ', ' ', { contents: '27', colspan: 2 } ],
  197. [ '31', ' ', '33', ' ', ' ', '37', '37' ],
  198. [ ' ', ' ', ' ', ' ', ' ', '47', '47' ],
  199. [ '51', '52', '53', ' ', ' ', { contents: '57', rowspan: 3 }, '57' ],
  200. [ { contents: '61', colspan: 3 }, ' ', ' ', ' ', '67' ],
  201. [ '71', '72', '73', '74', '75', '77' ]
  202. ] ) );
  203. } );
  204. it( 'should update table heading attributes (selection with headings)', () => {
  205. setModelData( model, modelTable( [
  206. [ '00', '01', '02', '03', '04' ],
  207. [ '10', '11', '12', '13', '14' ],
  208. [ '20', '21', '22', '23', '24' ],
  209. [ '30', '31', '32', '33', '34' ],
  210. [ '40', '41', '42', '43', '44' ]
  211. ], { headingRows: 3, headingColumns: 2 } ) );
  212. tableSelection._setCellSelection(
  213. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  214. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  215. );
  216. assertClipboardContentOnMethod( 'copy', viewTable( [
  217. [ '11', '12', '13' ],
  218. [ '21', '22', '23' ],
  219. [ { contents: '31', isHeading: true }, '32', '33' ] // TODO: bug in viewTable
  220. ], { headingRows: 2, headingColumns: 1 } ) );
  221. } );
  222. it( 'should update table heading attributes (selection without headings)', () => {
  223. setModelData( model, modelTable( [
  224. [ '00', '01', '02', '03', '04' ],
  225. [ '10', '11', '12', '13', '14' ],
  226. [ '20', '21', '22', '23', '24' ],
  227. [ '30', '31', '32', '33', '34' ],
  228. [ '40', '41', '42', '43', '44' ]
  229. ], { headingRows: 3, headingColumns: 2 } ) );
  230. tableSelection._setCellSelection(
  231. modelRoot.getNodeByPath( [ 0, 3, 2 ] ),
  232. modelRoot.getNodeByPath( [ 0, 4, 4 ] )
  233. );
  234. assertClipboardContentOnMethod( 'copy', viewTable( [
  235. [ '32', '33', '34' ],
  236. [ '42', '43', '44' ]
  237. ] ) );
  238. } );
  239. } );
  240. describe( 'cut', () => {
  241. it( 'should not block clipboardOutput if no multi-cell selection', () => {
  242. setModelData( model, modelTable( [
  243. [ '[00]', '01', '02' ],
  244. [ '10', '11', '12' ],
  245. [ '20', '21', '22' ]
  246. ] ) );
  247. const dataTransferMock = createDataTransfer();
  248. viewDocument.fire( 'cut', {
  249. dataTransfer: dataTransferMock,
  250. preventDefault: sinon.spy()
  251. } );
  252. expect( dataTransferMock.getData( 'text/html' ) ).to.equal( '00' );
  253. } );
  254. it( 'should be preventable', () => {
  255. tableSelection._setCellSelection(
  256. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  257. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  258. );
  259. viewDocument.on( 'clipboardOutput', evt => evt.stop(), { priority: 'high' } );
  260. viewDocument.fire( 'cut', {
  261. dataTransfer: createDataTransfer(),
  262. preventDefault: sinon.spy()
  263. } );
  264. assertEqualMarkup( getModelData( model ), modelTable( [
  265. [ { contents: '00', isSelected: true }, { contents: '01', isSelected: true }, '02' ],
  266. [ { contents: '10', isSelected: true }, { contents: '11', isSelected: true }, '12' ],
  267. [ '20', '21', '22' ]
  268. ] ) );
  269. } );
  270. it( 'is clears selected table cells', () => {
  271. const spy = sinon.spy();
  272. viewDocument.on( 'clipboardOutput', spy );
  273. tableSelection._setCellSelection(
  274. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  275. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  276. );
  277. viewDocument.fire( 'cut', {
  278. dataTransfer: createDataTransfer(),
  279. preventDefault: sinon.spy()
  280. } );
  281. assertEqualMarkup( getModelData( model ), modelTable( [
  282. [ '', '', '02' ],
  283. [ '', '[]', '12' ],
  284. [ '20', '21', '22' ]
  285. ] ) );
  286. } );
  287. it( 'should copy selected table cells as a standalone table', () => {
  288. const preventDefaultSpy = sinon.spy();
  289. tableSelection._setCellSelection(
  290. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  291. modelRoot.getNodeByPath( [ 0, 1, 2 ] )
  292. );
  293. const data = {
  294. dataTransfer: createDataTransfer(),
  295. preventDefault: preventDefaultSpy
  296. };
  297. viewDocument.fire( 'cut', data );
  298. sinon.assert.calledOnce( preventDefaultSpy );
  299. expect( data.dataTransfer.getData( 'text/html' ) ).to.equal( viewTable( [
  300. [ '01', '02' ],
  301. [ '11', '12' ]
  302. ] ) );
  303. } );
  304. it( 'should be disabled in a readonly mode', () => {
  305. const preventDefaultStub = sinon.stub();
  306. editor.isReadOnly = true;
  307. tableSelection._setCellSelection(
  308. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  309. modelRoot.getNodeByPath( [ 0, 1, 2 ] )
  310. );
  311. const data = {
  312. dataTransfer: createDataTransfer(),
  313. preventDefault: preventDefaultStub
  314. };
  315. viewDocument.fire( 'cut', data );
  316. editor.isReadOnly = false;
  317. expect( data.dataTransfer.getData( 'text/html' ) ).to.be.undefined;
  318. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  319. [ '00', '01', '02' ],
  320. [ '10', '11', '12' ],
  321. [ '20', '21', '22' ]
  322. ] ) );
  323. sinon.assert.calledOnce( preventDefaultStub );
  324. } );
  325. } );
  326. } );
  327. function assertClipboardContentOnMethod( method, expectedViewTable ) {
  328. const data = {
  329. dataTransfer: createDataTransfer(),
  330. preventDefault: sinon.spy()
  331. };
  332. viewDocument.fire( method, data );
  333. expect( data.dataTransfer.getData( 'text/html' ) ).to.equal( expectedViewTable );
  334. }
  335. function createDataTransfer() {
  336. const store = new Map();
  337. return {
  338. setData( type, data ) {
  339. store.set( type, data );
  340. },
  341. getData( type ) {
  342. return store.get( type );
  343. }
  344. };
  345. }
  346. } );