listcommand.js 17 KB

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