liststyleui.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. /* globals document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import BlockQuote from '@ckeditor/ckeditor5-block-quote/src/blockquote';
  9. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  10. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  11. import ListStyle from '../src/liststyle';
  12. import ListStyleUI from '../src/liststyleui';
  13. import DropdownView from '@ckeditor/ckeditor5-ui/src/dropdown/dropdownview';
  14. import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
  15. import bulletedListIcon from '../theme/icons/bulletedlist.svg';
  16. import numberedListIcon from '../theme/icons/numberedlist.svg';
  17. import listStyleDiscIcon from '../theme/icons/liststyledisc.svg';
  18. import listStyleCircleIcon from '../theme/icons/liststylecircle.svg';
  19. import listStyleSquareIcon from '../theme/icons/liststylesquare.svg';
  20. import listStyleDecimalIcon from '../theme/icons/liststyledecimal.svg';
  21. import listStyleDecimalWithLeadingZeroIcon from '../theme/icons/liststyledecimalleadingzero.svg';
  22. import listStyleLowerRomanIcon from '../theme/icons/liststylelowerroman.svg';
  23. import listStyleUpperRomanIcon from '../theme/icons/liststyleupperroman.svg';
  24. import listStyleLowerLatinIcon from '../theme/icons/liststylelowerlatin.svg';
  25. import listStyleUpperLatinIcon from '../theme/icons/liststyleupperlatin.svg';
  26. describe( 'ListStyleUI', () => {
  27. let editorElement, editor, model, listStyleCommand;
  28. beforeEach( () => {
  29. editorElement = document.createElement( 'div' );
  30. document.body.appendChild( editorElement );
  31. return ClassicTestEditor.create( editorElement, { plugins: [ Paragraph, BlockQuote, ListStyle, UndoEditing ] } )
  32. .then( newEditor => {
  33. editor = newEditor;
  34. model = editor.model;
  35. listStyleCommand = editor.commands.get( 'listStyle' );
  36. } );
  37. } );
  38. afterEach( () => {
  39. editorElement.remove();
  40. return editor.destroy();
  41. } );
  42. it( 'should be named', () => {
  43. expect( ListStyleUI.pluginName ).to.equal( 'ListStyleUI' );
  44. } );
  45. it( 'should be loaded', () => {
  46. expect( editor.plugins.get( ListStyleUI ) ).to.be.instanceOf( ListStyleUI );
  47. } );
  48. describe( 'init()', () => {
  49. describe( 'bulleted list dropdown', () => {
  50. let bulletedListCommand, bulletedListDropdown;
  51. beforeEach( () => {
  52. bulletedListCommand = editor.commands.get( 'bulletedList' );
  53. bulletedListDropdown = editor.ui.componentFactory.create( 'bulletedList' );
  54. } );
  55. it( 'should registered as "bulletedList" in the component factory', () => {
  56. expect( bulletedListDropdown ).to.be.instanceOf( DropdownView );
  57. } );
  58. it( 'should have #isEnabled bound to the "bulletedList" command state', () => {
  59. expect( bulletedListDropdown.isEnabled ).to.be.true;
  60. bulletedListCommand.isEnabled = true;
  61. expect( bulletedListDropdown.isEnabled ).to.be.true;
  62. bulletedListCommand.isEnabled = false;
  63. expect( bulletedListDropdown.isEnabled ).to.be.false;
  64. } );
  65. it( 'should have a specific CSS class', () => {
  66. expect( bulletedListDropdown.class ).to.equal( 'ck-list-styles-dropdown' );
  67. } );
  68. describe( 'main split button', () => {
  69. let mainButtonView;
  70. beforeEach( () => {
  71. mainButtonView = bulletedListDropdown.buttonView;
  72. } );
  73. it( 'should have a #label', () => {
  74. expect( mainButtonView.label ).to.equal( 'Bulleted List' );
  75. } );
  76. it( 'should have an #icon', () => {
  77. expect( mainButtonView.icon ).to.equal( bulletedListIcon );
  78. } );
  79. it( 'should have a #tooltip based on a label', () => {
  80. expect( mainButtonView.tooltip ).to.be.true;
  81. } );
  82. it( 'should be toggleable', () => {
  83. expect( mainButtonView.isToggleable ).to.be.true;
  84. } );
  85. it( 'should have the #isOn state bound to the value of the "bulletedList" command', () => {
  86. expect( mainButtonView.isOn ).to.be.false;
  87. bulletedListCommand.value = 'foo';
  88. expect( mainButtonView.isOn ).to.be.true;
  89. bulletedListCommand.value = null;
  90. expect( mainButtonView.isOn ).to.be.false;
  91. } );
  92. it( 'should execute the "bulletedList" command and focus the editing view when clicked', () => {
  93. sinon.spy( editor, 'execute' );
  94. sinon.spy( editor.editing.view, 'focus' );
  95. mainButtonView.fire( 'execute' );
  96. sinon.assert.calledWithExactly( editor.execute, 'bulletedList' );
  97. sinon.assert.calledOnce( editor.editing.view.focus );
  98. sinon.assert.callOrder( editor.execute, editor.editing.view.focus );
  99. } );
  100. } );
  101. describe( 'toolbar with style buttons', () => {
  102. let toolbarView;
  103. beforeEach( () => {
  104. toolbarView = bulletedListDropdown.toolbarView;
  105. } );
  106. it( 'should be in the dropdown panel', () => {
  107. expect( bulletedListDropdown.panelView.children.get( 0 ) ).to.equal( toolbarView );
  108. } );
  109. it( 'should have a proper ARIA label', () => {
  110. expect( toolbarView.ariaLabel ).to.equal( 'Bulleted list styles toolbar' );
  111. } );
  112. it( 'should bring the "disc" list style button', () => {
  113. const buttonView = toolbarView.items.get( 0 );
  114. expect( buttonView.label ).to.equal( 'Toggle the disc list style' );
  115. expect( buttonView.tooltip ).to.equal( 'Disc' );
  116. expect( buttonView.icon ).to.equal( listStyleDiscIcon );
  117. } );
  118. it( 'should bring the "circle" list style button', () => {
  119. const buttonView = toolbarView.items.get( 1 );
  120. expect( buttonView.label ).to.equal( 'Toggle the circle list style' );
  121. expect( buttonView.tooltip ).to.equal( 'Circle' );
  122. expect( buttonView.icon ).to.equal( listStyleCircleIcon );
  123. } );
  124. it( 'should bring the "square" list style button', () => {
  125. const buttonView = toolbarView.items.get( 2 );
  126. expect( buttonView.label ).to.equal( 'Toggle the square list style' );
  127. expect( buttonView.tooltip ).to.equal( 'Square' );
  128. expect( buttonView.icon ).to.equal( listStyleSquareIcon );
  129. } );
  130. describe( 'style button', () => {
  131. let styleButtonView;
  132. beforeEach( () => {
  133. // "circle"
  134. styleButtonView = toolbarView.items.get( 1 );
  135. sinon.spy( editor, 'execute' );
  136. sinon.spy( editor.editing.view, 'focus' );
  137. } );
  138. it( 'should be instances of ButtonView', () => {
  139. expect( styleButtonView ).to.be.instanceOf( ButtonView );
  140. } );
  141. it( 'should change its #isOn state when the value of the "listStyleCommand" command changes', () => {
  142. expect( styleButtonView.isOn ).to.be.false;
  143. listStyleCommand.value = 'foo';
  144. expect( styleButtonView.isOn ).to.be.false;
  145. listStyleCommand.value = 'circle';
  146. expect( styleButtonView.isOn ).to.be.true;
  147. listStyleCommand.value = null;
  148. expect( styleButtonView.isOn ).to.be.false;
  149. } );
  150. it( 'should apply the new style if none was set', () => {
  151. setData( model, '<listItem listType="bulleted" listIndent="0">[]foo</listItem>' );
  152. styleButtonView.fire( 'execute' );
  153. sinon.assert.calledWithExactly( editor.execute, 'listStyle', { type: 'circle' } );
  154. sinon.assert.calledOnce( editor.editing.view.focus );
  155. sinon.assert.callOrder( editor.execute, editor.editing.view.focus );
  156. } );
  157. it( 'should apply the new style if a different one was set', () => {
  158. setData( model, '<listItem listType="bulleted" listStyle="square" listIndent="0">[]foo</listItem>' );
  159. styleButtonView.fire( 'execute' );
  160. sinon.assert.calledWithExactly( editor.execute, 'listStyle', { type: 'circle' } );
  161. sinon.assert.calledOnce( editor.editing.view.focus );
  162. sinon.assert.callOrder( editor.execute, editor.editing.view.focus );
  163. } );
  164. it( 'should remove (toggle) the style if the same style was set', () => {
  165. setData( model, '<listItem listType="bulleted" listStyle="circle" listIndent="0">[]foo</listItem>' );
  166. styleButtonView.fire( 'execute' );
  167. sinon.assert.calledWithExactly( editor.execute, 'listStyle', { type: 'default' } );
  168. sinon.assert.calledOnce( editor.editing.view.focus );
  169. sinon.assert.callOrder( editor.execute, editor.editing.view.focus );
  170. } );
  171. it( 'should execute the "bulletedList" command and apply the style if selection was not anchored in a list', () => {
  172. setData( model, '<paragraph>foo[]</paragraph>' );
  173. styleButtonView.fire( 'execute' );
  174. sinon.assert.calledWithExactly( editor.execute, 'bulletedList' );
  175. sinon.assert.calledWithExactly( editor.execute, 'listStyle', { type: 'circle' } );
  176. sinon.assert.calledOnce( editor.editing.view.focus );
  177. sinon.assert.callOrder( editor.execute, editor.editing.view.focus );
  178. } );
  179. it( 'should create the single undo step while selection was not anchored in a list', () => {
  180. setData( model, '<paragraph>foo[]</paragraph>' );
  181. styleButtonView.fire( 'execute' );
  182. expect( getData( model ) ).to.equal(
  183. '<listItem listIndent="0" listStyle="circle" listType="bulleted">foo[]</listItem>'
  184. );
  185. editor.execute( 'undo' );
  186. expect( getData( model ) ).to.equal(
  187. '<paragraph>foo[]</paragraph>'
  188. );
  189. } );
  190. } );
  191. } );
  192. } );
  193. describe( 'numbered list dropdown', () => {
  194. let numberedListCommand, numberedListDropdown;
  195. beforeEach( () => {
  196. numberedListCommand = editor.commands.get( 'numberedList' );
  197. numberedListDropdown = editor.ui.componentFactory.create( 'numberedList' );
  198. } );
  199. it( 'should registered as "numberedList" in the component factory', () => {
  200. expect( numberedListDropdown ).to.be.instanceOf( DropdownView );
  201. } );
  202. it( 'should have #isEnabled bound to the "numberedList" command state', () => {
  203. expect( numberedListDropdown.isEnabled ).to.be.true;
  204. numberedListCommand.isEnabled = true;
  205. expect( numberedListDropdown.isEnabled ).to.be.true;
  206. numberedListCommand.isEnabled = false;
  207. expect( numberedListDropdown.isEnabled ).to.be.false;
  208. } );
  209. it( 'should have a specific CSS class', () => {
  210. expect( numberedListDropdown.class ).to.equal( 'ck-list-styles-dropdown' );
  211. } );
  212. describe( 'main split button', () => {
  213. let mainButtonView;
  214. beforeEach( () => {
  215. mainButtonView = numberedListDropdown.buttonView;
  216. } );
  217. it( 'should have a #label', () => {
  218. expect( mainButtonView.label ).to.equal( 'Numbered List' );
  219. } );
  220. it( 'should have an #icon', () => {
  221. expect( mainButtonView.icon ).to.equal( numberedListIcon );
  222. } );
  223. it( 'should have a #tooltip based on a label', () => {
  224. expect( mainButtonView.tooltip ).to.be.true;
  225. } );
  226. it( 'should be toggleable', () => {
  227. expect( mainButtonView.isToggleable ).to.be.true;
  228. } );
  229. it( 'should have the #isOn state bound to the value of the "numberedList" command', () => {
  230. expect( mainButtonView.isOn ).to.be.false;
  231. numberedListCommand.value = 'foo';
  232. expect( mainButtonView.isOn ).to.be.true;
  233. numberedListCommand.value = null;
  234. expect( mainButtonView.isOn ).to.be.false;
  235. } );
  236. it( 'should execute the "numberedList" command and focus the editing view when clicked', () => {
  237. sinon.spy( editor, 'execute' );
  238. sinon.spy( editor.editing.view, 'focus' );
  239. mainButtonView.fire( 'execute' );
  240. sinon.assert.calledWithExactly( editor.execute, 'numberedList' );
  241. sinon.assert.calledOnce( editor.editing.view.focus );
  242. sinon.assert.callOrder( editor.execute, editor.editing.view.focus );
  243. } );
  244. } );
  245. describe( 'toolbar with style buttons', () => {
  246. let toolbarView;
  247. beforeEach( () => {
  248. toolbarView = numberedListDropdown.toolbarView;
  249. } );
  250. it( 'should be in the dropdown panel', () => {
  251. expect( numberedListDropdown.panelView.children.get( 0 ) ).to.equal( toolbarView );
  252. } );
  253. it( 'should have a proper ARIA label', () => {
  254. expect( toolbarView.ariaLabel ).to.equal( 'Numbered list styles toolbar' );
  255. } );
  256. it( 'should bring the "decimal" list style button', () => {
  257. const buttonView = toolbarView.items.get( 0 );
  258. expect( buttonView.label ).to.equal( 'Toggle the decimal list style' );
  259. expect( buttonView.tooltip ).to.equal( 'Decimal' );
  260. expect( buttonView.icon ).to.equal( listStyleDecimalIcon );
  261. } );
  262. it( 'should bring the "decimal-leading-zero" list style button', () => {
  263. const buttonView = toolbarView.items.get( 1 );
  264. expect( buttonView.label ).to.equal( 'Toggle the decimal with leading zero list style' );
  265. expect( buttonView.tooltip ).to.equal( 'Decimal with leading zero' );
  266. expect( buttonView.icon ).to.equal( listStyleDecimalWithLeadingZeroIcon );
  267. } );
  268. it( 'should bring the "lower-roman" list style button', () => {
  269. const buttonView = toolbarView.items.get( 2 );
  270. expect( buttonView.label ).to.equal( 'Toggle the lower–roman list style' );
  271. expect( buttonView.tooltip ).to.equal( 'Lower–roman' );
  272. expect( buttonView.icon ).to.equal( listStyleLowerRomanIcon );
  273. } );
  274. it( 'should bring the "upper-roman" list style button', () => {
  275. const buttonView = toolbarView.items.get( 3 );
  276. expect( buttonView.label ).to.equal( 'Toggle the upper–roman list style' );
  277. expect( buttonView.tooltip ).to.equal( 'Upper-roman' );
  278. expect( buttonView.icon ).to.equal( listStyleUpperRomanIcon );
  279. } );
  280. it( 'should bring the "lower–latin" list style button', () => {
  281. const buttonView = toolbarView.items.get( 4 );
  282. expect( buttonView.label ).to.equal( 'Toggle the lower–latin list style' );
  283. expect( buttonView.tooltip ).to.equal( 'Lower-latin' );
  284. expect( buttonView.icon ).to.equal( listStyleLowerLatinIcon );
  285. } );
  286. it( 'should bring the "upper–latin" list style button', () => {
  287. const buttonView = toolbarView.items.get( 5 );
  288. expect( buttonView.label ).to.equal( 'Toggle the upper–latin list style' );
  289. expect( buttonView.tooltip ).to.equal( 'Upper-latin' );
  290. expect( buttonView.icon ).to.equal( listStyleUpperLatinIcon );
  291. } );
  292. describe( 'style button', () => {
  293. let styleButtonView;
  294. beforeEach( () => {
  295. // "decimal-leading-zero""
  296. styleButtonView = toolbarView.items.get( 1 );
  297. sinon.spy( editor, 'execute' );
  298. sinon.spy( editor.editing.view, 'focus' );
  299. } );
  300. it( 'should be instances of ButtonView', () => {
  301. expect( styleButtonView ).to.be.instanceOf( ButtonView );
  302. } );
  303. it( 'should change its #isOn state when the value of the "listStyleCommand" command changes', () => {
  304. expect( styleButtonView.isOn ).to.be.false;
  305. listStyleCommand.value = 'foo';
  306. expect( styleButtonView.isOn ).to.be.false;
  307. listStyleCommand.value = 'decimal-leading-zero';
  308. expect( styleButtonView.isOn ).to.be.true;
  309. listStyleCommand.value = null;
  310. expect( styleButtonView.isOn ).to.be.false;
  311. } );
  312. it( 'should apply the new style if none was set', () => {
  313. setData( model, '<listItem listType="numbered" listIndent="0">[]foo</listItem>' );
  314. styleButtonView.fire( 'execute' );
  315. sinon.assert.calledWithExactly( editor.execute, 'listStyle', { type: 'decimal-leading-zero' } );
  316. sinon.assert.calledOnce( editor.editing.view.focus );
  317. sinon.assert.callOrder( editor.execute, editor.editing.view.focus );
  318. } );
  319. it( 'should apply the new style if a different one was set', () => {
  320. setData( model, '<listItem listType="numbered" listStyle="square" listIndent="0">[]foo</listItem>' );
  321. styleButtonView.fire( 'execute' );
  322. sinon.assert.calledWithExactly( editor.execute, 'listStyle', { type: 'decimal-leading-zero' } );
  323. sinon.assert.calledOnce( editor.editing.view.focus );
  324. sinon.assert.callOrder( editor.execute, editor.editing.view.focus );
  325. } );
  326. it( 'should remove (toggle) the style if the same style was set', () => {
  327. setData( model, '<listItem listType="numbered" listStyle="decimal-leading-zero" listIndent="0">[]foo</listItem>' );
  328. styleButtonView.fire( 'execute' );
  329. sinon.assert.calledWithExactly( editor.execute, 'listStyle', { type: 'default' } );
  330. sinon.assert.calledOnce( editor.editing.view.focus );
  331. sinon.assert.callOrder( editor.execute, editor.editing.view.focus );
  332. } );
  333. it( 'should execute the "numberedList" command and apply the style if selection was not anchored in a list', () => {
  334. setData( model, '<paragraph>foo[]</paragraph>' );
  335. styleButtonView.fire( 'execute' );
  336. sinon.assert.calledWithExactly( editor.execute, 'numberedList' );
  337. sinon.assert.calledWithExactly( editor.execute, 'listStyle', { type: 'decimal-leading-zero' } );
  338. sinon.assert.calledOnce( editor.editing.view.focus );
  339. sinon.assert.callOrder( editor.execute, editor.editing.view.focus );
  340. } );
  341. it( 'should create the single undo step while selection was not anchored in a list', () => {
  342. setData( model, '<paragraph>foo[]</paragraph>' );
  343. styleButtonView.fire( 'execute' );
  344. expect( getData( model ) ).to.equal(
  345. '<listItem listIndent="0" listStyle="decimal-leading-zero" listType="numbered">foo[]</listItem>'
  346. );
  347. editor.execute( 'undo' );
  348. expect( getData( model ) ).to.equal(
  349. '<paragraph>foo[]</paragraph>'
  350. );
  351. } );
  352. } );
  353. } );
  354. } );
  355. } );
  356. } );