indentcommand.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Editor from '@ckeditor/ckeditor5-core/src/editor/editor';
  6. import Model from '@ckeditor/ckeditor5-engine/src/model/model';
  7. import IndentCommand from '../src/indentcommand';
  8. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  9. import Position from '@ckeditor/ckeditor5-engine/src/model/position';
  10. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  11. describe( 'IndentCommand', () => {
  12. let editor, model, doc, root;
  13. beforeEach( () => {
  14. editor = new Editor();
  15. editor.model = new Model();
  16. model = editor.model;
  17. doc = model.document;
  18. root = doc.createRoot();
  19. model.schema.register( 'listItem', {
  20. inheritAllFrom: '$block',
  21. allowAttributes: [ 'listType', 'listIndent' ]
  22. } );
  23. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  24. setData(
  25. model,
  26. '<listItem listIndent="0" listType="bulleted">a</listItem>' +
  27. '<listItem listIndent="0" listType="bulleted">b</listItem>' +
  28. '<listItem listIndent="1" listType="bulleted">c</listItem>' +
  29. '<listItem listIndent="2" listType="bulleted">d</listItem>' +
  30. '<listItem listIndent="2" listType="bulleted">e</listItem>' +
  31. '<listItem listIndent="1" listType="bulleted">f</listItem>' +
  32. '<listItem listIndent="0" listType="bulleted">g</listItem>'
  33. );
  34. } );
  35. describe( 'IndentCommand', () => {
  36. let command;
  37. beforeEach( () => {
  38. command = new IndentCommand( editor, 'forward' );
  39. } );
  40. afterEach( () => {
  41. command.destroy();
  42. } );
  43. describe( 'isEnabled', () => {
  44. it( 'should be true if selection starts in list item', () => {
  45. model.change( writer => {
  46. writer.setSelection( root.getChild( 5 ), 0 );
  47. } );
  48. expect( command.isEnabled ).to.be.true;
  49. } );
  50. it( 'should be false if selection starts in first list item', () => {
  51. model.change( writer => {
  52. writer.setSelection( root.getChild( 0 ), 0 );
  53. } );
  54. expect( command.isEnabled ).to.be.false;
  55. } );
  56. // Reported in PR #53.
  57. it( 'should be false if selection starts in first list item #2', () => {
  58. setData(
  59. model,
  60. '<listItem listIndent="0" listType="bulleted">a</listItem>' +
  61. '<listItem listIndent="1" listType="bulleted">b</listItem>' +
  62. '<listItem listIndent="0" listType="bulleted">c</listItem>' +
  63. '<listItem listIndent="1" listType="bulleted">[]d</listItem>' +
  64. '<listItem listIndent="2" listType="bulleted">e</listItem>'
  65. );
  66. expect( command.isEnabled ).to.be.false;
  67. } );
  68. // Reported in PR #53.
  69. it( 'should be false if selection starts in first list item #3', () => {
  70. setData(
  71. model,
  72. '<listItem listIndent="0" listType="bulleted">a</listItem>' +
  73. '<listItem listIndent="1" listType="bulleted">b</listItem>' +
  74. '<listItem listIndent="0" listType="numbered">c</listItem>' +
  75. '<listItem listIndent="1" listType="bulleted">d</listItem>' +
  76. '<listItem listIndent="0" listType="bulleted">[]e</listItem>'
  77. );
  78. expect( command.isEnabled ).to.be.false;
  79. } );
  80. it( 'should be false if selection starts in first list item of top level list with different type than previous list', () => {
  81. setData(
  82. model,
  83. '<listItem listIndent="0" listType="bulleted">a</listItem>' +
  84. '<listItem listIndent="0" listType="numbered">[]b</listItem>'
  85. );
  86. expect( command.isEnabled ).to.be.false;
  87. } );
  88. it( 'should be false if selection starts in a list item that has bigger indent than it\'s previous sibling', () => {
  89. model.change( writer => {
  90. writer.setSelection( root.getChild( 2 ), 0 );
  91. } );
  92. expect( command.isEnabled ).to.be.false;
  93. } );
  94. // Edge case but may happen that some other blocks will also use the indent attribute
  95. // and before we fixed it the command was enabled in such a case.
  96. it( 'should be false if selection starts in a paragraph with indent attribute', () => {
  97. model.schema.extend( 'paragraph', { allowAttributes: 'listIndent' } );
  98. setData( model, '<listItem listIndent="0">a</listItem><paragraph listIndent="0">b[]</paragraph>' );
  99. expect( command.isEnabled ).to.be.false;
  100. } );
  101. } );
  102. describe( 'execute()', () => {
  103. it( 'should use parent batch', () => {
  104. model.change( writer => {
  105. writer.setSelection( root.getChild( 5 ), 0 );
  106. } );
  107. model.change( writer => {
  108. expect( writer.batch.operations.length ).to.equal( 0 );
  109. command.execute();
  110. expect( writer.batch.operations.length ).to.be.above( 0 );
  111. } );
  112. } );
  113. it( 'should increment indent attribute by 1', () => {
  114. model.change( writer => {
  115. writer.setSelection( root.getChild( 5 ), 0 );
  116. } );
  117. command.execute();
  118. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  119. '<listItem listIndent="0" listType="bulleted">a</listItem>' +
  120. '<listItem listIndent="0" listType="bulleted">b</listItem>' +
  121. '<listItem listIndent="1" listType="bulleted">c</listItem>' +
  122. '<listItem listIndent="2" listType="bulleted">d</listItem>' +
  123. '<listItem listIndent="2" listType="bulleted">e</listItem>' +
  124. '<listItem listIndent="2" listType="bulleted">f</listItem>' +
  125. '<listItem listIndent="0" listType="bulleted">g</listItem>'
  126. );
  127. } );
  128. it( 'should increment indent of all sub-items of indented item', () => {
  129. model.change( writer => {
  130. writer.setSelection( root.getChild( 1 ), 0 );
  131. } );
  132. command.execute();
  133. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  134. '<listItem listIndent="0" listType="bulleted">a</listItem>' +
  135. '<listItem listIndent="1" listType="bulleted">b</listItem>' +
  136. '<listItem listIndent="2" listType="bulleted">c</listItem>' +
  137. '<listItem listIndent="3" listType="bulleted">d</listItem>' +
  138. '<listItem listIndent="3" listType="bulleted">e</listItem>' +
  139. '<listItem listIndent="2" listType="bulleted">f</listItem>' +
  140. '<listItem listIndent="0" listType="bulleted">g</listItem>'
  141. );
  142. } );
  143. it( 'should increment indent of all selected item when multiple items are selected', () => {
  144. model.change( writer => {
  145. writer.setSelection( new Range(
  146. new Position( root.getChild( 1 ), [ 0 ] ),
  147. new Position( root.getChild( 3 ), [ 1 ] )
  148. ) );
  149. } );
  150. command.execute();
  151. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  152. '<listItem listIndent="0" listType="bulleted">a</listItem>' +
  153. '<listItem listIndent="1" listType="bulleted">b</listItem>' +
  154. '<listItem listIndent="2" listType="bulleted">c</listItem>' +
  155. '<listItem listIndent="3" listType="bulleted">d</listItem>' +
  156. '<listItem listIndent="2" listType="bulleted">e</listItem>' +
  157. '<listItem listIndent="1" listType="bulleted">f</listItem>' +
  158. '<listItem listIndent="0" listType="bulleted">g</listItem>'
  159. );
  160. } );
  161. } );
  162. } );
  163. describe( 'IndentCommand - backward (outdent)', () => {
  164. let command;
  165. beforeEach( () => {
  166. command = new IndentCommand( editor, 'backward' );
  167. } );
  168. afterEach( () => {
  169. command.destroy();
  170. } );
  171. describe( 'isEnabled', () => {
  172. it( 'should be true if selection starts in list item', () => {
  173. model.change( writer => {
  174. writer.setSelection( root.getChild( 5 ), 0 );
  175. } );
  176. expect( command.isEnabled ).to.be.true;
  177. } );
  178. it( 'should be true if selection starts in first list item', () => {
  179. // This is in contrary to forward indent command.
  180. model.change( writer => {
  181. writer.setSelection( root.getChild( 0 ), 0 );
  182. } );
  183. expect( command.isEnabled ).to.be.true;
  184. } );
  185. it( 'should be true if selection starts in a list item that has bigger indent than it\'s previous sibling', () => {
  186. // This is in contrary to forward indent command.
  187. model.change( writer => {
  188. writer.setSelection( root.getChild( 2 ), 0 );
  189. } );
  190. expect( command.isEnabled ).to.be.true;
  191. } );
  192. } );
  193. describe( 'execute()', () => {
  194. it( 'should decrement indent attribute by 1 (if it is bigger than 0)', () => {
  195. model.change( writer => {
  196. writer.setSelection( root.getChild( 5 ), 0 );
  197. } );
  198. command.execute();
  199. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  200. '<listItem listIndent="0" listType="bulleted">a</listItem>' +
  201. '<listItem listIndent="0" listType="bulleted">b</listItem>' +
  202. '<listItem listIndent="1" listType="bulleted">c</listItem>' +
  203. '<listItem listIndent="2" listType="bulleted">d</listItem>' +
  204. '<listItem listIndent="2" listType="bulleted">e</listItem>' +
  205. '<listItem listIndent="0" listType="bulleted">f</listItem>' +
  206. '<listItem listIndent="0" listType="bulleted">g</listItem>'
  207. );
  208. } );
  209. it( 'should rename listItem to paragraph (if indent is equal to 0)', () => {
  210. model.change( writer => {
  211. writer.setSelection( root.getChild( 0 ), 0 );
  212. } );
  213. command.execute();
  214. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  215. '<paragraph listIndent="0" listType="bulleted">a</paragraph>' +
  216. '<listItem listIndent="0" listType="bulleted">b</listItem>' +
  217. '<listItem listIndent="1" listType="bulleted">c</listItem>' +
  218. '<listItem listIndent="2" listType="bulleted">d</listItem>' +
  219. '<listItem listIndent="2" listType="bulleted">e</listItem>' +
  220. '<listItem listIndent="1" listType="bulleted">f</listItem>' +
  221. '<listItem listIndent="0" listType="bulleted">g</listItem>'
  222. );
  223. } );
  224. it( 'should decrement indent of all sub-items of outdented item', () => {
  225. model.change( writer => {
  226. writer.setSelection( root.getChild( 1 ), 0 );
  227. } );
  228. command.execute();
  229. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  230. '<listItem listIndent="0" listType="bulleted">a</listItem>' +
  231. '<paragraph listIndent="0" listType="bulleted">b</paragraph>' +
  232. '<listItem listIndent="0" listType="bulleted">c</listItem>' +
  233. '<listItem listIndent="1" listType="bulleted">d</listItem>' +
  234. '<listItem listIndent="1" listType="bulleted">e</listItem>' +
  235. '<listItem listIndent="0" listType="bulleted">f</listItem>' +
  236. '<listItem listIndent="0" listType="bulleted">g</listItem>'
  237. );
  238. } );
  239. it( 'should outdent all selected item when multiple items are selected', () => {
  240. model.change( writer => {
  241. writer.setSelection( new Range(
  242. new Position( root.getChild( 1 ), [ 0 ] ),
  243. new Position( root.getChild( 3 ), [ 1 ] )
  244. ) );
  245. } );
  246. command.execute();
  247. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  248. '<listItem listIndent="0" listType="bulleted">a</listItem>' +
  249. '<paragraph listIndent="0" listType="bulleted">b</paragraph>' +
  250. '<listItem listIndent="0" listType="bulleted">c</listItem>' +
  251. '<listItem listIndent="1" listType="bulleted">d</listItem>' +
  252. '<listItem listIndent="2" listType="bulleted">e</listItem>' +
  253. '<listItem listIndent="1" listType="bulleted">f</listItem>' +
  254. '<listItem listIndent="0" listType="bulleted">g</listItem>'
  255. );
  256. } );
  257. } );
  258. } );
  259. } );