insertcolumncommand.js 8.8 KB

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