splitcellcommand.js 6.4 KB

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