8
0

settableheaderscommand.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 { downcastInsertTable } from '../../src/converters/downcast';
  10. import upcastTable from '../../src/converters/upcasttable';
  11. import { formatTable, formattedModelTable, modelTable } from '../_utils/utils';
  12. describe( 'SetTableHeadersCommand', () => {
  13. let editor, model, command;
  14. beforeEach( () => {
  15. return ModelTestEditor.create()
  16. .then( newEditor => {
  17. editor = newEditor;
  18. model = editor.model;
  19. command = new SetTableHeadersCommand( 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 false if not in a table', () => {
  59. setData( model, '<p>foo[]</p>' );
  60. expect( command.isEnabled ).to.be.false;
  61. } );
  62. it( 'should be true if in table', () => {
  63. setData( model, '<table><tableRow><tableCell>foo[]</tableCell></tableRow></table>' );
  64. expect( command.isEnabled ).to.be.true;
  65. } );
  66. } );
  67. describe( 'execute()', () => {
  68. it( 'should set heading rows attribute', () => {
  69. setData( model, modelTable( [
  70. [ '00', '01' ],
  71. [ '[]10', '11' ],
  72. [ '20', '21' ]
  73. ] ) );
  74. command.execute( { rows: 2 } );
  75. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  76. [ '00', '01' ],
  77. [ '[]10', '11' ],
  78. [ '20', '21' ]
  79. ], { headingRows: 2 } ) );
  80. } );
  81. it( 'should remove heading rows attribute', () => {
  82. setData( model, modelTable( [
  83. [ '00', '01' ],
  84. [ '[]10', '11' ],
  85. [ '20', '21' ]
  86. ], { headingRows: 2 } ) );
  87. command.execute( { rows: 0 } );
  88. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  89. [ '00', '01' ],
  90. [ '[]10', '11' ],
  91. [ '20', '21' ]
  92. ] ) );
  93. } );
  94. it( 'should set heading columns attribute', () => {
  95. setData( model, modelTable( [
  96. [ '00', '01' ],
  97. [ '[]10', '11' ],
  98. [ '20', '21' ]
  99. ] ) );
  100. command.execute( { columns: 2 } );
  101. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  102. [ '00', '01' ],
  103. [ '[]10', '11' ],
  104. [ '20', '21' ]
  105. ], { headingColumns: 2 } ) );
  106. } );
  107. it( 'should remove heading columns attribute', () => {
  108. setData( model, modelTable( [
  109. [ '00', '01' ],
  110. [ '[]10', '11' ],
  111. [ '20', '21' ]
  112. ], { headingColumns: 2 } ) );
  113. command.execute( { columns: 0 } );
  114. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  115. [ '00', '01' ],
  116. [ '[]10', '11' ],
  117. [ '20', '21' ]
  118. ] ) );
  119. } );
  120. it( 'should remove heading columns & heading rows attributes', () => {
  121. setData( model, modelTable( [
  122. [ '00', '01' ],
  123. [ '[]10', '11' ],
  124. [ '20', '21' ]
  125. ], { headingColumns: 2, headingRows: 2 } ) );
  126. command.execute();
  127. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  128. [ '00', '01' ],
  129. [ '[]10', '11' ],
  130. [ '20', '21' ]
  131. ] ) );
  132. } );
  133. it( 'should fix rowspaned cells on the edge of an table head section', () => {
  134. setData( model, modelTable( [
  135. [ '00', '01', '02' ],
  136. [ { colspan: 2, rowspan: 2, contents: '10[]' }, '12' ],
  137. [ '22' ]
  138. ], { headingColumns: 2, headingRows: 1 } ) );
  139. command.execute( { rows: 2, columns: 2 } );
  140. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  141. [ '00', '01', '02' ],
  142. [ { colspan: 2, contents: '10[]' }, '12' ],
  143. [ { colspan: 2, contents: '' }, '22' ]
  144. ], { headingColumns: 2, headingRows: 2 } ) );
  145. } );
  146. it( 'should split to at most 2 table cells when fixing rowspaned cells on the edge of an table head section', () => {
  147. setData( model, modelTable( [
  148. [ '00', '01', '02' ],
  149. [ { colspan: 2, rowspan: 5, contents: '10[]' }, '12' ],
  150. [ '22' ],
  151. [ '32' ],
  152. [ '42' ],
  153. [ '52' ]
  154. ], { headingColumns: 2, headingRows: 1 } ) );
  155. command.execute( { rows: 3, columns: 2 } );
  156. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  157. [ '00', '01', '02' ],
  158. [ { colspan: 2, rowspan: 2, contents: '10[]' }, '12' ],
  159. [ '22' ],
  160. [ { colspan: 2, rowspan: 3, contents: '' }, '32' ],
  161. [ '42' ],
  162. [ '52' ]
  163. ], { headingColumns: 2, headingRows: 3 } ) );
  164. } );
  165. it( 'should fix rowspaned cells on the edge of an table head section when creating section', () => {
  166. setData( model, modelTable( [
  167. [ { rowspan: 2, contents: '[]00' }, '01' ],
  168. [ '11' ]
  169. ], { headingRows: 2 } ) );
  170. command.execute( { rows: 1 } );
  171. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  172. [ '[]00', '01' ],
  173. [ '', '11' ],
  174. ], { headingRows: 1 } ) );
  175. } );
  176. it( 'should fix rowspaned cells inside a row', () => {
  177. setData( model, modelTable( [
  178. [ '00', { rowspan: 2, contents: '[]01' } ],
  179. [ '10' ]
  180. ], { headingRows: 2 } ) );
  181. command.execute( { rows: 1 } );
  182. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  183. [ '00', '[]01' ],
  184. [ '10', '' ]
  185. ], { headingRows: 1 } ) );
  186. } );
  187. } );
  188. } );