settableheaderscommand.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 SetTableHeadersCommand from '../../src/commands/settableheaderscommand';
  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. describe( 'SetTableHeadersCommand', () => {
  20. let editor, model, command;
  21. beforeEach( () => {
  22. return ModelTestEditor.create()
  23. .then( newEditor => {
  24. editor = newEditor;
  25. model = editor.model;
  26. command = new SetTableHeadersCommand( editor );
  27. const conversion = editor.conversion;
  28. const schema = model.schema;
  29. schema.register( 'table', {
  30. allowWhere: '$block',
  31. allowAttributes: [ 'headingRows' ],
  32. isObject: true
  33. } );
  34. schema.register( 'tableRow', { allowIn: 'table' } );
  35. schema.register( 'tableCell', {
  36. allowIn: 'tableRow',
  37. allowContentOf: '$block',
  38. allowAttributes: [ 'colspan', 'rowspan' ],
  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. // Insert row conversion.
  46. conversion.for( 'downcast' ).add( downcastInsertRow() );
  47. // Remove row conversion.
  48. conversion.for( 'downcast' ).add( downcastRemoveRow() );
  49. // Table cell conversion.
  50. conversion.for( 'downcast' ).add( downcastInsertCell() );
  51. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'td' } ) );
  52. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'th' } ) );
  53. // Table attributes conversion.
  54. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  55. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  56. conversion.for( 'downcast' ).add( downcastTableHeadingColumnsChange() );
  57. conversion.for( 'downcast' ).add( downcastTableHeadingRowsChange() );
  58. } );
  59. } );
  60. afterEach( () => {
  61. return editor.destroy();
  62. } );
  63. describe( 'isEnabled', () => {
  64. it( 'should be false if not in a table', () => {
  65. setData( model, '<p>foo[]</p>' );
  66. expect( command.isEnabled ).to.be.false;
  67. } );
  68. it( 'should be true if in table', () => {
  69. setData( model, '<table><tableRow><tableCell>foo[]</tableCell></tableRow></table>' );
  70. expect( command.isEnabled ).to.be.true;
  71. } );
  72. } );
  73. describe( 'execute()', () => {
  74. it( 'should set heading rows attribute', () => {
  75. setData( model, modelTable( [
  76. [ '00', '01' ],
  77. [ '[]10', '11' ],
  78. [ '20', '21' ]
  79. ] ) );
  80. command.execute( { rows: 2 } );
  81. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  82. [ '00', '01' ],
  83. [ '[]10', '11' ],
  84. [ '20', '21' ]
  85. ], { headingRows: 2 } ) );
  86. } );
  87. it( 'should remove heading rows attribute', () => {
  88. setData( model, modelTable( [
  89. [ '00', '01' ],
  90. [ '[]10', '11' ],
  91. [ '20', '21' ]
  92. ], { headingRows: 2 } ) );
  93. command.execute( { rows: 0 } );
  94. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  95. [ '00', '01' ],
  96. [ '[]10', '11' ],
  97. [ '20', '21' ]
  98. ] ) );
  99. } );
  100. it( 'should set heading columns attribute', () => {
  101. setData( model, modelTable( [
  102. [ '00', '01' ],
  103. [ '[]10', '11' ],
  104. [ '20', '21' ]
  105. ] ) );
  106. command.execute( { columns: 2 } );
  107. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  108. [ '00', '01' ],
  109. [ '[]10', '11' ],
  110. [ '20', '21' ]
  111. ], { headingColumns: 2 } ) );
  112. } );
  113. it( 'should remove heading columns attribute', () => {
  114. setData( model, modelTable( [
  115. [ '00', '01' ],
  116. [ '[]10', '11' ],
  117. [ '20', '21' ]
  118. ], { headingColumns: 2 } ) );
  119. command.execute( { columns: 0 } );
  120. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  121. [ '00', '01' ],
  122. [ '[]10', '11' ],
  123. [ '20', '21' ]
  124. ] ) );
  125. } );
  126. it( 'should remove heading columns & heading rows attributes', () => {
  127. setData( model, modelTable( [
  128. [ '00', '01' ],
  129. [ '[]10', '11' ],
  130. [ '20', '21' ]
  131. ], { headingColumns: 2, headingRows: 2 } ) );
  132. command.execute();
  133. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  134. [ '00', '01' ],
  135. [ '[]10', '11' ],
  136. [ '20', '21' ]
  137. ] ) );
  138. } );
  139. it( 'should fix rowspaned cells on the edge of an table head section', () => {
  140. setData( model, modelTable( [
  141. [ '00', '01', '02' ],
  142. [ { colspan: 2, rowspan: 2, contents: '10[]' }, '12' ],
  143. [ '22' ]
  144. ], { headingColumns: 2, headingRows: 1 } ) );
  145. command.execute( { rows: 2, columns: 2 } );
  146. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  147. [ '00', '01', '02' ],
  148. [ { colspan: 2, contents: '10[]' }, '12' ],
  149. [ { colspan: 2, contents: '' }, '22' ]
  150. ], { headingColumns: 2, headingRows: 2 } ) );
  151. } );
  152. it( 'should split to at most 2 table cells when fixing rowspaned cells on the edge of an table head section', () => {
  153. setData( model, modelTable( [
  154. [ '00', '01', '02' ],
  155. [ { colspan: 2, rowspan: 5, contents: '10[]' }, '12' ],
  156. [ '22' ],
  157. [ '32' ],
  158. [ '42' ],
  159. [ '52' ]
  160. ], { headingColumns: 2, headingRows: 1 } ) );
  161. command.execute( { rows: 3, columns: 2 } );
  162. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  163. [ '00', '01', '02' ],
  164. [ { colspan: 2, rowspan: 2, contents: '10[]' }, '12' ],
  165. [ '22' ],
  166. [ { colspan: 2, rowspan: 3, contents: '' }, '32' ],
  167. [ '42' ],
  168. [ '52' ]
  169. ], { headingColumns: 2, headingRows: 3 } ) );
  170. } );
  171. it( 'should fix rowspaned cells on the edge of an table head section when creating section', () => {
  172. setData( model, modelTable( [
  173. [ { rowspan: 2, contents: '[]00' }, '01' ],
  174. [ '11' ]
  175. ], { headingRows: 2 } ) );
  176. command.execute( { rows: 1 } );
  177. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  178. [ '[]00', '01' ],
  179. [ '', '11' ],
  180. ], { headingRows: 1 } ) );
  181. } );
  182. it( 'should fix rowspaned cells inside a row', () => {
  183. setData( model, modelTable( [
  184. [ '00', { rowspan: 2, contents: '[]01' } ],
  185. [ '10' ]
  186. ], { headingRows: 2 } ) );
  187. command.execute( { rows: 1 } );
  188. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  189. [ '00', '[]01' ],
  190. [ '10', '' ]
  191. ], { headingRows: 1 } ) );
  192. } );
  193. } );
  194. } );