insertcolumncommand.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  83. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  84. command.execute();
  85. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  86. [ '11', '12', '', '13' ],
  87. [ '21', '22', '', '23' ]
  88. ] ) );
  89. assertSelectedCells( model, [
  90. [ 1, 1, 0, 0 ],
  91. [ 1, 1, 0, 0 ]
  92. ] );
  93. } );
  94. it( 'should update table heading columns attribute when inserting column in headings section', () => {
  95. setData( model, modelTable( [
  96. [ '11[]', '12' ],
  97. [ '21', '22' ],
  98. [ '31', '32' ]
  99. ], { headingColumns: 2 } ) );
  100. command.execute();
  101. assertEqualMarkup( getData( model ), modelTable( [
  102. [ '11[]', '', '12' ],
  103. [ '21', '', '22' ],
  104. [ '31', '', '32' ]
  105. ], { headingColumns: 3 } ) );
  106. } );
  107. it( 'should not update table heading columns attribute when inserting column after headings section', () => {
  108. setData( model, modelTable( [
  109. [ '11', '12[]', '13' ],
  110. [ '21', '22', '23' ],
  111. [ '31', '32', '33' ]
  112. ], { headingColumns: 2 } ) );
  113. command.execute();
  114. assertEqualMarkup( getData( model ), modelTable( [
  115. [ '11', '12[]', '', '13' ],
  116. [ '21', '22', '', '23' ],
  117. [ '31', '32', '', '33' ]
  118. ], { headingColumns: 2 } ) );
  119. } );
  120. it( 'should skip spanned columns', () => {
  121. setData( model, modelTable( [
  122. [ '11[]', '12' ],
  123. [ { colspan: 2, contents: '21' } ],
  124. [ '31', '32' ]
  125. ], { headingColumns: 2 } ) );
  126. command.execute();
  127. assertEqualMarkup( getData( model ), modelTable( [
  128. [ '11[]', '', '12' ],
  129. [ { colspan: 3, contents: '21' } ],
  130. [ '31', '', '32' ]
  131. ], { headingColumns: 3 } ) );
  132. } );
  133. it( 'should skip wide spanned columns', () => {
  134. setData( model, modelTable( [
  135. [ '11', '12[]', '13', '14', '15' ],
  136. [ '21', '22', { colspan: 2, contents: '23' }, '25' ],
  137. [ { colspan: 4, contents: '31' }, { colspan: 2, contents: '34' } ]
  138. ], { headingColumns: 4 } ) );
  139. command.execute();
  140. assertEqualMarkup( getData( model ), modelTable( [
  141. [ '11', '12[]', '', '13', '14', '15' ],
  142. [ '21', '22', '', { colspan: 2, contents: '23' }, '25' ],
  143. [ { colspan: 5, contents: '31' }, { colspan: 2, contents: '34' } ]
  144. ], { headingColumns: 5 } ) );
  145. } );
  146. } );
  147. } );
  148. describe( 'order=left', () => {
  149. beforeEach( () => {
  150. command = new InsertColumnCommand( editor, { order: 'left' } );
  151. } );
  152. describe( 'isEnabled', () => {
  153. it( 'should be false if wrong node', () => {
  154. setData( model, '<paragraph>foo[]</paragraph>' );
  155. expect( command.isEnabled ).to.be.false;
  156. } );
  157. it( 'should be true if in table', () => {
  158. setData( model, modelTable( [ [ '[]' ] ] ) );
  159. expect( command.isEnabled ).to.be.true;
  160. } );
  161. } );
  162. describe( 'execute()', () => {
  163. it( 'should insert column in given table to the left of the selection\'s column', () => {
  164. setData( model, modelTable( [
  165. [ '11', '12[]' ],
  166. [ '21', '22' ]
  167. ] ) );
  168. command.execute();
  169. assertEqualMarkup( getData( model ), modelTable( [
  170. [ '11', '', '12[]' ],
  171. [ '21', '', '22' ]
  172. ] ) );
  173. } );
  174. it( 'should insert column in given table to the left of the selection\'s column (selection in block content)', () => {
  175. setData( model, modelTable( [
  176. [ '11', '<paragraph>12[]</paragraph>', '13' ]
  177. ] ) );
  178. command.execute();
  179. assertEqualMarkup( getData( model ), modelTable( [
  180. [ '11', '', '<paragraph>12[]</paragraph>', '13' ]
  181. ] ) );
  182. } );
  183. it( 'should insert columns at the table start', () => {
  184. setData( model, modelTable( [
  185. [ '11', '12' ],
  186. [ '[]21', '22' ]
  187. ] ) );
  188. command.execute();
  189. assertEqualMarkup( getData( model ), modelTable( [
  190. [ '', '11', '12' ],
  191. [ '', '[]21', '22' ]
  192. ] ) );
  193. } );
  194. it( 'should insert column before a multi column selection', () => {
  195. setData( model, modelTable( [
  196. [ '11', '12', '13' ],
  197. [ '21', '22', '23' ]
  198. ] ) );
  199. const tableSelection = editor.plugins.get( TableSelection );
  200. const modelRoot = model.document.getRoot();
  201. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  202. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  203. command.execute();
  204. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  205. [ '', '11', '12', '13' ],
  206. [ '', '21', '22', '23' ]
  207. ] ) );
  208. assertSelectedCells( model, [
  209. [ 0, 1, 1, 0 ],
  210. [ 0, 1, 1, 0 ]
  211. ] );
  212. } );
  213. it( 'should update table heading columns attribute when inserting column in headings section', () => {
  214. setData( model, modelTable( [
  215. [ '11', '12[]' ],
  216. [ '21', '22' ],
  217. [ '31', '32' ]
  218. ], { headingColumns: 2 } ) );
  219. command.execute();
  220. assertEqualMarkup( getData( model ), modelTable( [
  221. [ '11', '', '12[]' ],
  222. [ '21', '', '22' ],
  223. [ '31', '', '32' ]
  224. ], { headingColumns: 3 } ) );
  225. } );
  226. it( 'should not update table heading columns attribute when inserting column after headings section', () => {
  227. setData( model, modelTable( [
  228. [ '11', '12', '13[]' ],
  229. [ '21', '22', '23' ],
  230. [ '31', '32', '33' ]
  231. ], { headingColumns: 2 } ) );
  232. command.execute();
  233. assertEqualMarkup( getData( model ), modelTable( [
  234. [ '11', '12', '', '13[]' ],
  235. [ '21', '22', '', '23' ],
  236. [ '31', '32', '', '33' ]
  237. ], { headingColumns: 2 } ) );
  238. } );
  239. it( 'should skip spanned columns', () => {
  240. setData( model, modelTable( [
  241. [ '11', '12[]' ],
  242. [ { colspan: 2, contents: '21' } ],
  243. [ '31', '32' ]
  244. ], { headingColumns: 2 } ) );
  245. command.execute();
  246. assertEqualMarkup( getData( model ), modelTable( [
  247. [ '11', '', '12[]' ],
  248. [ { colspan: 3, contents: '21' } ],
  249. [ '31', '', '32' ]
  250. ], { headingColumns: 3 } ) );
  251. } );
  252. it( 'should skip wide spanned columns', () => {
  253. setData( model, modelTable( [
  254. [ '11', '12', '13[]', '14', '15' ],
  255. [ '21', '22', { colspan: 2, contents: '23' }, '25' ],
  256. [ { colspan: 4, contents: '31' }, { colspan: 2, contents: '34' } ]
  257. ], { headingColumns: 4 } ) );
  258. command.execute();
  259. assertEqualMarkup( getData( model ), modelTable( [
  260. [ '11', '12', '', '13[]', '14', '15' ],
  261. [ '21', '22', '', { colspan: 2, contents: '23' }, '25' ],
  262. [ { colspan: 5, contents: '31' }, { colspan: 2, contents: '34' } ]
  263. ], { headingColumns: 5 } ) );
  264. } );
  265. } );
  266. } );
  267. } );