specialcharacters.js 9.0 KB

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