8
0

specialcharacters.js 9.3 KB

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