8
0

setheadercolumncommand.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 SetHeaderColumnCommand from '../../src/commands/setheadercolumncommand';
  9. import {
  10. downcastInsertCell,
  11. downcastInsertRow,
  12. downcastInsertTable,
  13. downcastRemoveRow,
  14. downcastTableHeadingColumnsChange,
  15. downcastTableHeadingRowsChange
  16. } from '../../src/converters/downcast';
  17. import upcastTable from '../../src/converters/upcasttable';
  18. import { formatTable, formattedModelTable, modelTable } from '../_utils/utils';
  19. import TableUtils from '../../src/tableutils';
  20. describe( 'HeaderColumnCommand', () => {
  21. let editor, model, command;
  22. beforeEach( () => {
  23. return ModelTestEditor.create( {
  24. plugins: [ TableUtils ]
  25. } ).then( newEditor => {
  26. editor = newEditor;
  27. model = editor.model;
  28. command = new SetHeaderColumnCommand( editor );
  29. const conversion = editor.conversion;
  30. const schema = model.schema;
  31. schema.register( 'table', {
  32. allowWhere: '$block',
  33. allowAttributes: [ 'headingRows' ],
  34. isBlock: true,
  35. isObject: true
  36. } );
  37. schema.register( 'tableRow', {
  38. allowIn: 'table',
  39. allowAttributes: [],
  40. isBlock: true,
  41. isLimit: true
  42. } );
  43. schema.register( 'tableCell', {
  44. allowIn: 'tableRow',
  45. allowContentOf: '$block',
  46. allowAttributes: [ 'colspan', 'rowspan' ],
  47. isBlock: true,
  48. isLimit: true
  49. } );
  50. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  51. // Table conversion.
  52. conversion.for( 'upcast' ).add( upcastTable() );
  53. conversion.for( 'downcast' ).add( downcastInsertTable() );
  54. // Insert row conversion.
  55. conversion.for( 'downcast' ).add( downcastInsertRow() );
  56. // Remove row conversion.
  57. conversion.for( 'downcast' ).add( downcastRemoveRow() );
  58. // Table cell conversion.
  59. conversion.for( 'downcast' ).add( downcastInsertCell() );
  60. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'td' } ) );
  61. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'th' } ) );
  62. // Table attributes conversion.
  63. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  64. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  65. conversion.for( 'downcast' ).add( downcastTableHeadingColumnsChange() );
  66. conversion.for( 'downcast' ).add( downcastTableHeadingRowsChange() );
  67. } );
  68. } );
  69. afterEach( () => {
  70. return editor.destroy();
  71. } );
  72. describe( 'isEnabled', () => {
  73. it( 'should be false if selection is not in a table', () => {
  74. setData( model, '<p>foo[]</p>' );
  75. expect( command.isEnabled ).to.be.false;
  76. } );
  77. it( 'should be true if selection is in table', () => {
  78. setData( model, '<table><tableRow><tableCell>foo[]</tableCell></tableRow></table>' );
  79. expect( command.isEnabled ).to.be.true;
  80. } );
  81. } );
  82. describe( 'value', () => {
  83. it( 'should be false if selection is not in a heading column', () => {
  84. setData( model, modelTable( [
  85. [ '01', '02' ],
  86. [ '11', '12[]' ]
  87. ], { headingColumns: 1 } ) );
  88. expect( command.value ).to.be.false;
  89. } );
  90. it( 'should be true if selection is in a heading column', () => {
  91. setData( model, modelTable( [
  92. [ '01[]', '02' ],
  93. [ '11', '12' ]
  94. ], { headingColumns: 1 } ) );
  95. expect( command.value ).to.be.true;
  96. } );
  97. it( 'should be false if selection is in a heading row', () => {
  98. setData( model, modelTable( [
  99. [ '01', '02[]' ],
  100. [ '11', '12' ]
  101. ], { headingRows: 1, headingColumns: 1 } ) );
  102. expect( command.value ).to.be.false;
  103. } );
  104. } );
  105. describe( 'execute()', () => {
  106. it( 'should set heading columns attribute that cover column in which is selection', () => {
  107. setData( model, modelTable( [
  108. [ '00', '01' ],
  109. [ '[]10', '11' ],
  110. [ '20', '21' ]
  111. ] ) );
  112. command.execute();
  113. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  114. [ '00', '01' ],
  115. [ '[]10', '11' ],
  116. [ '20', '21' ]
  117. ], { headingColumns: 1 } ) );
  118. } );
  119. it(
  120. 'should set heading columns attribute if currently selected column is a heading so the heading section is before this column',
  121. () => {
  122. setData( model, modelTable( [
  123. [ '00', '01' ],
  124. [ '[]10', '11' ],
  125. [ '20', '21' ]
  126. ], { headingColumns: 2 } ) );
  127. command.execute();
  128. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  129. [ '00', '01' ],
  130. [ '[]10', '11' ],
  131. [ '20', '21' ]
  132. ], { headingColumns: 1 } ) );
  133. }
  134. );
  135. it( 'should toggle of selected column', () => {
  136. setData( model, modelTable( [
  137. [ '00', '01' ],
  138. [ '10', '11[]' ],
  139. [ '20', '21' ]
  140. ], { headingColumns: 2 } ) );
  141. command.execute();
  142. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  143. [ '00', '01' ],
  144. [ '10', '11[]' ],
  145. [ '20', '21' ]
  146. ], { headingColumns: 1 } ) );
  147. command.execute();
  148. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  149. [ '00', '01' ],
  150. [ '10', '11[]' ],
  151. [ '20', '21' ]
  152. ], { headingColumns: 2 } ) );
  153. } );
  154. } );
  155. } );