insertrowcommand.js 8.1 KB

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