8
0

setheaderrowcommand.js 7.6 KB

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