insertcolumncommand.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import InsertColumnCommand from '../../src/commands/insertcolumncommand';
  8. import TableSelection from '../../src/tableselection';
  9. import { assertSelectedCells, defaultConversion, defaultSchema, modelTable } from '../_utils/utils';
  10. import TableUtils from '../../src/tableutils';
  11. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  12. describe( 'InsertColumnCommand', () => {
  13. let editor, model, command;
  14. beforeEach( () => {
  15. return ModelTestEditor
  16. .create( {
  17. plugins: [ TableUtils, TableSelection ]
  18. } )
  19. .then( newEditor => {
  20. editor = newEditor;
  21. model = editor.model;
  22. defaultSchema( model.schema );
  23. defaultConversion( editor.conversion );
  24. } );
  25. } );
  26. afterEach( () => {
  27. return editor.destroy();
  28. } );
  29. describe( 'order=right', () => {
  30. beforeEach( () => {
  31. command = new InsertColumnCommand( editor );
  32. } );
  33. describe( 'isEnabled', () => {
  34. it( 'should be false if wrong node', () => {
  35. setData( model, '<paragraph>foo[]</paragraph>' );
  36. expect( command.isEnabled ).to.be.false;
  37. } );
  38. it( 'should be true if in table', () => {
  39. setData( model, modelTable( [ [ '[]' ] ] ) );
  40. expect( command.isEnabled ).to.be.true;
  41. } );
  42. } );
  43. describe( 'execute()', () => {
  44. it( 'should insert column in given table to the right of the selection\'s column', () => {
  45. setData( model, modelTable( [
  46. [ '11[]', '12' ],
  47. [ '21', '22' ]
  48. ] ) );
  49. command.execute();
  50. assertEqualMarkup( getData( model ), modelTable( [
  51. [ '11[]', '', '12' ],
  52. [ '21', '', '22' ]
  53. ] ) );
  54. } );
  55. it( 'should insert column in given table to the right of the selection\'s column (selection in block content)', () => {
  56. setData( model, modelTable( [
  57. [ '11', '<paragraph>12[]</paragraph>', '13' ]
  58. ] ) );
  59. command.execute();
  60. assertEqualMarkup( getData( model ), modelTable( [
  61. [ '11', '<paragraph>12[]</paragraph>', '', '13' ]
  62. ] ) );
  63. } );
  64. it( 'should insert column at table end', () => {
  65. setData( model, modelTable( [
  66. [ '11', '12' ],
  67. [ '21', '22[]' ]
  68. ] ) );
  69. command.execute();
  70. assertEqualMarkup( getData( model ), modelTable( [
  71. [ '11', '12', '' ],
  72. [ '21', '22[]', '' ]
  73. ] ) );
  74. } );
  75. it( 'should insert column after a multi column selection', () => {
  76. setData( model, modelTable( [
  77. [ '11', '12', '13' ],
  78. [ '21', '22', '23' ]
  79. ] ) );
  80. const tableSelection = editor.plugins.get( TableSelection );
  81. const modelRoot = model.document.getRoot();
  82. tableSelection._setCellSelection(
  83. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  84. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  85. );
  86. command.execute();
  87. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  88. [ '11', '12', '', '13' ],
  89. [ '21', '22', '', '23' ]
  90. ] ) );
  91. assertSelectedCells( model, [
  92. [ 1, 1, 0, 0 ],
  93. [ 1, 1, 0, 0 ]
  94. ] );
  95. } );
  96. it( 'should update table heading columns attribute when inserting column in headings section', () => {
  97. setData( model, modelTable( [
  98. [ '11[]', '12' ],
  99. [ '21', '22' ],
  100. [ '31', '32' ]
  101. ], { headingColumns: 2 } ) );
  102. command.execute();
  103. assertEqualMarkup( getData( model ), modelTable( [
  104. [ '11[]', '', '12' ],
  105. [ '21', '', '22' ],
  106. [ '31', '', '32' ]
  107. ], { headingColumns: 3 } ) );
  108. } );
  109. it( 'should not update table heading columns attribute when inserting column after headings section', () => {
  110. setData( model, modelTable( [
  111. [ '11', '12[]', '13' ],
  112. [ '21', '22', '23' ],
  113. [ '31', '32', '33' ]
  114. ], { headingColumns: 2 } ) );
  115. command.execute();
  116. assertEqualMarkup( getData( model ), modelTable( [
  117. [ '11', '12[]', '', '13' ],
  118. [ '21', '22', '', '23' ],
  119. [ '31', '32', '', '33' ]
  120. ], { headingColumns: 2 } ) );
  121. } );
  122. it( 'should skip spanned columns', () => {
  123. setData( model, modelTable( [
  124. [ '11[]', '12' ],
  125. [ { colspan: 2, contents: '21' } ],
  126. [ '31', '32' ]
  127. ], { headingColumns: 2 } ) );
  128. command.execute();
  129. assertEqualMarkup( getData( model ), modelTable( [
  130. [ '11[]', '', '12' ],
  131. [ { colspan: 3, contents: '21' } ],
  132. [ '31', '', '32' ]
  133. ], { headingColumns: 3 } ) );
  134. } );
  135. it( 'should skip wide spanned columns', () => {
  136. setData( model, modelTable( [
  137. [ '11', '12[]', '13', '14', '15' ],
  138. [ '21', '22', { colspan: 2, contents: '23' }, '25' ],
  139. [ { colspan: 4, contents: '31' }, { colspan: 2, contents: '34' } ]
  140. ], { headingColumns: 4 } ) );
  141. command.execute();
  142. assertEqualMarkup( getData( model ), modelTable( [
  143. [ '11', '12[]', '', '13', '14', '15' ],
  144. [ '21', '22', '', { colspan: 2, contents: '23' }, '25' ],
  145. [ { colspan: 5, contents: '31' }, { colspan: 2, contents: '34' } ]
  146. ], { headingColumns: 5 } ) );
  147. } );
  148. } );
  149. } );
  150. describe( 'order=left', () => {
  151. beforeEach( () => {
  152. command = new InsertColumnCommand( editor, { order: 'left' } );
  153. } );
  154. describe( 'isEnabled', () => {
  155. it( 'should be false if wrong node', () => {
  156. setData( model, '<paragraph>foo[]</paragraph>' );
  157. expect( command.isEnabled ).to.be.false;
  158. } );
  159. it( 'should be true if in table', () => {
  160. setData( model, modelTable( [ [ '[]' ] ] ) );
  161. expect( command.isEnabled ).to.be.true;
  162. } );
  163. } );
  164. describe( 'execute()', () => {
  165. it( 'should insert column in given table to the left of the selection\'s column', () => {
  166. setData( model, modelTable( [
  167. [ '11', '12[]' ],
  168. [ '21', '22' ]
  169. ] ) );
  170. command.execute();
  171. assertEqualMarkup( getData( model ), modelTable( [
  172. [ '11', '', '12[]' ],
  173. [ '21', '', '22' ]
  174. ] ) );
  175. } );
  176. it( 'should insert column in given table to the left of the selection\'s column (selection in block content)', () => {
  177. setData( model, modelTable( [
  178. [ '11', '<paragraph>12[]</paragraph>', '13' ]
  179. ] ) );
  180. command.execute();
  181. assertEqualMarkup( getData( model ), modelTable( [
  182. [ '11', '', '<paragraph>12[]</paragraph>', '13' ]
  183. ] ) );
  184. } );
  185. it( 'should insert columns at the table start', () => {
  186. setData( model, modelTable( [
  187. [ '11', '12' ],
  188. [ '[]21', '22' ]
  189. ] ) );
  190. command.execute();
  191. assertEqualMarkup( getData( model ), modelTable( [
  192. [ '', '11', '12' ],
  193. [ '', '[]21', '22' ]
  194. ] ) );
  195. } );
  196. it( 'should insert column before a multi column selection', () => {
  197. setData( model, modelTable( [
  198. [ '11', '12', '13' ],
  199. [ '21', '22', '23' ]
  200. ] ) );
  201. const tableSelection = editor.plugins.get( TableSelection );
  202. const modelRoot = model.document.getRoot();
  203. tableSelection._setCellSelection(
  204. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  205. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  206. );
  207. command.execute();
  208. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  209. [ '', '11', '12', '13' ],
  210. [ '', '21', '22', '23' ]
  211. ] ) );
  212. assertSelectedCells( model, [
  213. [ 0, 1, 1, 0 ],
  214. [ 0, 1, 1, 0 ]
  215. ] );
  216. } );
  217. it( 'should update table heading columns attribute when inserting column in headings section', () => {
  218. setData( model, modelTable( [
  219. [ '11', '12[]' ],
  220. [ '21', '22' ],
  221. [ '31', '32' ]
  222. ], { headingColumns: 2 } ) );
  223. command.execute();
  224. assertEqualMarkup( getData( model ), modelTable( [
  225. [ '11', '', '12[]' ],
  226. [ '21', '', '22' ],
  227. [ '31', '', '32' ]
  228. ], { headingColumns: 3 } ) );
  229. } );
  230. it( 'should not update table heading columns attribute when inserting column after headings section', () => {
  231. setData( model, modelTable( [
  232. [ '11', '12', '13[]' ],
  233. [ '21', '22', '23' ],
  234. [ '31', '32', '33' ]
  235. ], { headingColumns: 2 } ) );
  236. command.execute();
  237. assertEqualMarkup( getData( model ), modelTable( [
  238. [ '11', '12', '', '13[]' ],
  239. [ '21', '22', '', '23' ],
  240. [ '31', '32', '', '33' ]
  241. ], { headingColumns: 2 } ) );
  242. } );
  243. it( 'should skip spanned columns', () => {
  244. setData( model, modelTable( [
  245. [ '11', '12[]' ],
  246. [ { colspan: 2, contents: '21' } ],
  247. [ '31', '32' ]
  248. ], { headingColumns: 2 } ) );
  249. command.execute();
  250. assertEqualMarkup( getData( model ), modelTable( [
  251. [ '11', '', '12[]' ],
  252. [ { colspan: 3, contents: '21' } ],
  253. [ '31', '', '32' ]
  254. ], { headingColumns: 3 } ) );
  255. } );
  256. it( 'should skip wide spanned columns', () => {
  257. setData( model, modelTable( [
  258. [ '11', '12', '13[]', '14', '15' ],
  259. [ '21', '22', { colspan: 2, contents: '23' }, '25' ],
  260. [ { colspan: 4, contents: '31' }, { colspan: 2, contents: '34' } ]
  261. ], { headingColumns: 4 } ) );
  262. command.execute();
  263. assertEqualMarkup( getData( model ), modelTable( [
  264. [ '11', '12', '', '13[]', '14', '15' ],
  265. [ '21', '22', '', { colspan: 2, contents: '23' }, '25' ],
  266. [ { colspan: 5, contents: '31' }, { colspan: 2, contents: '34' } ]
  267. ], { headingColumns: 5 } ) );
  268. } );
  269. } );
  270. } );
  271. } );