insertcolumncommand.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 InsertColumnCommand from '../../src/commands/insertcolumncommand';
  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( 'InsertColumnCommand', () => {
  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=after', () => {
  65. beforeEach( () => {
  66. command = new InsertColumnCommand( 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 column in given table at given index', () => {
  80. setData( model, modelTable( [
  81. [ '11[]', '12' ],
  82. [ '21', '22' ]
  83. ] ) );
  84. command.execute();
  85. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  86. [ '11[]', '', '12' ],
  87. [ '21', '', '22' ]
  88. ] ) );
  89. } );
  90. it( 'should insert columns at table end', () => {
  91. setData( model, modelTable( [
  92. [ '11', '12' ],
  93. [ '21', '22[]' ]
  94. ] ) );
  95. command.execute();
  96. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  97. [ '11', '12', '' ],
  98. [ '21', '22[]', '' ]
  99. ] ) );
  100. } );
  101. it( 'should update table heading columns attribute when inserting column in headings section', () => {
  102. setData( model, modelTable( [
  103. [ '11[]', '12' ],
  104. [ '21', '22' ],
  105. [ '31', '32' ]
  106. ], { headingColumns: 2 } ) );
  107. command.execute();
  108. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  109. [ '11[]', '', '12' ],
  110. [ '21', '', '22' ],
  111. [ '31', '', '32' ]
  112. ], { headingColumns: 3 } ) );
  113. } );
  114. it( 'should not update table heading columns attribute when inserting column after headings section', () => {
  115. setData( model, modelTable( [
  116. [ '11', '12[]', '13' ],
  117. [ '21', '22', '23' ],
  118. [ '31', '32', '33' ]
  119. ], { headingColumns: 2 } ) );
  120. command.execute();
  121. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  122. [ '11', '12[]', '', '13' ],
  123. [ '21', '22', '', '23' ],
  124. [ '31', '32', '', '33' ]
  125. ], { headingColumns: 2 } ) );
  126. } );
  127. it( 'should skip spanned columns', () => {
  128. setData( model, modelTable( [
  129. [ '11[]', '12' ],
  130. [ { colspan: 2, contents: '21' } ],
  131. [ '31', '32' ]
  132. ], { headingColumns: 2 } ) );
  133. command.execute();
  134. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  135. [ '11[]', '', '12' ],
  136. [ { colspan: 3, contents: '21' } ],
  137. [ '31', '', '32' ]
  138. ], { headingColumns: 3 } ) );
  139. } );
  140. it( 'should skip wide spanned columns', () => {
  141. setData( model, modelTable( [
  142. [ '11', '12[]', '13', '14', '15' ],
  143. [ '21', '22', { colspan: 2, contents: '23' }, '25' ],
  144. [ { colspan: 4, contents: '31' }, { colspan: 2, contents: '34' } ]
  145. ], { headingColumns: 4 } ) );
  146. command.execute();
  147. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  148. [ '11', '12[]', '', '13', '14', '15' ],
  149. [ '21', '22', '', { colspan: 2, contents: '23' }, '25' ],
  150. [ { colspan: 5, contents: '31' }, { colspan: 2, contents: '34' } ]
  151. ], { headingColumns: 5 } ) );
  152. } );
  153. } );
  154. } );
  155. describe( 'order=before', () => {
  156. beforeEach( () => {
  157. command = new InsertColumnCommand( editor, { order: 'before' } );
  158. } );
  159. describe( 'isEnabled', () => {
  160. it( 'should be false if wrong node', () => {
  161. setData( model, '<p>foo[]</p>' );
  162. expect( command.isEnabled ).to.be.false;
  163. } );
  164. it( 'should be true if in table', () => {
  165. setData( model, modelTable( [ [ '[]' ] ] ) );
  166. expect( command.isEnabled ).to.be.true;
  167. } );
  168. } );
  169. describe( 'execute()', () => {
  170. it( 'should insert column in given table at given index', () => {
  171. setData( model, modelTable( [
  172. [ '11', '12[]' ],
  173. [ '21', '22' ]
  174. ] ) );
  175. command.execute();
  176. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  177. [ '11', '', '12[]' ],
  178. [ '21', '', '22' ]
  179. ] ) );
  180. } );
  181. it( 'should insert columns at the table start', () => {
  182. setData( model, modelTable( [
  183. [ '11', '12' ],
  184. [ '[]21', '22' ]
  185. ] ) );
  186. command.execute();
  187. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  188. [ '', '11', '12' ],
  189. [ '', '[]21', '22' ]
  190. ] ) );
  191. } );
  192. it( 'should update table heading columns attribute when inserting column in headings section', () => {
  193. setData( model, modelTable( [
  194. [ '11', '12[]' ],
  195. [ '21', '22' ],
  196. [ '31', '32' ]
  197. ], { headingColumns: 2 } ) );
  198. command.execute();
  199. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  200. [ '11', '', '12[]' ],
  201. [ '21', '', '22' ],
  202. [ '31', '', '32' ]
  203. ], { headingColumns: 3 } ) );
  204. } );
  205. it( 'should not update table heading columns attribute when inserting column after headings section', () => {
  206. setData( model, modelTable( [
  207. [ '11', '12', '13[]' ],
  208. [ '21', '22', '23' ],
  209. [ '31', '32', '33' ]
  210. ], { headingColumns: 2 } ) );
  211. command.execute();
  212. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  213. [ '11', '12', '', '13[]' ],
  214. [ '21', '22', '', '23' ],
  215. [ '31', '32', '', '33' ]
  216. ], { headingColumns: 2 } ) );
  217. } );
  218. it( 'should skip spanned columns', () => {
  219. setData( model, modelTable( [
  220. [ '11', '12[]' ],
  221. [ { colspan: 2, contents: '21' } ],
  222. [ '31', '32' ]
  223. ], { headingColumns: 2 } ) );
  224. command.execute();
  225. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  226. [ '11', '', '12[]' ],
  227. [ { colspan: 3, contents: '21' } ],
  228. [ '31', '', '32' ]
  229. ], { headingColumns: 3 } ) );
  230. } );
  231. it( 'should skip wide spanned columns', () => {
  232. setData( model, modelTable( [
  233. [ '11', '12', '13[]', '14', '15' ],
  234. [ '21', '22', { colspan: 2, contents: '23' }, '25' ],
  235. [ { colspan: 4, contents: '31' }, { colspan: 2, contents: '34' } ]
  236. ], { headingColumns: 4 } ) );
  237. command.execute();
  238. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  239. [ '11', '12', '', '13[]', '14', '15' ],
  240. [ '21', '22', '', { colspan: 2, contents: '23' }, '25' ],
  241. [ { colspan: 5, contents: '31' }, { colspan: 2, contents: '34' } ]
  242. ], { headingColumns: 5 } ) );
  243. } );
  244. } );
  245. } );
  246. } );