removerowcommand.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 { defaultConversion, defaultSchema, modelTable } from '../_utils/utils';
  9. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  10. describe( 'RemoveRowCommand', () => {
  11. let editor, model, command;
  12. beforeEach( () => {
  13. return ModelTestEditor.create()
  14. .then( newEditor => {
  15. editor = newEditor;
  16. model = editor.model;
  17. command = new RemoveRowCommand( editor );
  18. defaultSchema( model.schema );
  19. defaultConversion( editor.conversion );
  20. } );
  21. } );
  22. afterEach( () => {
  23. return editor.destroy();
  24. } );
  25. describe( 'isEnabled', () => {
  26. it( 'should be true if selection is inside table cell', () => {
  27. setData( model, modelTable( [
  28. [ '00[]', '01' ],
  29. [ '10', '11' ]
  30. ] ) );
  31. expect( command.isEnabled ).to.be.true;
  32. } );
  33. it( 'should be false if selection is inside table with one row only', () => {
  34. setData( model, modelTable( [
  35. [ '00[]', '01' ]
  36. ] ) );
  37. expect( command.isEnabled ).to.be.false;
  38. } );
  39. it( 'should be false if selection is outside a table', () => {
  40. setData( model, '<paragraph>11[]</paragraph>' );
  41. expect( command.isEnabled ).to.be.false;
  42. } );
  43. } );
  44. describe( 'execute()', () => {
  45. it( 'should remove a given row', () => {
  46. setData( model, modelTable( [
  47. [ '00', '01' ],
  48. [ '[]10', '11' ],
  49. [ '20', '21' ]
  50. ] ) );
  51. command.execute();
  52. assertEqualMarkup( getData( model ), modelTable( [
  53. [ '00', '01' ],
  54. [ '[]20', '21' ]
  55. ] ) );
  56. } );
  57. it( 'should remove a given row from a table start', () => {
  58. setData( model, modelTable( [
  59. [ '[]00', '01' ],
  60. [ '10', '11' ],
  61. [ '20', '21' ]
  62. ] ) );
  63. command.execute();
  64. assertEqualMarkup( getData( model ), modelTable( [
  65. [ '[]10', '11' ],
  66. [ '20', '21' ]
  67. ] ) );
  68. } );
  69. it( 'should remove last row', () => {
  70. setData( model, modelTable( [
  71. [ '00', '01' ],
  72. [ '[]10', '11' ]
  73. ] ) );
  74. command.execute();
  75. assertEqualMarkup( getData( model ), modelTable( [
  76. [ '[]00', '01' ]
  77. ] ) );
  78. } );
  79. it( 'should change heading rows if removing a heading row', () => {
  80. setData( model, modelTable( [
  81. [ '00', '01' ],
  82. [ '[]10', '11' ],
  83. [ '20', '21' ]
  84. ], { headingRows: 2 } ) );
  85. command.execute();
  86. assertEqualMarkup( getData( model ), modelTable( [
  87. [ '00', '01' ],
  88. [ '[]20', '21' ]
  89. ], { headingRows: 1 } ) );
  90. } );
  91. it( 'should decrease rowspan of table cells from previous rows', () => {
  92. setData( model, modelTable( [
  93. [ { rowspan: 4, contents: '00' }, { rowspan: 3, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  94. [ { rowspan: 2, contents: '13' }, '14' ],
  95. [ '22[]', '23', '24' ],
  96. [ '30', '31', '32', '33', '34' ]
  97. ] ) );
  98. command.execute();
  99. assertEqualMarkup( getData( model ), modelTable( [
  100. [ { rowspan: 3, contents: '00' }, { rowspan: 2, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  101. [ '13', '14' ],
  102. [ '30', '31', '[]32', '33', '34' ]
  103. ] ) );
  104. } );
  105. it( 'should move rowspaned cells to row below removing it\'s row', () => {
  106. setData( model, modelTable( [
  107. [ { rowspan: 3, contents: '[]00' }, { rowspan: 2, contents: '01' }, '02' ],
  108. [ '12' ],
  109. [ '22' ],
  110. [ '30', '31', '32' ]
  111. ] ) );
  112. command.execute();
  113. assertEqualMarkup( getData( model ), modelTable( [
  114. [ { rowspan: 2, contents: '[]00' }, '01', '12' ],
  115. [ '22' ],
  116. [ '30', '31', '32' ]
  117. ] ) );
  118. } );
  119. } );
  120. } );