splitcellcommand.js 6.5 KB

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