insertrowcommand.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /**
  2. * @license Copyright (c) 2003-2019, 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 InsertRowCommand from '../../src/commands/insertrowcommand';
  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( 'InsertRowCommand', () => {
  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=below', () => {
  29. beforeEach( () => {
  30. command = new InsertRowCommand( 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 row after current position', () => {
  44. setData( model, modelTable( [
  45. [ '00[]', '01' ],
  46. [ '10', '11' ]
  47. ] ) );
  48. command.execute();
  49. assertEqualMarkup( getData( model ), modelTable( [
  50. [ '00[]', '01' ],
  51. [ '', '' ],
  52. [ '10', '11' ]
  53. ] ) );
  54. } );
  55. it( 'should insert row after current position (selection in block content)', () => {
  56. setData( model, modelTable( [
  57. [ '00' ],
  58. [ '<paragraph>[]10</paragraph>' ],
  59. [ '20' ]
  60. ] ) );
  61. command.execute();
  62. assertEqualMarkup( getData( model ), modelTable( [
  63. [ '00' ],
  64. [ '<paragraph>[]10</paragraph>' ],
  65. [ '' ],
  66. [ '20' ]
  67. ] ) );
  68. } );
  69. it( 'should update table heading rows attribute when inserting row in headings section', () => {
  70. setData( model, modelTable( [
  71. [ '00[]', '01' ],
  72. [ '10', '11' ],
  73. [ '20', '21' ]
  74. ], { headingRows: 2 } ) );
  75. command.execute();
  76. assertEqualMarkup( getData( model ), modelTable( [
  77. [ '00[]', '01' ],
  78. [ '', '' ],
  79. [ '10', '11' ],
  80. [ '20', '21' ]
  81. ], { headingRows: 3 } ) );
  82. } );
  83. it( 'should not update table heading rows attribute when inserting row after headings section', () => {
  84. setData( model, modelTable( [
  85. [ '00', '01' ],
  86. [ '10[]', '11' ],
  87. [ '20', '21' ]
  88. ], { headingRows: 2 } ) );
  89. command.execute();
  90. assertEqualMarkup( getData( model ), modelTable( [
  91. [ '00', '01' ],
  92. [ '10[]', '11' ],
  93. [ '', '' ],
  94. [ '20', '21' ]
  95. ], { headingRows: 2 } ) );
  96. } );
  97. it( 'should expand rowspan of a cell that overlaps inserted rows', () => {
  98. setData( model, modelTable( [
  99. [ { colspan: 2, contents: '00' }, '02', '03' ],
  100. [ { colspan: 2, rowspan: 4, contents: '10[]' }, '12', '13' ],
  101. [ '22', '23' ]
  102. ], { headingColumns: 3, headingRows: 1 } ) );
  103. command.execute();
  104. assertEqualMarkup( getData( model ), modelTable( [
  105. [ { colspan: 2, contents: '00' }, '02', '03' ],
  106. [ { colspan: 2, rowspan: 5, contents: '10[]' }, '12', '13' ],
  107. [ '', '' ],
  108. [ '22', '23' ]
  109. ], { headingColumns: 3, headingRows: 1 } ) );
  110. } );
  111. it( 'should not expand rowspan of a cell that does not overlaps inserted rows', () => {
  112. setData( model, modelTable( [
  113. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  114. [ '11[]', '12' ],
  115. [ '20', '21', '22' ]
  116. ], { headingColumns: 3, headingRows: 1 } ) );
  117. command.execute();
  118. assertEqualMarkup( getData( model ), modelTable( [
  119. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  120. [ '11[]', '12' ],
  121. [ '', '', '' ],
  122. [ '20', '21', '22' ]
  123. ], { headingColumns: 3, headingRows: 1 } ) );
  124. } );
  125. it( 'should properly calculate columns if next row has colspans', () => {
  126. setData( model, modelTable( [
  127. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  128. [ '11[]', '12' ],
  129. [ { colspan: 3, contents: '20' } ]
  130. ], { headingColumns: 3, headingRows: 1 } ) );
  131. command.execute();
  132. assertEqualMarkup( getData( model ), modelTable( [
  133. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  134. [ '11[]', '12' ],
  135. [ '', '', '' ],
  136. [ { colspan: 3, contents: '20' } ]
  137. ], { headingColumns: 3, headingRows: 1 } ) );
  138. } );
  139. it( 'should insert rows at the end of a table', () => {
  140. setData( model, modelTable( [
  141. [ '00', '01' ],
  142. [ '10[]', '11' ]
  143. ] ) );
  144. command.execute();
  145. assertEqualMarkup( getData( model ), modelTable( [
  146. [ '00', '01' ],
  147. [ '10[]', '11' ],
  148. [ '', '' ]
  149. ] ) );
  150. } );
  151. } );
  152. } );
  153. describe( 'order=above', () => {
  154. beforeEach( () => {
  155. command = new InsertRowCommand( editor, { order: 'above' } );
  156. } );
  157. describe( 'isEnabled', () => {
  158. it( 'should be false if wrong node', () => {
  159. setData( model, '<paragraph>foo[]</paragraph>' );
  160. expect( command.isEnabled ).to.be.false;
  161. } );
  162. it( 'should be true if in table', () => {
  163. setData( model, modelTable( [ [ '[]' ] ] ) );
  164. expect( command.isEnabled ).to.be.true;
  165. } );
  166. } );
  167. describe( 'execute()', () => {
  168. it( 'should insert row before current position (selection in block content)', () => {
  169. setData( model, modelTable( [
  170. [ '00' ],
  171. [ '<paragraph>[]10</paragraph>' ],
  172. [ '20' ]
  173. ] ) );
  174. command.execute();
  175. assertEqualMarkup( getData( model ), modelTable( [
  176. [ '00' ],
  177. [ '' ],
  178. [ '<paragraph>[]10</paragraph>' ],
  179. [ '20' ]
  180. ] ) );
  181. } );
  182. it( 'should insert row at the beginning of a table', () => {
  183. setData( model, modelTable( [
  184. [ '00[]', '01' ],
  185. [ '10', '11' ]
  186. ] ) );
  187. command.execute();
  188. assertEqualMarkup( getData( model ), modelTable( [
  189. [ '', '' ],
  190. [ '00[]', '01' ],
  191. [ '10', '11' ]
  192. ] ) );
  193. } );
  194. it( 'should insert row at the end of a table', () => {
  195. setData( model, modelTable( [
  196. [ '00', '01' ],
  197. [ '10', '11' ],
  198. [ '20[]', '21' ]
  199. ] ) );
  200. command.execute();
  201. assertEqualMarkup( getData( model ), modelTable( [
  202. [ '00', '01' ],
  203. [ '10', '11' ],
  204. [ '', '' ],
  205. [ '20[]', '21' ]
  206. ] ) );
  207. } );
  208. it( 'should update table heading rows attribute when inserting row in headings section', () => {
  209. setData( model, modelTable( [
  210. [ '00[]', '01' ],
  211. [ '10', '11' ],
  212. [ '20', '21' ]
  213. ], { headingRows: 2 } ) );
  214. command.execute();
  215. assertEqualMarkup( getData( model ), modelTable( [
  216. [ '', '' ],
  217. [ '00[]', '01' ],
  218. [ '10', '11' ],
  219. [ '20', '21' ]
  220. ], { headingRows: 3 } ) );
  221. } );
  222. it( 'should not update table heading rows attribute when inserting row after headings section', () => {
  223. setData( model, modelTable( [
  224. [ '00', '01' ],
  225. [ '10', '11' ],
  226. [ '20[]', '21' ]
  227. ], { headingRows: 2 } ) );
  228. command.execute();
  229. assertEqualMarkup( getData( model ), modelTable( [
  230. [ '00', '01' ],
  231. [ '10', '11' ],
  232. [ '', '' ],
  233. [ '20[]', '21' ]
  234. ], { headingRows: 2 } ) );
  235. } );
  236. } );
  237. } );
  238. } );