settableheaderscommand.js 6.9 KB

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