insertrowcommand.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
  8. import InsertRowCommand from '../../src/commands/insertrowcommand';
  9. import {
  10. downcastInsertCell,
  11. downcastInsertRow,
  12. downcastInsertTable,
  13. downcastRemoveRow,
  14. downcastTableHeadingColumnsChange,
  15. downcastTableHeadingRowsChange
  16. } from '../../src/converters/downcast';
  17. import upcastTable from '../../src/converters/upcasttable';
  18. import { formatTable, formattedModelTable, modelTable } from '../_utils/utils';
  19. import TableUtils from '../../src/tableutils';
  20. describe( 'InsertRowCommand', () => {
  21. let editor, model, command;
  22. beforeEach( () => {
  23. return ModelTestEditor.create( {
  24. plugins: [ TableUtils ]
  25. } ).then( newEditor => {
  26. editor = newEditor;
  27. model = editor.model;
  28. const conversion = editor.conversion;
  29. const schema = model.schema;
  30. schema.register( 'table', {
  31. allowWhere: '$block',
  32. allowAttributes: [ 'headingRows' ],
  33. isObject: true
  34. } );
  35. schema.register( 'tableRow', { allowIn: 'table' } );
  36. schema.register( 'tableCell', {
  37. allowIn: 'tableRow',
  38. allowContentOf: '$block',
  39. allowAttributes: [ 'colspan', 'rowspan' ],
  40. isLimit: true
  41. } );
  42. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  43. // Table conversion.
  44. conversion.for( 'upcast' ).add( upcastTable() );
  45. conversion.for( 'downcast' ).add( downcastInsertTable() );
  46. // Insert row conversion.
  47. conversion.for( 'downcast' ).add( downcastInsertRow() );
  48. // Remove row conversion.
  49. conversion.for( 'downcast' ).add( downcastRemoveRow() );
  50. // Table cell conversion.
  51. conversion.for( 'downcast' ).add( downcastInsertCell() );
  52. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'td' } ) );
  53. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'th' } ) );
  54. // Table attributes conversion.
  55. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  56. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  57. conversion.for( 'downcast' ).add( downcastTableHeadingColumnsChange() );
  58. conversion.for( 'downcast' ).add( downcastTableHeadingRowsChange() );
  59. } );
  60. } );
  61. afterEach( () => {
  62. return editor.destroy();
  63. } );
  64. describe( 'order=below', () => {
  65. beforeEach( () => {
  66. command = new InsertRowCommand( editor );
  67. } );
  68. describe( 'isEnabled', () => {
  69. it( 'should be false if wrong node', () => {
  70. setData( model, '<p>foo[]</p>' );
  71. expect( command.isEnabled ).to.be.false;
  72. } );
  73. it( 'should be true if in table', () => {
  74. setData( model, modelTable( [ [ '[]' ] ] ) );
  75. expect( command.isEnabled ).to.be.true;
  76. } );
  77. } );
  78. describe( 'execute()', () => {
  79. it( 'should insert row after current position', () => {
  80. setData( model, modelTable( [
  81. [ '00[]', '01' ],
  82. [ '10', '11' ]
  83. ] ) );
  84. command.execute();
  85. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  86. [ '00[]', '01' ],
  87. [ '', '' ],
  88. [ '10', '11' ]
  89. ] ) );
  90. } );
  91. it( 'should update table heading rows attribute when inserting row in headings section', () => {
  92. setData( model, modelTable( [
  93. [ '00[]', '01' ],
  94. [ '10', '11' ],
  95. [ '20', '21' ]
  96. ], { headingRows: 2 } ) );
  97. command.execute();
  98. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  99. [ '00[]', '01' ],
  100. [ '', '' ],
  101. [ '10', '11' ],
  102. [ '20', '21' ]
  103. ], { headingRows: 3 } ) );
  104. } );
  105. it( 'should not update table heading rows attribute when inserting row after headings section', () => {
  106. setData( model, modelTable( [
  107. [ '00', '01' ],
  108. [ '10[]', '11' ],
  109. [ '20', '21' ]
  110. ], { headingRows: 2 } ) );
  111. command.execute();
  112. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  113. [ '00', '01' ],
  114. [ '10[]', '11' ],
  115. [ '', '' ],
  116. [ '20', '21' ]
  117. ], { headingRows: 2 } ) );
  118. } );
  119. it( 'should expand rowspan of a cell that overlaps inserted rows', () => {
  120. setData( model, modelTable( [
  121. [ { colspan: 2, contents: '00' }, '02', '03' ],
  122. [ { colspan: 2, rowspan: 4, contents: '10[]' }, '12', '13' ],
  123. [ '22', '23' ]
  124. ], { headingColumns: 3, headingRows: 1 } ) );
  125. command.execute();
  126. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  127. [ { colspan: 2, contents: '00' }, '02', '03' ],
  128. [ { colspan: 2, rowspan: 5, contents: '10[]' }, '12', '13' ],
  129. [ '', '' ],
  130. [ '22', '23' ]
  131. ], { headingColumns: 3, headingRows: 1 } ) );
  132. } );
  133. it( 'should not expand rowspan of a cell that does not overlaps inserted rows', () => {
  134. setData( model, modelTable( [
  135. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  136. [ '11[]', '12' ],
  137. [ '20', '21', '22' ]
  138. ], { headingColumns: 3, headingRows: 1 } ) );
  139. command.execute();
  140. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  141. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  142. [ '11[]', '12' ],
  143. [ '', '', '' ],
  144. [ '20', '21', '22' ]
  145. ], { headingColumns: 3, headingRows: 1 } ) );
  146. } );
  147. it( 'should properly calculate columns if next row has colspans', () => {
  148. setData( model, modelTable( [
  149. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  150. [ '11[]', '12' ],
  151. [ { colspan: 3, contents: '20' } ]
  152. ], { headingColumns: 3, headingRows: 1 } ) );
  153. command.execute();
  154. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  155. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  156. [ '11[]', '12' ],
  157. [ '', '', '' ],
  158. [ { colspan: 3, contents: '20' } ]
  159. ], { headingColumns: 3, headingRows: 1 } ) );
  160. } );
  161. it( 'should insert rows at the end of a table', () => {
  162. setData( model, modelTable( [
  163. [ '00', '01' ],
  164. [ '10[]', '11' ]
  165. ] ) );
  166. command.execute();
  167. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  168. [ '00', '01' ],
  169. [ '10[]', '11' ],
  170. [ '', '' ]
  171. ] ) );
  172. } );
  173. } );
  174. } );
  175. describe( 'order=above', () => {
  176. beforeEach( () => {
  177. command = new InsertRowCommand( editor, { order: 'above' } );
  178. } );
  179. describe( 'isEnabled', () => {
  180. it( 'should be false if wrong node', () => {
  181. setData( model, '<p>foo[]</p>' );
  182. expect( command.isEnabled ).to.be.false;
  183. } );
  184. it( 'should be true if in table', () => {
  185. setData( model, modelTable( [ [ '[]' ] ] ) );
  186. expect( command.isEnabled ).to.be.true;
  187. } );
  188. } );
  189. describe( 'execute()', () => {
  190. it( 'should insert row at the beginning of a table', () => {
  191. setData( model, modelTable( [
  192. [ '00[]', '01' ],
  193. [ '10', '11' ]
  194. ] ) );
  195. command.execute();
  196. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  197. [ '', '' ],
  198. [ '00[]', '01' ],
  199. [ '10', '11' ]
  200. ] ) );
  201. } );
  202. it( 'should insert row at the end of a table', () => {
  203. setData( model, modelTable( [
  204. [ '00', '01' ],
  205. [ '10', '11' ],
  206. [ '20[]', '21' ]
  207. ] ) );
  208. command.execute();
  209. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  210. [ '00', '01' ],
  211. [ '10', '11' ],
  212. [ '', '' ],
  213. [ '20[]', '21' ]
  214. ] ) );
  215. } );
  216. it( 'should update table heading rows attribute when inserting row in headings section', () => {
  217. setData( model, modelTable( [
  218. [ '00[]', '01' ],
  219. [ '10', '11' ],
  220. [ '20', '21' ]
  221. ], { headingRows: 2 } ) );
  222. command.execute();
  223. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  224. [ '', '' ],
  225. [ '00[]', '01' ],
  226. [ '10', '11' ],
  227. [ '20', '21' ]
  228. ], { headingRows: 3 } ) );
  229. } );
  230. it( 'should not update table heading rows attribute when inserting row after headings section', () => {
  231. setData( model, modelTable( [
  232. [ '00', '01' ],
  233. [ '10', '11' ],
  234. [ '20[]', '21' ]
  235. ], { headingRows: 2 } ) );
  236. command.execute();
  237. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  238. [ '00', '01' ],
  239. [ '10', '11' ],
  240. [ '', '' ],
  241. [ '20[]', '21' ]
  242. ], { headingRows: 2 } ) );
  243. } );
  244. } );
  245. } );
  246. } );