listcommand.js 12 KB

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