8
0

splitcellcommand.js 4.8 KB

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