8
0

tableclipboard.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  47. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 2 ] ) );
  48. const data = {
  49. dataTransfer: createDataTransfer(),
  50. preventDefault: preventDefaultSpy
  51. };
  52. viewDocument.fire( 'copy', data );
  53. sinon.assert.calledOnce( preventDefaultSpy );
  54. expect( data.dataTransfer.getData( 'text/html' ) ).to.equal( viewTable( [
  55. [ '01', '02' ],
  56. [ '11', '12' ]
  57. ] ) );
  58. } );
  59. it( 'should trim selected table to a selection rectangle (inner cell with colspan, no colspan after trim)', () => {
  60. setModelData( model, modelTable( [
  61. [ '00[]', '01', '02' ],
  62. [ '10', { contents: '11', colspan: 2 } ],
  63. [ '20', '21', '22' ]
  64. ] ) );
  65. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  66. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 1 ] ) );
  67. assertClipboardContentOnMethod( 'copy', viewTable( [
  68. [ '00', '01' ],
  69. [ '10', '11' ],
  70. [ '20', '21' ]
  71. ] ) );
  72. } );
  73. it( 'should trim selected table to a selection rectangle (inner cell with colspan, has colspan after trim)', () => {
  74. setModelData( model, modelTable( [
  75. [ '00[]', '01', '02' ],
  76. [ { contents: '10', colspan: 3 } ],
  77. [ '20', '21', '22' ]
  78. ] ) );
  79. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  80. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 1 ] ) );
  81. assertClipboardContentOnMethod( 'copy', viewTable( [
  82. [ '00', '01' ],
  83. [ { contents: '10', colspan: 2 } ],
  84. [ '20', '21' ]
  85. ] ) );
  86. } );
  87. it( 'should trim selected table to a selection rectangle (inner cell with rowspan, no colspan after trim)', () => {
  88. setModelData( model, modelTable( [
  89. [ '00[]', '01', '02' ],
  90. [ '10', { contents: '11', rowspan: 2 }, '12' ],
  91. [ '20', '21', '22' ]
  92. ] ) );
  93. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  94. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 2 ] ) );
  95. assertClipboardContentOnMethod( 'copy', viewTable( [
  96. [ '00', '01', '02' ],
  97. [ '10', '11', '12' ]
  98. ] ) );
  99. } );
  100. it( 'should trim selected table to a selection rectangle (inner cell with rowspan, has rowspan after trim)', () => {
  101. setModelData( model, modelTable( [
  102. [ '00[]', { contents: '01', rowspan: 3 }, '02' ],
  103. [ '10', '12' ],
  104. [ '20', '22' ]
  105. ] ) );
  106. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  107. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  108. assertClipboardContentOnMethod( 'copy', viewTable( [
  109. [ '00', { contents: '01', rowspan: 2 }, '02' ],
  110. [ '10', '12' ]
  111. ] ) );
  112. } );
  113. it( 'should prepend spanned columns with empty cells (outside cell with colspan)', () => {
  114. setModelData( model, modelTable( [
  115. [ '00[]', '01', '02' ],
  116. [ { contents: '10', colspan: 2 }, '12' ],
  117. [ '20', '21', '22' ]
  118. ] ) );
  119. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  120. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 2 ] ) );
  121. assertClipboardContentOnMethod( 'copy', viewTable( [
  122. [ '01', '02' ],
  123. [ ' ', '12' ],
  124. [ '21', '22' ]
  125. ] ) );
  126. } );
  127. it( 'should prepend spanned columns with empty cells (outside cell with rowspan)', () => {
  128. setModelData( model, modelTable( [
  129. [ '00[]', { contents: '01', rowspan: 2 }, '02' ],
  130. [ '10', '12' ],
  131. [ '20', '21', '22' ]
  132. ] ) );
  133. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 1, 0 ] ) );
  134. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 2 ] ) );
  135. assertClipboardContentOnMethod( 'copy', viewTable( [
  136. [ '10', ' ', '12' ],
  137. [ '20', '21', '22' ]
  138. ] ) );
  139. } );
  140. it( 'should fix selected table to a selection rectangle (hardcore case)', () => {
  141. // This test check how previous simple rules run together (mixed prepending and trimming).
  142. // In the example below a selection is set from cell "32" to "88"
  143. //
  144. // Input table: Copied table:
  145. //
  146. // +----+----+----+----+----+----+----+----+----+
  147. // | 00 | 01 | 02 | 03 | 04 | 06 | 07 | 08 |
  148. // +----+----+ +----+ +----+----+----+
  149. // | 10 | 11 | | 13 | | 16 | 17 | 18 |
  150. // +----+----+ +----+ +----+----+----+ +----+----+----+---------+----+----+
  151. // | 20 | 21 | | 23 | | 26 | | 21 | | 23 | | | 26 | |
  152. // +----+----+ +----+ +----+----+----+ +----+----+----+----+----+----+----+
  153. // | 30 | 31 | | 33 | | 36 | 37 | | 31 | | 33 | | | 36 | 37 |
  154. // +----+----+----+----+ +----+----+----+ +----+----+----+----+----+----+----+
  155. // | 40 | | 46 | 47 | 48 | | | | | | | 46 | 47 |
  156. // +----+----+----+----+ +----+----+----+ ==> +----+----+----+----+----+----+----+
  157. // | 50 | 51 | 52 | 53 | | 56 | 57 | 58 | | 51 | 52 | 53 | | | 56 | 57 |
  158. // +----+----+----+----+----+----+ +----+----+ +----+----+----+----+----+----+----+
  159. // | 60 | 61 | 64 | 65 | | 67 | 68 | | 61 | | | 64 | 65 | | 67 |
  160. // +----+----+----+----+----+----+ +----+----+ +----+----+----+----+----+----+----+
  161. // | 70 | 71 | 72 | 73 | 74 | 75 | | 77 | 78 | | 71 | 72 | 73 | 74 | 75 | | 77 |
  162. // +----+ +----+----+----+----+ +----+----+ +----+----+----+----+----+----+----+
  163. // | 80 | | 82 | 83 | 84 | 85 | | 87 | 88 |
  164. // +----+----+----+----+----+----+----+----+----+
  165. //
  166. setModelData( model, modelTable( [
  167. [ '00', '01', { contents: '02', rowspan: 4 }, '03', { contents: '04', colspan: 2, rowspan: 7 }, '07', '07', '08' ],
  168. [ '10', '11', '13', '17', '17', '18' ],
  169. [ '20', '21', '23', { contents: '27', colspan: 3 } ],
  170. [ '30', '31', '33', '37', { contents: '37', colspan: 2 } ],
  171. [ { contents: '40', colspan: 4 }, '47', '47', '48' ],
  172. [ '50', '51', '52', '53', { contents: '57', rowspan: 4 }, '57', '58' ],
  173. [ '60', { contents: '61', colspan: 3 }, '67', '68' ],
  174. [ '70', { contents: '71', rowspan: 2 }, '72', '73', '74', '75', '77', '78' ],
  175. [ '80', '82', '83', '84', '85', '87', '88' ]
  176. ] ) );
  177. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 2, 1 ] ) );
  178. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 7, 6 ] ) );
  179. assertClipboardContentOnMethod( 'copy', viewTable( [
  180. [ '21', ' ', '23', ' ', ' ', { contents: '27', colspan: 2 } ],
  181. [ '31', ' ', '33', ' ', ' ', '37', '37' ],
  182. [ ' ', ' ', ' ', ' ', ' ', '47', '47' ],
  183. [ '51', '52', '53', ' ', ' ', { contents: '57', rowspan: 3 }, '57' ],
  184. [ { contents: '61', colspan: 3 }, ' ', ' ', ' ', '67' ],
  185. [ '71', '72', '73', '74', '75', '77' ]
  186. ] ) );
  187. } );
  188. it( 'should update table heading attributes (selection with headings)', () => {
  189. setModelData( model, modelTable( [
  190. [ '00', '01', '02', '03', '04' ],
  191. [ '10', '11', '12', '13', '14' ],
  192. [ '20', '21', '22', '23', '24' ],
  193. [ '30', '31', '32', '33', '34' ],
  194. [ '40', '41', '42', '43', '44' ]
  195. ], { headingRows: 3, headingColumns: 2 } ) );
  196. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  197. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 3, 3 ] ) );
  198. assertClipboardContentOnMethod( 'copy', viewTable( [
  199. [ '11', '12', '13' ],
  200. [ '21', '22', '23' ],
  201. [ { contents: '31', isHeading: true }, '32', '33' ] // TODO: bug in viewTable
  202. ], { headingRows: 2, headingColumns: 1 } ) );
  203. } );
  204. it( 'should update table heading attributes (selection without 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.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 3, 2 ] ) );
  213. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 4, 4 ] ) );
  214. assertClipboardContentOnMethod( 'copy', viewTable( [
  215. [ '32', '33', '34' ],
  216. [ '42', '43', '44' ]
  217. ] ) );
  218. } );
  219. } );
  220. describe( 'cut', () => {
  221. it( 'should not block clipboardOutput if no multi-cell selection', () => {
  222. setModelData( model, modelTable( [
  223. [ '[00]', '01', '02' ],
  224. [ '10', '11', '12' ],
  225. [ '20', '21', '22' ]
  226. ] ) );
  227. const dataTransferMock = createDataTransfer();
  228. viewDocument.fire( 'cut', {
  229. dataTransfer: dataTransferMock,
  230. preventDefault: sinon.spy()
  231. } );
  232. expect( dataTransferMock.getData( 'text/html' ) ).to.equal( '00' );
  233. } );
  234. it( 'should be preventable', () => {
  235. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  236. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  237. viewDocument.on( 'clipboardOutput', evt => evt.stop(), { priority: 'high' } );
  238. viewDocument.fire( 'cut', {
  239. dataTransfer: createDataTransfer(),
  240. preventDefault: sinon.spy()
  241. } );
  242. assertEqualMarkup( getModelData( model ), modelTable( [
  243. [ { contents: '00', isSelected: true }, { contents: '01', isSelected: true }, '02' ],
  244. [ { contents: '10', isSelected: true }, { contents: '11', isSelected: true }, '12' ],
  245. [ '20', '21', '22' ]
  246. ] ) );
  247. } );
  248. it( 'is clears selected table cells', () => {
  249. const spy = sinon.spy();
  250. viewDocument.on( 'clipboardOutput', spy );
  251. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  252. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  253. viewDocument.fire( 'cut', {
  254. dataTransfer: createDataTransfer(),
  255. preventDefault: sinon.spy()
  256. } );
  257. assertEqualMarkup( getModelData( model ), modelTable( [
  258. [ '', '', '02' ],
  259. [ '', '[]', '12' ],
  260. [ '20', '21', '22' ]
  261. ] ) );
  262. } );
  263. it( 'should copy selected table cells as a standalone table', () => {
  264. const preventDefaultSpy = sinon.spy();
  265. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  266. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 2 ] ) );
  267. const data = {
  268. dataTransfer: createDataTransfer(),
  269. preventDefault: preventDefaultSpy
  270. };
  271. viewDocument.fire( 'cut', data );
  272. sinon.assert.calledOnce( preventDefaultSpy );
  273. expect( data.dataTransfer.getData( 'text/html' ) ).to.equal( viewTable( [
  274. [ '01', '02' ],
  275. [ '11', '12' ]
  276. ] ) );
  277. } );
  278. } );
  279. } );
  280. function assertClipboardContentOnMethod( method, expectedViewTable ) {
  281. const data = {
  282. dataTransfer: createDataTransfer(),
  283. preventDefault: sinon.spy()
  284. };
  285. viewDocument.fire( method, data );
  286. expect( data.dataTransfer.getData( 'text/html' ) ).to.equal( expectedViewTable );
  287. }
  288. function createDataTransfer() {
  289. const store = new Map();
  290. return {
  291. setData( type, data ) {
  292. store.set( type, data );
  293. },
  294. getData( type ) {
  295. return store.get( type );
  296. }
  297. };
  298. }
  299. } );