splitcellcommand.js 6.5 KB

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