splitcellcommand.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 SplitCellCommand from '../../src/commands/splitcellcommand';
  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( 'SplitCellCommand', () => {
  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. command = new SplitCellCommand( editor );
  22. const conversion = editor.conversion;
  23. const schema = model.schema;
  24. schema.register( 'table', {
  25. allowWhere: '$block',
  26. allowAttributes: [ 'headingRows' ],
  27. isBlock: true,
  28. isObject: true
  29. } );
  30. schema.register( 'tableRow', {
  31. allowIn: 'table',
  32. allowAttributes: [],
  33. isBlock: true,
  34. isLimit: true
  35. } );
  36. schema.register( 'tableCell', {
  37. allowIn: 'tableRow',
  38. allowContentOf: '$block',
  39. allowAttributes: [ 'colspan', 'rowspan' ],
  40. isBlock: true,
  41. isLimit: true
  42. } );
  43. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  44. // Table conversion.
  45. conversion.for( 'upcast' ).add( upcastTable() );
  46. conversion.for( 'downcast' ).add( downcastInsertTable() );
  47. // Table row upcast only since downcast conversion is done in `downcastTable()`.
  48. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableRow', view: 'tr' } ) );
  49. // Table cell conversion.
  50. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'td' } ) );
  51. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'th' } ) );
  52. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  53. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  54. } );
  55. } );
  56. afterEach( () => {
  57. return editor.destroy();
  58. } );
  59. describe( 'direction=vertically', () => {
  60. beforeEach( () => {
  61. command = new SplitCellCommand( editor, { direction: 'vertically' } );
  62. } );
  63. describe( 'isEnabled', () => {
  64. it( 'should be true if in a table cell', () => {
  65. setData( model, modelTable( [
  66. [ '00[]' ]
  67. ] ) );
  68. expect( command.isEnabled ).to.be.true;
  69. } );
  70. it( 'should be false if not in cell', () => {
  71. setData( model, '<p>11[]</p>' );
  72. expect( command.isEnabled ).to.be.false;
  73. } );
  74. } );
  75. describe( 'execute()', () => {
  76. it( 'should split table cell for two table cells', () => {
  77. setData( model, modelTable( [
  78. [ '00', '01', '02' ],
  79. [ '10', '[]11', '12' ],
  80. [ '20', { colspan: 2, contents: '21' } ],
  81. [ { colspan: 2, contents: '30' }, '32' ]
  82. ] ) );
  83. command.execute();
  84. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  85. [ '00', { colspan: 2, contents: '01' }, '02' ],
  86. [ '10', '[]11', '', '12' ],
  87. [ '20', { colspan: 3, contents: '21' } ],
  88. [ { colspan: 3, contents: '30' }, '32' ]
  89. ] ) );
  90. } );
  91. it( 'should unsplit table cell if split is equal to colspan', () => {
  92. setData( model, modelTable( [
  93. [ '00', '01', '02' ],
  94. [ '10', '11', '12' ],
  95. [ '20', { colspan: 2, contents: '21[]' } ],
  96. [ { colspan: 2, contents: '30' }, '32' ]
  97. ] ) );
  98. command.execute();
  99. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  100. [ '00', '01', '02' ],
  101. [ '10', '11', '12' ],
  102. [ '20', '21[]', '' ],
  103. [ { colspan: 2, contents: '30' }, '32' ]
  104. ] ) );
  105. } );
  106. it( 'should properly unsplit table cell if split is uneven', () => {
  107. setData( model, modelTable( [
  108. [ '00', '01', '02' ],
  109. [ { colspan: 3, contents: '10[]' } ]
  110. ] ) );
  111. command.execute();
  112. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  113. [ '00', '01', '02' ],
  114. [ { colspan: 2, contents: '10[]' }, '' ]
  115. ] ) );
  116. } );
  117. it( 'should properly set colspan of inserted cells', () => {
  118. setData( model, modelTable( [
  119. [ '00', '01', '02', '03' ],
  120. [ { colspan: 4, contents: '10[]' } ]
  121. ] ) );
  122. command.execute();
  123. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  124. [ '00', '01', '02', '03' ],
  125. [ { colspan: 2, contents: '10[]' }, { colspan: 2, contents: '' } ]
  126. ] ) );
  127. } );
  128. it( 'should keep rowspan attribute for newly inserted cells', () => {
  129. setData( model, modelTable( [
  130. [ '00', '01', '02', '03', '04', '05' ],
  131. [ { colspan: 5, rowspan: 2, contents: '10[]' }, '15' ],
  132. [ '25' ]
  133. ] ) );
  134. command.execute();
  135. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  136. [ '00', '01', '02', '03', '04', '05' ],
  137. [ { colspan: 3, rowspan: 2, contents: '10[]' }, { colspan: 2, rowspan: 2, contents: '' }, '15' ],
  138. [ '25' ]
  139. ] ) );
  140. } );
  141. } );
  142. } );
  143. describe( 'direction=horizontally', () => {
  144. beforeEach( () => {
  145. command = new SplitCellCommand( editor, { direction: 'horizontally' } );
  146. } );
  147. describe( 'isEnabled', () => {
  148. it( 'should be true if in a table cell', () => {
  149. setData( model, modelTable( [
  150. [ '00[]' ]
  151. ] ) );
  152. expect( command.isEnabled ).to.be.true;
  153. } );
  154. it( 'should be false if not in cell', () => {
  155. setData( model, '<p>11[]</p>' );
  156. expect( command.isEnabled ).to.be.false;
  157. } );
  158. } );
  159. describe( 'execute()', () => {
  160. it( 'should split table cell for two table cells', () => {
  161. setData( model, modelTable( [
  162. [ '00', '01', '02' ],
  163. [ '10', '[]11', '12' ],
  164. [ '20', '21', '22' ]
  165. ] ) );
  166. command.execute();
  167. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  168. [ '00', '01', '02' ],
  169. [ { rowspan: 2, contents: '10' }, '[]11', { rowspan: 2, contents: '12' } ],
  170. [ '' ],
  171. [ '20', '21', '22' ]
  172. ] ) );
  173. } );
  174. } );
  175. } );
  176. } );