removerowcommand.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import RemoveRowCommand from '../../src/commands/removerowcommand';
  8. import TableSelection from '../../src/tableselection';
  9. import { defaultConversion, defaultSchema, modelTable } from '../_utils/utils';
  10. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  11. describe( 'RemoveRowCommand', () => {
  12. let editor, model, command;
  13. beforeEach( () => {
  14. return ModelTestEditor.create( { plugins: [ TableSelection ] } )
  15. .then( newEditor => {
  16. editor = newEditor;
  17. model = editor.model;
  18. command = new RemoveRowCommand( editor );
  19. defaultSchema( model.schema );
  20. defaultConversion( editor.conversion );
  21. } );
  22. } );
  23. afterEach( () => {
  24. return editor.destroy();
  25. } );
  26. describe( 'isEnabled', () => {
  27. it( 'should be true if selection is inside table cell', () => {
  28. setData( model, modelTable( [
  29. [ '00[]', '01' ],
  30. [ '10', '11' ]
  31. ] ) );
  32. expect( command.isEnabled ).to.be.true;
  33. } );
  34. it( 'should be true if selection contains multiple cells', () => {
  35. setData( model, modelTable( [
  36. [ '00', '01' ],
  37. [ '10', '11' ]
  38. ] ) );
  39. const tableSelection = editor.plugins.get( TableSelection );
  40. const modelRoot = model.document.getRoot();
  41. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  42. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  43. expect( command.isEnabled ).to.be.true;
  44. } );
  45. it( 'should be false if selection is inside table with one row only', () => {
  46. setData( model, modelTable( [
  47. [ '00[]', '01' ]
  48. ] ) );
  49. expect( command.isEnabled ).to.be.false;
  50. } );
  51. it( 'should be false if selection is outside a table', () => {
  52. setData( model, '<paragraph>11[]</paragraph>' );
  53. expect( command.isEnabled ).to.be.false;
  54. } );
  55. } );
  56. describe( 'execute()', () => {
  57. it( 'should remove a given row', () => {
  58. setData( model, modelTable( [
  59. [ '00', '01' ],
  60. [ '[]10', '11' ],
  61. [ '20', '21' ]
  62. ] ) );
  63. command.execute();
  64. assertEqualMarkup( getData( model ), modelTable( [
  65. [ '00', '01' ],
  66. [ '[]20', '21' ]
  67. ] ) );
  68. } );
  69. it( 'should remove a given row from a table start', () => {
  70. setData( model, modelTable( [
  71. [ '[]00', '01' ],
  72. [ '10', '11' ],
  73. [ '20', '21' ]
  74. ] ) );
  75. command.execute();
  76. assertEqualMarkup( getData( model ), modelTable( [
  77. [ '[]10', '11' ],
  78. [ '20', '21' ]
  79. ] ) );
  80. } );
  81. it( 'should change heading rows if removing a heading row', () => {
  82. setData( model, modelTable( [
  83. [ '00', '01' ],
  84. [ '[]10', '11' ],
  85. [ '20', '21' ]
  86. ], { headingRows: 2 } ) );
  87. command.execute();
  88. assertEqualMarkup( getData( model ), modelTable( [
  89. [ '00', '01' ],
  90. [ '[]20', '21' ]
  91. ], { headingRows: 1 } ) );
  92. } );
  93. it( 'should decrease rowspan of table cells from previous rows', () => {
  94. setData( model, modelTable( [
  95. [ { rowspan: 4, contents: '00' }, { rowspan: 3, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  96. [ { rowspan: 2, contents: '13' }, '14' ],
  97. [ '22[]', '23', '24' ],
  98. [ '30', '31', '32', '33', '34' ]
  99. ] ) );
  100. command.execute();
  101. assertEqualMarkup( getData( model ), modelTable( [
  102. [ { rowspan: 3, contents: '00' }, { rowspan: 2, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  103. [ '13', '14' ],
  104. [ '30', '31', '[]32', '33', '34' ]
  105. ] ) );
  106. } );
  107. it( 'should move rowspaned cells to row below removing it\'s row', () => {
  108. setData( model, modelTable( [
  109. [ { rowspan: 3, contents: '[]00' }, { rowspan: 2, contents: '01' }, '02' ],
  110. [ '12' ],
  111. [ '22' ],
  112. [ '30', '31', '32' ]
  113. ] ) );
  114. command.execute();
  115. assertEqualMarkup( getData( model ), modelTable( [
  116. [ { rowspan: 2, contents: '[]00' }, '01', '12' ],
  117. [ '22' ],
  118. [ '30', '31', '32' ]
  119. ] ) );
  120. } );
  121. } );
  122. } );