8
0

tablecellpaddingcommand.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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, setTableCellWithObjectAttributes, viewTable } from '../../_utils/utils';
  9. import TableCellPropertiesEditing from '../../../src/tablecellproperties/tablecellpropertiesediting';
  10. import TableCellPaddingCommand from '../../../src/tablecellproperties/commands/tablecellpaddingcommand';
  11. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  12. describe( 'table cell properties', () => {
  13. describe( 'commands', () => {
  14. describe( 'TableCellPaddingCommand', () => {
  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 TableCellPaddingCommand( 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 padding property', () => {
  60. setData( model, modelTable( [ [ '[]foo' ] ] ) );
  61. expect( command.value ).to.be.undefined;
  62. } );
  63. it( 'should be set if selected table cell has padding property (single string)', () => {
  64. setData( model, modelTable( [ [ { padding: '2em', contents: '[]foo' } ] ] ) );
  65. expect( command.value ).to.equal( '2em' );
  66. } );
  67. it( 'should be set if selected table cell has padding property object with same values', () => {
  68. setTableCellWithObjectAttributes( model, {
  69. padding: {
  70. top: '2em',
  71. right: '2em',
  72. bottom: '2em',
  73. left: '2em'
  74. }
  75. }, '[]foo' );
  76. expect( command.value ).to.equal( '2em' );
  77. } );
  78. it( 'should be undefined if selected table cell has padding property object with different values', () => {
  79. setTableCellWithObjectAttributes( model, {
  80. padding: {
  81. top: '2em',
  82. right: '1px',
  83. bottom: '2em',
  84. left: '2em'
  85. }
  86. }, '[]foo' );
  87. expect( command.value ).to.be.undefined;
  88. } );
  89. } );
  90. describe( 'non-collapsed selection', () => {
  91. it( 'should be false if selection does not have table cell', () => {
  92. setData( model, '<paragraph>f[oo]</paragraph>' );
  93. expect( command.value ).to.be.undefined;
  94. } );
  95. it( 'should be true is selection has table cell', () => {
  96. setData( model, modelTable( [ [ { padding: '2em', contents: 'f[o]o' } ] ] ) );
  97. expect( command.value ).to.equal( '2em' );
  98. } );
  99. } );
  100. describe( 'multi-cell selection', () => {
  101. it( 'should be undefined if no table cells have the "padding" property', () => {
  102. setData( model, modelTable( [
  103. [
  104. { contents: '00', isSelected: true },
  105. { contents: '01', isSelected: true }
  106. ],
  107. [
  108. '10',
  109. { contents: '11', isSelected: true }
  110. ]
  111. ] ) );
  112. expect( command.value ).to.be.undefined;
  113. } );
  114. it( 'should be undefined if only some table cells have the "padding" property', () => {
  115. setData( model, modelTable( [
  116. [
  117. { contents: '00', isSelected: true, padding: '2em' },
  118. { contents: '01', isSelected: true }
  119. ],
  120. [
  121. '10',
  122. { contents: '11', isSelected: true, padding: '2em' }
  123. ]
  124. ] ) );
  125. expect( command.value ).to.be.undefined;
  126. } );
  127. it( 'should be undefined if one of selected table cells has a different "padding" property value', () => {
  128. setData( model, modelTable( [
  129. [
  130. { contents: '00', isSelected: true, padding: '2em' },
  131. { contents: '01', isSelected: true, padding: '3em' }
  132. ],
  133. [
  134. '10',
  135. { contents: '11', isSelected: true, padding: '2em' }
  136. ]
  137. ] ) );
  138. expect( command.value ).to.be.undefined;
  139. } );
  140. it( 'should be set if all table cells have the same "padding" property value', () => {
  141. setData( model, modelTable( [
  142. [
  143. { contents: '00', isSelected: true, padding: '2em' },
  144. { contents: '01', isSelected: true, padding: '2em' }
  145. ],
  146. [
  147. '10',
  148. { contents: '11', isSelected: true, padding: '2em' }
  149. ]
  150. ] ) );
  151. expect( command.value ).to.equal( '2em' );
  152. } );
  153. } );
  154. } );
  155. describe( 'execute()', () => {
  156. it( 'should use provided batch', () => {
  157. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  158. const batch = model.createBatch();
  159. const spy = sinon.spy( model, 'enqueueChange' );
  160. command.execute( { value: '0.5em', batch } );
  161. sinon.assert.calledWith( spy, batch );
  162. } );
  163. it( 'should add default unit for numeric values (number passed)', () => {
  164. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  165. command.execute( { value: 25 } );
  166. assertTableCellStyle( editor, 'padding:25px;' );
  167. } );
  168. it( 'should add default unit for numeric values (string passed)', () => {
  169. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  170. command.execute( { value: 25 } );
  171. assertTableCellStyle( editor, 'padding:25px;' );
  172. } );
  173. it( 'should not add default unit for numeric values with unit', () => {
  174. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  175. command.execute( { value: '25pt' } );
  176. assertTableCellStyle( editor, 'padding:25pt;' );
  177. } );
  178. it( 'should add default unit to floats (number passed)', () => {
  179. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  180. command.execute( { value: 25.1 } );
  181. assertTableCellStyle( editor, 'padding:25.1px;' );
  182. } );
  183. it( 'should add default unit to floats (string passed)', () => {
  184. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  185. command.execute( { value: '0.1' } );
  186. assertTableCellStyle( editor, 'padding:0.1px;' );
  187. } );
  188. it( 'should pass invalid values', () => {
  189. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  190. command.execute( { value: 'bar' } );
  191. assertTableCellStyle( editor, 'padding:bar;' );
  192. } );
  193. it( 'should pass invalid value (string passed, CSS float without leading 0)', () => {
  194. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  195. command.execute( { value: '.2' } );
  196. assertTableCellStyle( editor, 'padding:.2;' );
  197. } );
  198. describe( 'collapsed selection', () => {
  199. it( 'should set selected table cell padding to a passed value', () => {
  200. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  201. command.execute( { value: '0.5em' } );
  202. assertTableCellStyle( editor, 'padding:0.5em;' );
  203. } );
  204. it( 'should change selected table cell padding to a passed value', () => {
  205. setData( model, modelTable( [ [ { padding: '2em', contents: '[]foo' } ] ] ) );
  206. command.execute( { value: '0.5em' } );
  207. assertTableCellStyle( editor, 'padding:0.5em;' );
  208. } );
  209. it( 'should remove padding from a selected table cell if no value is passed', () => {
  210. setData( model, modelTable( [ [ { padding: '2em', contents: '[]foo' } ] ] ) );
  211. command.execute();
  212. assertTableCellStyle( editor, '' );
  213. } );
  214. } );
  215. describe( 'non-collapsed selection', () => {
  216. it( 'should set selected table cell padding to a passed value', () => {
  217. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  218. command.execute( { value: '0.5em' } );
  219. assertTableCellStyle( editor, 'padding:0.5em;' );
  220. } );
  221. it( 'should change selected table cell padding to a passed value', () => {
  222. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  223. command.execute( { value: '0.5em' } );
  224. assertTableCellStyle( editor, 'padding:0.5em;' );
  225. } );
  226. it( 'should remove padding from a selected table cell if no value is passed', () => {
  227. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  228. command.execute();
  229. assertTableCellStyle( editor, '' );
  230. } );
  231. } );
  232. describe( 'multi-cell selection', () => {
  233. beforeEach( () => {
  234. setData( model, modelTable( [
  235. [ { contents: '00', isSelected: true }, '01' ],
  236. [ '10', { contents: '11', isSelected: true } ]
  237. ] ) );
  238. } );
  239. it( 'should set the "padding" attribute value of selected table cells', () => {
  240. command.execute( { value: '25px' } );
  241. assertEqualMarkup( editor.getData(), viewTable( [
  242. [ { contents: '00', style: 'padding:25px;' }, '01' ],
  243. [ '10', { contents: '11', style: 'padding:25px;' } ]
  244. ] ) );
  245. } );
  246. it( 'should remove "padding" from selected table cells if no value is passed', () => {
  247. setData( model, modelTable( [
  248. [ { contents: '00', isSelected: true, padding: '25px' }, '01' ],
  249. [ '10', { contents: '11', isSelected: true, padding: '25px' } ]
  250. ] ) );
  251. command.execute();
  252. assertEqualMarkup( editor.getData(), viewTable( [
  253. [ '00', '01' ],
  254. [ '10', '11' ]
  255. ] ) );
  256. } );
  257. } );
  258. } );
  259. } );
  260. } );
  261. } );