8
0

listcommand.js 13 KB

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