mergecellcommand.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 MergeCellCommand from '../../src/commands/mergecellcommand';
  9. import { downcastInsertTable } from '../../src/converters/downcast';
  10. import upcastTable from '../../src/converters/upcasttable';
  11. import { formatTable, formattedModelTable, modelTable } from '../_utils/utils';
  12. describe.only( 'MergeCellCommand', () => {
  13. let editor, model, command, root;
  14. beforeEach( () => {
  15. return ModelTestEditor.create()
  16. .then( newEditor => {
  17. editor = newEditor;
  18. model = editor.model;
  19. root = model.document.getRoot( 'main' );
  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( 'direction=right', () => {
  58. beforeEach( () => {
  59. command = new MergeCellCommand( editor, { direction: 'right' } );
  60. } );
  61. describe( 'isEnabled', () => {
  62. it( 'should be true if in cell that has sibling on the right', () => {
  63. setData( model, modelTable( [
  64. [ '00[]', '01' ]
  65. ] ) );
  66. expect( command.isEnabled ).to.be.true;
  67. } );
  68. it( 'should be false if last cell of a row', () => {
  69. setData( model, modelTable( [
  70. [ '00', '01[]' ]
  71. ] ) );
  72. expect( command.isEnabled ).to.be.false;
  73. } );
  74. it( 'should be true if in a cell that has sibling on the right with the same rowspan', () => {
  75. setData( model, modelTable( [
  76. [ { rowspan: 2, contents: '00[]' }, { rowspan: 2, contents: '01' } ]
  77. ] ) );
  78. expect( command.isEnabled ).to.be.true;
  79. } );
  80. it( 'should be false if in a cell that has sibling but with different rowspan', () => {
  81. setData( model, modelTable( [
  82. [ { rowspan: 2, contents: '00[]' }, { rowspan: 3, contents: '01' } ]
  83. ] ) );
  84. expect( command.isEnabled ).to.be.false;
  85. } );
  86. it( 'should be false if not in a cell', () => {
  87. setData( model, '<p>11[]</p>' );
  88. expect( command.isEnabled ).to.be.false;
  89. } );
  90. } );
  91. describe( 'value', () => {
  92. it( 'should be set to mergeable sibling if in cell that has sibling on the right', () => {
  93. setData( model, modelTable( [
  94. [ '00[]', '01' ]
  95. ] ) );
  96. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 1 ] ) );
  97. } );
  98. it( 'should be undefined if last cell of a row', () => {
  99. setData( model, modelTable( [
  100. [ '00', '01[]' ]
  101. ] ) );
  102. expect( command.value ).to.be.undefined;
  103. } );
  104. it( 'should be set to mergeable sibling if in a cell that has sibling on the right with the same rowspan', () => {
  105. setData( model, modelTable( [
  106. [ { rowspan: 2, contents: '00[]' }, { rowspan: 2, contents: '01' } ]
  107. ] ) );
  108. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 1 ] ) );
  109. } );
  110. it( 'should be undefined if in a cell that has sibling but with different rowspan', () => {
  111. setData( model, modelTable( [
  112. [ { rowspan: 2, contents: '00[]' }, { rowspan: 3, contents: '01' } ]
  113. ] ) );
  114. expect( command.value ).to.be.undefined;
  115. } );
  116. it( 'should be undefined if not in a cell', () => {
  117. setData( model, '<p>11[]</p>' );
  118. expect( command.value ).to.be.undefined;
  119. } );
  120. } );
  121. describe( 'execute()', () => {
  122. it( 'should merge table cells ', () => {
  123. setData( model, modelTable( [
  124. [ '[]00', '01' ]
  125. ] ) );
  126. command.execute();
  127. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  128. [ { colspan: 2, contents: '[]0001' } ]
  129. ] ) );
  130. } );
  131. } );
  132. } );
  133. describe( 'direction=left', () => {
  134. beforeEach( () => {
  135. command = new MergeCellCommand( editor, { direction: 'left' } );
  136. } );
  137. describe( 'isEnabled', () => {
  138. it( 'should be true if in cell that has sibling on the left', () => {
  139. setData( model, modelTable( [
  140. [ '00', '01[]' ]
  141. ] ) );
  142. expect( command.isEnabled ).to.be.true;
  143. } );
  144. it( 'should be false if first cell of a row', () => {
  145. setData( model, modelTable( [
  146. [ '00[]', '01' ]
  147. ] ) );
  148. expect( command.isEnabled ).to.be.false;
  149. } );
  150. it( 'should be true if in a cell that has sibling on the left with the same rowspan', () => {
  151. setData( model, modelTable( [
  152. [ { rowspan: 2, contents: '00' }, { rowspan: 2, contents: '01[]' } ]
  153. ] ) );
  154. expect( command.isEnabled ).to.be.true;
  155. } );
  156. it( 'should be false if in a cell that has sibling but with different rowspan', () => {
  157. setData( model, modelTable( [
  158. [ { rowspan: 2, contents: '00' }, { rowspan: 3, contents: '01[]' } ]
  159. ] ) );
  160. expect( command.isEnabled ).to.be.false;
  161. } );
  162. it( 'should be false if not in a cell', () => {
  163. setData( model, '<p>11[]</p>' );
  164. expect( command.isEnabled ).to.be.false;
  165. } );
  166. } );
  167. describe( 'value', () => {
  168. it( 'should be set to mergeable sibling if in cell that has sibling on the left', () => {
  169. setData( model, modelTable( [
  170. [ '00', '01[]' ]
  171. ] ) );
  172. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  173. } );
  174. it( 'should be undefined if first cell of a row', () => {
  175. setData( model, modelTable( [
  176. [ '00[]', '01' ]
  177. ] ) );
  178. expect( command.value ).to.be.undefined;
  179. } );
  180. it( 'should be set to mergeable sibling if in a cell that has sibling on the left with the same rowspan', () => {
  181. setData( model, modelTable( [
  182. [ { rowspan: 2, contents: '00' }, { rowspan: 2, contents: '01[]' } ]
  183. ] ) );
  184. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  185. } );
  186. it( 'should be undefined if in a cell that has sibling but with different rowspan', () => {
  187. setData( model, modelTable( [
  188. [ { rowspan: 2, contents: '00' }, { rowspan: 3, contents: '01[]' } ]
  189. ] ) );
  190. expect( command.value ).to.be.undefined;
  191. } );
  192. it( 'should be undefined if not in a cell', () => {
  193. setData( model, '<p>11[]</p>' );
  194. expect( command.value ).to.be.undefined;
  195. } );
  196. } );
  197. describe( 'execute()', () => {
  198. it( 'should merge table cells ', () => {
  199. setData( model, modelTable( [
  200. [ '00', '[]01' ]
  201. ] ) );
  202. command.execute();
  203. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  204. [ { colspan: 2, contents: '00[]01' } ]
  205. ] ) );
  206. } );
  207. } );
  208. } );
  209. } );