listcommand.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Editor from '@ckeditor/ckeditor5-core/src/editor/editor';
  6. import Model from '@ckeditor/ckeditor5-engine/src/model/model';
  7. import ListCommand from '../src/listcommand';
  8. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. describe( 'ListCommand', () => {
  10. let editor, command, model, doc, root;
  11. beforeEach( () => {
  12. editor = new Editor();
  13. editor.model = new Model();
  14. model = editor.model;
  15. doc = model.document;
  16. root = doc.createRoot();
  17. command = new ListCommand( editor, 'bulleted' );
  18. model.schema.register( 'listItem', {
  19. inheritAllFrom: '$block',
  20. allowAttributes: [ 'listType', 'listIndent' ]
  21. } );
  22. model.schema.register( 'paragraph', {
  23. inheritAllFrom: '$block',
  24. allowIn: 'widget'
  25. } );
  26. model.schema.register( 'widget', { inheritAllFrom: '$block' } );
  27. model.schema.addChildCheck( ( ctx, childDef ) => {
  28. if ( ctx.endsWith( 'widget' ) && childDef.name == 'listItem' ) {
  29. return false;
  30. }
  31. } );
  32. setData(
  33. model,
  34. '<paragraph>foo</paragraph>' +
  35. '<listItem listType="bulleted" listIndent="0">bulleted</listItem>' +
  36. '<listItem listType="numbered" listIndent="0">numbered</listItem>' +
  37. '<paragraph>bar</paragraph>' +
  38. '<widget>' +
  39. '<paragraph>xyz</paragraph>' +
  40. '</widget>'
  41. );
  42. model.change( writer => {
  43. writer.setSelection( doc.getRoot().getChild( 0 ), 0 );
  44. } );
  45. } );
  46. afterEach( () => {
  47. command.destroy();
  48. } );
  49. describe( 'ListCommand', () => {
  50. describe( 'constructor()', () => {
  51. it( 'should create list command with given type and value set to false', () => {
  52. expect( command.type ).to.equal( 'bulleted' );
  53. expect( command.value ).to.be.false;
  54. const numberedList = new ListCommand( editor, 'numbered' );
  55. expect( numberedList.type ).to.equal( 'numbered' );
  56. } );
  57. } );
  58. describe( 'value', () => {
  59. it( 'should be false if first position in selection is not in a list item', () => {
  60. model.change( writer => {
  61. writer.setSelection( doc.getRoot().getChild( 3 ), 0 );
  62. } );
  63. expect( command.value ).to.be.false;
  64. } );
  65. it( 'should be false if first position in selection is in a list item of different type', () => {
  66. model.change( writer => {
  67. writer.setSelection( doc.getRoot().getChild( 2 ), 0 );
  68. } );
  69. expect( command.value ).to.be.false;
  70. } );
  71. it( 'should be true if first position in selection is in a list item of same type', () => {
  72. model.change( writer => {
  73. writer.setSelection( doc.getRoot().getChild( 1 ), 0 );
  74. } );
  75. expect( command.value ).to.be.true;
  76. } );
  77. } );
  78. describe( 'isEnabled', () => {
  79. it( 'should be true if entire selection is in a list', () => {
  80. setData( model, '<listItem listType="bulleted" listIndent="0">[a]</listItem>' );
  81. expect( command.isEnabled ).to.be.true;
  82. } );
  83. it( 'should be true if entire selection is in a block which can be turned into a list', () => {
  84. setData( model, '<paragraph>[a]</paragraph>' );
  85. expect( command.isEnabled ).to.be.true;
  86. } );
  87. it( 'should be true if selection first position is in a block which can be turned into a list', () => {
  88. setData( model, '<paragraph>[a</paragraph><widget>b]</widget>' );
  89. expect( command.isEnabled ).to.be.true;
  90. } );
  91. it( 'should be false if selection first position is in an element which cannot be converted to a list item', () => {
  92. setData( model, '<widget><paragraph>[a</paragraph></widget><paragraph>b]</paragraph>' );
  93. expect( command.isEnabled ).to.be.false;
  94. } );
  95. it( 'should be false in a root which does not allow blocks at all', () => {
  96. doc.createRoot( 'paragraph', 'inlineOnlyRoot' );
  97. setData( model, 'a[]b', { rootName: 'inlineOnlyRoot' } );
  98. expect( command.isEnabled ).to.be.false;
  99. } );
  100. } );
  101. describe( 'execute()', () => {
  102. it( 'should use parent batch', () => {
  103. model.change( writer => {
  104. expect( writer.batch.operations.length ).to.equal( 0 );
  105. command.execute();
  106. expect( writer.batch.operations.length ).to.be.above( 0 );
  107. } );
  108. } );
  109. describe( 'collapsed selection', () => {
  110. it( 'should rename closest block to listItem and set correct attributes', () => {
  111. setData( model, '<paragraph>fo[]o</paragraph>' );
  112. command.execute();
  113. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="bulleted">fo[]o</listItem>' );
  114. } );
  115. it( 'should rename closest listItem to paragraph', () => {
  116. setData( model, '<listItem listIndent="0" listType="bulleted">fo[]o</listItem>' );
  117. command.execute();
  118. // Attributes will be removed by post fixer.
  119. expect( getData( model ) ).to.equal( '<paragraph listIndent="0" listType="bulleted">fo[]o</paragraph>' );
  120. } );
  121. it( 'should change closest listItem\' type', () => {
  122. setData( model, '<listItem listIndent="0" listType="numbered">fo[]o</listItem>' );
  123. command.execute();
  124. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="bulleted">fo[]o</listItem>' );
  125. } );
  126. it( 'should handle outdenting sub-items when list item is turned off', () => {
  127. /* eslint-disable max-len */
  128. // Taken from docs.
  129. //
  130. // 1 * --------
  131. // 2 * --------
  132. // 3 * -------- <- this is turned off.
  133. // 4 * -------- <- this has to become indent = 0, because it will be first item on a new list.
  134. // 5 * -------- <- this should be still be a child of item above, so indent = 1.
  135. // 6 * -------- <- this also has to become indent = 0, because it shouldn't end up as a child of any of items above.
  136. // 7 * -------- <- this should be still be a child of item above, so indent = 1.
  137. // 8 * -------- <- this has to become indent = 0.
  138. // 9 * -------- <- this should still be a child of item above, so indent = 1.
  139. // 10 * -------- <- this should still be a child of item above, so indent = 2.
  140. // 11 * -------- <- this should still be at the same level as item above, so indent = 2.
  141. // 12 * -------- <- this and all below are left unchanged.
  142. // 13 * --------
  143. // 14 * --------
  144. //
  145. // After turning off "3", the list becomes:
  146. //
  147. // 1 * --------
  148. // 2 * --------
  149. //
  150. // 3 --------
  151. //
  152. // 4 * --------
  153. // 5 * --------
  154. // 6 * --------
  155. // 7 * --------
  156. // 8 * --------
  157. // 9 * --------
  158. // 10 * --------
  159. // 11 * --------
  160. // 12 * --------
  161. // 13 * --------
  162. // 14 * --------
  163. /* eslint-enable max-len */
  164. setData(
  165. model,
  166. '<listItem listIndent="0" listType="bulleted">---</listItem>' +
  167. '<listItem listIndent="1" listType="bulleted">---</listItem>' +
  168. '<listItem listIndent="2" listType="bulleted">[]---</listItem>' +
  169. '<listItem listIndent="3" listType="bulleted">---</listItem>' +
  170. '<listItem listIndent="4" listType="bulleted">---</listItem>' +
  171. '<listItem listIndent="2" listType="bulleted">---</listItem>' +
  172. '<listItem listIndent="3" listType="bulleted">---</listItem>' +
  173. '<listItem listIndent="1" listType="bulleted">---</listItem>' +
  174. '<listItem listIndent="2" listType="bulleted">---</listItem>' +
  175. '<listItem listIndent="3" listType="bulleted">---</listItem>' +
  176. '<listItem listIndent="3" listType="bulleted">---</listItem>' +
  177. '<listItem listIndent="0" listType="bulleted">---</listItem>' +
  178. '<listItem listIndent="1" listType="bulleted">---</listItem>' +
  179. '<listItem listIndent="2" listType="bulleted">---</listItem>'
  180. );
  181. command.execute();
  182. const expectedData =
  183. '<listItem listIndent="0" listType="bulleted">---</listItem>' +
  184. '<listItem listIndent="1" listType="bulleted">---</listItem>' +
  185. '<paragraph listIndent="2" listType="bulleted">[]---</paragraph>' + // Attributes will be removed by post fixer.
  186. '<listItem listIndent="0" listType="bulleted">---</listItem>' +
  187. '<listItem listIndent="1" listType="bulleted">---</listItem>' +
  188. '<listItem listIndent="0" listType="bulleted">---</listItem>' +
  189. '<listItem listIndent="1" listType="bulleted">---</listItem>' +
  190. '<listItem listIndent="0" listType="bulleted">---</listItem>' +
  191. '<listItem listIndent="1" listType="bulleted">---</listItem>' +
  192. '<listItem listIndent="2" listType="bulleted">---</listItem>' +
  193. '<listItem listIndent="2" listType="bulleted">---</listItem>' +
  194. '<listItem listIndent="0" listType="bulleted">---</listItem>' +
  195. '<listItem listIndent="1" listType="bulleted">---</listItem>' +
  196. '<listItem listIndent="2" listType="bulleted">---</listItem>';
  197. expect( getData( model ) ).to.equal( expectedData );
  198. } );
  199. } );
  200. describe( 'non-collapsed selection', () => {
  201. beforeEach( () => {
  202. setData(
  203. model,
  204. '<listItem listIndent="0" listType="bulleted">---</listItem>' +
  205. '<listItem listIndent="0" listType="bulleted">---</listItem>' +
  206. '<paragraph>---</paragraph>' +
  207. '<paragraph>---</paragraph>' +
  208. '<listItem listIndent="0" listType="numbered">---</listItem>' +
  209. '<listItem listIndent="0" listType="numbered">---</listItem>' +
  210. '<listItem listIndent="1" listType="bulleted">---</listItem>' +
  211. '<listItem listIndent="2" listType="bulleted">---</listItem>'
  212. );
  213. } );
  214. // https://github.com/ckeditor/ckeditor5-list/issues/62
  215. it( 'should not rename blocks which cannot become listItems (list item is not allowed in their parent)', () => {
  216. model.schema.register( 'restricted' );
  217. model.schema.extend( 'restricted', { allowIn: '$root' } );
  218. model.schema.register( 'fooBlock', { inheritAllFrom: '$block' } );
  219. model.schema.extend( 'fooBlock', { allowIn: 'restricted' } );
  220. setData(
  221. model,
  222. '<paragraph>a[bc</paragraph>' +
  223. '<restricted><fooBlock></fooBlock></restricted>' +
  224. '<paragraph>de]f</paragraph>'
  225. );
  226. command.execute();
  227. expect( getData( model ) ).to.equal(
  228. '<listItem listIndent="0" listType="bulleted">a[bc</listItem>' +
  229. '<restricted><fooBlock></fooBlock></restricted>' +
  230. '<listItem listIndent="0" listType="bulleted">de]f</listItem>'
  231. );
  232. } );
  233. it( 'should not rename blocks which cannot become listItems (block is an object)', () => {
  234. model.schema.register( 'image', {
  235. isBlock: true,
  236. isObject: true,
  237. allowIn: '$root'
  238. } );
  239. setData(
  240. model,
  241. '<paragraph>a[bc</paragraph>' +
  242. '<image></image>' +
  243. '<paragraph>de]f</paragraph>'
  244. );
  245. command.execute();
  246. expect( getData( model ) ).to.equal(
  247. '<listItem listIndent="0" listType="bulleted">a[bc</listItem>' +
  248. '<image></image>' +
  249. '<listItem listIndent="0" listType="bulleted">de]f</listItem>'
  250. );
  251. } );
  252. it( 'should rename closest block to listItem and set correct attributes', () => {
  253. // From first paragraph to second paragraph.
  254. // Command value=false, we are turning on list items.
  255. model.change( writer => {
  256. writer.setSelection( writer.createRange(
  257. writer.createPositionAt( root.getChild( 2 ), 0 ),
  258. writer.createPositionAt( root.getChild( 3 ), 'end' )
  259. ) );
  260. } );
  261. command.execute();
  262. const expectedData =
  263. '<listItem listIndent="0" listType="bulleted">---</listItem>' +
  264. '<listItem listIndent="0" listType="bulleted">---</listItem>' +
  265. '<listItem listIndent="0" listType="bulleted">[---</listItem>' +
  266. '<listItem listIndent="0" listType="bulleted">---]</listItem>' +
  267. '<listItem listIndent="0" listType="numbered">---</listItem>' +
  268. '<listItem listIndent="0" listType="numbered">---</listItem>' +
  269. '<listItem listIndent="1" listType="bulleted">---</listItem>' +
  270. '<listItem listIndent="2" listType="bulleted">---</listItem>';
  271. expect( getData( model ) ).to.equal( expectedData );
  272. } );
  273. it( 'should rename closest listItem to paragraph', () => {
  274. // From second bullet list item to first numbered list item.
  275. // Command value=true, we are turning off list items.
  276. model.change( writer => {
  277. writer.setSelection( writer.createRange(
  278. writer.createPositionAt( root.getChild( 1 ), 0 ),
  279. writer.createPositionAt( root.getChild( 4 ), 'end' )
  280. ) );
  281. } );
  282. // Convert paragraphs, leave numbered list items.
  283. command.execute();
  284. const expectedData =
  285. '<listItem listIndent="0" listType="bulleted">---</listItem>' +
  286. '<paragraph listIndent="0" listType="bulleted">[---</paragraph>' + // Attributes will be removed by post fixer.
  287. '<paragraph>---</paragraph>' +
  288. '<paragraph>---</paragraph>' +
  289. '<paragraph listIndent="0" listType="numbered">---]</paragraph>' + // Attributes will be removed by post fixer.
  290. '<listItem listIndent="0" listType="numbered">---</listItem>' +
  291. '<listItem listIndent="1" listType="bulleted">---</listItem>' +
  292. '<listItem listIndent="2" listType="bulleted">---</listItem>';
  293. expect( getData( model ) ).to.equal( expectedData );
  294. } );
  295. it( 'should change closest listItem\'s type', () => {
  296. // From first numbered lsit item to third bulleted list item.
  297. model.change( writer => {
  298. writer.setSelection( writer.createRange(
  299. writer.createPositionAt( root.getChild( 4 ), 0 ),
  300. writer.createPositionAt( root.getChild( 6 ), 0 )
  301. ) );
  302. } );
  303. // Convert paragraphs, leave numbered list items.
  304. command.execute();
  305. const expectedData =
  306. '<listItem listIndent="0" listType="bulleted">---</listItem>' +
  307. '<listItem listIndent="0" listType="bulleted">---</listItem>' +
  308. '<paragraph>---</paragraph>' +
  309. '<paragraph>---</paragraph>' +
  310. '<listItem listIndent="0" listType="bulleted">[---</listItem>' +
  311. '<listItem listIndent="0" listType="bulleted">---</listItem>' +
  312. '<listItem listIndent="1" listType="bulleted">]---</listItem>' +
  313. '<listItem listIndent="2" listType="bulleted">---</listItem>';
  314. expect( getData( model ) ).to.equal( expectedData );
  315. } );
  316. it( 'should handle outdenting sub-items when list item is turned off', () => {
  317. // From first numbered list item to third bulleted list item.
  318. model.change( writer => {
  319. writer.setSelection( writer.createRange(
  320. writer.createPositionAt( root.getChild( 1 ), 0 ),
  321. writer.createPositionAt( root.getChild( 5 ), 'end' )
  322. ) );
  323. } );
  324. // Convert paragraphs, leave numbered list items.
  325. command.execute();
  326. const expectedData =
  327. '<listItem listIndent="0" listType="bulleted">---</listItem>' +
  328. '<paragraph listIndent="0" listType="bulleted">[---</paragraph>' + // Attributes will be removed by post fixer.
  329. '<paragraph>---</paragraph>' +
  330. '<paragraph>---</paragraph>' +
  331. '<paragraph listIndent="0" listType="numbered">---</paragraph>' + // Attributes will be removed by post fixer.
  332. '<paragraph listIndent="0" listType="numbered">---]</paragraph>' + // Attributes will be removed by post fixer.
  333. '<listItem listIndent="0" listType="bulleted">---</listItem>' +
  334. '<listItem listIndent="1" listType="bulleted">---</listItem>';
  335. expect( getData( model ) ).to.equal( expectedData );
  336. } );
  337. // Example from docs.
  338. it( 'should change type of all items in nested list if one of items changed', () => {
  339. setData(
  340. model,
  341. '<listItem listIndent="0" listType="numbered">---</listItem>' +
  342. '<listItem listIndent="1" listType="numbered">---</listItem>' +
  343. '<listItem listIndent="2" listType="numbered">---</listItem>' +
  344. '<listItem listIndent="1" listType="numbered">---</listItem>' +
  345. '<listItem listIndent="2" listType="numbered">---</listItem>' +
  346. '<listItem listIndent="2" listType="numbered">-[-</listItem>' +
  347. '<listItem listIndent="1" listType="numbered">---</listItem>' +
  348. '<listItem listIndent="1" listType="numbered">---</listItem>' +
  349. '<listItem listIndent="0" listType="numbered">---</listItem>' +
  350. '<listItem listIndent="1" listType="numbered">-]-</listItem>' +
  351. '<listItem listIndent="1" listType="numbered">---</listItem>' +
  352. '<listItem listIndent="2" listType="numbered">---</listItem>' +
  353. '<listItem listIndent="0" listType="numbered">---</listItem>'
  354. );
  355. // * ------ <-- do not fix, top level item
  356. // * ------ <-- fix, because latter list item of this item's list is changed
  357. // * ------ <-- do not fix, item is not affected (different list)
  358. // * ------ <-- fix, because latter list item of this item's list is changed
  359. // * ------ <-- fix, because latter list item of this item's list is changed
  360. // * ---[-- <-- already in selection
  361. // * ------ <-- already in selection
  362. // * ------ <-- already in selection
  363. // * ------ <-- already in selection, but does not cause other list items to change because is top-level
  364. // * ---]-- <-- already in selection
  365. // * ------ <-- fix, because preceding list item of this item's list is changed
  366. // * ------ <-- do not fix, item is not affected (different list)
  367. // * ------ <-- do not fix, top level item
  368. command.execute();
  369. const expectedData =
  370. '<listItem listIndent="0" listType="numbered">---</listItem>' +
  371. '<listItem listIndent="1" listType="bulleted">---</listItem>' +
  372. '<listItem listIndent="2" listType="numbered">---</listItem>' +
  373. '<listItem listIndent="1" listType="bulleted">---</listItem>' +
  374. '<listItem listIndent="2" listType="bulleted">---</listItem>' +
  375. '<listItem listIndent="2" listType="bulleted">-[-</listItem>' +
  376. '<listItem listIndent="1" listType="bulleted">---</listItem>' +
  377. '<listItem listIndent="1" listType="bulleted">---</listItem>' +
  378. '<listItem listIndent="0" listType="bulleted">---</listItem>' +
  379. '<listItem listIndent="1" listType="bulleted">-]-</listItem>' +
  380. '<listItem listIndent="1" listType="bulleted">---</listItem>' +
  381. '<listItem listIndent="2" listType="numbered">---</listItem>' +
  382. '<listItem listIndent="0" listType="numbered">---</listItem>';
  383. expect( getData( model ) ).to.equal( expectedData );
  384. } );
  385. } );
  386. it( 'should fire "_executeCleanup" event after finish all operations with all changed items', done => {
  387. setData( model,
  388. '<paragraph>Foo 1.</paragraph>' +
  389. '<paragraph>[Foo 2.</paragraph>' +
  390. '<paragraph>Foo 3.]</paragraph>' +
  391. '<paragraph>Foo 4.</paragraph>'
  392. );
  393. command.execute();
  394. expect( getData( model ) ).to.equal(
  395. '<paragraph>Foo 1.</paragraph>' +
  396. '<listItem listIndent="0" listType="bulleted">[Foo 2.</listItem>' +
  397. '<listItem listIndent="0" listType="bulleted">Foo 3.]</listItem>' +
  398. '<paragraph>Foo 4.</paragraph>'
  399. );
  400. command.on( '_executeCleanup', ( evt, data ) => {
  401. expect( data ).to.deep.equal( [
  402. root.getChild( 2 ),
  403. root.getChild( 1 )
  404. ] );
  405. done();
  406. } );
  407. command.execute();
  408. } );
  409. } );
  410. } );
  411. } );