tableclipboard.js 12 KB

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