8
0

insertcolumncommand.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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/insertcolumncommand';
  9. import { downcastInsertTable } from '../src/converters/downcast';
  10. import upcastTable from '../src/converters/upcasttable';
  11. import { formatModelTable, formattedModelTable, modelTable } from './_utils/utils';
  12. describe( 'InsertColumnCommand', () => {
  13. let editor, model, command;
  14. beforeEach( () => {
  15. return ModelTestEditor.create()
  16. .then( newEditor => {
  17. editor = newEditor;
  18. model = editor.model;
  19. command = new InsertColumnCommand( editor );
  20. const conversion = editor.conversion;
  21. const schema = model.schema;
  22. schema.register( 'table', {
  23. allowWhere: '$block',
  24. allowAttributes: [ 'headingRows' ],
  25. isBlock: true,
  26. isObject: true
  27. } );
  28. schema.register( 'tableRow', {
  29. allowIn: 'table',
  30. allowAttributes: [],
  31. isBlock: true,
  32. isLimit: true
  33. } );
  34. schema.register( 'tableCell', {
  35. allowIn: 'tableRow',
  36. allowContentOf: '$block',
  37. allowAttributes: [ 'colspan', 'rowspan' ],
  38. isBlock: true,
  39. isLimit: true
  40. } );
  41. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  42. // Table conversion.
  43. conversion.for( 'upcast' ).add( upcastTable() );
  44. conversion.for( 'downcast' ).add( downcastInsertTable() );
  45. // Table row upcast only since downcast conversion is done in `downcastTable()`.
  46. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableRow', view: 'tr' } ) );
  47. // Table cell conversion.
  48. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'td' } ) );
  49. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'th' } ) );
  50. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  51. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  52. } );
  53. } );
  54. afterEach( () => {
  55. return editor.destroy();
  56. } );
  57. describe( 'isEnabled', () => {
  58. describe( 'when selection is collapsed', () => {
  59. it( 'should be false if wrong node', () => {
  60. setData( model, '<p>foo[]</p>' );
  61. expect( command.isEnabled ).to.be.false;
  62. } );
  63. it( 'should be true if in table', () => {
  64. setData( model, modelTable( [ [ '[]' ] ] ) );
  65. expect( command.isEnabled ).to.be.true;
  66. } );
  67. } );
  68. } );
  69. describe( 'execute()', () => {
  70. it( 'should insert column in given table at given index', () => {
  71. setData( model, modelTable( [
  72. [ '11[]', '12' ],
  73. [ '21', '22' ]
  74. ] ) );
  75. command.execute( { at: 1 } );
  76. expect( formatModelTable( getData( model ) ) ).to.equal( formattedModelTable( [
  77. [ '11[]', '', '12' ],
  78. [ '21', '', '22' ]
  79. ] ) );
  80. } );
  81. it( 'should insert column in given table at default index', () => {
  82. setData( model, modelTable( [
  83. [ '11[]', '12' ],
  84. [ '21', '22' ]
  85. ] ) );
  86. command.execute();
  87. expect( formatModelTable( 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( { at: 2, columns: 2 } );
  98. expect( formatModelTable( 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( { at: 1 } );
  110. expect( formatModelTable( 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( { at: 2 } );
  123. expect( formatModelTable( 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( { at: 1 } );
  136. expect( formatModelTable( 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( { at: 2, columns: 2 } );
  149. expect( formatModelTable( getData( model ) ) ).to.equal( formattedModelTable( [
  150. [ '11[]', '12', '', '', '13', '14', '15' ],
  151. [ '21', '22', '', '', { colspan: 2, contents: '23' }, '25' ],
  152. [ { colspan: 6, contents: '31' }, { colspan: 2, contents: '34' } ]
  153. ], { headingColumns: 6 } ) );
  154. } );
  155. it( 'should skip row spanned cells', () => {
  156. setData( model, modelTable( [
  157. [ { colspan: 2, rowspan: 2, contents: '11[]' }, '13' ],
  158. [ '23' ]
  159. ], { headingColumns: 2 } ) );
  160. command.execute( { at: 1, columns: 2 } );
  161. expect( formatModelTable( getData( model ) ) ).to.equal( formattedModelTable( [
  162. [ { colspan: 4, rowspan: 2, contents: '11[]' }, '13' ],
  163. [ '23' ]
  164. ], { headingColumns: 4 } ) );
  165. } );
  166. } );
  167. } );