tablecellborderstylecommand.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 TableCellBorderStyleCommand from '../../../src/tablecellproperties/commands/tablecellborderstylecommand';
  11. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  12. describe( 'table cell properties', () => {
  13. describe( 'commands', () => {
  14. describe( 'TableCellBorderStyleCommand', () => {
  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 TableCellBorderStyleCommand( 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 is selection has 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 borderStyle property', () => {
  60. setData( model, modelTable( [ [ '[]foo' ] ] ) );
  61. expect( command.value ).to.be.undefined;
  62. } );
  63. it( 'should be set if selected table cell has borderStyle property (single string)', () => {
  64. setData( model, modelTable( [ [ { borderStyle: 'ridge', contents: '[]foo' } ] ] ) );
  65. expect( command.value ).to.equal( 'ridge' );
  66. } );
  67. it( 'should be set if selected table cell has borderStyle property object with same values', () => {
  68. setTableCellWithObjectAttributes( model, {
  69. borderStyle: {
  70. top: 'ridge',
  71. right: 'ridge',
  72. bottom: 'ridge',
  73. left: 'ridge'
  74. }
  75. }, '[]foo' );
  76. expect( command.value ).to.equal( 'ridge' );
  77. } );
  78. it( 'should be undefined if selected table cell has borderStyle property object with different values', () => {
  79. setTableCellWithObjectAttributes( model, {
  80. borderStyle: {
  81. top: 'ridge',
  82. right: 'dashed',
  83. bottom: 'ridge',
  84. left: 'ridge'
  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( [ [ { borderStyle: 'ridge', contents: 'f[o]o' } ] ] ) );
  97. expect( command.value ).to.equal( 'ridge' );
  98. } );
  99. } );
  100. describe( 'multi-cell selection', () => {
  101. it( 'should be undefined if no table cell has a borderStyle 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 cell has a borderStyle property', () => {
  115. setData( model, modelTable( [
  116. [
  117. { contents: '00', isSelected: true, borderStyle: 'solid' },
  118. { contents: '01', isSelected: true }
  119. ],
  120. [
  121. '10',
  122. { contents: '11', isSelected: true, borderStyle: 'solid' }
  123. ]
  124. ] ) );
  125. expect( command.value ).to.be.undefined;
  126. } );
  127. it( 'should be undefined if one of selected table cells has different borderStyle property value', () => {
  128. setData( model, modelTable( [
  129. [
  130. { contents: '00', isSelected: true, borderStyle: 'solid' },
  131. { contents: '01', isSelected: true, borderStyle: 'ridge' }
  132. ],
  133. [
  134. '10',
  135. { contents: '11', isSelected: true, borderStyle: 'solid' }
  136. ]
  137. ] ) );
  138. expect( command.value ).to.be.undefined;
  139. } );
  140. it( 'should be set if all table cell has the same borderStyle property value', () => {
  141. setData( model, modelTable( [
  142. [
  143. { contents: '00', isSelected: true, borderStyle: 'solid' },
  144. { contents: '01', isSelected: true, borderStyle: 'solid' }
  145. ],
  146. [
  147. '10',
  148. { contents: '11', isSelected: true, borderStyle: 'solid' }
  149. ]
  150. ] ) );
  151. expect( command.value ).to.equal( 'solid' );
  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: 'solid', batch } );
  161. sinon.assert.calledWith( spy, batch );
  162. } );
  163. describe( 'collapsed selection', () => {
  164. it( 'should set selected table cell borderStyle to a passed value', () => {
  165. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  166. command.execute( { value: 'solid' } );
  167. assertTableCellStyle( editor, 'border-bottom:solid;border-left:solid;border-right:solid;border-top:solid;' );
  168. } );
  169. it( 'should change selected table cell borderStyle to a passed value', () => {
  170. setData( model, modelTable( [ [ { borderStyle: 'ridge', contents: '[]foo' } ] ] ) );
  171. command.execute( { value: 'solid' } );
  172. assertTableCellStyle( editor, 'border-bottom:solid;border-left:solid;border-right:solid;border-top:solid;' );
  173. } );
  174. it( 'should remove borderStyle from a selected table cell if no value is passed', () => {
  175. setData( model, modelTable( [ [ { borderStyle: 'ridge', contents: '[]foo' } ] ] ) );
  176. command.execute();
  177. assertTableCellStyle( editor, '' );
  178. } );
  179. } );
  180. describe( 'non-collapsed selection', () => {
  181. it( 'should set selected table cell borderStyle to a passed value', () => {
  182. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  183. command.execute( { value: 'solid' } );
  184. assertTableCellStyle( editor, 'border-bottom:solid;border-left:solid;border-right:solid;border-top:solid;' );
  185. } );
  186. it( 'should change selected table cell borderStyle to a passed value', () => {
  187. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  188. command.execute( { value: 'solid' } );
  189. assertTableCellStyle( editor, 'border-bottom:solid;border-left:solid;border-right:solid;border-top:solid;' );
  190. } );
  191. it( 'should remove borderStyle from a selected table cell if no value is passed', () => {
  192. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  193. command.execute();
  194. assertTableCellStyle( editor, '' );
  195. } );
  196. } );
  197. describe( 'multi-cell selection', () => {
  198. beforeEach( () => {
  199. setData( model, modelTable( [
  200. [ { contents: '00', isSelected: true }, '01' ],
  201. [ '10', { contents: '11', isSelected: true } ]
  202. ] ) );
  203. } );
  204. it( 'should set selected table cell borderStyle to a passed value', () => {
  205. command.execute( { value: 'solid' } );
  206. assertEqualMarkup( editor.getData(), viewTable( [
  207. [
  208. { contents: '00', style: 'border-bottom:solid;border-left:solid;border-right:solid;border-top:solid;' },
  209. '01'
  210. ],
  211. [
  212. '10',
  213. { contents: '11', style: 'border-bottom:solid;border-left:solid;border-right:solid;border-top:solid;' }
  214. ]
  215. ] ) );
  216. } );
  217. it( 'should remove borderStyle from a selected table cell if no value is passed', () => {
  218. setData( model, modelTable( [
  219. [ { contents: '00', isSelected: true, borderStyle: 'solid' }, '01' ],
  220. [ '10', { contents: '11', isSelected: true, borderStyle: 'solid' } ]
  221. ] ) );
  222. command.execute();
  223. assertEqualMarkup( editor.getData(), viewTable( [
  224. [ '00', '01' ],
  225. [ '10', '11' ]
  226. ] ) );
  227. } );
  228. } );
  229. } );
  230. } );
  231. } );
  232. } );