tableclipboard-copy-cut.js 14 KB

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