8
0

listcommand.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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( 'collapsed selection', () => {
  87. it( 'should rename closest block to listItem and set correct attributes', () => {
  88. setData( doc, '<paragraph>fo[]o</paragraph>' );
  89. command._doExecute();
  90. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">fo[]o</listItem>' );
  91. } );
  92. it( 'should rename closest listItem to paragraph and remove attributes', () => {
  93. setData( doc, '<listItem indent="0" type="bulleted">fo[]o</listItem>' );
  94. command._doExecute();
  95. expect( getData( doc ) ).to.equal( '<paragraph>fo[]o</paragraph>' );
  96. } );
  97. it( 'should change closest listItem\' type', () => {
  98. setData( doc, '<listItem indent="0" type="numbered">fo[]o</listItem>' );
  99. command._doExecute();
  100. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">fo[]o</listItem>' );
  101. } );
  102. it( 'should handle outdenting sub-items when list item is turned off', () => {
  103. // Taken from docs.
  104. //
  105. // 1 * --------
  106. // 2 * --------
  107. // 3 * -------- <- this is turned off.
  108. // 4 * -------- <- this has to become indent = 0, because it will be first item on a new list.
  109. // 5 * -------- <- this should be still be a child of item above, so indent = 1.
  110. // 6 * -------- <- this also has to become indent = 0, because it shouldn't end up as a child of any of items above.
  111. // 7 * -------- <- this should be still be a child of item above, so indent = 1.
  112. // 8 * -------- <- this has to become indent = 0.
  113. // 9 * -------- <- this should still be a child of item above, so indent = 1.
  114. // 10 * -------- <- this should still be a child of item above, so indent = 2.
  115. // 11 * -------- <- this should still be at the same level as item above, so indent = 2.
  116. // 12 * -------- <- this and all below are left unchanged.
  117. // 13 * --------
  118. // 14 * --------
  119. //
  120. // After turning off "3", the list becomes:
  121. //
  122. // 1 * --------
  123. // 2 * --------
  124. //
  125. // 3 --------
  126. //
  127. // 4 * --------
  128. // 5 * --------
  129. // 6 * --------
  130. // 7 * --------
  131. // 8 * --------
  132. // 9 * --------
  133. // 10 * --------
  134. // 11 * --------
  135. // 12 * --------
  136. // 13 * --------
  137. // 14 * --------
  138. setData(
  139. doc,
  140. '<listItem indent="0" type="bulleted">---</listItem>' +
  141. '<listItem indent="1" type="bulleted">---</listItem>' +
  142. '<listItem indent="2" type="bulleted">[]---</listItem>' +
  143. '<listItem indent="3" type="bulleted">---</listItem>' +
  144. '<listItem indent="4" type="bulleted">---</listItem>' +
  145. '<listItem indent="2" type="bulleted">---</listItem>' +
  146. '<listItem indent="3" type="bulleted">---</listItem>' +
  147. '<listItem indent="1" type="bulleted">---</listItem>' +
  148. '<listItem indent="2" type="bulleted">---</listItem>' +
  149. '<listItem indent="3" type="bulleted">---</listItem>' +
  150. '<listItem indent="3" type="bulleted">---</listItem>' +
  151. '<listItem indent="0" type="bulleted">---</listItem>' +
  152. '<listItem indent="1" type="bulleted">---</listItem>' +
  153. '<listItem indent="2" type="bulleted">---</listItem>'
  154. );
  155. command._doExecute();
  156. const expectedData =
  157. '<listItem indent="0" type="bulleted">---</listItem>' +
  158. '<listItem indent="1" type="bulleted">---</listItem>' +
  159. '<paragraph>[]---</paragraph>' +
  160. '<listItem indent="0" type="bulleted">---</listItem>' +
  161. '<listItem indent="1" type="bulleted">---</listItem>' +
  162. '<listItem indent="0" type="bulleted">---</listItem>' +
  163. '<listItem indent="1" type="bulleted">---</listItem>' +
  164. '<listItem indent="0" type="bulleted">---</listItem>' +
  165. '<listItem indent="1" type="bulleted">---</listItem>' +
  166. '<listItem indent="2" type="bulleted">---</listItem>' +
  167. '<listItem indent="2" type="bulleted">---</listItem>' +
  168. '<listItem indent="0" type="bulleted">---</listItem>' +
  169. '<listItem indent="1" type="bulleted">---</listItem>' +
  170. '<listItem indent="2" type="bulleted">---</listItem>';
  171. expect( getData( doc ) ).to.equal( expectedData );
  172. } );
  173. } );
  174. describe( 'non-collapsed selection', () => {
  175. beforeEach( () => {
  176. setData(
  177. doc,
  178. '<listItem indent="0" type="bulleted">---</listItem>' +
  179. '<listItem indent="0" type="bulleted">---</listItem>' +
  180. '<paragraph>---</paragraph>' +
  181. '<paragraph>---</paragraph>' +
  182. '<listItem indent="0" type="numbered">---</listItem>' +
  183. '<listItem indent="0" type="numbered">---</listItem>' +
  184. '<listItem indent="1" type="bulleted">---</listItem>' +
  185. '<listItem indent="2" type="bulleted">---</listItem>'
  186. );
  187. } );
  188. it( 'should rename closest block to listItem and set correct attributes', () => {
  189. // From first paragraph to second paragraph.
  190. // Command value=false, we are turning on list items.
  191. doc.selection.setRanges( [ new Range(
  192. Position.createAt( root.getChild( 2 ) ),
  193. Position.createAt( root.getChild( 3 ) )
  194. ) ] );
  195. command._doExecute();
  196. const expectedData =
  197. '<listItem indent="0" type="bulleted">---</listItem>' +
  198. '<listItem indent="0" type="bulleted">---</listItem>' +
  199. '<listItem indent="0" type="bulleted">[---</listItem>' +
  200. '<listItem indent="0" type="bulleted">]---</listItem>' +
  201. '<listItem indent="0" type="numbered">---</listItem>' +
  202. '<listItem indent="0" type="numbered">---</listItem>' +
  203. '<listItem indent="1" type="bulleted">---</listItem>' +
  204. '<listItem indent="2" type="bulleted">---</listItem>';
  205. expect( getData( doc ) ).to.equal( expectedData );
  206. } );
  207. it( 'should rename closest listItem to paragraph and remove attributes', () => {
  208. // From second bullet list item to first numbered list item.
  209. // Command value=true, we are turning off list items.
  210. doc.selection.setRanges( [ new Range(
  211. Position.createAt( root.getChild( 1 ) ),
  212. Position.createAt( root.getChild( 4 ) )
  213. ) ] );
  214. // Convert paragraphs, leave numbered list items.
  215. command._doExecute();
  216. const expectedData =
  217. '<listItem indent="0" type="bulleted">---</listItem>' +
  218. '<paragraph>[---</paragraph>' +
  219. '<paragraph>---</paragraph>' +
  220. '<paragraph>---</paragraph>' +
  221. '<paragraph>]---</paragraph>' +
  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 change closest listItem\'s type', () => {
  228. // From first numbered lsit item to third bulleted list item.
  229. doc.selection.setRanges( [ new Range(
  230. Position.createAt( root.getChild( 4 ) ),
  231. Position.createAt( root.getChild( 6 ) )
  232. ) ] );
  233. // Convert paragraphs, leave numbered list items.
  234. command._doExecute();
  235. const expectedData =
  236. '<listItem indent="0" type="bulleted">---</listItem>' +
  237. '<listItem indent="0" type="bulleted">---</listItem>' +
  238. '<paragraph>---</paragraph>' +
  239. '<paragraph>---</paragraph>' +
  240. '<listItem indent="0" type="bulleted">[---</listItem>' +
  241. '<listItem indent="0" type="bulleted">---</listItem>' +
  242. '<listItem indent="1" type="bulleted">]---</listItem>' +
  243. '<listItem indent="2" type="bulleted">---</listItem>';
  244. expect( getData( doc ) ).to.equal( expectedData );
  245. } );
  246. it( 'should handle outdenting sub-items when list item is turned off', () => {
  247. // From first numbered lsit item to third bulleted list item.
  248. doc.selection.setRanges( [ new Range(
  249. Position.createAt( root.getChild( 1 ) ),
  250. Position.createAt( root.getChild( 5 ) )
  251. ) ] );
  252. // Convert paragraphs, leave numbered list items.
  253. command._doExecute();
  254. const expectedData =
  255. '<listItem indent="0" type="bulleted">---</listItem>' +
  256. '<paragraph>[---</paragraph>' +
  257. '<paragraph>---</paragraph>' +
  258. '<paragraph>---</paragraph>' +
  259. '<paragraph>---</paragraph>' +
  260. '<paragraph>]---</paragraph>' +
  261. '<listItem indent="0" type="bulleted">---</listItem>' +
  262. '<listItem indent="1" type="bulleted">---</listItem>';
  263. expect( getData( doc ) ).to.equal( expectedData );
  264. } );
  265. } );
  266. } );
  267. } );