splitcellcommand.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 { downcastInsertTable } from '../../src/converters/downcast';
  10. import upcastTable from '../../src/converters/upcasttable';
  11. import { formatTable, formattedModelTable, modelTable } from '../_utils/utils';
  12. describe( 'SplitCellCommand', () => {
  13. let editor, model, command;
  14. beforeEach( () => {
  15. return ModelTestEditor.create()
  16. .then( newEditor => {
  17. editor = newEditor;
  18. model = editor.model;
  19. command = new SplitCellCommand( editor );
  20. const conversion = editor.conversion;
  21. const schema = model.schema;
  22. schema.register( 'table', {
  23. allowWhere: '$block',
  24. allowAttributes: [ 'headingRows' ],
  25. isBlock: true,
  26. isObject: true
  27. } );
  28. schema.register( 'tableRow', {
  29. allowIn: 'table',
  30. allowAttributes: [],
  31. isBlock: true,
  32. isLimit: true
  33. } );
  34. schema.register( 'tableCell', {
  35. allowIn: 'tableRow',
  36. allowContentOf: '$block',
  37. allowAttributes: [ 'colspan', 'rowspan' ],
  38. isBlock: true,
  39. isLimit: true
  40. } );
  41. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  42. // Table conversion.
  43. conversion.for( 'upcast' ).add( upcastTable() );
  44. conversion.for( 'downcast' ).add( downcastInsertTable() );
  45. // Table row upcast only since downcast conversion is done in `downcastTable()`.
  46. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableRow', view: 'tr' } ) );
  47. // Table cell conversion.
  48. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'td' } ) );
  49. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'th' } ) );
  50. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  51. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  52. } );
  53. } );
  54. afterEach( () => {
  55. return editor.destroy();
  56. } );
  57. describe( 'isEnabled', () => {
  58. it( 'should be true if in a table cell', () => {
  59. setData( model, modelTable( [
  60. [ '00[]' ]
  61. ] ) );
  62. expect( command.isEnabled ).to.be.true;
  63. } );
  64. it( 'should be false if not in cell', () => {
  65. setData( model, '<p>11[]</p>' );
  66. expect( command.isEnabled ).to.be.false;
  67. } );
  68. } );
  69. describe( 'execute()', () => {
  70. describe( 'options.horizontally', () => {
  71. it( 'should split table cell for given table cells', () => {
  72. setData( model, modelTable( [
  73. [ '00', '01', '02' ],
  74. [ '10', '[]11', '12' ],
  75. [ '20', { colspan: 2, contents: '21' } ],
  76. [ { colspan: 2, contents: '30' }, '32' ]
  77. ] ) );
  78. command.execute( { horizontally: 3 } );
  79. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  80. [ '00', { colspan: 3, contents: '01' }, '02' ],
  81. [ '10', '[]11', '', '', '12' ],
  82. [ '20', { colspan: 4, contents: '21' } ],
  83. [ { colspan: 4, contents: '30' }, '32' ]
  84. ] ) );
  85. } );
  86. it( 'should unsplit table cell if split is equal to colspan', () => {
  87. setData( model, modelTable( [
  88. [ '00', '01', '02' ],
  89. [ '10', '11', '12' ],
  90. [ '20', { colspan: 2, contents: '21[]' } ],
  91. [ { colspan: 2, contents: '30' }, '32' ]
  92. ] ) );
  93. command.execute( { horizontally: 2 } );
  94. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  95. [ '00', '01', '02' ],
  96. [ '10', '11', '12' ],
  97. [ '20', '21[]', '' ],
  98. [ { colspan: 2, contents: '30' }, '32' ]
  99. ] ) );
  100. } );
  101. it( 'should properly unsplit table cell if split is uneven', () => {
  102. setData( model, modelTable( [
  103. [ '00', '01', '02' ],
  104. [ { colspan: 3, contents: '10[]' } ]
  105. ] ) );
  106. command.execute( { horizontally: 2 } );
  107. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  108. [ '00', '01', '02' ],
  109. [ { colspan: 2, contents: '10[]' }, '' ]
  110. ] ) );
  111. } );
  112. it( 'should properly set colspan of inserted cells', () => {
  113. setData( model, modelTable( [
  114. [ '00', '01', '02', '03' ],
  115. [ { colspan: 4, contents: '10[]' } ]
  116. ] ) );
  117. command.execute( { horizontally: 2 } );
  118. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  119. [ '00', '01', '02', '03' ],
  120. [ { colspan: 2, contents: '10[]' }, { colspan: 2, contents: '' } ]
  121. ] ) );
  122. } );
  123. it( 'should keep rowspan attribute for newly inserted cells', () => {
  124. setData( model, modelTable( [
  125. [ '00', '01', '02', '03', '04', '05' ],
  126. [ { colspan: 5, rowspan: 2, contents: '10[]' }, '15' ],
  127. [ '25' ]
  128. ] ) );
  129. command.execute( { horizontally: 2 } );
  130. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  131. [ '00', '01', '02', '03', '04', '05' ],
  132. [ { colspan: 3, rowspan: 2, contents: '10[]' }, { colspan: 2, rowspan: 2, contents: '' }, '15' ],
  133. [ '25' ]
  134. ] ) );
  135. } );
  136. } );
  137. describe( 'options.horizontally', () => {} );
  138. } );
  139. } );