insertrowcommand.js 7.6 KB

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