splitcellcommand.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import SplitCellCommand from '../../src/commands/splitcellcommand';
  8. import { defaultConversion, defaultSchema, modelTable } from '../_utils/utils';
  9. import TableSelection from '../../src/tableselection';
  10. import TableUtils from '../../src/tableutils';
  11. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  12. describe( 'SplitCellCommand', () => {
  13. let editor, model, command;
  14. beforeEach( () => {
  15. return ModelTestEditor
  16. .create( {
  17. plugins: [ TableUtils, TableSelection ]
  18. } )
  19. .then( newEditor => {
  20. editor = newEditor;
  21. model = editor.model;
  22. command = new SplitCellCommand( editor );
  23. defaultSchema( model.schema );
  24. defaultConversion( editor.conversion );
  25. } );
  26. } );
  27. afterEach( () => {
  28. return editor.destroy();
  29. } );
  30. describe( 'direction=vertically', () => {
  31. beforeEach( () => {
  32. command = new SplitCellCommand( editor, { direction: 'vertically' } );
  33. } );
  34. describe( 'isEnabled', () => {
  35. it( 'should be true if in a table cell', () => {
  36. setData( model, modelTable( [
  37. [ '00[]' ]
  38. ] ) );
  39. expect( command.isEnabled ).to.be.true;
  40. } );
  41. it( 'should be true if in an entire cell is selected', () => {
  42. setData( model, modelTable( [
  43. [ '00', '01' ]
  44. ] ) );
  45. const tableSelection = editor.plugins.get( TableSelection );
  46. const modelRoot = model.document.getRoot();
  47. tableSelection._setCellSelection(
  48. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  49. modelRoot.getNodeByPath( [ 0, 0, 0 ] )
  50. );
  51. expect( command.isEnabled ).to.be.true;
  52. } );
  53. it( 'should be false if multiple cells are selected', () => {
  54. setData( model, modelTable( [
  55. [ '00', '01' ]
  56. ] ) );
  57. const tableSelection = editor.plugins.get( TableSelection );
  58. const modelRoot = model.document.getRoot();
  59. tableSelection._setCellSelection(
  60. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  61. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  62. );
  63. expect( command.isEnabled ).to.be.false;
  64. } );
  65. it( 'should be false if not in cell', () => {
  66. setData( model, '<paragraph>11[]</paragraph>' );
  67. expect( command.isEnabled ).to.be.false;
  68. } );
  69. } );
  70. describe( 'execute()', () => {
  71. it( 'should split table cell for two table cells', () => {
  72. setData( model, modelTable( [
  73. [ '00', '01', '02' ],
  74. [ '10', '[]11', '12' ],
  75. [ '20', { colspan: 2, contents: '21' } ],
  76. [ { colspan: 2, contents: '30' }, '32' ]
  77. ] ) );
  78. command.execute();
  79. assertEqualMarkup( getData( model ), modelTable( [
  80. [ '00', { colspan: 2, contents: '01' }, '02' ],
  81. [ '10', '[]11', '', '12' ],
  82. [ '20', { colspan: 3, contents: '21' } ],
  83. [ { colspan: 3, contents: '30' }, '32' ]
  84. ] ) );
  85. } );
  86. it( 'should unsplit table cell if split is equal to colspan', () => {
  87. setData( model, modelTable( [
  88. [ '00', '01', '02' ],
  89. [ '10', '11', '12' ],
  90. [ '20', { colspan: 2, contents: '21[]' } ],
  91. [ { colspan: 2, contents: '30' }, '32' ]
  92. ] ) );
  93. command.execute();
  94. assertEqualMarkup( getData( model ), modelTable( [
  95. [ '00', '01', '02' ],
  96. [ '10', '11', '12' ],
  97. [ '20', '21[]', '' ],
  98. [ { colspan: 2, contents: '30' }, '32' ]
  99. ] ) );
  100. } );
  101. it( 'should properly unsplit table cell if split is uneven', () => {
  102. setData( model, modelTable( [
  103. [ '00', '01', '02' ],
  104. [ { colspan: 3, contents: '10[]' } ]
  105. ] ) );
  106. command.execute();
  107. assertEqualMarkup( getData( model ), modelTable( [
  108. [ '00', '01', '02' ],
  109. [ { colspan: 2, contents: '10[]' }, '' ]
  110. ] ) );
  111. } );
  112. it( 'should properly set colspan of inserted cells', () => {
  113. setData( model, modelTable( [
  114. [ '00', '01', '02', '03' ],
  115. [ { colspan: 4, contents: '10[]' } ]
  116. ] ) );
  117. command.execute();
  118. assertEqualMarkup( getData( model ), modelTable( [
  119. [ '00', '01', '02', '03' ],
  120. [ { colspan: 2, contents: '10[]' }, { colspan: 2, contents: '' } ]
  121. ] ) );
  122. } );
  123. it( 'should keep rowspan attribute for newly inserted cells', () => {
  124. setData( model, modelTable( [
  125. [ '00', '01', '02', '03', '04', '05' ],
  126. [ { colspan: 5, rowspan: 2, contents: '10[]' }, '15' ],
  127. [ '25' ]
  128. ] ) );
  129. command.execute();
  130. assertEqualMarkup( getData( model ), modelTable( [
  131. [ '00', '01', '02', '03', '04', '05' ],
  132. [ { colspan: 3, rowspan: 2, contents: '10[]' }, { colspan: 2, rowspan: 2, contents: '' }, '15' ],
  133. [ '25' ]
  134. ] ) );
  135. } );
  136. } );
  137. } );
  138. describe( 'direction=horizontally', () => {
  139. beforeEach( () => {
  140. command = new SplitCellCommand( editor, { direction: 'horizontally' } );
  141. } );
  142. describe( 'isEnabled', () => {
  143. it( 'should be true if in a table cell', () => {
  144. setData( model, modelTable( [
  145. [ '00[]' ]
  146. ] ) );
  147. expect( command.isEnabled ).to.be.true;
  148. } );
  149. it( 'should be false if not in cell', () => {
  150. setData( model, '<paragraph>11[]</paragraph>' );
  151. expect( command.isEnabled ).to.be.false;
  152. } );
  153. } );
  154. describe( 'execute()', () => {
  155. it( 'should split table cell for two table cells', () => {
  156. setData( model, modelTable( [
  157. [ '00', '01', '02' ],
  158. [ '10', '[]11', '12' ],
  159. [ '20', '21', '22' ]
  160. ] ) );
  161. command.execute();
  162. assertEqualMarkup( getData( model ), modelTable( [
  163. [ '00', '01', '02' ],
  164. [ { rowspan: 2, contents: '10' }, '[]11', { rowspan: 2, contents: '12' } ],
  165. [ '' ],
  166. [ '20', '21', '22' ]
  167. ] ) );
  168. } );
  169. } );
  170. } );
  171. } );