tablecellheightcommand.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import { assertTableCellStyle, modelTable, viewTable } from '../../_utils/utils';
  9. import TableCellPropertiesEditing from '../../../src/tablecellproperties/tablecellpropertiesediting';
  10. import TableCellHeightCommand from '../../../src/tablecellproperties/commands/tablecellheightcommand';
  11. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  12. describe( 'table cell properties', () => {
  13. describe( 'commands', () => {
  14. describe( 'TableCellHeightCommand', () => {
  15. let editor, model, command;
  16. beforeEach( async () => {
  17. editor = await ModelTestEditor.create( {
  18. plugins: [ Paragraph, TableCellPropertiesEditing ]
  19. } );
  20. model = editor.model;
  21. command = new TableCellHeightCommand( editor );
  22. } );
  23. afterEach( () => {
  24. return editor.destroy();
  25. } );
  26. describe( 'isEnabled', () => {
  27. describe( 'collapsed selection', () => {
  28. it( 'should be false if selection does not have table cell', () => {
  29. setData( model, '<paragraph>foo[]</paragraph>' );
  30. expect( command.isEnabled ).to.be.false;
  31. } );
  32. it( 'should be true is selection has table cell', () => {
  33. setData( model, modelTable( [ [ '[]foo' ] ] ) );
  34. expect( command.isEnabled ).to.be.true;
  35. } );
  36. } );
  37. describe( 'non-collapsed selection', () => {
  38. it( 'should be false if selection does not have table cell', () => {
  39. setData( model, '<paragraph>f[oo]</paragraph>' );
  40. expect( command.isEnabled ).to.be.false;
  41. } );
  42. it( 'should be true is selection has table cell', () => {
  43. setData( model, modelTable( [ [ 'f[o]o' ] ] ) );
  44. expect( command.isEnabled ).to.be.true;
  45. } );
  46. } );
  47. describe( 'multi-cell selection', () => {
  48. it( 'should be true if the selection contains some table cells', () => {
  49. setData( model, modelTable( [
  50. [ { contents: '00', isSelected: true }, '01' ],
  51. [ '10', { contents: '11', isSelected: true } ]
  52. ] ) );
  53. expect( command.isEnabled ).to.be.true;
  54. } );
  55. } );
  56. } );
  57. describe( 'value', () => {
  58. describe( 'collapsed selection', () => {
  59. it( 'should be undefined if selected table cell has no height property', () => {
  60. setData( model, modelTable( [ [ '[]foo' ] ] ) );
  61. expect( command.value ).to.be.undefined;
  62. } );
  63. it( 'should be set if selected table cell has height property', () => {
  64. setData( model, modelTable( [ [ { height: '100px', contents: '[]foo' } ] ] ) );
  65. expect( command.value ).to.equal( '100px' );
  66. } );
  67. } );
  68. describe( 'non-collapsed selection', () => {
  69. it( 'should be false if selection does not have table cell', () => {
  70. setData( model, '<paragraph>f[oo]</paragraph>' );
  71. expect( command.value ).to.be.undefined;
  72. } );
  73. it( 'should be true is selection has table cell', () => {
  74. setData( model, modelTable( [ [ { height: '100px', contents: 'f[o]o' } ] ] ) );
  75. expect( command.value ).to.equal( '100px' );
  76. } );
  77. } );
  78. describe( 'multi-cell selection', () => {
  79. it( 'should be undefined if no table cell have the "height" property', () => {
  80. setData( model, modelTable( [
  81. [
  82. { contents: '00', isSelected: true },
  83. { contents: '01', isSelected: true }
  84. ],
  85. [
  86. '10',
  87. { contents: '11', isSelected: true }
  88. ]
  89. ] ) );
  90. expect( command.value ).to.be.undefined;
  91. } );
  92. it( 'should be undefined if only some table cells have the "height" property', () => {
  93. setData( model, modelTable( [
  94. [
  95. { contents: '00', isSelected: true, height: '100px' },
  96. { contents: '01', isSelected: true }
  97. ],
  98. [
  99. '10',
  100. { contents: '11', isSelected: true, height: '100px' }
  101. ]
  102. ] ) );
  103. expect( command.value ).to.be.undefined;
  104. } );
  105. it( 'should be undefined if one of selected table cells has a different "height" property value', () => {
  106. setData( model, modelTable( [
  107. [
  108. { contents: '00', isSelected: true, height: '100px' },
  109. { contents: '01', isSelected: true, height: '23px' }
  110. ],
  111. [
  112. '10',
  113. { contents: '11', isSelected: true, height: '100px' }
  114. ]
  115. ] ) );
  116. expect( command.value ).to.be.undefined;
  117. } );
  118. it( 'should be set if all table cell have the same "height" property value', () => {
  119. setData( model, modelTable( [
  120. [
  121. { contents: '00', isSelected: true, height: '100px' },
  122. { contents: '01', isSelected: true, height: '100px' }
  123. ],
  124. [
  125. '10',
  126. { contents: '11', isSelected: true, height: '100px' }
  127. ]
  128. ] ) );
  129. expect( command.value ).to.equal( '100px' );
  130. } );
  131. } );
  132. } );
  133. describe( 'execute()', () => {
  134. it( 'should use provided batch', () => {
  135. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  136. const batch = model.createBatch();
  137. const spy = sinon.spy( model, 'enqueueChange' );
  138. command.execute( { value: '25px', batch } );
  139. sinon.assert.calledWith( spy, batch );
  140. } );
  141. it( 'should add default unit for numeric values (number passed)', () => {
  142. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  143. command.execute( { value: 25 } );
  144. assertTableCellStyle( editor, 'height:25px;' );
  145. } );
  146. it( 'should add default unit for numeric values (string passed)', () => {
  147. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  148. command.execute( { value: 25 } );
  149. assertTableCellStyle( editor, 'height:25px;' );
  150. } );
  151. it( 'should not add default unit for numeric values with unit', () => {
  152. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  153. command.execute( { value: '25pt' } );
  154. assertTableCellStyle( editor, 'height:25pt;' );
  155. } );
  156. it( 'should add default unit to floats (number passed)', () => {
  157. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  158. command.execute( { value: 25.1 } );
  159. assertTableCellStyle( editor, 'height:25.1px;' );
  160. } );
  161. it( 'should add default unit to floats (string passed)', () => {
  162. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  163. command.execute( { value: '0.1' } );
  164. assertTableCellStyle( editor, 'height:0.1px;' );
  165. } );
  166. it( 'should pass invalid values', () => {
  167. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  168. command.execute( { value: 'bar' } );
  169. assertTableCellStyle( editor, 'height:bar;' );
  170. } );
  171. it( 'should pass invalid value (string passed, CSS float without leading 0)', () => {
  172. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  173. command.execute( { value: '.2' } );
  174. assertTableCellStyle( editor, 'height:.2;' );
  175. } );
  176. describe( 'collapsed selection', () => {
  177. it( 'should set selected table cell height to a passed value', () => {
  178. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  179. command.execute( { value: '25px' } );
  180. assertTableCellStyle( editor, 'height:25px;' );
  181. } );
  182. it( 'should change selected table cell height to a passed value', () => {
  183. setData( model, modelTable( [ [ { height: '100px', contents: '[]foo' } ] ] ) );
  184. command.execute( { value: '25px' } );
  185. assertTableCellStyle( editor, 'height:25px;' );
  186. } );
  187. it( 'should remove height from a selected table cell if no value is passed', () => {
  188. setData( model, modelTable( [ [ { height: '100px', contents: '[]foo' } ] ] ) );
  189. command.execute();
  190. assertTableCellStyle( editor, '' );
  191. } );
  192. } );
  193. describe( 'non-collapsed selection', () => {
  194. it( 'should set selected table cell height to a passed value', () => {
  195. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  196. command.execute( { value: '25px' } );
  197. assertTableCellStyle( editor, 'height:25px;' );
  198. } );
  199. it( 'should change selected table cell height to a passed value', () => {
  200. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  201. command.execute( { value: '25px' } );
  202. assertTableCellStyle( editor, 'height:25px;' );
  203. } );
  204. it( 'should remove height from a selected table cell if no value is passed', () => {
  205. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  206. command.execute();
  207. assertTableCellStyle( editor, '' );
  208. } );
  209. } );
  210. describe( 'multi-cell selection', () => {
  211. beforeEach( () => {
  212. setData( model, modelTable( [
  213. [ { contents: '00', isSelected: true }, '01' ],
  214. [ '10', { contents: '11', isSelected: true } ]
  215. ] ) );
  216. } );
  217. it( 'should set the "height" attribute value of selected table cells', () => {
  218. command.execute( { value: '100px' } );
  219. assertEqualMarkup( editor.getData(), viewTable( [
  220. [ { contents: '00', style: 'height:100px;' }, '01' ],
  221. [ '10', { contents: '11', style: 'height:100px;' } ]
  222. ] ) );
  223. } );
  224. it( 'should remove "height" from selected table cells if no value is passed', () => {
  225. setData( model, modelTable( [
  226. [ { contents: '00', isSelected: true, height: '100px' }, '01' ],
  227. [ '10', { contents: '11', isSelected: true, height: '100px' } ]
  228. ] ) );
  229. command.execute();
  230. assertEqualMarkup( editor.getData(), viewTable( [
  231. [ '00', '01' ],
  232. [ '10', '11' ]
  233. ] ) );
  234. } );
  235. } );
  236. } );
  237. } );
  238. } );
  239. } );