linkformview.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 Event, document */
  6. import LinkFormView from '../../src/ui/linkformview';
  7. import View from '@ckeditor/ckeditor5-ui/src/view';
  8. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  9. import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
  10. import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  11. import FocusCycler from '@ckeditor/ckeditor5-ui/src/focuscycler';
  12. import ViewCollection from '@ckeditor/ckeditor5-ui/src/viewcollection';
  13. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  14. import ManualDecorator from '../../src/utils/manualdecorator';
  15. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  16. import { add as addTranslations, _clear as clearTranslations } from '@ckeditor/ckeditor5-utils/src/translation-service';
  17. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  18. import Link from '../../src/link';
  19. describe( 'LinkFormView', () => {
  20. let view;
  21. testUtils.createSinonSandbox();
  22. beforeEach( () => {
  23. view = new LinkFormView( { t: val => val } );
  24. view.render();
  25. } );
  26. afterEach( () => {
  27. view.destroy();
  28. } );
  29. describe( 'constructor()', () => {
  30. it( 'should create element from template', () => {
  31. expect( view.element.classList.contains( 'ck' ) ).to.true;
  32. expect( view.element.classList.contains( 'ck-link-form' ) ).to.true;
  33. expect( view.element.getAttribute( 'tabindex' ) ).to.equal( '-1' );
  34. } );
  35. it( 'should create child views', () => {
  36. expect( view.urlInputView ).to.be.instanceOf( View );
  37. expect( view.saveButtonView ).to.be.instanceOf( View );
  38. expect( view.cancelButtonView ).to.be.instanceOf( View );
  39. expect( view.saveButtonView.element.classList.contains( 'ck-button-save' ) ).to.be.true;
  40. expect( view.cancelButtonView.element.classList.contains( 'ck-button-cancel' ) ).to.be.true;
  41. expect( view.children.get( 0 ) ).to.equal( view.urlInputView );
  42. expect( view.children.get( 1 ) ).to.equal( view.saveButtonView );
  43. expect( view.children.get( 2 ) ).to.equal( view.cancelButtonView );
  44. } );
  45. it( 'should create #focusTracker instance', () => {
  46. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  47. } );
  48. it( 'should create #keystrokes instance', () => {
  49. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  50. } );
  51. it( 'should create #_focusCycler instance', () => {
  52. expect( view._focusCycler ).to.be.instanceOf( FocusCycler );
  53. } );
  54. it( 'should create #_focusables view collection', () => {
  55. expect( view._focusables ).to.be.instanceOf( ViewCollection );
  56. } );
  57. it( 'should fire `cancel` event on cancelButtonView#execute', () => {
  58. const spy = sinon.spy();
  59. view.on( 'cancel', spy );
  60. view.cancelButtonView.fire( 'execute' );
  61. expect( spy.calledOnce ).to.true;
  62. } );
  63. describe( 'url input view', () => {
  64. it( 'has placeholder', () => {
  65. expect( view.urlInputView.fieldView.placeholder ).to.equal( 'https://example.com' );
  66. } );
  67. } );
  68. describe( 'template', () => {
  69. it( 'has url input view', () => {
  70. expect( view.template.children[ 0 ].get( 0 ) ).to.equal( view.urlInputView );
  71. } );
  72. it( 'has button views', () => {
  73. expect( view.template.children[ 0 ].get( 1 ) ).to.equal( view.saveButtonView );
  74. expect( view.template.children[ 0 ].get( 2 ) ).to.equal( view.cancelButtonView );
  75. } );
  76. } );
  77. } );
  78. describe( 'render()', () => {
  79. it( 'should register child views in #_focusables', () => {
  80. expect( view._focusables.map( f => f ) ).to.have.members( [
  81. view.urlInputView,
  82. view.saveButtonView,
  83. view.cancelButtonView
  84. ] );
  85. } );
  86. it( 'should register child views\' #element in #focusTracker', () => {
  87. const spy = testUtils.sinon.spy( FocusTracker.prototype, 'add' );
  88. view = new LinkFormView( { t: () => {} } );
  89. view.render();
  90. sinon.assert.calledWithExactly( spy.getCall( 0 ), view.urlInputView.element );
  91. sinon.assert.calledWithExactly( spy.getCall( 1 ), view.saveButtonView.element );
  92. sinon.assert.calledWithExactly( spy.getCall( 2 ), view.cancelButtonView.element );
  93. } );
  94. it( 'starts listening for #keystrokes coming from #element', () => {
  95. view = new LinkFormView( { t: () => {} } );
  96. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  97. view.render();
  98. sinon.assert.calledOnce( spy );
  99. sinon.assert.calledWithExactly( spy, view.element );
  100. } );
  101. describe( 'activates keyboard navigation for the toolbar', () => {
  102. it( 'so "tab" focuses the next focusable item', () => {
  103. const keyEvtData = {
  104. keyCode: keyCodes.tab,
  105. preventDefault: sinon.spy(),
  106. stopPropagation: sinon.spy()
  107. };
  108. // Mock the url input is focused.
  109. view.focusTracker.isFocused = true;
  110. view.focusTracker.focusedElement = view.urlInputView.element;
  111. const spy = sinon.spy( view.saveButtonView, 'focus' );
  112. view.keystrokes.press( keyEvtData );
  113. sinon.assert.calledOnce( keyEvtData.preventDefault );
  114. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  115. sinon.assert.calledOnce( spy );
  116. } );
  117. it( 'so "shift + tab" focuses the previous focusable item', () => {
  118. const keyEvtData = {
  119. keyCode: keyCodes.tab,
  120. shiftKey: true,
  121. preventDefault: sinon.spy(),
  122. stopPropagation: sinon.spy()
  123. };
  124. // Mock the cancel button is focused.
  125. view.focusTracker.isFocused = true;
  126. view.focusTracker.focusedElement = view.cancelButtonView.element;
  127. const spy = sinon.spy( view.saveButtonView, 'focus' );
  128. view.keystrokes.press( keyEvtData );
  129. sinon.assert.calledOnce( keyEvtData.preventDefault );
  130. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  131. sinon.assert.calledOnce( spy );
  132. } );
  133. } );
  134. } );
  135. describe( 'DOM bindings', () => {
  136. describe( 'submit event', () => {
  137. it( 'should trigger submit event', () => {
  138. const spy = sinon.spy();
  139. view.on( 'submit', spy );
  140. view.element.dispatchEvent( new Event( 'submit' ) );
  141. expect( spy.calledOnce ).to.true;
  142. } );
  143. } );
  144. } );
  145. describe( 'focus()', () => {
  146. it( 'focuses the #urlInputView', () => {
  147. const spy = sinon.spy( view.urlInputView, 'focus' );
  148. view.focus();
  149. sinon.assert.calledOnce( spy );
  150. } );
  151. } );
  152. describe( 'manual decorators', () => {
  153. let view, collection;
  154. beforeEach( () => {
  155. collection = new Collection();
  156. collection.add( new ManualDecorator( {
  157. id: 'decorator1',
  158. label: 'Foo',
  159. attributes: {
  160. foo: 'bar'
  161. }
  162. } ) );
  163. collection.add( new ManualDecorator( {
  164. id: 'decorator2',
  165. label: 'Download',
  166. attributes: {
  167. download: 'download'
  168. }
  169. } ) );
  170. collection.add( new ManualDecorator( {
  171. id: 'decorator3',
  172. label: 'Multi',
  173. attributes: {
  174. class: 'fancy-class',
  175. target: '_blank',
  176. rel: 'noopener noreferrer'
  177. }
  178. } ) );
  179. view = new LinkFormView( { t: val => val }, collection );
  180. view.render();
  181. } );
  182. afterEach( () => {
  183. view.destroy();
  184. collection.clear();
  185. } );
  186. it( 'switch buttons reflects state of manual decorators', () => {
  187. expect( view._manualDecoratorSwitches.length ).to.equal( 3 );
  188. expect( view._manualDecoratorSwitches.get( 0 ) ).to.deep.include( {
  189. name: 'decorator1',
  190. label: 'Foo'
  191. } );
  192. expect( view._manualDecoratorSwitches.get( 1 ) ).to.deep.include( {
  193. name: 'decorator2',
  194. label: 'Download'
  195. } );
  196. expect( view._manualDecoratorSwitches.get( 2 ) ).to.deep.include( {
  197. name: 'decorator3',
  198. label: 'Multi'
  199. } );
  200. } );
  201. it( 'reacts on switch button changes', () => {
  202. const modelItem = collection.first;
  203. const viewItem = view._manualDecoratorSwitches.first;
  204. expect( modelItem.value ).to.be.undefined;
  205. expect( viewItem.isOn ).to.be.undefined;
  206. viewItem.element.dispatchEvent( new Event( 'click' ) );
  207. expect( modelItem.value ).to.be.true;
  208. expect( viewItem.isOn ).to.be.true;
  209. viewItem.element.dispatchEvent( new Event( 'click' ) );
  210. expect( modelItem.value ).to.be.false;
  211. expect( viewItem.isOn ).to.be.false;
  212. } );
  213. describe( 'getDecoratorSwitchesState()', () => {
  214. it( 'should provide object with decorators states', () => {
  215. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  216. decorator1: undefined,
  217. decorator2: undefined,
  218. decorator3: undefined
  219. } );
  220. view._manualDecoratorSwitches.map( item => {
  221. item.element.dispatchEvent( new Event( 'click' ) );
  222. } );
  223. view._manualDecoratorSwitches.get( 2 ).element.dispatchEvent( new Event( 'click' ) );
  224. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  225. decorator1: true,
  226. decorator2: true,
  227. decorator3: false
  228. } );
  229. } );
  230. } );
  231. } );
  232. describe( 'localization of manual decorators', () => {
  233. before( () => {
  234. addTranslations( 'pl', {
  235. 'Open in a new tab': 'Otwórz w nowym oknie'
  236. } );
  237. } );
  238. after( () => {
  239. clearTranslations();
  240. } );
  241. let editor, editorElement, linkFormView;
  242. beforeEach( () => {
  243. editorElement = document.createElement( 'div' );
  244. document.body.appendChild( editorElement );
  245. return ClassicTestEditor
  246. .create( editorElement, {
  247. plugins: [ Link ],
  248. toolbar: [ 'link' ],
  249. language: 'pl',
  250. link: {
  251. decorators: {
  252. IsExternal: {
  253. mode: 'manual',
  254. label: 'Open in a new tab',
  255. attributes: {
  256. target: '_blank'
  257. }
  258. }
  259. }
  260. }
  261. } )
  262. .then( newEditor => {
  263. editor = newEditor;
  264. linkFormView = new LinkFormView( editor.locale, editor.commands.get( 'link' ).manualDecorators );
  265. } );
  266. } );
  267. afterEach( () => {
  268. editorElement.remove();
  269. return editor.destroy();
  270. } );
  271. it( 'translates labels of manual decorators UI', () => {
  272. expect( linkFormView._manualDecoratorSwitches.first.label ).to.equal( 'Otwórz w nowym oknie' );
  273. } );
  274. } );
  275. } );