liststylecommand.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  6. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import ListStyleEditing from '../src/liststyleediting';
  9. describe( 'ListStyleCommand', () => {
  10. let editor, model, bulletedListCommand, numberedListCommand, listStyleCommand;
  11. beforeEach( () => {
  12. return VirtualTestEditor
  13. .create( {
  14. plugins: [ Paragraph, ListStyleEditing ]
  15. } )
  16. .then( newEditor => {
  17. editor = newEditor;
  18. model = editor.model;
  19. bulletedListCommand = editor.commands.get( 'bulletedList' );
  20. numberedListCommand = editor.commands.get( 'numberedList' );
  21. listStyleCommand = editor.commands.get( 'listStyle' );
  22. } );
  23. } );
  24. afterEach( () => {
  25. return editor.destroy();
  26. } );
  27. describe( '#isEnabled', () => {
  28. it( 'should be true if bulletedList or numberedList is enabled', () => {
  29. bulletedListCommand.isEnabled = true;
  30. numberedListCommand.isEnabled = false;
  31. listStyleCommand.refresh();
  32. expect( listStyleCommand.isEnabled ).to.equal( true );
  33. bulletedListCommand.isEnabled = false;
  34. numberedListCommand.isEnabled = true;
  35. listStyleCommand.refresh();
  36. expect( listStyleCommand.isEnabled ).to.equal( true );
  37. } );
  38. it( 'should be false if bulletedList and numberedList are enabled', () => {
  39. bulletedListCommand.isEnabled = false;
  40. numberedListCommand.isEnabled = false;
  41. listStyleCommand.refresh();
  42. expect( listStyleCommand.isEnabled ).to.equal( false );
  43. } );
  44. } );
  45. describe( '#value', () => {
  46. it( 'should return null if selected a paragraph', () => {
  47. setData( model, '<paragraph>Foo[]</paragraph>' );
  48. expect( listStyleCommand.value ).to.equal( null );
  49. } );
  50. it( 'should return null if selection starts in a paragraph and ends in a list item', () => {
  51. setData( model,
  52. '<paragraph>Fo[o</paragraph>' +
  53. '<listItem listIndent="0" listType="bulleted" listStyle="default">Foo]</listItem>'
  54. );
  55. expect( listStyleCommand.value ).to.equal( null );
  56. } );
  57. it( 'should return the value of `listStyle` attribute if selection is inside a listItem (collapsed selection)', () => {
  58. setData( model, '<listItem listIndent="0" listType="bulleted" listStyle="default">Foo[]</listItem>' );
  59. expect( listStyleCommand.value ).to.equal( 'default' );
  60. } );
  61. it( 'should return the value of `listStyle` attribute if selection is inside a listItem (non-collapsed selection)', () => {
  62. setData( model, '<listItem listIndent="0" listType="bulleted" listStyle="default">[Foo]</listItem>' );
  63. expect( listStyleCommand.value ).to.equal( 'default' );
  64. } );
  65. it( 'should return the value of `listStyle` attribute if selected more elements in the same list', () => {
  66. setData( model,
  67. '<listItem listIndent="0" listType="bulleted" listStyle="square">[1.</listItem>' +
  68. '<listItem listIndent="0" listType="bulleted" listStyle="square">2.]</listItem>' +
  69. '<listItem listIndent="0" listType="bulleted" listStyle="square">3.</listItem>'
  70. );
  71. expect( listStyleCommand.value ).to.equal( 'square' );
  72. } );
  73. it( 'should return the value of `listStyle` attribute for the selection inside a nested list', () => {
  74. setData( model,
  75. '<listItem listIndent="0" listType="bulleted" listStyle="square">1.</listItem>' +
  76. '<listItem listIndent="0" listType="bulleted" listStyle="disc">1.1.[]</listItem>' +
  77. '<listItem listIndent="0" listType="bulleted" listStyle="square">2.</listItem>'
  78. );
  79. expect( listStyleCommand.value ).to.equal( 'disc' );
  80. } );
  81. it( 'should return the value of `listStyle` attribute from a list where the selection starts (selection over nested list)', () => {
  82. setData( model,
  83. '<listItem listIndent="0" listType="bulleted" listStyle="square">1.</listItem>' +
  84. '<listItem listIndent="0" listType="bulleted" listStyle="disc">1.1.[</listItem>' +
  85. '<listItem listIndent="0" listType="bulleted" listStyle="square">2.]</listItem>'
  86. );
  87. expect( listStyleCommand.value ).to.equal( 'disc' );
  88. } );
  89. } );
  90. describe( 'execute()', () => {
  91. it( 'should set the `listStyle` attribute for collapsed selection', () => {
  92. setData( model,
  93. '<listItem listIndent="0" listStyle="default" listType="bulleted">1.[]</listItem>'
  94. );
  95. listStyleCommand.execute( { type: 'circle' } );
  96. expect( getData( model ) ).to.equal(
  97. '<listItem listIndent="0" listStyle="circle" listType="bulleted">1.[]</listItem>'
  98. );
  99. } );
  100. it( 'should set the `listStyle` attribute for non-collapsed selection', () => {
  101. setData( model,
  102. '<listItem listIndent="0" listStyle="default" listType="bulleted">[1.]</listItem>'
  103. );
  104. listStyleCommand.execute( { type: 'circle' } );
  105. expect( getData( model ) ).to.equal(
  106. '<listItem listIndent="0" listStyle="circle" listType="bulleted">[1.]</listItem>'
  107. );
  108. } );
  109. it( 'should set the `listStyle` attribute for all the same list items (collapsed selection)', () => {
  110. setData( model,
  111. '<listItem listIndent="0" listStyle="default" listType="bulleted">1.[]</listItem>' +
  112. '<listItem listIndent="0" listStyle="default" listType="bulleted">2.</listItem>' +
  113. '<listItem listIndent="0" listStyle="default" listType="bulleted">3.</listItem>'
  114. );
  115. listStyleCommand.execute( { type: 'circle' } );
  116. expect( getData( model ) ).to.equal(
  117. '<listItem listIndent="0" listStyle="circle" listType="bulleted">1.[]</listItem>' +
  118. '<listItem listIndent="0" listStyle="circle" listType="bulleted">2.</listItem>' +
  119. '<listItem listIndent="0" listStyle="circle" listType="bulleted">3.</listItem>'
  120. );
  121. } );
  122. it( 'should set the `listStyle` attribute for all the same list items and ignores nested lists', () => {
  123. setData( model,
  124. '<listItem listIndent="0" listStyle="default" listType="bulleted">1.[]</listItem>' +
  125. '<listItem listIndent="0" listStyle="default" listType="bulleted">2.</listItem>' +
  126. '<listItem listIndent="1" listStyle="default" listType="bulleted">2.1.</listItem>' +
  127. '<listItem listIndent="1" listStyle="default" listType="bulleted">2.2.</listItem>' +
  128. '<listItem listIndent="0" listStyle="default" listType="bulleted">3.</listItem>' +
  129. '<listItem listIndent="1" listStyle="default" listType="bulleted">3.1.</listItem>'
  130. );
  131. listStyleCommand.execute( { type: 'circle' } );
  132. expect( getData( model ) ).to.equal(
  133. '<listItem listIndent="0" listStyle="circle" listType="bulleted">1.[]</listItem>' +
  134. '<listItem listIndent="0" listStyle="circle" listType="bulleted">2.</listItem>' +
  135. '<listItem listIndent="1" listStyle="default" listType="bulleted">2.1.</listItem>' +
  136. '<listItem listIndent="1" listStyle="default" listType="bulleted">2.2.</listItem>' +
  137. '<listItem listIndent="0" listStyle="circle" listType="bulleted">3.</listItem>' +
  138. '<listItem listIndent="1" listStyle="default" listType="bulleted">3.1.</listItem>'
  139. );
  140. } );
  141. it( 'should set the `listStyle` attribute for all the same list items and ignores "parent" list (selection in nested list)', () => {
  142. setData( model,
  143. '<listItem listIndent="0" listStyle="default" listType="bulleted">1.</listItem>' +
  144. '<listItem listIndent="0" listStyle="default" listType="bulleted">2.</listItem>' +
  145. '<listItem listIndent="1" listStyle="default" listType="bulleted">2.1.[]</listItem>' +
  146. '<listItem listIndent="1" listStyle="default" listType="bulleted">2.2.</listItem>' +
  147. '<listItem listIndent="0" listStyle="default" listType="bulleted">3.</listItem>' +
  148. '<listItem listIndent="1" listStyle="default" listType="bulleted">3.1.</listItem>'
  149. );
  150. listStyleCommand.execute( { type: 'disc' } );
  151. expect( getData( model ) ).to.equal(
  152. '<listItem listIndent="0" listStyle="default" listType="bulleted">1.</listItem>' +
  153. '<listItem listIndent="0" listStyle="default" listType="bulleted">2.</listItem>' +
  154. '<listItem listIndent="1" listStyle="disc" listType="bulleted">2.1.[]</listItem>' +
  155. '<listItem listIndent="1" listStyle="disc" listType="bulleted">2.2.</listItem>' +
  156. '<listItem listIndent="0" listStyle="default" listType="bulleted">3.</listItem>' +
  157. '<listItem listIndent="1" listStyle="default" listType="bulleted">3.1.</listItem>'
  158. );
  159. } );
  160. it( 'should stop searching for the list items when spotted non-listItem element', () => {
  161. setData( model,
  162. '<paragraph>Foo.</paragraph>' +
  163. '<listItem listIndent="0" listStyle="default" listType="bulleted">1.[]</listItem>' +
  164. '<listItem listIndent="0" listStyle="default" listType="bulleted">2.</listItem>' +
  165. '<listItem listIndent="0" listStyle="default" listType="bulleted">3.</listItem>'
  166. );
  167. listStyleCommand.execute( { type: 'circle' } );
  168. expect( getData( model ) ).to.equal(
  169. '<paragraph>Foo.</paragraph>' +
  170. '<listItem listIndent="0" listStyle="circle" listType="bulleted">1.[]</listItem>' +
  171. '<listItem listIndent="0" listStyle="circle" listType="bulleted">2.</listItem>' +
  172. '<listItem listIndent="0" listStyle="circle" listType="bulleted">3.</listItem>'
  173. );
  174. } );
  175. it( 'should stop searching for the list items when spotted listItem with different listType attribute', () => {
  176. setData( model,
  177. '<paragraph>Foo.</paragraph>' +
  178. '<listItem listIndent="0" listStyle="default" listType="bulleted">1.[]</listItem>' +
  179. '<listItem listIndent="0" listStyle="default" listType="bulleted">2.</listItem>' +
  180. '<listItem listIndent="0" listStyle="default" listType="numbered">1.</listItem>'
  181. );
  182. listStyleCommand.execute( { type: 'circle' } );
  183. expect( getData( model ) ).to.equal(
  184. '<paragraph>Foo.</paragraph>' +
  185. '<listItem listIndent="0" listStyle="circle" listType="bulleted">1.[]</listItem>' +
  186. '<listItem listIndent="0" listStyle="circle" listType="bulleted">2.</listItem>' +
  187. '<listItem listIndent="0" listStyle="default" listType="numbered">1.</listItem>'
  188. );
  189. } );
  190. it( 'should stop searching for the list items when spotted listItem with different listStyle attribute', () => {
  191. setData( model,
  192. '<paragraph>Foo.</paragraph>' +
  193. '<listItem listIndent="0" listStyle="default" listType="bulleted">1.[]</listItem>' +
  194. '<listItem listIndent="0" listStyle="default" listType="bulleted">2.</listItem>' +
  195. '<listItem listIndent="0" listStyle="disc" listType="bulleted">1.</listItem>'
  196. );
  197. listStyleCommand.execute( { type: 'circle' } );
  198. expect( getData( model ) ).to.equal(
  199. '<paragraph>Foo.</paragraph>' +
  200. '<listItem listIndent="0" listStyle="circle" listType="bulleted">1.[]</listItem>' +
  201. '<listItem listIndent="0" listStyle="circle" listType="bulleted">2.</listItem>' +
  202. '<listItem listIndent="0" listStyle="disc" listType="bulleted">1.</listItem>'
  203. );
  204. } );
  205. it( 'should start searching for the list items from starting position (collapsed selection)', () => {
  206. setData( model,
  207. '<listItem listIndent="0" listStyle="default" listType="bulleted">1.</listItem>' +
  208. '<listItem listIndent="0" listStyle="default" listType="bulleted">2.</listItem>' +
  209. '<listItem listIndent="0" listStyle="default" listType="bulleted">[3.</listItem>' +
  210. '<paragraph>Foo.]</paragraph>'
  211. );
  212. listStyleCommand.execute( { type: 'circle' } );
  213. expect( getData( model ) ).to.equal(
  214. '<listItem listIndent="0" listStyle="circle" listType="bulleted">1.</listItem>' +
  215. '<listItem listIndent="0" listStyle="circle" listType="bulleted">2.</listItem>' +
  216. '<listItem listIndent="0" listStyle="circle" listType="bulleted">[3.</listItem>' +
  217. '<paragraph>Foo.]</paragraph>'
  218. );
  219. } );
  220. it( 'should start searching for the list items from ending position (collapsed selection)', () => {
  221. setData( model,
  222. '<paragraph>[Foo.</paragraph>' +
  223. '<listItem listIndent="0" listStyle="default" listType="bulleted">1.]</listItem>' +
  224. '<listItem listIndent="0" listStyle="default" listType="bulleted">2.</listItem>' +
  225. '<listItem listIndent="0" listStyle="default" listType="bulleted">3.</listItem>'
  226. );
  227. listStyleCommand.execute( { type: 'circle' } );
  228. expect( getData( model ) ).to.equal(
  229. '<paragraph>[Foo.</paragraph>' +
  230. '<listItem listIndent="0" listStyle="circle" listType="bulleted">1.]</listItem>' +
  231. '<listItem listIndent="0" listStyle="circle" listType="bulleted">2.</listItem>' +
  232. '<listItem listIndent="0" listStyle="circle" listType="bulleted">3.</listItem>'
  233. );
  234. } );
  235. it( 'should use default type if not specified (no options passed)', () => {
  236. setData( model,
  237. '<listItem listIndent="0" listStyle="circle" listType="bulleted">1.[]</listItem>'
  238. );
  239. listStyleCommand.execute();
  240. expect( getData( model ) ).to.equal(
  241. '<listItem listIndent="0" listStyle="default" listType="bulleted">1.[]</listItem>'
  242. );
  243. } );
  244. it( 'should use default type if not specified (passed an empty object)', () => {
  245. setData( model,
  246. '<listItem listIndent="0" listStyle="circle" listType="bulleted">1.[]</listItem>'
  247. );
  248. listStyleCommand.execute( {} );
  249. expect( getData( model ) ).to.equal(
  250. '<listItem listIndent="0" listStyle="default" listType="bulleted">1.[]</listItem>'
  251. );
  252. } );
  253. it( 'should use default type if not specified (passed null as value)', () => {
  254. setData( model,
  255. '<listItem listIndent="0" listStyle="circle" listType="bulleted">1.[]</listItem>'
  256. );
  257. listStyleCommand.execute( { type: null } );
  258. expect( getData( model ) ).to.equal(
  259. '<listItem listIndent="0" listStyle="default" listType="bulleted">1.[]</listItem>'
  260. );
  261. } );
  262. it( 'should not update anything if no listItem found in the selection', () => {
  263. setData( model,
  264. '<paragraph>[Foo.]</paragraph>' +
  265. '<listItem listIndent="0" listStyle="default" listType="bulleted">1.</listItem>'
  266. );
  267. const modelChangeStub = sinon.stub( model, 'change' ).named( 'model#change' );
  268. listStyleCommand.execute( { type: 'circle' } );
  269. expect( getData( model ) ).to.equal(
  270. '<paragraph>[Foo.]</paragraph>' +
  271. '<listItem listIndent="0" listStyle="default" listType="bulleted">1.</listItem>'
  272. );
  273. expect( modelChangeStub.called ).to.equal( false );
  274. } );
  275. it( 'should update all items that belong to selected elements', () => {
  276. // [x] = items that should be updated.
  277. // All list items that belong to the same lists that selected items should be updated.
  278. // "2." is the most outer list (listIndent=0)
  279. // "2.1" a child list of the "2." element (listIndent=1)
  280. // "2.1.1" a child list of the "2.1" element (listIndent=2)
  281. //
  282. // [x] ■ 1.
  283. // [x] ■ [2.
  284. // [x] ○ 2.1.
  285. // [x] ▶ 2.1.1.]
  286. // [x] ▶ 2.1.2.
  287. // [x] ○ 2.2.
  288. // [x] ■ 3.
  289. // [ ] ○ 3.1.
  290. // [ ] ▶ 3.1.1.
  291. //
  292. // "3.1" is not selected and this list should not be updated.
  293. setData( model,
  294. '<listItem listIndent="0" listStyle="default" listType="bulleted">1.</listItem>' +
  295. '<listItem listIndent="0" listStyle="default" listType="bulleted">[2.</listItem>' +
  296. '<listItem listIndent="1" listStyle="default" listType="bulleted">2.1.</listItem>' +
  297. '<listItem listIndent="2" listStyle="default" listType="bulleted">2.1.1.]</listItem>' +
  298. '<listItem listIndent="2" listStyle="default" listType="bulleted">2.1.2.</listItem>' +
  299. '<listItem listIndent="1" listStyle="default" listType="bulleted">2.2.</listItem>' +
  300. '<listItem listIndent="0" listStyle="default" listType="bulleted">3.</listItem>' +
  301. '<listItem listIndent="1" listStyle="default" listType="bulleted">3.1.</listItem>' +
  302. '<listItem listIndent="2" listStyle="default" listType="bulleted">3.1.1.</listItem>'
  303. );
  304. listStyleCommand.execute( { type: 'disc' } );
  305. expect( getData( model ) ).to.equal(
  306. '<listItem listIndent="0" listStyle="disc" listType="bulleted">1.</listItem>' +
  307. '<listItem listIndent="0" listStyle="disc" listType="bulleted">[2.</listItem>' +
  308. '<listItem listIndent="1" listStyle="disc" listType="bulleted">2.1.</listItem>' +
  309. '<listItem listIndent="2" listStyle="disc" listType="bulleted">2.1.1.]</listItem>' +
  310. '<listItem listIndent="2" listStyle="disc" listType="bulleted">2.1.2.</listItem>' +
  311. '<listItem listIndent="1" listStyle="disc" listType="bulleted">2.2.</listItem>' +
  312. '<listItem listIndent="0" listStyle="disc" listType="bulleted">3.</listItem>' +
  313. '<listItem listIndent="1" listStyle="default" listType="bulleted">3.1.</listItem>' +
  314. '<listItem listIndent="2" listStyle="default" listType="bulleted">3.1.1.</listItem>'
  315. );
  316. } );
  317. } );
  318. } );