liststylesui.js 16 KB

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