specialcharacters.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import EventInfo from '@ckeditor/ckeditor5-utils/src/eventinfo';
  8. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  9. import SpecialCharacters from '../src/specialcharacters';
  10. import SpecialCharactersMathematical from '../src/specialcharactersmathematical';
  11. import SpecialCharactersArrows from '../src/specialcharactersarrows';
  12. import SpecialCharactersNavigationView from '../src/ui/specialcharactersnavigationview';
  13. import CharacterGridView from '../src/ui/charactergridview';
  14. import specialCharactersIcon from '../theme/icons/specialcharacters.svg';
  15. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  16. describe( 'SpecialCharacters', () => {
  17. let plugin;
  18. beforeEach( () => {
  19. plugin = new SpecialCharacters( {} );
  20. } );
  21. it( 'should require proper plugins', () => {
  22. expect( SpecialCharacters.requires ).to.deep.equal( [ Typing ] );
  23. } );
  24. it( 'should be named', () => {
  25. expect( SpecialCharacters.pluginName ).to.equal( 'SpecialCharacters' );
  26. } );
  27. describe( 'init()', () => {
  28. let editor, command, element;
  29. beforeEach( () => {
  30. element = document.createElement( 'div' );
  31. document.body.appendChild( element );
  32. return ClassicTestEditor
  33. .create( element, {
  34. plugins: [
  35. SpecialCharacters,
  36. SpecialCharactersMathematical,
  37. SpecialCharactersArrows
  38. ]
  39. } )
  40. .then( newEditor => {
  41. editor = newEditor;
  42. command = editor.commands.get( 'input' );
  43. } );
  44. } );
  45. afterEach( () => {
  46. element.remove();
  47. return editor.destroy();
  48. } );
  49. describe( '"specialCharacters" dropdown', () => {
  50. let dropdown;
  51. beforeEach( () => {
  52. dropdown = editor.ui.componentFactory.create( 'specialCharacters' );
  53. } );
  54. afterEach( () => {
  55. dropdown.destroy();
  56. } );
  57. it( 'has a navigation view', () => {
  58. expect( dropdown.panelView.children.first ).to.be.instanceOf( SpecialCharactersNavigationView );
  59. } );
  60. it( 'a navigation contains the "All" special category', () => {
  61. const listView = dropdown.panelView.children.first.groupDropdownView.panelView.children.first;
  62. // "Mathematical" and "Arrows" are provided by other plugins. "All" is being added by SpecialCharacters itself.
  63. expect( listView.items.length ).to.equal( 3 );
  64. expect( listView.items.last.children.first.label ).to.equal( 'All' );
  65. } );
  66. it( 'has a grid view', () => {
  67. expect( dropdown.panelView.children.last ).to.be.instanceOf( CharacterGridView );
  68. } );
  69. describe( '#buttonView', () => {
  70. it( 'should get basic properties', () => {
  71. expect( dropdown.buttonView.label ).to.equal( 'Special characters' );
  72. expect( dropdown.buttonView.icon ).to.equal( specialCharactersIcon );
  73. expect( dropdown.buttonView.tooltip ).to.be.true;
  74. } );
  75. it( 'should bind #isEnabled to the command', () => {
  76. expect( dropdown.isEnabled ).to.be.true;
  77. command.isEnabled = false;
  78. expect( dropdown.isEnabled ).to.be.false;
  79. command.isEnabled = true;
  80. } );
  81. } );
  82. it( 'executes a command and focuses the editing view', () => {
  83. const grid = dropdown.panelView.children.last;
  84. const executeSpy = sinon.stub( editor, 'execute' );
  85. const focusSpy = sinon.stub( editor.editing.view, 'focus' );
  86. grid.tiles.get( 2 ).fire( 'execute' );
  87. sinon.assert.calledOnce( executeSpy );
  88. sinon.assert.calledOnce( focusSpy );
  89. sinon.assert.calledWithExactly( executeSpy.firstCall, 'input', {
  90. text: '≤'
  91. } );
  92. } );
  93. describe( 'grid view', () => {
  94. let grid;
  95. beforeEach( () => {
  96. grid = dropdown.panelView.children.last;
  97. } );
  98. it( 'delegates #execute to the dropdown', () => {
  99. const spy = sinon.spy();
  100. dropdown.on( 'execute', spy );
  101. grid.fire( 'execute', { name: 'foo' } );
  102. sinon.assert.calledOnce( spy );
  103. } );
  104. it( 'has default contents', () => {
  105. expect( grid.tiles ).to.have.length.greaterThan( 10 );
  106. } );
  107. it( 'is updated when navigation view fires #execute', () => {
  108. const navigation = dropdown.panelView.children.first;
  109. expect( grid.tiles.get( 0 ).label ).to.equal( '<' );
  110. navigation.groupDropdownView.fire( new EventInfo( { label: 'Arrows' }, 'execute' ) );
  111. expect( grid.tiles.get( 0 ).label ).to.equal( '⇐' );
  112. } );
  113. } );
  114. } );
  115. } );
  116. describe( 'addItems()', () => {
  117. it( 'adds special characters to the available symbols', () => {
  118. plugin.addItems( 'Arrows', [
  119. { title: 'arrow left', character: '←' },
  120. { title: 'arrow right', character: '→' }
  121. ] );
  122. expect( plugin._groups.size ).to.equal( 1 );
  123. expect( plugin._groups.has( 'Arrows' ) ).to.equal( true );
  124. expect( plugin._characters.size ).to.equal( 2 );
  125. expect( plugin._characters.has( 'arrow left' ) ).to.equal( true );
  126. expect( plugin._characters.has( 'arrow right' ) ).to.equal( true );
  127. } );
  128. it( 'works with subsequent calls to the same group', () => {
  129. plugin.addItems( 'Mathematical', [ {
  130. title: 'dot',
  131. character: '.'
  132. } ] );
  133. plugin.addItems( 'Mathematical', [ {
  134. title: ',',
  135. character: 'comma'
  136. } ] );
  137. const groups = [ ...plugin.getGroups() ];
  138. expect( groups ).to.deep.equal( [ 'Mathematical' ] );
  139. } );
  140. it( 'does not accept "All" as a group name', () => {
  141. expectToThrowCKEditorError( () => {
  142. plugin.addItems( 'All', [] );
  143. }, /special-character-invalid-group-name/ );
  144. } );
  145. } );
  146. describe( 'getGroups()', () => {
  147. it( 'returns iterator of defined groups', () => {
  148. plugin.addItems( 'Arrows', [
  149. { title: 'arrow left', character: '←' }
  150. ] );
  151. plugin.addItems( 'Mathematical', [
  152. { title: 'precedes', character: '≺' },
  153. { title: 'succeeds', character: '≻' }
  154. ] );
  155. const groups = [ ...plugin.getGroups() ];
  156. expect( groups ).to.deep.equal( [ 'Arrows', 'Mathematical' ] );
  157. } );
  158. } );
  159. describe( 'getCharactersForGroup()', () => {
  160. it( 'returns a collection of defined special characters names', () => {
  161. plugin.addItems( 'Mathematical', [
  162. { title: 'precedes', character: '≺' },
  163. { title: 'succeeds', character: '≻' }
  164. ] );
  165. const characters = plugin.getCharactersForGroup( 'Mathematical' );
  166. expect( characters.size ).to.equal( 2 );
  167. expect( characters.has( 'precedes' ) ).to.equal( true );
  168. expect( characters.has( 'succeeds' ) ).to.equal( true );
  169. } );
  170. it( 'returns a collection of all special characters for "All" group', () => {
  171. plugin.addItems( 'Arrows', [
  172. { title: 'arrow left', character: '←' },
  173. { title: 'arrow right', character: '→' }
  174. ] );
  175. plugin.addItems( 'Mathematical', [
  176. { title: 'precedes', character: '≺' },
  177. { title: 'succeeds', character: '≻' }
  178. ] );
  179. const characters = plugin.getCharactersForGroup( 'All' );
  180. expect( characters.size ).to.equal( 4 );
  181. expect( characters.has( 'arrow left' ) ).to.equal( true );
  182. expect( characters.has( 'arrow right' ) ).to.equal( true );
  183. expect( characters.has( 'precedes' ) ).to.equal( true );
  184. expect( characters.has( 'succeeds' ) ).to.equal( true );
  185. } );
  186. it( 'returns undefined for non-existing group', () => {
  187. plugin.addItems( 'Mathematical', [
  188. { title: 'precedes', character: '≺' },
  189. { title: 'succeeds', character: '≻' }
  190. ] );
  191. const characters = plugin.getCharactersForGroup( 'Foo' );
  192. expect( characters ).to.be.undefined;
  193. } );
  194. } );
  195. describe( 'getCharacter()', () => {
  196. it( 'returns a collection of defined special characters names', () => {
  197. plugin.addItems( 'Mathematical', [
  198. { title: 'precedes', character: '≺' },
  199. { title: 'succeeds', character: '≻' }
  200. ] );
  201. expect( plugin.getCharacter( 'succeeds' ) ).to.equal( '≻' );
  202. } );
  203. it( 'returns undefined for non-existing character', () => {
  204. expect( plugin.getCharacter( 'succeeds' ) ).to.be.undefined;
  205. } );
  206. } );
  207. } );