splitcellcommand.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 cell with colspan attribute set', () => {
  59. setData( model, modelTable( [
  60. [ { colspan: 2, contents: '11[]' } ]
  61. ] ) );
  62. expect( command.isEnabled ).to.be.true;
  63. } );
  64. it( 'should be true if in cell with rowspan attribute set', () => {
  65. setData( model, modelTable( [
  66. [ { rowspan: 2, contents: '11[]' } ]
  67. ] ) );
  68. expect( command.isEnabled ).to.be.true;
  69. } );
  70. it( 'should be false in cell without rowspan or colspan attribute', () => {
  71. setData( model, modelTable( [
  72. [ '11[]' ]
  73. ] ) );
  74. expect( command.isEnabled ).to.be.false;
  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 with colspan', () => {
  83. setData( model, modelTable( [
  84. [ { colspan: 2, contents: '[]11' } ]
  85. ] ) );
  86. command.execute();
  87. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  88. [ '[]11', '' ]
  89. ] ) );
  90. } );
  91. it( 'should split table cell with rowspan', () => {
  92. setData( model, modelTable( [
  93. [ { rowspan: 2, contents: '[]11' }, '12' ],
  94. [ '22' ]
  95. ] ) );
  96. command.execute();
  97. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  98. [ '[]11', '12' ],
  99. [ '', '22' ]
  100. ] ) );
  101. } );
  102. it( 'should split table cell with rowspan in the middle of a table', () => {
  103. setData( model, modelTable( [
  104. [ '11', { rowspan: 3, contents: '[]12' }, '13' ],
  105. [ { rowspan: 2, contents: '21' }, '23' ],
  106. [ '33' ]
  107. ] ) );
  108. command.execute();
  109. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  110. [ '11', '[]12', '13' ],
  111. [ { rowspan: 2, contents: '21' }, '', '23' ],
  112. [ '', '33' ]
  113. ] ) );
  114. } );
  115. it( 'should split table cell with rowspan and colspan in the middle of a table', () => {
  116. setData( model, modelTable( [
  117. [ '11', { rowspan: 3, colspan: 2, contents: '[]12' }, '14' ],
  118. [ { rowspan: 2, contents: '21' }, '24' ],
  119. [ '34' ]
  120. ] ) );
  121. command.execute();
  122. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  123. [ '11', '[]12', '', '14' ],
  124. [ { rowspan: 2, contents: '21' }, '', '', '24' ],
  125. [ '', '', '34' ]
  126. ] ) );
  127. } );
  128. } );
  129. } );