insertcolumncommand.js 8.8 KB

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