indentcommand.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Editor from '/ckeditor5/core/editor/editor.js';
  6. import Document from '/ckeditor5/engine/model/document.js';
  7. import IndentCommand from '/ckeditor5/list/indentcommand.js';
  8. import Range from '/ckeditor5/engine/model/range.js';
  9. import Position from '/ckeditor5/engine/model/position.js';
  10. import { setData, getData } from '/ckeditor5/engine/dev-utils/model.js';
  11. let editor, doc, root;
  12. beforeEach( () => {
  13. editor = new Editor();
  14. editor.document = new Document();
  15. doc = editor.document;
  16. root = doc.createRoot();
  17. doc.schema.registerItem( 'listItem', '$block' );
  18. doc.schema.allow( { name: '$block', inside: '$root' } );
  19. doc.schema.allow( { name: 'listItem', attributes: [ 'type', 'indent' ], inside: '$root' } );
  20. setData(
  21. doc,
  22. '<listItem indent="0" type="bulleted">a</listItem>' +
  23. '<listItem indent="0" type="bulleted">b</listItem>' +
  24. '<listItem indent="1" type="bulleted">c</listItem>' +
  25. '<listItem indent="2" type="bulleted">d</listItem>' +
  26. '<listItem indent="2" type="bulleted">e</listItem>' +
  27. '<listItem indent="1" type="bulleted">f</listItem>' +
  28. '<listItem indent="0" type="bulleted">g</listItem>'
  29. );
  30. } );
  31. describe( 'IndentCommand', () => {
  32. let command;
  33. beforeEach( () => {
  34. command = new IndentCommand( editor, 'forward' );
  35. } );
  36. afterEach( () => {
  37. command.destroy();
  38. } );
  39. describe( 'isEnabled', () => {
  40. it( 'should be true if selection starts in list item', () => {
  41. doc.selection.collapse( root.getChild( 5 ) );
  42. expect( command.isEnabled ).to.be.true;
  43. } );
  44. it( 'should be false if selection starts in first list item', () => {
  45. doc.selection.collapse( root.getChild( 0 ) );
  46. expect( command.isEnabled ).to.be.false;
  47. } );
  48. it( 'should be false if selection starts in a list item that has bigger indent than it\'s previous sibling', () => {
  49. doc.selection.collapse( root.getChild( 2 ) );
  50. expect( command.isEnabled ).to.be.false;
  51. } );
  52. } );
  53. describe( '_doExecute', () => {
  54. it( 'should increment indent attribute by 1', () => {
  55. doc.selection.collapse( root.getChild( 5 ) );
  56. command._doExecute();
  57. expect( getData( doc, { withoutSelection: true } ) ).to.equal(
  58. '<listItem indent="0" type="bulleted">a</listItem>' +
  59. '<listItem indent="0" type="bulleted">b</listItem>' +
  60. '<listItem indent="1" type="bulleted">c</listItem>' +
  61. '<listItem indent="2" type="bulleted">d</listItem>' +
  62. '<listItem indent="2" type="bulleted">e</listItem>' +
  63. '<listItem indent="2" type="bulleted">f</listItem>' +
  64. '<listItem indent="0" type="bulleted">g</listItem>'
  65. );
  66. } );
  67. it( 'should increment indent of only first selected item when multiple items are selected', () => {
  68. doc.selection.setRanges( [ new Range(
  69. new Position( root.getChild( 4 ), [ 0 ] ),
  70. new Position( root.getChild( 6 ), [ 0 ] )
  71. ) ] );
  72. command._doExecute();
  73. expect( getData( doc, { withoutSelection: true } ) ).to.equal(
  74. '<listItem indent="0" type="bulleted">a</listItem>' +
  75. '<listItem indent="0" type="bulleted">b</listItem>' +
  76. '<listItem indent="1" type="bulleted">c</listItem>' +
  77. '<listItem indent="2" type="bulleted">d</listItem>' +
  78. '<listItem indent="3" type="bulleted">e</listItem>' +
  79. '<listItem indent="1" type="bulleted">f</listItem>' +
  80. '<listItem indent="0" type="bulleted">g</listItem>'
  81. );
  82. } );
  83. it( 'should increment indent of all sub-items of indented item', () => {
  84. doc.selection.collapse( root.getChild( 1 ) );
  85. command._doExecute();
  86. expect( getData( doc, { withoutSelection: true } ) ).to.equal(
  87. '<listItem indent="0" type="bulleted">a</listItem>' +
  88. '<listItem indent="1" type="bulleted">b</listItem>' +
  89. '<listItem indent="2" type="bulleted">c</listItem>' +
  90. '<listItem indent="3" type="bulleted">d</listItem>' +
  91. '<listItem indent="3" type="bulleted">e</listItem>' +
  92. '<listItem indent="2" type="bulleted">f</listItem>' +
  93. '<listItem indent="0" type="bulleted">g</listItem>'
  94. );
  95. } );
  96. } );
  97. } );
  98. describe( 'IndentCommand - backward (outdent)', () => {
  99. let command;
  100. beforeEach( () => {
  101. command = new IndentCommand( editor, 'backward' );
  102. } );
  103. afterEach( () => {
  104. command.destroy();
  105. } );
  106. describe( 'isEnabled', () => {
  107. it( 'should be true if selection starts in list item', () => {
  108. doc.selection.collapse( root.getChild( 5 ) );
  109. expect( command.isEnabled ).to.be.true;
  110. } );
  111. it( 'should be true if selection starts in first list item', () => {
  112. // This is in contrary to forward indent command.
  113. doc.selection.collapse( root.getChild( 0 ) );
  114. expect( command.isEnabled ).to.be.true;
  115. } );
  116. it( 'should be true if selection starts in a list item that has bigger indent than it\'s previous sibling', () => {
  117. // This is in contrary to forward indent command.
  118. doc.selection.collapse( root.getChild( 2 ) );
  119. expect( command.isEnabled ).to.be.true;
  120. } );
  121. } );
  122. describe( '_doExecute', () => {
  123. it( 'should decrement indent attribute by 1 (if it is bigger than 0)', () => {
  124. doc.selection.collapse( root.getChild( 5 ) );
  125. command._doExecute();
  126. expect( getData( doc, { withoutSelection: true } ) ).to.equal(
  127. '<listItem indent="0" type="bulleted">a</listItem>' +
  128. '<listItem indent="0" type="bulleted">b</listItem>' +
  129. '<listItem indent="1" type="bulleted">c</listItem>' +
  130. '<listItem indent="2" type="bulleted">d</listItem>' +
  131. '<listItem indent="2" type="bulleted">e</listItem>' +
  132. '<listItem indent="0" type="bulleted">f</listItem>' +
  133. '<listItem indent="0" type="bulleted">g</listItem>'
  134. );
  135. } );
  136. it( 'should rename listItem to paragraph (if indent is equal to 0)', () => {
  137. doc.selection.collapse( root.getChild( 0 ) );
  138. command._doExecute();
  139. expect( getData( doc, { withoutSelection: true } ) ).to.equal(
  140. '<paragraph>a</paragraph>' +
  141. '<listItem indent="0" type="bulleted">b</listItem>' +
  142. '<listItem indent="1" type="bulleted">c</listItem>' +
  143. '<listItem indent="2" type="bulleted">d</listItem>' +
  144. '<listItem indent="2" type="bulleted">e</listItem>' +
  145. '<listItem indent="1" type="bulleted">f</listItem>' +
  146. '<listItem indent="0" type="bulleted">g</listItem>'
  147. );
  148. } );
  149. it( 'should decrement indent of all sub-items of outdented item', () => {
  150. doc.selection.collapse( root.getChild( 1 ) );
  151. command._doExecute();
  152. expect( getData( doc, { withoutSelection: true } ) ).to.equal(
  153. '<listItem indent="0" type="bulleted">a</listItem>' +
  154. '<paragraph>b</paragraph>' +
  155. '<listItem indent="0" type="bulleted">c</listItem>' +
  156. '<listItem indent="1" type="bulleted">d</listItem>' +
  157. '<listItem indent="1" type="bulleted">e</listItem>' +
  158. '<listItem indent="0" type="bulleted">f</listItem>' +
  159. '<listItem indent="0" type="bulleted">g</listItem>'
  160. );
  161. } );
  162. } );
  163. } );