listcommand.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Editor from 'ckeditor5/core/editor/editor.js';
  6. import Document from 'ckeditor5/engine/model/document.js';
  7. import ListCommand from 'ckeditor5/list/listcommand.js';
  8. import Range from 'ckeditor5/engine/model/range.js';
  9. import Position from 'ckeditor5/engine/model/position.js';
  10. import { setData, getData } from 'ckeditor5/engine/dev-utils/model.js';
  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: 'listItem', attributes: [ 'type', 'indent' ], inside: '$root' } );
  24. doc.schema.disallow( { name: 'listItem', attributes: [ 'type', 'indent' ], inside: 'widget' } );
  25. setData(
  26. doc,
  27. '<paragraph>foo</paragraph>' +
  28. '<listItem type="bulleted" indent="0">bulleted</listItem>' +
  29. '<listItem type="numbered" indent="0">numbered</listItem>' +
  30. '<paragraph>bar</paragraph>' +
  31. '<widget>' +
  32. '<paragraph>xyz</paragraph>' +
  33. '</widget>'
  34. );
  35. doc.selection.collapse( doc.getRoot().getChild( 0 ) );
  36. } );
  37. afterEach( () => {
  38. command.destroy();
  39. } );
  40. describe( 'ListCommand', () => {
  41. describe( 'constructor()', () => {
  42. it( 'should create list command with given type and value set to false', () => {
  43. expect( command.type ).to.equal( 'bulleted' );
  44. expect( command.value ).to.be.false;
  45. const numberedList = new ListCommand( editor, 'numbered' );
  46. expect( numberedList.type ).to.equal( 'numbered' );
  47. } );
  48. } );
  49. describe( 'value', () => {
  50. it( 'should be false if first position in selection is not in a list item', () => {
  51. doc.selection.collapse( doc.getRoot().getChild( 3 ) );
  52. expect( command.value ).to.be.false;
  53. } );
  54. it( 'should be false if first position in selection is in a list item of different type', () => {
  55. doc.selection.collapse( doc.getRoot().getChild( 2 ) );
  56. expect( command.value ).to.be.false;
  57. } );
  58. it( 'should be true if first position in selection is in a list item of same type', () => {
  59. doc.selection.collapse( doc.getRoot().getChild( 1 ) );
  60. expect( command.value ).to.be.true;
  61. } );
  62. } );
  63. describe( 'isEnabled', () => {
  64. it( 'should be true if command value is true', () => {
  65. command.value = true;
  66. command.refreshState();
  67. expect( command.isEnabled ).to.be.true;
  68. command.value = false;
  69. doc.selection.collapse( doc.getRoot().getChild( 1 ) );
  70. expect( command.value ).to.be.true;
  71. expect( command.isEnabled ).to.be.true;
  72. } );
  73. it( 'should be true if selection first position is in a place where listItem can be inserted', () => {
  74. doc.selection.collapse( doc.getRoot(), 2 );
  75. expect( command.isEnabled ).to.be.true;
  76. doc.selection.collapse( doc.getRoot().getChild( 0 ) );
  77. expect( command.isEnabled ).to.be.true;
  78. doc.selection.collapse( doc.getRoot().getChild( 2 ) );
  79. expect( command.isEnabled ).to.be.true;
  80. } );
  81. it( 'should be false if selection first position is in a place where listItem cannot be inserted', () => {
  82. doc.selection.collapse( doc.getRoot().getChild( 4 ) );
  83. expect( command.isEnabled ).to.be.false;
  84. } );
  85. } );
  86. describe( '_doExecute', () => {
  87. describe( 'custom options', () => {
  88. it( 'should use provided batch', () => {
  89. const batch = editor.document.batch();
  90. expect( batch.deltas.length ).to.equal( 0 );
  91. command._doExecute( { batch } );
  92. expect( batch.deltas.length ).to.be.above( 0 );
  93. } );
  94. } );
  95. describe( 'collapsed selection', () => {
  96. it( 'should rename closest block to listItem and set correct attributes', () => {
  97. setData( doc, '<paragraph>fo[]o</paragraph>' );
  98. command._doExecute();
  99. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">fo[]o</listItem>' );
  100. } );
  101. it( 'should rename closest listItem to paragraph and remove attributes', () => {
  102. setData( doc, '<listItem indent="0" type="bulleted">fo[]o</listItem>' );
  103. command._doExecute();
  104. expect( getData( doc ) ).to.equal( '<paragraph>fo[]o</paragraph>' );
  105. } );
  106. it( 'should change closest listItem\' type', () => {
  107. setData( doc, '<listItem indent="0" type="numbered">fo[]o</listItem>' );
  108. command._doExecute();
  109. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">fo[]o</listItem>' );
  110. } );
  111. it( 'should handle outdenting sub-items when list item is turned off', () => {
  112. // Taken from docs.
  113. //
  114. // 1 * --------
  115. // 2 * --------
  116. // 3 * -------- <- this is turned off.
  117. // 4 * -------- <- this has to become indent = 0, because it will be first item on a new list.
  118. // 5 * -------- <- this should be still be a child of item above, so indent = 1.
  119. // 6 * -------- <- this also has to become indent = 0, because it shouldn't end up as a child of any of items above.
  120. // 7 * -------- <- this should be still be a child of item above, so indent = 1.
  121. // 8 * -------- <- this has to become indent = 0.
  122. // 9 * -------- <- this should still be a child of item above, so indent = 1.
  123. // 10 * -------- <- this should still be a child of item above, so indent = 2.
  124. // 11 * -------- <- this should still be at the same level as item above, so indent = 2.
  125. // 12 * -------- <- this and all below are left unchanged.
  126. // 13 * --------
  127. // 14 * --------
  128. //
  129. // After turning off "3", the list becomes:
  130. //
  131. // 1 * --------
  132. // 2 * --------
  133. //
  134. // 3 --------
  135. //
  136. // 4 * --------
  137. // 5 * --------
  138. // 6 * --------
  139. // 7 * --------
  140. // 8 * --------
  141. // 9 * --------
  142. // 10 * --------
  143. // 11 * --------
  144. // 12 * --------
  145. // 13 * --------
  146. // 14 * --------
  147. setData(
  148. doc,
  149. '<listItem indent="0" type="bulleted">---</listItem>' +
  150. '<listItem indent="1" type="bulleted">---</listItem>' +
  151. '<listItem indent="2" type="bulleted">[]---</listItem>' +
  152. '<listItem indent="3" type="bulleted">---</listItem>' +
  153. '<listItem indent="4" type="bulleted">---</listItem>' +
  154. '<listItem indent="2" type="bulleted">---</listItem>' +
  155. '<listItem indent="3" type="bulleted">---</listItem>' +
  156. '<listItem indent="1" type="bulleted">---</listItem>' +
  157. '<listItem indent="2" type="bulleted">---</listItem>' +
  158. '<listItem indent="3" type="bulleted">---</listItem>' +
  159. '<listItem indent="3" type="bulleted">---</listItem>' +
  160. '<listItem indent="0" type="bulleted">---</listItem>' +
  161. '<listItem indent="1" type="bulleted">---</listItem>' +
  162. '<listItem indent="2" type="bulleted">---</listItem>'
  163. );
  164. command._doExecute();
  165. const expectedData =
  166. '<listItem indent="0" type="bulleted">---</listItem>' +
  167. '<listItem indent="1" type="bulleted">---</listItem>' +
  168. '<paragraph>[]---</paragraph>' +
  169. '<listItem indent="0" type="bulleted">---</listItem>' +
  170. '<listItem indent="1" type="bulleted">---</listItem>' +
  171. '<listItem indent="0" type="bulleted">---</listItem>' +
  172. '<listItem indent="1" type="bulleted">---</listItem>' +
  173. '<listItem indent="0" type="bulleted">---</listItem>' +
  174. '<listItem indent="1" type="bulleted">---</listItem>' +
  175. '<listItem indent="2" type="bulleted">---</listItem>' +
  176. '<listItem indent="2" type="bulleted">---</listItem>' +
  177. '<listItem indent="0" type="bulleted">---</listItem>' +
  178. '<listItem indent="1" type="bulleted">---</listItem>' +
  179. '<listItem indent="2" type="bulleted">---</listItem>';
  180. expect( getData( doc ) ).to.equal( expectedData );
  181. } );
  182. } );
  183. describe( 'non-collapsed selection', () => {
  184. beforeEach( () => {
  185. setData(
  186. doc,
  187. '<listItem indent="0" type="bulleted">---</listItem>' +
  188. '<listItem indent="0" type="bulleted">---</listItem>' +
  189. '<paragraph>---</paragraph>' +
  190. '<paragraph>---</paragraph>' +
  191. '<listItem indent="0" type="numbered">---</listItem>' +
  192. '<listItem indent="0" type="numbered">---</listItem>' +
  193. '<listItem indent="1" type="bulleted">---</listItem>' +
  194. '<listItem indent="2" type="bulleted">---</listItem>'
  195. );
  196. } );
  197. it( 'should rename closest block to listItem and set correct attributes', () => {
  198. // From first paragraph to second paragraph.
  199. // Command value=false, we are turning on list items.
  200. doc.selection.setRanges( [ new Range(
  201. Position.createAt( root.getChild( 2 ) ),
  202. Position.createAt( root.getChild( 3 ) )
  203. ) ] );
  204. command._doExecute();
  205. const expectedData =
  206. '<listItem indent="0" type="bulleted">---</listItem>' +
  207. '<listItem indent="0" type="bulleted">---</listItem>' +
  208. '<listItem indent="0" type="bulleted">[---</listItem>' +
  209. '<listItem indent="0" type="bulleted">]---</listItem>' +
  210. '<listItem indent="0" type="numbered">---</listItem>' +
  211. '<listItem indent="0" type="numbered">---</listItem>' +
  212. '<listItem indent="1" type="bulleted">---</listItem>' +
  213. '<listItem indent="2" type="bulleted">---</listItem>';
  214. expect( getData( doc ) ).to.equal( expectedData );
  215. } );
  216. it( 'should rename closest listItem to paragraph and remove attributes', () => {
  217. // From second bullet list item to first numbered list item.
  218. // Command value=true, we are turning off list items.
  219. doc.selection.setRanges( [ new Range(
  220. Position.createAt( root.getChild( 1 ) ),
  221. Position.createAt( root.getChild( 4 ) )
  222. ) ] );
  223. // Convert paragraphs, leave numbered list items.
  224. command._doExecute();
  225. const expectedData =
  226. '<listItem indent="0" type="bulleted">---</listItem>' +
  227. '<paragraph>[---</paragraph>' +
  228. '<paragraph>---</paragraph>' +
  229. '<paragraph>---</paragraph>' +
  230. '<paragraph>]---</paragraph>' +
  231. '<listItem indent="0" type="numbered">---</listItem>' +
  232. '<listItem indent="1" type="bulleted">---</listItem>' +
  233. '<listItem indent="2" type="bulleted">---</listItem>';
  234. expect( getData( doc ) ).to.equal( expectedData );
  235. } );
  236. it( 'should change closest listItem\'s type', () => {
  237. // From first numbered lsit item to third bulleted list item.
  238. doc.selection.setRanges( [ new Range(
  239. Position.createAt( root.getChild( 4 ) ),
  240. Position.createAt( root.getChild( 6 ) )
  241. ) ] );
  242. // Convert paragraphs, leave numbered list items.
  243. command._doExecute();
  244. const expectedData =
  245. '<listItem indent="0" type="bulleted">---</listItem>' +
  246. '<listItem indent="0" type="bulleted">---</listItem>' +
  247. '<paragraph>---</paragraph>' +
  248. '<paragraph>---</paragraph>' +
  249. '<listItem indent="0" type="bulleted">[---</listItem>' +
  250. '<listItem indent="0" type="bulleted">---</listItem>' +
  251. '<listItem indent="1" type="bulleted">]---</listItem>' +
  252. '<listItem indent="2" type="bulleted">---</listItem>';
  253. expect( getData( doc ) ).to.equal( expectedData );
  254. } );
  255. it( 'should handle outdenting sub-items when list item is turned off', () => {
  256. // From first numbered lsit item to third bulleted list item.
  257. doc.selection.setRanges( [ new Range(
  258. Position.createAt( root.getChild( 1 ) ),
  259. Position.createAt( root.getChild( 5 ) )
  260. ) ] );
  261. // Convert paragraphs, leave numbered list items.
  262. command._doExecute();
  263. const expectedData =
  264. '<listItem indent="0" type="bulleted">---</listItem>' +
  265. '<paragraph>[---</paragraph>' +
  266. '<paragraph>---</paragraph>' +
  267. '<paragraph>---</paragraph>' +
  268. '<paragraph>---</paragraph>' +
  269. '<paragraph>]---</paragraph>' +
  270. '<listItem indent="0" type="bulleted">---</listItem>' +
  271. '<listItem indent="1" type="bulleted">---</listItem>';
  272. expect( getData( doc ) ).to.equal( expectedData );
  273. } );
  274. } );
  275. } );
  276. } );
  277. } );