listcommand.js 18 KB

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