insertrowcommand.js 8.4 KB

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