splitcellcommand.js 5.7 KB

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