tableselection-clipboard.js 14 KB

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