setheaderrowcommand.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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' ],
  111. [ '[]10' ],
  112. [ '20' ],
  113. [ '30' ]
  114. ] ) );
  115. command.execute();
  116. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  117. [ '00' ],
  118. [ '[]10' ],
  119. [ '20' ],
  120. [ '30' ]
  121. ], { headingRows: 2 } ) );
  122. } );
  123. it( 'should toggle heading rows attribute', () => {
  124. setData( model, modelTable( [
  125. [ '00' ],
  126. [ '[]10' ],
  127. [ '20' ],
  128. [ '30' ]
  129. ], { headingRows: 2 } ) );
  130. command.execute();
  131. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  132. [ '00' ],
  133. [ '[]10' ],
  134. [ '20' ],
  135. [ '30' ]
  136. ], { headingRows: 1 } ) );
  137. command.execute();
  138. setData( model, modelTable( [
  139. [ '00' ],
  140. [ '[]10' ],
  141. [ '20' ],
  142. [ '30' ]
  143. ], { headingRows: 2 } ) );
  144. } );
  145. it( 'should set heading rows attribute if currently selected row is a heading so the heading section is below this row', () => {
  146. setData( model, modelTable( [
  147. [ '00' ],
  148. [ '[]10' ],
  149. [ '20' ],
  150. [ '30' ]
  151. ], { headingRows: 3 } ) );
  152. command.execute();
  153. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  154. [ '00' ],
  155. [ '[]10' ],
  156. [ '20' ],
  157. [ '30' ]
  158. ], { headingRows: 1 } ) );
  159. } );
  160. it( 'should unsetset heading rows attribute', () => {
  161. setData( model, modelTable( [
  162. [ '[]00' ],
  163. [ '10' ],
  164. [ '20' ],
  165. [ '30' ]
  166. ], { headingRows: 3 } ) );
  167. command.execute();
  168. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  169. [ '[]00' ],
  170. [ '10' ],
  171. [ '20' ],
  172. [ '30' ]
  173. ] ) );
  174. } );
  175. it( 'should fix rowspaned cells on the edge of an table head section', () => {
  176. setData( model, modelTable( [
  177. [ '00', '01', '02' ],
  178. [ { colspan: 2, rowspan: 2, contents: '10[]' }, '12' ],
  179. [ '22' ]
  180. ], { headingColumns: 2, headingRows: 1 } ) );
  181. command.execute();
  182. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  183. [ '00', '01', '02' ],
  184. [ { colspan: 2, contents: '10[]' }, '12' ],
  185. [ { colspan: 2, contents: '' }, '22' ]
  186. ], { headingColumns: 2, headingRows: 2 } ) );
  187. } );
  188. it( 'should split to at most 2 table cells when fixing rowspaned cells on the edge of an table head section', () => {
  189. setData( model, modelTable( [
  190. [ '00', '01', '02' ],
  191. [ { colspan: 2, rowspan: 5, contents: '10' }, '12' ],
  192. [ '22[]' ],
  193. [ '32' ],
  194. [ '42' ],
  195. [ '52' ]
  196. ], { headingColumns: 2, headingRows: 1 } ) );
  197. command.execute();
  198. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  199. [ '00', '01', '02' ],
  200. [ { colspan: 2, rowspan: 2, contents: '10' }, '12' ],
  201. [ '22[]' ],
  202. [ { colspan: 2, rowspan: 3, contents: '' }, '32' ],
  203. [ '42' ],
  204. [ '52' ]
  205. ], { headingColumns: 2, headingRows: 3 } ) );
  206. } );
  207. it( 'should fix rowspaned cells on the edge of an table head section when creating section', () => {
  208. setData( model, modelTable( [
  209. [ { rowspan: 2, contents: '00' }, '01' ],
  210. [ '[]11' ]
  211. ], { headingRows: 2 } ) );
  212. command.execute();
  213. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  214. [ '00', '01' ],
  215. [ '', '[]11' ]
  216. ], { headingRows: 1 } ) );
  217. } );
  218. it( 'should fix rowspaned cells inside a row', () => {
  219. setData( model, modelTable( [
  220. [ '00', { rowspan: 2, contents: '01' } ],
  221. [ '[]10' ]
  222. ], { headingRows: 2 } ) );
  223. command.execute();
  224. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  225. [ '00', '01' ],
  226. [ '[]10', '' ]
  227. ], { headingRows: 1 } ) );
  228. } );
  229. it( 'should work properly in the first row of a table', () => {
  230. setData( model, modelTable( [
  231. [ '00', '[]01', '02' ],
  232. [ { colspan: 2, rowspan: 2, contents: '10' }, '12' ],
  233. [ '22' ]
  234. ] ) );
  235. command.execute();
  236. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  237. [ '00', '[]01', '02' ],
  238. [ { colspan: 2, rowspan: 2, contents: '10' }, '12' ],
  239. [ '22' ]
  240. ], { headingRows: 1 } ) );
  241. } );
  242. } );
  243. } );