listcommand.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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.enqueueChanges( () => {
  53. doc.selection.collapse( doc.getRoot().getChild( 3 ) );
  54. } );
  55. expect( command.value ).to.be.false;
  56. } );
  57. it( 'should be false if first position in selection is in a list item of different type', () => {
  58. doc.enqueueChanges( () => {
  59. doc.selection.collapse( doc.getRoot().getChild( 2 ) );
  60. } );
  61. expect( command.value ).to.be.false;
  62. } );
  63. it( 'should be true if first position in selection is in a list item of same type', () => {
  64. doc.enqueueChanges( () => {
  65. doc.selection.collapse( doc.getRoot().getChild( 1 ) );
  66. } );
  67. expect( command.value ).to.be.true;
  68. } );
  69. } );
  70. describe( 'isEnabled', () => {
  71. it( 'should be true if entire selection is in a list', () => {
  72. setData( doc, '<listItem type="bulleted" indent="0">[a]</listItem>' );
  73. expect( command.isEnabled ).to.be.true;
  74. } );
  75. it( 'should be true if entire selection is in a block which can be turned into a list', () => {
  76. setData( doc, '<paragraph>[a]</paragraph>' );
  77. expect( command.isEnabled ).to.be.true;
  78. } );
  79. it( 'should be true if selection first position is in a block which can be turned into a list', () => {
  80. setData( doc, '<paragraph>[a</paragraph><widget>b]</widget>' );
  81. expect( command.isEnabled ).to.be.true;
  82. } );
  83. it( 'should be false if selection first position is in an element which cannot be converted to a list item', () => {
  84. setData( doc, '<widget><paragraph>[a</paragraph></widget><paragraph>b]</paragraph>' );
  85. expect( command.isEnabled ).to.be.false;
  86. } );
  87. it( 'should be false in a root which does not allow blocks at all', () => {
  88. doc.createRoot( 'paragraph', 'inlineOnlyRoot' );
  89. setData( doc, 'a[]b', { rootName: 'inlineOnlyRoot' } );
  90. expect( command.isEnabled ).to.be.false;
  91. } );
  92. } );
  93. describe( 'execute()', () => {
  94. describe( 'custom options', () => {
  95. it( 'should use provided batch', () => {
  96. const batch = editor.document.batch();
  97. expect( batch.deltas.length ).to.equal( 0 );
  98. command.execute( { batch } );
  99. expect( batch.deltas.length ).to.be.above( 0 );
  100. } );
  101. } );
  102. describe( 'collapsed selection', () => {
  103. it( 'should rename closest block to listItem and set correct attributes', () => {
  104. setData( doc, '<paragraph>fo[]o</paragraph>' );
  105. command.execute();
  106. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">fo[]o</listItem>' );
  107. } );
  108. it( 'should rename closest listItem to paragraph', () => {
  109. setData( doc, '<listItem indent="0" type="bulleted">fo[]o</listItem>' );
  110. command.execute();
  111. // Attributes will be removed by post fixer.
  112. expect( getData( doc ) ).to.equal( '<paragraph indent="0" type="bulleted">fo[]o</paragraph>' );
  113. } );
  114. it( 'should change closest listItem\' type', () => {
  115. setData( doc, '<listItem indent="0" type="numbered">fo[]o</listItem>' );
  116. command.execute();
  117. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">fo[]o</listItem>' );
  118. } );
  119. it( 'should handle outdenting sub-items when list item is turned off', () => {
  120. /* eslint-disable max-len */
  121. // Taken from docs.
  122. //
  123. // 1 * --------
  124. // 2 * --------
  125. // 3 * -------- <- this is turned off.
  126. // 4 * -------- <- this has to become indent = 0, because it will be first item on a new list.
  127. // 5 * -------- <- this should be still be a child of item above, so indent = 1.
  128. // 6 * -------- <- this also has to become indent = 0, because it shouldn't end up as a child of any of items above.
  129. // 7 * -------- <- this should be still be a child of item above, so indent = 1.
  130. // 8 * -------- <- this has to become indent = 0.
  131. // 9 * -------- <- this should still be a child of item above, so indent = 1.
  132. // 10 * -------- <- this should still be a child of item above, so indent = 2.
  133. // 11 * -------- <- this should still be at the same level as item above, so indent = 2.
  134. // 12 * -------- <- this and all below are left unchanged.
  135. // 13 * --------
  136. // 14 * --------
  137. //
  138. // After turning off "3", the list becomes:
  139. //
  140. // 1 * --------
  141. // 2 * --------
  142. //
  143. // 3 --------
  144. //
  145. // 4 * --------
  146. // 5 * --------
  147. // 6 * --------
  148. // 7 * --------
  149. // 8 * --------
  150. // 9 * --------
  151. // 10 * --------
  152. // 11 * --------
  153. // 12 * --------
  154. // 13 * --------
  155. // 14 * --------
  156. /* eslint-enable max-len */
  157. setData(
  158. doc,
  159. '<listItem indent="0" type="bulleted">---</listItem>' +
  160. '<listItem indent="1" type="bulleted">---</listItem>' +
  161. '<listItem indent="2" type="bulleted">[]---</listItem>' +
  162. '<listItem indent="3" type="bulleted">---</listItem>' +
  163. '<listItem indent="4" type="bulleted">---</listItem>' +
  164. '<listItem indent="2" type="bulleted">---</listItem>' +
  165. '<listItem indent="3" type="bulleted">---</listItem>' +
  166. '<listItem indent="1" type="bulleted">---</listItem>' +
  167. '<listItem indent="2" type="bulleted">---</listItem>' +
  168. '<listItem indent="3" type="bulleted">---</listItem>' +
  169. '<listItem indent="3" type="bulleted">---</listItem>' +
  170. '<listItem indent="0" type="bulleted">---</listItem>' +
  171. '<listItem indent="1" type="bulleted">---</listItem>' +
  172. '<listItem indent="2" type="bulleted">---</listItem>'
  173. );
  174. command.execute();
  175. const expectedData =
  176. '<listItem indent="0" type="bulleted">---</listItem>' +
  177. '<listItem indent="1" type="bulleted">---</listItem>' +
  178. '<paragraph indent="2" type="bulleted">[]---</paragraph>' + // Attributes will be removed by post fixer.
  179. '<listItem indent="0" type="bulleted">---</listItem>' +
  180. '<listItem indent="1" type="bulleted">---</listItem>' +
  181. '<listItem indent="0" type="bulleted">---</listItem>' +
  182. '<listItem indent="1" type="bulleted">---</listItem>' +
  183. '<listItem indent="0" type="bulleted">---</listItem>' +
  184. '<listItem indent="1" type="bulleted">---</listItem>' +
  185. '<listItem indent="2" type="bulleted">---</listItem>' +
  186. '<listItem indent="2" type="bulleted">---</listItem>' +
  187. '<listItem indent="0" type="bulleted">---</listItem>' +
  188. '<listItem indent="1" type="bulleted">---</listItem>' +
  189. '<listItem indent="2" type="bulleted">---</listItem>';
  190. expect( getData( doc ) ).to.equal( expectedData );
  191. } );
  192. } );
  193. describe( 'non-collapsed selection', () => {
  194. beforeEach( () => {
  195. setData(
  196. doc,
  197. '<listItem indent="0" type="bulleted">---</listItem>' +
  198. '<listItem indent="0" type="bulleted">---</listItem>' +
  199. '<paragraph>---</paragraph>' +
  200. '<paragraph>---</paragraph>' +
  201. '<listItem indent="0" type="numbered">---</listItem>' +
  202. '<listItem indent="0" type="numbered">---</listItem>' +
  203. '<listItem indent="1" type="bulleted">---</listItem>' +
  204. '<listItem indent="2" type="bulleted">---</listItem>'
  205. );
  206. } );
  207. it( 'should rename closest block to listItem and set correct attributes', () => {
  208. // From first paragraph to second paragraph.
  209. // Command value=false, we are turning on list items.
  210. doc.enqueueChanges( () => {
  211. doc.selection.setRanges( [ new Range(
  212. Position.createAt( root.getChild( 2 ) ),
  213. Position.createAt( root.getChild( 3 ) )
  214. ) ] );
  215. } );
  216. command.execute();
  217. const expectedData =
  218. '<listItem indent="0" type="bulleted">---</listItem>' +
  219. '<listItem indent="0" type="bulleted">---</listItem>' +
  220. '<listItem indent="0" type="bulleted">[---</listItem>' +
  221. '<listItem indent="0" type="bulleted">]---</listItem>' +
  222. '<listItem indent="0" type="numbered">---</listItem>' +
  223. '<listItem indent="0" type="numbered">---</listItem>' +
  224. '<listItem indent="1" type="bulleted">---</listItem>' +
  225. '<listItem indent="2" type="bulleted">---</listItem>';
  226. expect( getData( doc ) ).to.equal( expectedData );
  227. } );
  228. it( 'should rename closest listItem to paragraph', () => {
  229. // From second bullet list item to first numbered list item.
  230. // Command value=true, we are turning off list items.
  231. doc.enqueueChanges( () => {
  232. doc.selection.setRanges( [ new Range(
  233. Position.createAt( root.getChild( 1 ) ),
  234. Position.createAt( root.getChild( 4 ) )
  235. ) ] );
  236. } );
  237. // Convert paragraphs, leave numbered list items.
  238. command.execute();
  239. const expectedData =
  240. '<listItem indent="0" type="bulleted">---</listItem>' +
  241. '<paragraph indent="0" type="bulleted">[---</paragraph>' + // Attributes will be removed by post fixer.
  242. '<paragraph>---</paragraph>' +
  243. '<paragraph>---</paragraph>' +
  244. '<paragraph indent="0" type="numbered">]---</paragraph>' + // Attributes will be removed by post fixer.
  245. '<listItem indent="0" type="numbered">---</listItem>' +
  246. '<listItem indent="1" type="bulleted">---</listItem>' +
  247. '<listItem indent="2" type="bulleted">---</listItem>';
  248. expect( getData( doc ) ).to.equal( expectedData );
  249. } );
  250. it( 'should change closest listItem\'s type', () => {
  251. // From first numbered lsit item to third bulleted list item.
  252. doc.enqueueChanges( () => {
  253. doc.selection.setRanges( [ new Range(
  254. Position.createAt( root.getChild( 4 ) ),
  255. Position.createAt( root.getChild( 6 ) )
  256. ) ] );
  257. } );
  258. // Convert paragraphs, leave numbered list items.
  259. command.execute();
  260. const expectedData =
  261. '<listItem indent="0" type="bulleted">---</listItem>' +
  262. '<listItem indent="0" type="bulleted">---</listItem>' +
  263. '<paragraph>---</paragraph>' +
  264. '<paragraph>---</paragraph>' +
  265. '<listItem indent="0" type="bulleted">[---</listItem>' +
  266. '<listItem indent="0" type="bulleted">---</listItem>' +
  267. '<listItem indent="1" type="bulleted">]---</listItem>' +
  268. '<listItem indent="2" type="bulleted">---</listItem>';
  269. expect( getData( doc ) ).to.equal( expectedData );
  270. } );
  271. it( 'should handle outdenting sub-items when list item is turned off', () => {
  272. // From first numbered list item to third bulleted list item.
  273. doc.enqueueChanges( () => {
  274. doc.selection.setRanges( [ new Range(
  275. Position.createAt( root.getChild( 1 ) ),
  276. Position.createAt( root.getChild( 5 ) )
  277. ) ] );
  278. } );
  279. // Convert paragraphs, leave numbered list items.
  280. command.execute();
  281. const expectedData =
  282. '<listItem indent="0" type="bulleted">---</listItem>' +
  283. '<paragraph indent="0" type="bulleted">[---</paragraph>' + // Attributes will be removed by post fixer.
  284. '<paragraph>---</paragraph>' +
  285. '<paragraph>---</paragraph>' +
  286. '<paragraph indent="0" type="numbered">---</paragraph>' + // Attributes will be removed by post fixer.
  287. '<paragraph indent="0" type="numbered">]---</paragraph>' + // Attributes will be removed by post fixer.
  288. '<listItem indent="0" type="bulleted">---</listItem>' +
  289. '<listItem indent="1" type="bulleted">---</listItem>';
  290. expect( getData( doc ) ).to.equal( expectedData );
  291. } );
  292. // Example from docs.
  293. it( 'should change type of all items in nested list if one of items changed', () => {
  294. setData(
  295. doc,
  296. '<listItem indent="0" type="numbered">---</listItem>' +
  297. '<listItem indent="1" type="numbered">---</listItem>' +
  298. '<listItem indent="2" type="numbered">---</listItem>' +
  299. '<listItem indent="1" type="numbered">---</listItem>' +
  300. '<listItem indent="2" type="numbered">---</listItem>' +
  301. '<listItem indent="2" type="numbered">-[-</listItem>' +
  302. '<listItem indent="1" type="numbered">---</listItem>' +
  303. '<listItem indent="1" type="numbered">---</listItem>' +
  304. '<listItem indent="0" type="numbered">---</listItem>' +
  305. '<listItem indent="1" type="numbered">-]-</listItem>' +
  306. '<listItem indent="1" type="numbered">---</listItem>' +
  307. '<listItem indent="2" type="numbered">---</listItem>' +
  308. '<listItem indent="0" type="numbered">---</listItem>'
  309. );
  310. // * ------ <-- do not fix, top level item
  311. // * ------ <-- fix, because latter list item of this item's list is changed
  312. // * ------ <-- do not fix, item is not affected (different list)
  313. // * ------ <-- fix, because latter list item of this item's list is changed
  314. // * ------ <-- fix, because latter list item of this item's list is changed
  315. // * ---[-- <-- already in selection
  316. // * ------ <-- already in selection
  317. // * ------ <-- already in selection
  318. // * ------ <-- already in selection, but does not cause other list items to change because is top-level
  319. // * ---]-- <-- already in selection
  320. // * ------ <-- fix, because preceding list item of this item's list is changed
  321. // * ------ <-- do not fix, item is not affected (different list)
  322. // * ------ <-- do not fix, top level item
  323. command.execute();
  324. const expectedData =
  325. '<listItem indent="0" type="numbered">---</listItem>' +
  326. '<listItem indent="1" type="bulleted">---</listItem>' +
  327. '<listItem indent="2" type="numbered">---</listItem>' +
  328. '<listItem indent="1" type="bulleted">---</listItem>' +
  329. '<listItem indent="2" type="bulleted">---</listItem>' +
  330. '<listItem indent="2" type="bulleted">-[-</listItem>' +
  331. '<listItem indent="1" type="bulleted">---</listItem>' +
  332. '<listItem indent="1" type="bulleted">---</listItem>' +
  333. '<listItem indent="0" type="bulleted">---</listItem>' +
  334. '<listItem indent="1" type="bulleted">-]-</listItem>' +
  335. '<listItem indent="1" type="bulleted">---</listItem>' +
  336. '<listItem indent="2" type="numbered">---</listItem>' +
  337. '<listItem indent="0" type="numbered">---</listItem>';
  338. expect( getData( doc ) ).to.equal( expectedData );
  339. } );
  340. } );
  341. } );
  342. } );
  343. } );