listcommand.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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.setCollapsedAt( 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.setCollapsedAt( 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.setCollapsedAt( 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.setCollapsedAt( 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. // https://github.com/ckeditor/ckeditor5-list/issues/62
  208. it( 'should not rename blocks which cannot become listItems', () => {
  209. doc.schema.registerItem( 'restricted' );
  210. doc.schema.allow( { name: 'restricted', inside: '$root' } );
  211. doc.schema.disallow( { name: 'paragraph', inside: 'restricted' } );
  212. doc.schema.registerItem( 'fooBlock', '$block' );
  213. doc.schema.allow( { name: 'fooBlock', inside: 'restricted' } );
  214. setData(
  215. doc,
  216. '<paragraph>a[bc</paragraph>' +
  217. '<restricted><fooBlock></fooBlock></restricted>' +
  218. '<paragraph>de]f</paragraph>'
  219. );
  220. command.execute();
  221. expect( getData( doc ) ).to.equal(
  222. '<listItem indent="0" type="bulleted">a[bc</listItem>' +
  223. '<restricted><fooBlock></fooBlock></restricted>' +
  224. '<listItem indent="0" type="bulleted">de]f</listItem>'
  225. );
  226. } );
  227. it( 'should rename closest block to listItem and set correct attributes', () => {
  228. // From first paragraph to second paragraph.
  229. // Command value=false, we are turning on list items.
  230. doc.enqueueChanges( () => {
  231. doc.selection.setRanges( [ new Range(
  232. Position.createAt( root.getChild( 2 ) ),
  233. Position.createAt( root.getChild( 3 ), 'end' )
  234. ) ] );
  235. } );
  236. command.execute();
  237. const expectedData =
  238. '<listItem indent="0" type="bulleted">---</listItem>' +
  239. '<listItem indent="0" type="bulleted">---</listItem>' +
  240. '<listItem indent="0" type="bulleted">[---</listItem>' +
  241. '<listItem indent="0" type="bulleted">---]</listItem>' +
  242. '<listItem indent="0" type="numbered">---</listItem>' +
  243. '<listItem indent="0" type="numbered">---</listItem>' +
  244. '<listItem indent="1" type="bulleted">---</listItem>' +
  245. '<listItem indent="2" type="bulleted">---</listItem>';
  246. expect( getData( doc ) ).to.equal( expectedData );
  247. } );
  248. it( 'should rename closest listItem to paragraph', () => {
  249. // From second bullet list item to first numbered list item.
  250. // Command value=true, we are turning off list items.
  251. doc.enqueueChanges( () => {
  252. doc.selection.setRanges( [ new Range(
  253. Position.createAt( root.getChild( 1 ) ),
  254. Position.createAt( root.getChild( 4 ), 'end' )
  255. ) ] );
  256. } );
  257. // Convert paragraphs, leave numbered list items.
  258. command.execute();
  259. const expectedData =
  260. '<listItem indent="0" type="bulleted">---</listItem>' +
  261. '<paragraph indent="0" type="bulleted">[---</paragraph>' + // Attributes will be removed by post fixer.
  262. '<paragraph>---</paragraph>' +
  263. '<paragraph>---</paragraph>' +
  264. '<paragraph indent="0" type="numbered">---]</paragraph>' + // Attributes will be removed by post fixer.
  265. '<listItem indent="0" type="numbered">---</listItem>' +
  266. '<listItem indent="1" type="bulleted">---</listItem>' +
  267. '<listItem indent="2" type="bulleted">---</listItem>';
  268. expect( getData( doc ) ).to.equal( expectedData );
  269. } );
  270. it( 'should change closest listItem\'s type', () => {
  271. // From first numbered lsit item to third bulleted list item.
  272. doc.enqueueChanges( () => {
  273. doc.selection.setRanges( [ new Range(
  274. Position.createAt( root.getChild( 4 ) ),
  275. Position.createAt( root.getChild( 6 ) )
  276. ) ] );
  277. } );
  278. // Convert paragraphs, leave numbered list items.
  279. command.execute();
  280. const expectedData =
  281. '<listItem indent="0" type="bulleted">---</listItem>' +
  282. '<listItem indent="0" type="bulleted">---</listItem>' +
  283. '<paragraph>---</paragraph>' +
  284. '<paragraph>---</paragraph>' +
  285. '<listItem indent="0" type="bulleted">[---</listItem>' +
  286. '<listItem indent="0" type="bulleted">---</listItem>' +
  287. '<listItem indent="1" type="bulleted">]---</listItem>' +
  288. '<listItem indent="2" type="bulleted">---</listItem>';
  289. expect( getData( doc ) ).to.equal( expectedData );
  290. } );
  291. it( 'should handle outdenting sub-items when list item is turned off', () => {
  292. // From first numbered list item to third bulleted list item.
  293. doc.enqueueChanges( () => {
  294. doc.selection.setRanges( [ new Range(
  295. Position.createAt( root.getChild( 1 ) ),
  296. Position.createAt( root.getChild( 5 ), 'end' )
  297. ) ] );
  298. } );
  299. // Convert paragraphs, leave numbered list items.
  300. command.execute();
  301. const expectedData =
  302. '<listItem indent="0" type="bulleted">---</listItem>' +
  303. '<paragraph indent="0" type="bulleted">[---</paragraph>' + // Attributes will be removed by post fixer.
  304. '<paragraph>---</paragraph>' +
  305. '<paragraph>---</paragraph>' +
  306. '<paragraph indent="0" type="numbered">---</paragraph>' + // Attributes will be removed by post fixer.
  307. '<paragraph indent="0" type="numbered">---]</paragraph>' + // Attributes will be removed by post fixer.
  308. '<listItem indent="0" type="bulleted">---</listItem>' +
  309. '<listItem indent="1" type="bulleted">---</listItem>';
  310. expect( getData( doc ) ).to.equal( expectedData );
  311. } );
  312. // Example from docs.
  313. it( 'should change type of all items in nested list if one of items changed', () => {
  314. setData(
  315. doc,
  316. '<listItem indent="0" type="numbered">---</listItem>' +
  317. '<listItem indent="1" type="numbered">---</listItem>' +
  318. '<listItem indent="2" type="numbered">---</listItem>' +
  319. '<listItem indent="1" type="numbered">---</listItem>' +
  320. '<listItem indent="2" type="numbered">---</listItem>' +
  321. '<listItem indent="2" type="numbered">-[-</listItem>' +
  322. '<listItem indent="1" type="numbered">---</listItem>' +
  323. '<listItem indent="1" type="numbered">---</listItem>' +
  324. '<listItem indent="0" type="numbered">---</listItem>' +
  325. '<listItem indent="1" type="numbered">-]-</listItem>' +
  326. '<listItem indent="1" type="numbered">---</listItem>' +
  327. '<listItem indent="2" type="numbered">---</listItem>' +
  328. '<listItem indent="0" type="numbered">---</listItem>'
  329. );
  330. // * ------ <-- do not fix, top level item
  331. // * ------ <-- fix, because latter list item of this item's list is changed
  332. // * ------ <-- do not fix, item is not affected (different list)
  333. // * ------ <-- fix, because latter list item of this item's list is changed
  334. // * ------ <-- fix, because latter list item of this item's list is changed
  335. // * ---[-- <-- already in selection
  336. // * ------ <-- already in selection
  337. // * ------ <-- already in selection
  338. // * ------ <-- already in selection, but does not cause other list items to change because is top-level
  339. // * ---]-- <-- already in selection
  340. // * ------ <-- fix, because preceding list item of this item's list is changed
  341. // * ------ <-- do not fix, item is not affected (different list)
  342. // * ------ <-- do not fix, top level item
  343. command.execute();
  344. const expectedData =
  345. '<listItem indent="0" type="numbered">---</listItem>' +
  346. '<listItem indent="1" type="bulleted">---</listItem>' +
  347. '<listItem indent="2" type="numbered">---</listItem>' +
  348. '<listItem indent="1" type="bulleted">---</listItem>' +
  349. '<listItem indent="2" type="bulleted">---</listItem>' +
  350. '<listItem indent="2" type="bulleted">-[-</listItem>' +
  351. '<listItem indent="1" type="bulleted">---</listItem>' +
  352. '<listItem indent="1" type="bulleted">---</listItem>' +
  353. '<listItem indent="0" type="bulleted">---</listItem>' +
  354. '<listItem indent="1" type="bulleted">-]-</listItem>' +
  355. '<listItem indent="1" type="bulleted">---</listItem>' +
  356. '<listItem indent="2" type="numbered">---</listItem>' +
  357. '<listItem indent="0" type="numbered">---</listItem>';
  358. expect( getData( doc ) ).to.equal( expectedData );
  359. } );
  360. } );
  361. } );
  362. } );
  363. } );