insertrowcommand.js 8.4 KB

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