tableclipboard.js 12 KB

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