8
0

tableclipboard-copy-cut.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 fix selected table to a selection rectangle (hardcore case)', () => {
  158. // This test check how previous simple rules run together (mixed prepending and trimming).
  159. // In the example below a selection is set from cell "21" to "77"
  160. //
  161. // Input table: Copied table:
  162. //
  163. // +----+----+----+----+----+----+----+----+----+
  164. // | 00 | 01 | 02 | 03 | 04 | 06 | 07 | 08 |
  165. // +----+----+ +----+ +----+----+----+
  166. // | 10 | 11 | | 13 | | 16 | 17 | 18 |
  167. // +----+----+ +----+ +----+----+----+ +----+----+----+----+----+----+----+
  168. // | 20 | 21 | | 23 | | 26 | | 21 | | 23 | | | 26 |
  169. // +----+----+ +----+ +----+----+----+ +----+----+----+----+----+----+----+
  170. // | 30 | 31 | | 33 | | 36 | 37 | | 31 | | 33 | | | 36 | 37 |
  171. // +----+----+----+----+ +----+----+----+ +----+----+----+----+----+----+----+
  172. // | 40 | | 46 | 47 | 48 | | | | | | | 46 | 47 |
  173. // +----+----+----+----+ +----+----+----+ ==> +----+----+----+----+----+----+----+
  174. // | 50 | 51 | 52 | 53 | | 56 | 57 | 58 | | 51 | 52 | 53 | | | 56 | 57 |
  175. // +----+----+----+----+ + +----+----+ +----+----+----+----+----+ +----+
  176. // | 60 | 61 | | | 67 | 68 | | 61 | | | | 67 |
  177. // +----+----+----+----+----+----+ +----+----+ +----+----+----+----+----+ +----+
  178. // | 70 | 71 | 72 | 73 | 74 | 75 | | 77 | 78 | | 71 | 72 | 73 | 74 | 75 | | 77 |
  179. // +----+ +----+----+----+----+ +----+----+ +----+----+----+----+----+----+----+
  180. // | 80 | | 82 | 83 | 84 | 85 | | 87 | 88 |
  181. // +----+----+----+----+----+----+----+----+----+
  182. //
  183. setModelData( model, modelTable( [
  184. [ '00', '01', { contents: '02', rowspan: 4 }, '03', { contents: '04', colspan: 2, rowspan: 7 }, '06', '07', '08' ],
  185. [ '10', '11', '13', '16', '17', '18' ],
  186. [ '20', '21', '23', { contents: '26', colspan: 3 } ],
  187. [ '30', '31', '33', '36', { contents: '37', colspan: 2 } ],
  188. [ { contents: '40', colspan: 4 }, '46', '47', '48' ],
  189. [ '50', '51', '52', '53', { contents: '56', rowspan: 4 }, '57', '58' ],
  190. [ '60', { contents: '61', colspan: 3 }, '67', '68' ],
  191. [ '70', { contents: '71', rowspan: 2 }, '72', '73', '74', '75', '77', '78' ],
  192. [ '80', '82', '83', '84', '85', '87', '88' ]
  193. ] ) );
  194. tableSelection.setCellSelection(
  195. modelRoot.getNodeByPath( [ 0, 2, 1 ] ),
  196. modelRoot.getNodeByPath( [ 0, 7, 6 ] )
  197. );
  198. assertClipboardContentOnMethod( 'copy', viewTable( [
  199. [ '21', ' ', '23', ' ', ' ', { contents: '26', colspan: 2 } ],
  200. [ '31', ' ', '33', ' ', ' ', '36', '37' ],
  201. [ ' ', ' ', ' ', ' ', ' ', '46', '47' ],
  202. [ '51', '52', '53', ' ', ' ', { contents: '56', rowspan: 3 }, '57' ],
  203. [ { contents: '61', colspan: 3 }, ' ', ' ', '67' ],
  204. [ '71', '72', '73', '74', '75', '77' ]
  205. ] ) );
  206. } );
  207. it( 'should update table heading attributes (selection with headings)', () => {
  208. setModelData( model, modelTable( [
  209. [ '00', '01', '02', '03', '04' ],
  210. [ '10', '11', '12', '13', '14' ],
  211. [ '20', '21', '22', '23', '24' ],
  212. [ '30', '31', '32', '33', '34' ],
  213. [ '40', '41', '42', '43', '44' ]
  214. ], { headingRows: 3, headingColumns: 2 } ) );
  215. tableSelection.setCellSelection(
  216. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  217. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  218. );
  219. assertClipboardContentOnMethod( 'copy', viewTable( [
  220. [ '11', '12', '13' ],
  221. [ '21', '22', '23' ],
  222. [ { contents: '31', isHeading: true }, '32', '33' ] // TODO: bug in viewTable
  223. ], { headingRows: 2, headingColumns: 1 } ) );
  224. } );
  225. it( 'should update table heading attributes (selection without headings)', () => {
  226. setModelData( model, modelTable( [
  227. [ '00', '01', '02', '03', '04' ],
  228. [ '10', '11', '12', '13', '14' ],
  229. [ '20', '21', '22', '23', '24' ],
  230. [ '30', '31', '32', '33', '34' ],
  231. [ '40', '41', '42', '43', '44' ]
  232. ], { headingRows: 3, headingColumns: 2 } ) );
  233. tableSelection.setCellSelection(
  234. modelRoot.getNodeByPath( [ 0, 3, 2 ] ),
  235. modelRoot.getNodeByPath( [ 0, 4, 4 ] )
  236. );
  237. assertClipboardContentOnMethod( 'copy', viewTable( [
  238. [ '32', '33', '34' ],
  239. [ '42', '43', '44' ]
  240. ] ) );
  241. } );
  242. } );
  243. describe( 'Clipboard integration - cut', () => {
  244. it( 'should not block clipboardOutput if no multi-cell selection', () => {
  245. setModelData( model, modelTable( [
  246. [ '[00]', '01', '02' ],
  247. [ '10', '11', '12' ],
  248. [ '20', '21', '22' ]
  249. ] ) );
  250. const dataTransferMock = createDataTransfer();
  251. viewDocument.fire( 'cut', {
  252. dataTransfer: dataTransferMock,
  253. preventDefault: sinon.spy()
  254. } );
  255. expect( dataTransferMock.getData( 'text/html' ) ).to.equal( '00' );
  256. } );
  257. it( 'should be preventable', () => {
  258. tableSelection.setCellSelection(
  259. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  260. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  261. );
  262. viewDocument.on( 'clipboardOutput', evt => evt.stop(), { priority: 'high' } );
  263. viewDocument.fire( 'cut', {
  264. dataTransfer: createDataTransfer(),
  265. preventDefault: sinon.spy()
  266. } );
  267. assertEqualMarkup( getModelData( model ), modelTable( [
  268. [ { contents: '00', isSelected: true }, { contents: '01', isSelected: true }, '02' ],
  269. [ { contents: '10', isSelected: true }, { contents: '11', isSelected: true }, '12' ],
  270. [ '20', '21', '22' ]
  271. ] ) );
  272. } );
  273. it( 'is clears selected table cells', () => {
  274. const spy = sinon.spy();
  275. viewDocument.on( 'clipboardOutput', spy );
  276. tableSelection.setCellSelection(
  277. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  278. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  279. );
  280. viewDocument.fire( 'cut', {
  281. dataTransfer: createDataTransfer(),
  282. preventDefault: sinon.spy()
  283. } );
  284. assertEqualMarkup( getModelData( model ), modelTable( [
  285. [ '', '', '02' ],
  286. [ '', '[]', '12' ],
  287. [ '20', '21', '22' ]
  288. ] ) );
  289. } );
  290. it( 'should copy selected table cells as a standalone table', () => {
  291. const preventDefaultSpy = sinon.spy();
  292. tableSelection.setCellSelection(
  293. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  294. modelRoot.getNodeByPath( [ 0, 1, 2 ] )
  295. );
  296. const data = {
  297. dataTransfer: createDataTransfer(),
  298. preventDefault: preventDefaultSpy
  299. };
  300. viewDocument.fire( 'cut', data );
  301. sinon.assert.calledOnce( preventDefaultSpy );
  302. expect( data.dataTransfer.getData( 'text/html' ) ).to.equal( viewTable( [
  303. [ '01', '02' ],
  304. [ '11', '12' ]
  305. ] ) );
  306. } );
  307. it( 'should be disabled in a readonly mode', () => {
  308. const preventDefaultStub = sinon.stub();
  309. editor.isReadOnly = true;
  310. tableSelection.setCellSelection(
  311. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  312. modelRoot.getNodeByPath( [ 0, 1, 2 ] )
  313. );
  314. const data = {
  315. dataTransfer: createDataTransfer(),
  316. preventDefault: preventDefaultStub
  317. };
  318. viewDocument.fire( 'cut', data );
  319. editor.isReadOnly = false;
  320. expect( data.dataTransfer.getData( 'text/html' ) ).to.be.undefined;
  321. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  322. [ '00', '01', '02' ],
  323. [ '10', '11', '12' ],
  324. [ '20', '21', '22' ]
  325. ] ) );
  326. sinon.assert.calledOnce( preventDefaultStub );
  327. } );
  328. } );
  329. function assertClipboardContentOnMethod( method, expectedViewTable ) {
  330. const data = {
  331. dataTransfer: createDataTransfer(),
  332. preventDefault: sinon.spy()
  333. };
  334. viewDocument.fire( method, data );
  335. expect( data.dataTransfer.getData( 'text/html' ) ).to.equal( expectedViewTable );
  336. }
  337. function createDataTransfer() {
  338. const store = new Map();
  339. return {
  340. setData( type, data ) {
  341. store.set( type, data );
  342. },
  343. getData( type ) {
  344. return store.get( type );
  345. }
  346. };
  347. }
  348. } );