setheaderrowcommand.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 SetHeaderRowCommand from '../../src/commands/setheaderrowcommand';
  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( 'HeaderRowCommand', () => {
  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 SetHeaderRowCommand( editor );
  29. const conversion = editor.conversion;
  30. const schema = model.schema;
  31. schema.register( 'table', {
  32. allowWhere: '$block',
  33. allowAttributes: [ 'headingRows' ],
  34. isObject: true
  35. } );
  36. schema.register( 'tableRow', { allowIn: 'table' } );
  37. schema.register( 'tableCell', {
  38. allowIn: 'tableRow',
  39. allowContentOf: '$block',
  40. allowAttributes: [ 'colspan', 'rowspan' ],
  41. isLimit: true
  42. } );
  43. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  44. // Table conversion.
  45. conversion.for( 'upcast' ).add( upcastTable() );
  46. conversion.for( 'downcast' ).add( downcastInsertTable() );
  47. // Insert row conversion.
  48. conversion.for( 'downcast' ).add( downcastInsertRow() );
  49. // Remove row conversion.
  50. conversion.for( 'downcast' ).add( downcastRemoveRow() );
  51. // Table cell conversion.
  52. conversion.for( 'downcast' ).add( downcastInsertCell() );
  53. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'td' } ) );
  54. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'th' } ) );
  55. // Table attributes conversion.
  56. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  57. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  58. conversion.for( 'downcast' ).add( downcastTableHeadingColumnsChange() );
  59. conversion.for( 'downcast' ).add( downcastTableHeadingRowsChange() );
  60. } );
  61. } );
  62. afterEach( () => {
  63. return editor.destroy();
  64. } );
  65. describe( 'isEnabled', () => {
  66. it( 'should be false if selection is not in a table', () => {
  67. setData( model, '<p>foo[]</p>' );
  68. expect( command.isEnabled ).to.be.false;
  69. } );
  70. it( 'should be true if selection is in table', () => {
  71. setData( model, '<table><tableRow><tableCell>foo[]</tableCell></tableRow></table>' );
  72. expect( command.isEnabled ).to.be.true;
  73. } );
  74. } );
  75. describe( 'value', () => {
  76. it( 'should be false if selection is not in a table without heading row', () => {
  77. setData( model, modelTable( [
  78. [ '01[]', '02' ],
  79. [ '11', '12' ]
  80. ] ) );
  81. expect( command.value ).to.be.false;
  82. } );
  83. it( 'should be false if selection is not in a heading row', () => {
  84. setData( model, modelTable( [
  85. [ '01', '02' ],
  86. [ '11', '12[]' ]
  87. ], { headingRows: 1 } ) );
  88. expect( command.value ).to.be.false;
  89. } );
  90. it( 'should be true if selection is in a heading row', () => {
  91. setData( model, modelTable( [
  92. [ '01[]', '02' ],
  93. [ '11', '12' ]
  94. ], { headingRows: 1 } ) );
  95. expect( command.value ).to.be.true;
  96. } );
  97. it( 'should be false if selection is in a heading column', () => {
  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 rows attribute that cover row 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. ], { headingRows: 2 } ) );
  118. } );
  119. it( 'should toggle heading rows attribute', () => {
  120. setData( model, modelTable( [
  121. [ '[]00', '01' ],
  122. [ '10', '11' ],
  123. [ '20', '21' ]
  124. ] ) );
  125. command.execute();
  126. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  127. [ '[]00', '01' ],
  128. [ '10', '11' ],
  129. [ '20', '21' ]
  130. ], { headingRows: 1 } ) );
  131. command.execute();
  132. setData( model, modelTable( [
  133. [ '[]00', '01' ],
  134. [ '10', '11' ],
  135. [ '20', '21' ]
  136. ] ) );
  137. } );
  138. it( 'should set heading rows attribute if currently selected row is a heading so the heading section is below this row', () => {
  139. setData( model, modelTable( [
  140. [ '00', '01' ],
  141. [ '[]10', '11' ],
  142. [ '20', '21' ]
  143. ], { headingRows: 2 } ) );
  144. command.execute();
  145. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  146. [ '00', '01' ],
  147. [ '[]10', '11' ],
  148. [ '20', '21' ]
  149. ], { headingRows: 1 } ) );
  150. } );
  151. it( 'should fix rowspaned cells on the edge of an table head section', () => {
  152. setData( model, modelTable( [
  153. [ '00', '01', '02' ],
  154. [ { colspan: 2, rowspan: 2, contents: '10[]' }, '12' ],
  155. [ '22' ]
  156. ], { headingColumns: 2, headingRows: 1 } ) );
  157. command.execute();
  158. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  159. [ '00', '01', '02' ],
  160. [ { colspan: 2, contents: '10[]' }, '12' ],
  161. [ { colspan: 2, contents: '' }, '22' ]
  162. ], { headingColumns: 2, headingRows: 2 } ) );
  163. } );
  164. it( 'should split to at most 2 table cells when fixing rowspaned cells on the edge of an table head section', () => {
  165. setData( model, modelTable( [
  166. [ '00', '01', '02' ],
  167. [ { colspan: 2, rowspan: 5, contents: '10' }, '12' ],
  168. [ '22[]' ],
  169. [ '32' ],
  170. [ '42' ],
  171. [ '52' ]
  172. ], { headingColumns: 2, headingRows: 1 } ) );
  173. command.execute();
  174. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  175. [ '00', '01', '02' ],
  176. [ { colspan: 2, rowspan: 2, contents: '10' }, '12' ],
  177. [ '22[]' ],
  178. [ { colspan: 2, rowspan: 3, contents: '' }, '32' ],
  179. [ '42' ],
  180. [ '52' ]
  181. ], { headingColumns: 2, headingRows: 3 } ) );
  182. } );
  183. it( 'should fix rowspaned cells on the edge of an table head section when creating section', () => {
  184. setData( model, modelTable( [
  185. [ { rowspan: 2, contents: '[]00' }, '01' ],
  186. [ '11' ]
  187. ], { headingRows: 2 } ) );
  188. command.execute();
  189. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  190. [ '[]00', '01' ],
  191. [ '', '11' ]
  192. ], { headingRows: 1 } ) );
  193. } );
  194. it( 'should fix rowspaned cells inside a row', () => {
  195. setData( model, modelTable( [
  196. [ '00', { rowspan: 2, contents: '[]01' } ],
  197. [ '10' ]
  198. ], { headingRows: 2 } ) );
  199. command.execute();
  200. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  201. [ '00', '[]01' ],
  202. [ '10', '' ]
  203. ], { headingRows: 1 } ) );
  204. } );
  205. it( 'should work properly in the first row of a table', () => {
  206. setData( model, modelTable( [
  207. [ '00', '[]01', '02' ],
  208. [ { colspan: 2, rowspan: 2, contents: '10' }, '12' ],
  209. [ '22' ]
  210. ] ) );
  211. command.execute();
  212. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  213. [ '00', '[]01', '02' ],
  214. [ { colspan: 2, rowspan: 2, contents: '10' }, '12' ],
  215. [ '22' ]
  216. ], { headingRows: 1 } ) );
  217. } );
  218. } );
  219. } );