splitcellcommand.js 4.5 KB

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