indentblockcommand.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**
  2. * @license Copyright (c) 2003-2019, 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 { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import IndentBlockCommand from '../src/indentblockcommand';
  8. import IndentUsingClasses from '../src/indentcommandbehavior/indentusingclasses';
  9. import IndentUsingOffset from '../src/indentcommandbehavior/indentusingoffset';
  10. describe( 'IndentBlockCommand', () => {
  11. let editor, command, model;
  12. beforeEach( () => {
  13. return ModelTestEditor
  14. .create()
  15. .then( newEditor => {
  16. editor = newEditor;
  17. model = editor.model;
  18. model.schema.register( 'paragraph', {
  19. inheritAllFrom: '$block',
  20. allowAttributes: [ 'blockIndent' ]
  21. } );
  22. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  23. } );
  24. } );
  25. afterEach( () => {
  26. command.destroy();
  27. return editor.destroy();
  28. } );
  29. describe( 'common behavior', () => {
  30. let indentBehavior;
  31. beforeEach( () => {
  32. indentBehavior = {
  33. checkEnabled: sinon.stub().returns( true ),
  34. getNextIndent: sinon.stub()
  35. };
  36. command = new IndentBlockCommand( editor, indentBehavior );
  37. } );
  38. describe( 'execute()', () => {
  39. it( 'should be executed for all selected blocks', () => {
  40. setData( model,
  41. '<paragraph>f[oo</paragraph>' +
  42. '<paragraph>foo</paragraph>' +
  43. '<paragraph>f]oo</paragraph>'
  44. );
  45. command.execute();
  46. sinon.assert.calledThrice( indentBehavior.getNextIndent );
  47. } );
  48. it( 'should be executed only for blocks that can have indentBlock attribute', () => {
  49. setData( model,
  50. '<paragraph>f[oo</paragraph>' +
  51. '<block>foo</block>' +
  52. '<paragraph>f]oo</paragraph>'
  53. );
  54. command.execute();
  55. sinon.assert.calledTwice( indentBehavior.getNextIndent );
  56. } );
  57. } );
  58. } );
  59. describe( 'indent', () => {
  60. describe( 'using classes', () => {
  61. beforeEach( () => {
  62. command = new IndentBlockCommand( editor, new IndentUsingClasses( {
  63. classes: [
  64. 'indent-1',
  65. 'indent-2',
  66. 'indent-3',
  67. 'indent-4'
  68. ],
  69. direction: 'forward'
  70. } ) );
  71. } );
  72. describe( 'isEnabled', () => {
  73. it( 'should be false in block that does not support indent', () => {
  74. setData( model, '<block>f[]oo</block>' );
  75. expect( command.isEnabled ).to.be.false;
  76. } );
  77. it( 'should be true in non-indented block', () => {
  78. setData( model, '<paragraph>f[]oo</paragraph>' );
  79. expect( command.isEnabled ).to.be.true;
  80. } );
  81. it( 'should be true in indented block and there are still indentation classes', () => {
  82. setData( model, '<paragraph blockIndent="indent-2">f[]oo</paragraph>' );
  83. expect( command.isEnabled ).to.be.true;
  84. } );
  85. it( 'should be false in indented block in last indentation class', () => {
  86. setData( model, '<paragraph blockIndent="indent-4">f[]oo</paragraph>' );
  87. expect( command.isEnabled ).to.be.false;
  88. } );
  89. } );
  90. describe( 'execute()', () => {
  91. it( 'should set first indent class for non-indented block', () => {
  92. setData( model, '<paragraph>f[]oo</paragraph>' );
  93. command.execute();
  94. expect( getData( model ) ).to.equal( '<paragraph blockIndent="indent-1">f[]oo</paragraph>' );
  95. } );
  96. it( 'should set next indent class for indented block', () => {
  97. setData( model, '<paragraph blockIndent="indent-2">f[]oo</paragraph>' );
  98. command.execute();
  99. expect( getData( model ) ).to.equal( '<paragraph blockIndent="indent-3">f[]oo</paragraph>' );
  100. } );
  101. } );
  102. } );
  103. describe( 'using offset', () => {
  104. beforeEach( () => {
  105. command = new IndentBlockCommand( editor, new IndentUsingOffset( {
  106. offset: 50,
  107. unit: 'px',
  108. direction: 'forward'
  109. } ) );
  110. } );
  111. describe( 'isEnabled', () => {
  112. it( 'should be false in block that does not support indent', () => {
  113. setData( model, '<block>f[]oo</block>' );
  114. expect( command.isEnabled ).to.be.false;
  115. } );
  116. it( 'should be true in non-indented block', () => {
  117. setData( model, '<paragraph>f[]oo</paragraph>' );
  118. expect( command.isEnabled ).to.be.true;
  119. } );
  120. it( 'should be true in indented block', () => {
  121. setData( model, '<paragraph blockIndent="50px">f[]oo</paragraph>' );
  122. expect( command.isEnabled ).to.be.true;
  123. } );
  124. it( 'should be true in indented block with different unit', () => {
  125. setData( model, '<paragraph blockIndent="2em">f[]oo</paragraph>' );
  126. expect( command.isEnabled ).to.be.true;
  127. } );
  128. } );
  129. describe( 'execute()', () => {
  130. it( 'should set first offset for non-indented block', () => {
  131. setData( model, '<paragraph>f[]oo</paragraph>' );
  132. command.execute();
  133. expect( getData( model ) ).to.equal( '<paragraph blockIndent="50px">f[]oo</paragraph>' );
  134. } );
  135. it( 'should calculate next offset for indented block', () => {
  136. setData( model, '<paragraph blockIndent="100px">f[]oo</paragraph>' );
  137. command.execute();
  138. expect( getData( model ) ).to.equal( '<paragraph blockIndent="150px">f[]oo</paragraph>' );
  139. } );
  140. it( 'should calculate next offset for indented block even if current indent is not tied to offset', () => {
  141. setData( model, '<paragraph blockIndent="27px">f[]oo</paragraph>' );
  142. command.execute();
  143. expect( getData( model ) ).to.equal( '<paragraph blockIndent="77px">f[]oo</paragraph>' );
  144. } );
  145. it( 'should set first offset if current indent has different unit', () => {
  146. setData( model, '<paragraph blockIndent="3mm">f[]oo</paragraph>' );
  147. command.execute();
  148. expect( getData( model ) ).to.equal( '<paragraph blockIndent="50px">f[]oo</paragraph>' );
  149. } );
  150. } );
  151. } );
  152. } );
  153. describe( 'outdent', () => {
  154. describe( 'using classes', () => {
  155. beforeEach( () => {
  156. command = new IndentBlockCommand( editor, new IndentUsingClasses( {
  157. classes: [
  158. 'indent-1',
  159. 'indent-2',
  160. 'indent-3',
  161. 'indent-4'
  162. ],
  163. direction: 'backward'
  164. } ) );
  165. } );
  166. describe( 'isEnabled', () => {
  167. it( 'should be false in block that does not support indent', () => {
  168. setData( model, '<block>f[]oo</block>' );
  169. expect( command.isEnabled ).to.be.false;
  170. } );
  171. it( 'should be false in non-indented block', () => {
  172. setData( model, '<paragraph>f[]oo</paragraph>' );
  173. expect( command.isEnabled ).to.be.false;
  174. } );
  175. it( 'should be true in indented block onf first indentation class', () => {
  176. setData( model, '<paragraph blockIndent="indent-1">f[]oo</paragraph>' );
  177. expect( command.isEnabled ).to.be.true;
  178. } );
  179. it( 'should be true in indented block and there are still indentation classes', () => {
  180. setData( model, '<paragraph blockIndent="indent-2">f[]oo</paragraph>' );
  181. expect( command.isEnabled ).to.be.true;
  182. } );
  183. it( 'should be true in indented block in last indentation class', () => {
  184. setData( model, '<paragraph blockIndent="indent-4">f[]oo</paragraph>' );
  185. expect( command.isEnabled ).to.be.true;
  186. } );
  187. } );
  188. describe( 'execute()', () => {
  189. it( 'should set previous indent class for indented block', () => {
  190. setData( model, '<paragraph blockIndent="indent-2">f[]oo</paragraph>' );
  191. command.execute();
  192. expect( getData( model ) ).to.equal( '<paragraph blockIndent="indent-1">f[]oo</paragraph>' );
  193. } );
  194. } );
  195. } );
  196. describe( 'using offset', () => {
  197. beforeEach( () => {
  198. command = new IndentBlockCommand( editor, new IndentUsingOffset( {
  199. offset: 50,
  200. unit: 'px',
  201. direction: 'backward'
  202. } ) );
  203. } );
  204. describe( 'isEnabled', () => {
  205. it( 'should be false in block that does not support indent', () => {
  206. setData( model, '<block>f[]oo</block>' );
  207. expect( command.isEnabled ).to.be.false;
  208. } );
  209. it( 'should be false in non-indented block', () => {
  210. setData( model, '<paragraph>f[]oo</paragraph>' );
  211. expect( command.isEnabled ).to.be.false;
  212. } );
  213. it( 'should be true in indented block', () => {
  214. setData( model, '<paragraph blockIndent="50px">f[]oo</paragraph>' );
  215. expect( command.isEnabled ).to.be.true;
  216. } );
  217. it( 'should be true in indented block with different unit', () => {
  218. setData( model, '<paragraph blockIndent="2em">f[]oo</paragraph>' );
  219. expect( command.isEnabled ).to.be.true;
  220. } );
  221. } );
  222. describe( 'execute()', () => {
  223. it( 'should set remove offset if indent is on first offset', () => {
  224. setData( model, '<paragraph blockIndent="50px">f[]oo</paragraph>' );
  225. command.execute();
  226. expect( getData( model ) ).to.equal( '<paragraph>f[]oo</paragraph>' );
  227. } );
  228. it( 'should calculate next offset for indented block', () => {
  229. setData( model, '<paragraph blockIndent="100px">f[]oo</paragraph>' );
  230. command.execute();
  231. expect( getData( model ) ).to.equal( '<paragraph blockIndent="50px">f[]oo</paragraph>' );
  232. } );
  233. it( 'should calculate next offset for indented block even if current indent is not tied to offset', () => {
  234. setData( model, '<paragraph blockIndent="92px">f[]oo</paragraph>' );
  235. command.execute();
  236. expect( getData( model ) ).to.equal( '<paragraph blockIndent="42px">f[]oo</paragraph>' );
  237. } );
  238. it( 'should remove offset if current indent has different unit', () => {
  239. setData( model, '<paragraph blockIndent="3mm">f[]oo</paragraph>' );
  240. command.execute();
  241. expect( getData( model ) ).to.equal( '<paragraph>f[]oo</paragraph>' );
  242. } );
  243. } );
  244. } );
  245. } );
  246. } );