8
0

insertcolumncommand.js 7.9 KB

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