8
0

indentblockcommand.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 { 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( 'parentBlock', {
  19. allowWhere: '$block',
  20. isLimit: true,
  21. isObject: true,
  22. isBlock: true
  23. } );
  24. model.schema.register( 'paragraph', {
  25. inheritAllFrom: '$block',
  26. allowAttributes: [ 'blockIndent' ],
  27. allowIn: 'parentBlock'
  28. } );
  29. model.schema.register( 'block', {
  30. inheritAllFrom: '$block'
  31. } );
  32. } );
  33. } );
  34. afterEach( () => {
  35. command.destroy();
  36. return editor.destroy();
  37. } );
  38. describe( 'common behavior', () => {
  39. let indentBehavior;
  40. beforeEach( () => {
  41. indentBehavior = {
  42. checkEnabled: sinon.stub().returns( true ),
  43. getNextIndent: sinon.stub()
  44. };
  45. command = new IndentBlockCommand( editor, indentBehavior );
  46. } );
  47. describe( 'refresh()', () => {
  48. it( 'should be disabled if a top-most block disallows indentBlock attribute', () => {
  49. setData( model, '[<parentBlock><paragraph>foo</paragraph></parentBlock>]' );
  50. command.refresh();
  51. expect( command.isEnabled ).to.be.false;
  52. } );
  53. } );
  54. describe( 'execute()', () => {
  55. it( 'should be executed for all selected blocks', () => {
  56. setData( model,
  57. '<paragraph>f[oo</paragraph>' +
  58. '<paragraph>foo</paragraph>' +
  59. '<paragraph>f]oo</paragraph>'
  60. );
  61. command.execute();
  62. sinon.assert.calledThrice( indentBehavior.getNextIndent );
  63. } );
  64. it( 'should be executed only for blocks that can have indentBlock attribute', () => {
  65. setData( model,
  66. '<paragraph>f[oo</paragraph>' +
  67. '<block>foo</block>' +
  68. '<paragraph>f]oo</paragraph>'
  69. );
  70. command.execute();
  71. sinon.assert.calledTwice( indentBehavior.getNextIndent );
  72. } );
  73. it( 'should be executed only for top-most blocks that can have indentBlock attribute', () => {
  74. setData( model,
  75. '<paragraph>f[oo</paragraph>' +
  76. '<parentBlock><paragraph>foo</paragraph><paragraph>foo</paragraph></parentBlock>' +
  77. '<paragraph>f]oo</paragraph>'
  78. );
  79. command.execute();
  80. sinon.assert.calledTwice( indentBehavior.getNextIndent );
  81. } );
  82. } );
  83. } );
  84. describe( 'indent', () => {
  85. describe( 'using classes', () => {
  86. beforeEach( () => {
  87. command = new IndentBlockCommand( editor, new IndentUsingClasses( {
  88. classes: [
  89. 'indent-1',
  90. 'indent-2',
  91. 'indent-3',
  92. 'indent-4'
  93. ],
  94. direction: 'forward'
  95. } ) );
  96. } );
  97. describe( 'isEnabled', () => {
  98. it( 'should be false in block that does not support indent', () => {
  99. setData( model, '<block>f[]oo</block>' );
  100. expect( command.isEnabled ).to.be.false;
  101. } );
  102. it( 'should be true in non-indented block', () => {
  103. setData( model, '<paragraph>f[]oo</paragraph>' );
  104. expect( command.isEnabled ).to.be.true;
  105. } );
  106. it( 'should be true in indented block and there are still indentation classes', () => {
  107. setData( model, '<paragraph blockIndent="indent-2">f[]oo</paragraph>' );
  108. expect( command.isEnabled ).to.be.true;
  109. } );
  110. it( 'should be false in indented block in last indentation class', () => {
  111. setData( model, '<paragraph blockIndent="indent-4">f[]oo</paragraph>' );
  112. expect( command.isEnabled ).to.be.false;
  113. } );
  114. } );
  115. describe( 'execute()', () => {
  116. it( 'should set first indent class for non-indented block', () => {
  117. setData( model, '<paragraph>f[]oo</paragraph>' );
  118. command.execute();
  119. expect( getData( model ) ).to.equal( '<paragraph blockIndent="indent-1">f[]oo</paragraph>' );
  120. } );
  121. it( 'should set next indent class for indented block', () => {
  122. setData( model, '<paragraph blockIndent="indent-2">f[]oo</paragraph>' );
  123. command.execute();
  124. expect( getData( model ) ).to.equal( '<paragraph blockIndent="indent-3">f[]oo</paragraph>' );
  125. } );
  126. it( 'should be executed only for top-most blocks that can have indentBlock attribute', () => {
  127. setData( model,
  128. '<paragraph>f[oo</paragraph>' +
  129. '<parentBlock><paragraph>foo</paragraph><paragraph>foo</paragraph></parentBlock>' +
  130. '<paragraph>f]oo</paragraph>'
  131. );
  132. command.execute();
  133. expect( getData( model ) ).to.equal(
  134. '<paragraph blockIndent="indent-1">f[oo</paragraph>' +
  135. '<parentBlock><paragraph>foo</paragraph><paragraph>foo</paragraph></parentBlock>' +
  136. '<paragraph blockIndent="indent-1">f]oo</paragraph>'
  137. );
  138. } );
  139. } );
  140. } );
  141. describe( 'using offset', () => {
  142. beforeEach( () => {
  143. command = new IndentBlockCommand( editor, new IndentUsingOffset( {
  144. offset: 50,
  145. unit: 'px',
  146. direction: 'forward'
  147. } ) );
  148. } );
  149. describe( 'isEnabled', () => {
  150. it( 'should be false in block that does not support indent', () => {
  151. setData( model, '<block>f[]oo</block>' );
  152. expect( command.isEnabled ).to.be.false;
  153. } );
  154. it( 'should be true in non-indented block', () => {
  155. setData( model, '<paragraph>f[]oo</paragraph>' );
  156. expect( command.isEnabled ).to.be.true;
  157. } );
  158. it( 'should be true in indented block', () => {
  159. setData( model, '<paragraph blockIndent="50px">f[]oo</paragraph>' );
  160. expect( command.isEnabled ).to.be.true;
  161. } );
  162. it( 'should be true in indented block with different unit', () => {
  163. setData( model, '<paragraph blockIndent="2em">f[]oo</paragraph>' );
  164. expect( command.isEnabled ).to.be.true;
  165. } );
  166. } );
  167. describe( 'execute()', () => {
  168. it( 'should set first offset for non-indented block', () => {
  169. setData( model, '<paragraph>f[]oo</paragraph>' );
  170. command.execute();
  171. expect( getData( model ) ).to.equal( '<paragraph blockIndent="50px">f[]oo</paragraph>' );
  172. } );
  173. it( 'should calculate next offset for indented block', () => {
  174. setData( model, '<paragraph blockIndent="100px">f[]oo</paragraph>' );
  175. command.execute();
  176. expect( getData( model ) ).to.equal( '<paragraph blockIndent="150px">f[]oo</paragraph>' );
  177. } );
  178. it( 'should calculate next offset for indented block even if current indent is not tied to offset', () => {
  179. setData( model, '<paragraph blockIndent="27px">f[]oo</paragraph>' );
  180. command.execute();
  181. expect( getData( model ) ).to.equal( '<paragraph blockIndent="77px">f[]oo</paragraph>' );
  182. } );
  183. it( 'should set first offset if current indent has different unit', () => {
  184. setData( model, '<paragraph blockIndent="3mm">f[]oo</paragraph>' );
  185. command.execute();
  186. expect( getData( model ) ).to.equal( '<paragraph blockIndent="50px">f[]oo</paragraph>' );
  187. } );
  188. } );
  189. } );
  190. } );
  191. describe( 'outdent', () => {
  192. describe( 'using classes', () => {
  193. beforeEach( () => {
  194. command = new IndentBlockCommand( editor, new IndentUsingClasses( {
  195. classes: [
  196. 'indent-1',
  197. 'indent-2',
  198. 'indent-3',
  199. 'indent-4'
  200. ],
  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 onf first indentation class', () => {
  214. setData( model, '<paragraph blockIndent="indent-1">f[]oo</paragraph>' );
  215. expect( command.isEnabled ).to.be.true;
  216. } );
  217. it( 'should be true in indented block and there are still indentation classes', () => {
  218. setData( model, '<paragraph blockIndent="indent-2">f[]oo</paragraph>' );
  219. expect( command.isEnabled ).to.be.true;
  220. } );
  221. it( 'should be true in indented block in last indentation class', () => {
  222. setData( model, '<paragraph blockIndent="indent-4">f[]oo</paragraph>' );
  223. expect( command.isEnabled ).to.be.true;
  224. } );
  225. } );
  226. describe( 'execute()', () => {
  227. it( 'should set previous indent class for indented block', () => {
  228. setData( model, '<paragraph blockIndent="indent-2">f[]oo</paragraph>' );
  229. command.execute();
  230. expect( getData( model ) ).to.equal( '<paragraph blockIndent="indent-1">f[]oo</paragraph>' );
  231. } );
  232. } );
  233. } );
  234. describe( 'using offset', () => {
  235. beforeEach( () => {
  236. command = new IndentBlockCommand( editor, new IndentUsingOffset( {
  237. offset: 50,
  238. unit: 'px',
  239. direction: 'backward'
  240. } ) );
  241. } );
  242. describe( 'isEnabled', () => {
  243. it( 'should be false in block that does not support indent', () => {
  244. setData( model, '<block>f[]oo</block>' );
  245. expect( command.isEnabled ).to.be.false;
  246. } );
  247. it( 'should be false in non-indented block', () => {
  248. setData( model, '<paragraph>f[]oo</paragraph>' );
  249. expect( command.isEnabled ).to.be.false;
  250. } );
  251. it( 'should be true in indented block', () => {
  252. setData( model, '<paragraph blockIndent="50px">f[]oo</paragraph>' );
  253. expect( command.isEnabled ).to.be.true;
  254. } );
  255. it( 'should be true in indented block with different unit', () => {
  256. setData( model, '<paragraph blockIndent="2em">f[]oo</paragraph>' );
  257. expect( command.isEnabled ).to.be.true;
  258. } );
  259. } );
  260. describe( 'execute()', () => {
  261. it( 'should set remove offset if indent is on first offset', () => {
  262. setData( model, '<paragraph blockIndent="50px">f[]oo</paragraph>' );
  263. command.execute();
  264. expect( getData( model ) ).to.equal( '<paragraph>f[]oo</paragraph>' );
  265. } );
  266. it( 'should calculate next offset for indented block', () => {
  267. setData( model, '<paragraph blockIndent="100px">f[]oo</paragraph>' );
  268. command.execute();
  269. expect( getData( model ) ).to.equal( '<paragraph blockIndent="50px">f[]oo</paragraph>' );
  270. } );
  271. it( 'should calculate next offset for indented block even if current indent is not tied to offset', () => {
  272. setData( model, '<paragraph blockIndent="92px">f[]oo</paragraph>' );
  273. command.execute();
  274. expect( getData( model ) ).to.equal( '<paragraph blockIndent="42px">f[]oo</paragraph>' );
  275. } );
  276. it( 'should remove offset if current indent has different unit', () => {
  277. setData( model, '<paragraph blockIndent="3mm">f[]oo</paragraph>' );
  278. command.execute();
  279. expect( getData( model ) ).to.equal( '<paragraph>f[]oo</paragraph>' );
  280. } );
  281. } );
  282. } );
  283. } );
  284. } );