insertcolumncommand.js 8.5 KB

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