splitcellcommand.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * @license Copyright (c) 2003-2019, 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, formatTable, formattedModelTable, modelTable } from '../_utils/utils';
  9. import TableUtils from '../../src/tableutils';
  10. describe( 'SplitCellCommand', () => {
  11. let editor, model, command;
  12. beforeEach( () => {
  13. return ModelTestEditor
  14. .create( {
  15. plugins: [ TableUtils ]
  16. } )
  17. .then( newEditor => {
  18. editor = newEditor;
  19. model = editor.model;
  20. command = new SplitCellCommand( editor );
  21. defaultSchema( model.schema );
  22. defaultConversion( editor.conversion );
  23. } );
  24. } );
  25. afterEach( () => {
  26. return editor.destroy();
  27. } );
  28. describe( 'direction=vertically', () => {
  29. beforeEach( () => {
  30. command = new SplitCellCommand( editor, { direction: 'vertically' } );
  31. } );
  32. describe( 'isEnabled', () => {
  33. it( 'should be true if in a table cell', () => {
  34. setData( model, modelTable( [
  35. [ '00[]' ]
  36. ] ) );
  37. expect( command.isEnabled ).to.be.true;
  38. } );
  39. it( 'should be false if not in cell', () => {
  40. setData( model, '<paragraph>11[]</paragraph>' );
  41. expect( command.isEnabled ).to.be.false;
  42. } );
  43. } );
  44. describe( 'execute()', () => {
  45. it( 'should split table cell for two table cells', () => {
  46. setData( model, modelTable( [
  47. [ '00', '01', '02' ],
  48. [ '10', '[]11', '12' ],
  49. [ '20', { colspan: 2, contents: '21' } ],
  50. [ { colspan: 2, contents: '30' }, '32' ]
  51. ] ) );
  52. command.execute();
  53. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  54. [ '00', { colspan: 2, contents: '01' }, '02' ],
  55. [ '10', '[]11', '', '12' ],
  56. [ '20', { colspan: 3, contents: '21' } ],
  57. [ { colspan: 3, contents: '30' }, '32' ]
  58. ] ) );
  59. } );
  60. it( 'should unsplit table cell if split is equal to colspan', () => {
  61. setData( model, modelTable( [
  62. [ '00', '01', '02' ],
  63. [ '10', '11', '12' ],
  64. [ '20', { colspan: 2, contents: '21[]' } ],
  65. [ { colspan: 2, contents: '30' }, '32' ]
  66. ] ) );
  67. command.execute();
  68. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  69. [ '00', '01', '02' ],
  70. [ '10', '11', '12' ],
  71. [ '20', '21[]', '' ],
  72. [ { colspan: 2, contents: '30' }, '32' ]
  73. ] ) );
  74. } );
  75. it( 'should properly unsplit table cell if split is uneven', () => {
  76. setData( model, modelTable( [
  77. [ '00', '01', '02' ],
  78. [ { colspan: 3, contents: '10[]' } ]
  79. ] ) );
  80. command.execute();
  81. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  82. [ '00', '01', '02' ],
  83. [ { colspan: 2, contents: '10[]' }, '' ]
  84. ] ) );
  85. } );
  86. it( 'should properly set colspan of inserted cells', () => {
  87. setData( model, modelTable( [
  88. [ '00', '01', '02', '03' ],
  89. [ { colspan: 4, contents: '10[]' } ]
  90. ] ) );
  91. command.execute();
  92. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  93. [ '00', '01', '02', '03' ],
  94. [ { colspan: 2, contents: '10[]' }, { colspan: 2, contents: '' } ]
  95. ] ) );
  96. } );
  97. it( 'should keep rowspan attribute for newly inserted cells', () => {
  98. setData( model, modelTable( [
  99. [ '00', '01', '02', '03', '04', '05' ],
  100. [ { colspan: 5, rowspan: 2, contents: '10[]' }, '15' ],
  101. [ '25' ]
  102. ] ) );
  103. command.execute();
  104. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  105. [ '00', '01', '02', '03', '04', '05' ],
  106. [ { colspan: 3, rowspan: 2, contents: '10[]' }, { colspan: 2, rowspan: 2, contents: '' }, '15' ],
  107. [ '25' ]
  108. ] ) );
  109. } );
  110. } );
  111. } );
  112. describe( 'direction=horizontally', () => {
  113. beforeEach( () => {
  114. command = new SplitCellCommand( editor, { direction: 'horizontally' } );
  115. } );
  116. describe( 'isEnabled', () => {
  117. it( 'should be true if in a table cell', () => {
  118. setData( model, modelTable( [
  119. [ '00[]' ]
  120. ] ) );
  121. expect( command.isEnabled ).to.be.true;
  122. } );
  123. it( 'should be false if not in cell', () => {
  124. setData( model, '<paragraph>11[]</paragraph>' );
  125. expect( command.isEnabled ).to.be.false;
  126. } );
  127. } );
  128. describe( 'execute()', () => {
  129. it( 'should split table cell for two table cells', () => {
  130. setData( model, modelTable( [
  131. [ '00', '01', '02' ],
  132. [ '10', '[]11', '12' ],
  133. [ '20', '21', '22' ]
  134. ] ) );
  135. command.execute();
  136. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  137. [ '00', '01', '02' ],
  138. [ { rowspan: 2, contents: '10' }, '[]11', { rowspan: 2, contents: '12' } ],
  139. [ '' ],
  140. [ '20', '21', '22' ]
  141. ] ) );
  142. } );
  143. } );
  144. } );
  145. } );