8
0

listcommand.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /**
  2. * @license Copyright (c) 2003-2017, 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 Document from '@ckeditor/ckeditor5-engine/src/model/document';
  7. import ListCommand from '../src/listcommand';
  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( 'ListCommand', () => {
  12. let editor, command, doc, root;
  13. beforeEach( () => {
  14. editor = new Editor();
  15. editor.document = new Document();
  16. doc = editor.document;
  17. root = doc.createRoot();
  18. command = new ListCommand( editor, 'bulleted' );
  19. doc.schema.registerItem( 'listItem', '$block' );
  20. doc.schema.registerItem( 'paragraph', '$block' );
  21. doc.schema.registerItem( 'widget', '$block' );
  22. doc.schema.allow( { name: '$block', inside: '$root' } );
  23. doc.schema.allow( { name: 'paragraph', inside: 'widget' } );
  24. doc.schema.allow( { name: 'listItem', attributes: [ 'type', 'indent' ], inside: '$root' } );
  25. doc.schema.disallow( { name: 'listItem', attributes: [ 'type', 'indent' ], inside: 'widget' } );
  26. setData(
  27. doc,
  28. '<paragraph>foo</paragraph>' +
  29. '<listItem type="bulleted" indent="0">bulleted</listItem>' +
  30. '<listItem type="numbered" indent="0">numbered</listItem>' +
  31. '<paragraph>bar</paragraph>' +
  32. '<widget>' +
  33. '<paragraph>xyz</paragraph>' +
  34. '</widget>'
  35. );
  36. doc.selection.collapse( doc.getRoot().getChild( 0 ) );
  37. } );
  38. afterEach( () => {
  39. command.destroy();
  40. } );
  41. describe( 'ListCommand', () => {
  42. describe( 'constructor()', () => {
  43. it( 'should create list command with given type and value set to false', () => {
  44. expect( command.type ).to.equal( 'bulleted' );
  45. expect( command.value ).to.be.false;
  46. const numberedList = new ListCommand( editor, 'numbered' );
  47. expect( numberedList.type ).to.equal( 'numbered' );
  48. } );
  49. } );
  50. describe( 'value', () => {
  51. it( 'should be false if first position in selection is not in a list item', () => {
  52. doc.selection.collapse( doc.getRoot().getChild( 3 ) );
  53. expect( command.value ).to.be.false;
  54. } );
  55. it( 'should be false if first position in selection is in a list item of different type', () => {
  56. doc.selection.collapse( doc.getRoot().getChild( 2 ) );
  57. expect( command.value ).to.be.false;
  58. } );
  59. it( 'should be true if first position in selection is in a list item of same type', () => {
  60. doc.selection.collapse( doc.getRoot().getChild( 1 ) );
  61. expect( command.value ).to.be.true;
  62. } );
  63. } );
  64. describe( 'isEnabled', () => {
  65. it( 'should be true if command value is true', () => {
  66. command.value = true;
  67. command.refreshState();
  68. expect( command.isEnabled ).to.be.true;
  69. command.value = false;
  70. doc.selection.collapse( doc.getRoot().getChild( 1 ) );
  71. expect( command.value ).to.be.true;
  72. expect( command.isEnabled ).to.be.true;
  73. } );
  74. it( 'should be true if selection first position is in a place where listItem can be inserted', () => {
  75. doc.selection.collapse( doc.getRoot(), 2 );
  76. expect( command.isEnabled ).to.be.true;
  77. doc.selection.collapse( doc.getRoot().getChild( 0 ) );
  78. expect( command.isEnabled ).to.be.true;
  79. doc.selection.collapse( doc.getRoot().getChild( 2 ) );
  80. expect( command.isEnabled ).to.be.true;
  81. } );
  82. it( 'should be false if selection first position is in a place where listItem cannot be inserted', () => {
  83. doc.selection.collapse( doc.getRoot().getChild( 4 ) );
  84. expect( command.isEnabled ).to.be.false;
  85. } );
  86. } );
  87. describe( '_doExecute', () => {
  88. describe( 'custom options', () => {
  89. it( 'should use provided batch', () => {
  90. const batch = editor.document.batch();
  91. expect( batch.deltas.length ).to.equal( 0 );
  92. command._doExecute( { batch } );
  93. expect( batch.deltas.length ).to.be.above( 0 );
  94. } );
  95. } );
  96. describe( 'collapsed selection', () => {
  97. it( 'should rename closest block to listItem and set correct attributes', () => {
  98. setData( doc, '<paragraph>fo[]o</paragraph>' );
  99. command._doExecute();
  100. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">fo[]o</listItem>' );
  101. } );
  102. it( 'should rename closest listItem to paragraph and remove attributes', () => {
  103. setData( doc, '<listItem indent="0" type="bulleted">fo[]o</listItem>' );
  104. command._doExecute();
  105. expect( getData( doc ) ).to.equal( '<paragraph>fo[]o</paragraph>' );
  106. } );
  107. it( 'should change closest listItem\' type', () => {
  108. setData( doc, '<listItem indent="0" type="numbered">fo[]o</listItem>' );
  109. command._doExecute();
  110. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">fo[]o</listItem>' );
  111. } );
  112. it( 'should handle outdenting sub-items when list item is turned off', () => {
  113. // Taken from docs.
  114. //
  115. // 1 * --------
  116. // 2 * --------
  117. // 3 * -------- <- this is turned off.
  118. // 4 * -------- <- this has to become indent = 0, because it will be first item on a new list.
  119. // 5 * -------- <- this should be still be a child of item above, so indent = 1.
  120. // 6 * -------- <- this also has to become indent = 0, because it shouldn't end up as a child of any of items above.
  121. // 7 * -------- <- this should be still be a child of item above, so indent = 1.
  122. // 8 * -------- <- this has to become indent = 0.
  123. // 9 * -------- <- this should still be a child of item above, so indent = 1.
  124. // 10 * -------- <- this should still be a child of item above, so indent = 2.
  125. // 11 * -------- <- this should still be at the same level as item above, so indent = 2.
  126. // 12 * -------- <- this and all below are left unchanged.
  127. // 13 * --------
  128. // 14 * --------
  129. //
  130. // After turning off "3", the list becomes:
  131. //
  132. // 1 * --------
  133. // 2 * --------
  134. //
  135. // 3 --------
  136. //
  137. // 4 * --------
  138. // 5 * --------
  139. // 6 * --------
  140. // 7 * --------
  141. // 8 * --------
  142. // 9 * --------
  143. // 10 * --------
  144. // 11 * --------
  145. // 12 * --------
  146. // 13 * --------
  147. // 14 * --------
  148. setData(
  149. doc,
  150. '<listItem indent="0" type="bulleted">---</listItem>' +
  151. '<listItem indent="1" type="bulleted">---</listItem>' +
  152. '<listItem indent="2" type="bulleted">[]---</listItem>' +
  153. '<listItem indent="3" type="bulleted">---</listItem>' +
  154. '<listItem indent="4" type="bulleted">---</listItem>' +
  155. '<listItem indent="2" type="bulleted">---</listItem>' +
  156. '<listItem indent="3" type="bulleted">---</listItem>' +
  157. '<listItem indent="1" type="bulleted">---</listItem>' +
  158. '<listItem indent="2" type="bulleted">---</listItem>' +
  159. '<listItem indent="3" type="bulleted">---</listItem>' +
  160. '<listItem indent="3" type="bulleted">---</listItem>' +
  161. '<listItem indent="0" type="bulleted">---</listItem>' +
  162. '<listItem indent="1" type="bulleted">---</listItem>' +
  163. '<listItem indent="2" type="bulleted">---</listItem>'
  164. );
  165. command._doExecute();
  166. const expectedData =
  167. '<listItem indent="0" type="bulleted">---</listItem>' +
  168. '<listItem indent="1" type="bulleted">---</listItem>' +
  169. '<paragraph>[]---</paragraph>' +
  170. '<listItem indent="0" type="bulleted">---</listItem>' +
  171. '<listItem indent="1" type="bulleted">---</listItem>' +
  172. '<listItem indent="0" type="bulleted">---</listItem>' +
  173. '<listItem indent="1" type="bulleted">---</listItem>' +
  174. '<listItem indent="0" type="bulleted">---</listItem>' +
  175. '<listItem indent="1" type="bulleted">---</listItem>' +
  176. '<listItem indent="2" type="bulleted">---</listItem>' +
  177. '<listItem indent="2" type="bulleted">---</listItem>' +
  178. '<listItem indent="0" type="bulleted">---</listItem>' +
  179. '<listItem indent="1" type="bulleted">---</listItem>' +
  180. '<listItem indent="2" type="bulleted">---</listItem>';
  181. expect( getData( doc ) ).to.equal( expectedData );
  182. } );
  183. } );
  184. describe( 'non-collapsed selection', () => {
  185. beforeEach( () => {
  186. setData(
  187. doc,
  188. '<listItem indent="0" type="bulleted">---</listItem>' +
  189. '<listItem indent="0" type="bulleted">---</listItem>' +
  190. '<paragraph>---</paragraph>' +
  191. '<paragraph>---</paragraph>' +
  192. '<listItem indent="0" type="numbered">---</listItem>' +
  193. '<listItem indent="0" type="numbered">---</listItem>' +
  194. '<listItem indent="1" type="bulleted">---</listItem>' +
  195. '<listItem indent="2" type="bulleted">---</listItem>'
  196. );
  197. } );
  198. it( 'should rename closest block to listItem and set correct attributes', () => {
  199. // From first paragraph to second paragraph.
  200. // Command value=false, we are turning on list items.
  201. doc.selection.setRanges( [ new Range(
  202. Position.createAt( root.getChild( 2 ) ),
  203. Position.createAt( root.getChild( 3 ) )
  204. ) ] );
  205. command._doExecute();
  206. const expectedData =
  207. '<listItem indent="0" type="bulleted">---</listItem>' +
  208. '<listItem indent="0" type="bulleted">---</listItem>' +
  209. '<listItem indent="0" type="bulleted">[---</listItem>' +
  210. '<listItem indent="0" type="bulleted">]---</listItem>' +
  211. '<listItem indent="0" type="numbered">---</listItem>' +
  212. '<listItem indent="0" type="numbered">---</listItem>' +
  213. '<listItem indent="1" type="bulleted">---</listItem>' +
  214. '<listItem indent="2" type="bulleted">---</listItem>';
  215. expect( getData( doc ) ).to.equal( expectedData );
  216. } );
  217. it( 'should rename closest listItem to paragraph and remove attributes', () => {
  218. // From second bullet list item to first numbered list item.
  219. // Command value=true, we are turning off list items.
  220. doc.selection.setRanges( [ new Range(
  221. Position.createAt( root.getChild( 1 ) ),
  222. Position.createAt( root.getChild( 4 ) )
  223. ) ] );
  224. // Convert paragraphs, leave numbered list items.
  225. command._doExecute();
  226. const expectedData =
  227. '<listItem indent="0" type="bulleted">---</listItem>' +
  228. '<paragraph>[---</paragraph>' +
  229. '<paragraph>---</paragraph>' +
  230. '<paragraph>---</paragraph>' +
  231. '<paragraph>]---</paragraph>' +
  232. '<listItem indent="0" type="numbered">---</listItem>' +
  233. '<listItem indent="1" type="bulleted">---</listItem>' +
  234. '<listItem indent="2" type="bulleted">---</listItem>';
  235. expect( getData( doc ) ).to.equal( expectedData );
  236. } );
  237. it( 'should change closest listItem\'s type', () => {
  238. // From first numbered lsit item to third bulleted list item.
  239. doc.selection.setRanges( [ new Range(
  240. Position.createAt( root.getChild( 4 ) ),
  241. Position.createAt( root.getChild( 6 ) )
  242. ) ] );
  243. // Convert paragraphs, leave numbered list items.
  244. command._doExecute();
  245. const expectedData =
  246. '<listItem indent="0" type="bulleted">---</listItem>' +
  247. '<listItem indent="0" type="bulleted">---</listItem>' +
  248. '<paragraph>---</paragraph>' +
  249. '<paragraph>---</paragraph>' +
  250. '<listItem indent="0" type="bulleted">[---</listItem>' +
  251. '<listItem indent="0" type="bulleted">---</listItem>' +
  252. '<listItem indent="1" type="bulleted">]---</listItem>' +
  253. '<listItem indent="2" type="bulleted">---</listItem>';
  254. expect( getData( doc ) ).to.equal( expectedData );
  255. } );
  256. it( 'should handle outdenting sub-items when list item is turned off', () => {
  257. // From first numbered lsit item to third bulleted list item.
  258. doc.selection.setRanges( [ new Range(
  259. Position.createAt( root.getChild( 1 ) ),
  260. Position.createAt( root.getChild( 5 ) )
  261. ) ] );
  262. // Convert paragraphs, leave numbered list items.
  263. command._doExecute();
  264. const expectedData =
  265. '<listItem indent="0" type="bulleted">---</listItem>' +
  266. '<paragraph>[---</paragraph>' +
  267. '<paragraph>---</paragraph>' +
  268. '<paragraph>---</paragraph>' +
  269. '<paragraph>---</paragraph>' +
  270. '<paragraph>]---</paragraph>' +
  271. '<listItem indent="0" type="bulleted">---</listItem>' +
  272. '<listItem indent="1" type="bulleted">---</listItem>';
  273. expect( getData( doc ) ).to.equal( expectedData );
  274. } );
  275. } );
  276. } );
  277. } );
  278. } );