linkformview.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
  20. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  21. describe( 'LinkFormView', () => {
  22. let view;
  23. testUtils.createSinonSandbox();
  24. beforeEach( () => {
  25. view = new LinkFormView( { t: val => val }, { manualDecorators: [] } );
  26. view.render();
  27. } );
  28. afterEach( () => {
  29. view.destroy();
  30. } );
  31. describe( 'constructor()', () => {
  32. it( 'should create element from template', () => {
  33. expect( view.element.classList.contains( 'ck' ) ).to.true;
  34. expect( view.element.classList.contains( 'ck-link-form' ) ).to.true;
  35. expect( view.element.getAttribute( 'tabindex' ) ).to.equal( '-1' );
  36. } );
  37. it( 'should create child views', () => {
  38. expect( view.urlInputView ).to.be.instanceOf( View );
  39. expect( view.saveButtonView ).to.be.instanceOf( View );
  40. expect( view.cancelButtonView ).to.be.instanceOf( View );
  41. expect( view.saveButtonView.element.classList.contains( 'ck-button-save' ) ).to.be.true;
  42. expect( view.cancelButtonView.element.classList.contains( 'ck-button-cancel' ) ).to.be.true;
  43. expect( view.children.get( 0 ) ).to.equal( view.urlInputView );
  44. expect( view.children.get( 1 ) ).to.equal( view.saveButtonView );
  45. expect( view.children.get( 2 ) ).to.equal( view.cancelButtonView );
  46. } );
  47. it( 'should create #focusTracker instance', () => {
  48. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  49. } );
  50. it( 'should create #keystrokes instance', () => {
  51. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  52. } );
  53. it( 'should create #_focusCycler instance', () => {
  54. expect( view._focusCycler ).to.be.instanceOf( FocusCycler );
  55. } );
  56. it( 'should create #_focusables view collection', () => {
  57. expect( view._focusables ).to.be.instanceOf( ViewCollection );
  58. } );
  59. it( 'should fire `cancel` event on cancelButtonView#execute', () => {
  60. const spy = sinon.spy();
  61. view.on( 'cancel', spy );
  62. view.cancelButtonView.fire( 'execute' );
  63. expect( spy.calledOnce ).to.true;
  64. } );
  65. describe( 'template', () => {
  66. it( 'has url input view', () => {
  67. expect( view.template.children[ 0 ].get( 0 ) ).to.equal( view.urlInputView );
  68. } );
  69. it( 'has button views', () => {
  70. expect( view.template.children[ 0 ].get( 1 ) ).to.equal( view.saveButtonView );
  71. expect( view.template.children[ 0 ].get( 2 ) ).to.equal( view.cancelButtonView );
  72. } );
  73. } );
  74. it( 'should implement the CSS transition disabling feature', () => {
  75. expect( view.disableCSSTransitions ).to.be.a( 'function' );
  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. view = new LinkFormView( { t: () => {} }, { manualDecorators: [] } );
  88. const spy = testUtils.sinon.spy( view.focusTracker, 'add' );
  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: () => {} }, { manualDecorators: [] } );
  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, linkCommand;
  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. defaultValue: true
  170. } ) );
  171. collection.add( new ManualDecorator( {
  172. id: 'decorator3',
  173. label: 'Multi',
  174. attributes: {
  175. class: 'fancy-class',
  176. target: '_blank',
  177. rel: 'noopener noreferrer'
  178. }
  179. } ) );
  180. class LinkCommandMock {
  181. constructor( manualDecorators ) {
  182. this.manualDecorators = manualDecorators;
  183. this.set( 'value' );
  184. }
  185. }
  186. mix( LinkCommandMock, ObservableMixin );
  187. linkCommand = new LinkCommandMock( collection );
  188. view = new LinkFormView( { t: val => val }, linkCommand );
  189. view.render();
  190. } );
  191. afterEach( () => {
  192. view.destroy();
  193. collection.clear();
  194. } );
  195. it( 'switch buttons reflects state of manual decorators', () => {
  196. expect( view._manualDecoratorSwitches.length ).to.equal( 3 );
  197. expect( view._manualDecoratorSwitches.get( 0 ) ).to.deep.include( {
  198. name: 'decorator1',
  199. label: 'Foo',
  200. isOn: undefined
  201. } );
  202. expect( view._manualDecoratorSwitches.get( 1 ) ).to.deep.include( {
  203. name: 'decorator2',
  204. label: 'Download',
  205. isOn: true
  206. } );
  207. expect( view._manualDecoratorSwitches.get( 2 ) ).to.deep.include( {
  208. name: 'decorator3',
  209. label: 'Multi',
  210. isOn: undefined
  211. } );
  212. } );
  213. it( 'reacts on switch button changes', () => {
  214. const modelItem = collection.first;
  215. const viewItem = view._manualDecoratorSwitches.first;
  216. expect( modelItem.value ).to.be.undefined;
  217. expect( viewItem.isOn ).to.be.undefined;
  218. viewItem.element.dispatchEvent( new Event( 'click' ) );
  219. expect( modelItem.value ).to.be.true;
  220. expect( viewItem.isOn ).to.be.true;
  221. viewItem.element.dispatchEvent( new Event( 'click' ) );
  222. expect( modelItem.value ).to.be.false;
  223. expect( viewItem.isOn ).to.be.false;
  224. } );
  225. it( 'reacts on switch button changes for the decorator with defaultValue', () => {
  226. const modelItem = collection.get( 1 );
  227. const viewItem = view._manualDecoratorSwitches.get( 1 );
  228. expect( modelItem.value ).to.be.undefined;
  229. expect( viewItem.isOn ).to.be.true;
  230. viewItem.element.dispatchEvent( new Event( 'click' ) );
  231. expect( modelItem.value ).to.be.false;
  232. expect( viewItem.isOn ).to.be.false;
  233. viewItem.element.dispatchEvent( new Event( 'click' ) );
  234. expect( modelItem.value ).to.be.true;
  235. expect( viewItem.isOn ).to.be.true;
  236. } );
  237. describe( 'getDecoratorSwitchesState()', () => {
  238. it( 'should provide object with decorators states', () => {
  239. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  240. decorator1: undefined,
  241. decorator2: true,
  242. decorator3: undefined
  243. } );
  244. view._manualDecoratorSwitches.map( item => {
  245. item.element.dispatchEvent( new Event( 'click' ) );
  246. } );
  247. view._manualDecoratorSwitches.get( 2 ).element.dispatchEvent( new Event( 'click' ) );
  248. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  249. decorator1: true,
  250. decorator2: false,
  251. decorator3: false
  252. } );
  253. } );
  254. it( 'should use decorator default value if command and decorator values are not set', () => {
  255. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  256. decorator1: undefined,
  257. decorator2: true,
  258. decorator3: undefined
  259. } );
  260. } );
  261. it( 'should use a decorator value if decorator value is set', () => {
  262. for ( const decorator of collection ) {
  263. decorator.value = true;
  264. }
  265. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  266. decorator1: true,
  267. decorator2: true,
  268. decorator3: true
  269. } );
  270. for ( const decorator of collection ) {
  271. decorator.value = false;
  272. }
  273. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  274. decorator1: false,
  275. decorator2: false,
  276. decorator3: false
  277. } );
  278. } );
  279. it( 'should use a decorator value if link command value is set', () => {
  280. linkCommand.value = '';
  281. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  282. decorator1: undefined,
  283. decorator2: undefined,
  284. decorator3: undefined
  285. } );
  286. for ( const decorator of collection ) {
  287. decorator.value = false;
  288. }
  289. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  290. decorator1: false,
  291. decorator2: false,
  292. decorator3: false
  293. } );
  294. for ( const decorator of collection ) {
  295. decorator.value = true;
  296. }
  297. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  298. decorator1: true,
  299. decorator2: true,
  300. decorator3: true
  301. } );
  302. } );
  303. } );
  304. } );
  305. describe( 'localization of manual decorators', () => {
  306. before( () => {
  307. addTranslations( 'pl', {
  308. 'Open in a new tab': 'Otwórz w nowym oknie'
  309. } );
  310. } );
  311. after( () => {
  312. clearTranslations();
  313. } );
  314. let editor, editorElement, linkFormView;
  315. beforeEach( () => {
  316. editorElement = document.createElement( 'div' );
  317. document.body.appendChild( editorElement );
  318. return ClassicTestEditor
  319. .create( editorElement, {
  320. plugins: [ Link ],
  321. toolbar: [ 'link' ],
  322. language: 'pl',
  323. link: {
  324. decorators: {
  325. IsExternal: {
  326. mode: 'manual',
  327. label: 'Open in a new tab',
  328. attributes: {
  329. target: '_blank'
  330. }
  331. }
  332. }
  333. }
  334. } )
  335. .then( newEditor => {
  336. editor = newEditor;
  337. linkFormView = new LinkFormView( editor.locale, editor.commands.get( 'link' ) );
  338. } );
  339. } );
  340. afterEach( () => {
  341. editorElement.remove();
  342. return editor.destroy();
  343. } );
  344. it( 'translates labels of manual decorators UI', () => {
  345. expect( linkFormView._manualDecoratorSwitches.first.label ).to.equal( 'Otwórz w nowym oknie' );
  346. } );
  347. } );
  348. } );