insertrowcommand.js 9.3 KB

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