insertcolumncommand.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 { defaultConversion, defaultSchema, modelTable } from '../_utils/utils';
  9. import TableUtils from '../../src/tableutils';
  10. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  11. describe( 'InsertColumnCommand', () => {
  12. let editor, model, command;
  13. beforeEach( () => {
  14. return ModelTestEditor
  15. .create( {
  16. plugins: [ TableUtils ]
  17. } )
  18. .then( newEditor => {
  19. editor = newEditor;
  20. model = editor.model;
  21. defaultSchema( model.schema );
  22. defaultConversion( editor.conversion );
  23. } );
  24. } );
  25. afterEach( () => {
  26. return editor.destroy();
  27. } );
  28. describe( 'order=right', () => {
  29. beforeEach( () => {
  30. command = new InsertColumnCommand( editor );
  31. } );
  32. describe( 'isEnabled', () => {
  33. it( 'should be false if wrong node', () => {
  34. setData( model, '<paragraph>foo[]</paragraph>' );
  35. expect( command.isEnabled ).to.be.false;
  36. } );
  37. it( 'should be true if in table', () => {
  38. setData( model, modelTable( [ [ '[]' ] ] ) );
  39. expect( command.isEnabled ).to.be.true;
  40. } );
  41. } );
  42. describe( 'execute()', () => {
  43. it( 'should insert column in given table to the right of the selection\'s column', () => {
  44. setData( model, modelTable( [
  45. [ '11[]', '12' ],
  46. [ '21', '22' ]
  47. ] ) );
  48. command.execute();
  49. assertEqualMarkup( getData( model ), modelTable( [
  50. [ '11[]', '', '12' ],
  51. [ '21', '', '22' ]
  52. ] ) );
  53. } );
  54. it( 'should insert column in given table to the right of the selection\'s column (selection in block content)', () => {
  55. setData( model, modelTable( [
  56. [ '11', '<paragraph>12[]</paragraph>', '13' ]
  57. ] ) );
  58. command.execute();
  59. assertEqualMarkup( getData( model ), modelTable( [
  60. [ '11', '<paragraph>12[]</paragraph>', '', '13' ]
  61. ] ) );
  62. } );
  63. it( 'should insert columns at table end', () => {
  64. setData( model, modelTable( [
  65. [ '11', '12' ],
  66. [ '21', '22[]' ]
  67. ] ) );
  68. command.execute();
  69. assertEqualMarkup( getData( model ), modelTable( [
  70. [ '11', '12', '' ],
  71. [ '21', '22[]', '' ]
  72. ] ) );
  73. } );
  74. it( 'should update table heading columns attribute when inserting column in headings section', () => {
  75. setData( model, modelTable( [
  76. [ '11[]', '12' ],
  77. [ '21', '22' ],
  78. [ '31', '32' ]
  79. ], { headingColumns: 2 } ) );
  80. command.execute();
  81. assertEqualMarkup( getData( model ), modelTable( [
  82. [ '11[]', '', '12' ],
  83. [ '21', '', '22' ],
  84. [ '31', '', '32' ]
  85. ], { headingColumns: 3 } ) );
  86. } );
  87. it( 'should not update table heading columns attribute when inserting column after headings section', () => {
  88. setData( model, modelTable( [
  89. [ '11', '12[]', '13' ],
  90. [ '21', '22', '23' ],
  91. [ '31', '32', '33' ]
  92. ], { headingColumns: 2 } ) );
  93. command.execute();
  94. assertEqualMarkup( getData( model ), modelTable( [
  95. [ '11', '12[]', '', '13' ],
  96. [ '21', '22', '', '23' ],
  97. [ '31', '32', '', '33' ]
  98. ], { headingColumns: 2 } ) );
  99. } );
  100. it( 'should skip spanned columns', () => {
  101. setData( model, modelTable( [
  102. [ '11[]', '12' ],
  103. [ { colspan: 2, contents: '21' } ],
  104. [ '31', '32' ]
  105. ], { headingColumns: 2 } ) );
  106. command.execute();
  107. assertEqualMarkup( getData( model ), modelTable( [
  108. [ '11[]', '', '12' ],
  109. [ { colspan: 3, contents: '21' } ],
  110. [ '31', '', '32' ]
  111. ], { headingColumns: 3 } ) );
  112. } );
  113. it( 'should skip wide spanned columns', () => {
  114. setData( model, modelTable( [
  115. [ '11', '12[]', '13', '14', '15' ],
  116. [ '21', '22', { colspan: 2, contents: '23' }, '25' ],
  117. [ { colspan: 4, contents: '31' }, { colspan: 2, contents: '34' } ]
  118. ], { headingColumns: 4 } ) );
  119. command.execute();
  120. assertEqualMarkup( getData( model ), modelTable( [
  121. [ '11', '12[]', '', '13', '14', '15' ],
  122. [ '21', '22', '', { colspan: 2, contents: '23' }, '25' ],
  123. [ { colspan: 5, contents: '31' }, { colspan: 2, contents: '34' } ]
  124. ], { headingColumns: 5 } ) );
  125. } );
  126. } );
  127. } );
  128. describe( 'order=left', () => {
  129. beforeEach( () => {
  130. command = new InsertColumnCommand( editor, { order: 'left' } );
  131. } );
  132. describe( 'isEnabled', () => {
  133. it( 'should be false if wrong node', () => {
  134. setData( model, '<paragraph>foo[]</paragraph>' );
  135. expect( command.isEnabled ).to.be.false;
  136. } );
  137. it( 'should be true if in table', () => {
  138. setData( model, modelTable( [ [ '[]' ] ] ) );
  139. expect( command.isEnabled ).to.be.true;
  140. } );
  141. } );
  142. describe( 'execute()', () => {
  143. it( 'should insert column in given table to the left of the selection\'s column', () => {
  144. setData( model, modelTable( [
  145. [ '11', '12[]' ],
  146. [ '21', '22' ]
  147. ] ) );
  148. command.execute();
  149. assertEqualMarkup( getData( model ), modelTable( [
  150. [ '11', '', '12[]' ],
  151. [ '21', '', '22' ]
  152. ] ) );
  153. } );
  154. it( 'should insert column in given table to the left of the selection\'s column (selection in block content)', () => {
  155. setData( model, modelTable( [
  156. [ '11', '<paragraph>12[]</paragraph>', '13' ]
  157. ] ) );
  158. command.execute();
  159. assertEqualMarkup( getData( model ), modelTable( [
  160. [ '11', '', '<paragraph>12[]</paragraph>', '13' ]
  161. ] ) );
  162. } );
  163. it( 'should insert columns at the table start', () => {
  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 update table heading columns attribute when inserting column in headings section', () => {
  175. setData( model, modelTable( [
  176. [ '11', '12[]' ],
  177. [ '21', '22' ],
  178. [ '31', '32' ]
  179. ], { headingColumns: 2 } ) );
  180. command.execute();
  181. assertEqualMarkup( getData( model ), modelTable( [
  182. [ '11', '', '12[]' ],
  183. [ '21', '', '22' ],
  184. [ '31', '', '32' ]
  185. ], { headingColumns: 3 } ) );
  186. } );
  187. it( 'should not update table heading columns attribute when inserting column after headings section', () => {
  188. setData( model, modelTable( [
  189. [ '11', '12', '13[]' ],
  190. [ '21', '22', '23' ],
  191. [ '31', '32', '33' ]
  192. ], { headingColumns: 2 } ) );
  193. command.execute();
  194. assertEqualMarkup( getData( model ), modelTable( [
  195. [ '11', '12', '', '13[]' ],
  196. [ '21', '22', '', '23' ],
  197. [ '31', '32', '', '33' ]
  198. ], { headingColumns: 2 } ) );
  199. } );
  200. it( 'should skip spanned columns', () => {
  201. setData( model, modelTable( [
  202. [ '11', '12[]' ],
  203. [ { colspan: 2, contents: '21' } ],
  204. [ '31', '32' ]
  205. ], { headingColumns: 2 } ) );
  206. command.execute();
  207. assertEqualMarkup( getData( model ), modelTable( [
  208. [ '11', '', '12[]' ],
  209. [ { colspan: 3, contents: '21' } ],
  210. [ '31', '', '32' ]
  211. ], { headingColumns: 3 } ) );
  212. } );
  213. it( 'should skip wide spanned columns', () => {
  214. setData( model, modelTable( [
  215. [ '11', '12', '13[]', '14', '15' ],
  216. [ '21', '22', { colspan: 2, contents: '23' }, '25' ],
  217. [ { colspan: 4, contents: '31' }, { colspan: 2, contents: '34' } ]
  218. ], { headingColumns: 4 } ) );
  219. command.execute();
  220. assertEqualMarkup( getData( model ), modelTable( [
  221. [ '11', '12', '', '13[]', '14', '15' ],
  222. [ '21', '22', '', { colspan: 2, contents: '23' }, '25' ],
  223. [ { colspan: 5, contents: '31' }, { colspan: 2, contents: '34' } ]
  224. ], { headingColumns: 5 } ) );
  225. } );
  226. } );
  227. } );
  228. } );