linkformview.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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.separatorView ).to.be.instanceOf( View );
  41. expect( view.cancelButtonView ).to.be.instanceOf( View );
  42. expect( view.saveButtonView.element.classList.contains( 'ck-button-save' ) ).to.be.true;
  43. expect( view.separatorView.element.classList.contains( 'ck-toolbar__separator' ) ).to.be.true;
  44. expect( view.cancelButtonView.element.classList.contains( 'ck-button-cancel' ) ).to.be.true;
  45. expect( view.children.get( 0 ) ).to.equal( view.urlInputView );
  46. expect( view.children.get( 1 ) ).to.equal( view.saveButtonView );
  47. expect( view.children.get( 2 ) ).to.equal( view.separatorView );
  48. expect( view.children.get( 3 ) ).to.equal( view.cancelButtonView );
  49. } );
  50. it( 'should create #focusTracker instance', () => {
  51. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  52. } );
  53. it( 'should create #keystrokes instance', () => {
  54. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  55. } );
  56. it( 'should create #_focusCycler instance', () => {
  57. expect( view._focusCycler ).to.be.instanceOf( FocusCycler );
  58. } );
  59. it( 'should create #_focusables view collection', () => {
  60. expect( view._focusables ).to.be.instanceOf( ViewCollection );
  61. } );
  62. it( 'should fire `cancel` event on cancelButtonView#execute', () => {
  63. const spy = sinon.spy();
  64. view.on( 'cancel', spy );
  65. view.cancelButtonView.fire( 'execute' );
  66. expect( spy.calledOnce ).to.true;
  67. } );
  68. describe( 'url input view', () => {
  69. it( 'has placeholder', () => {
  70. expect( view.urlInputView.fieldView.placeholder ).to.equal( 'https://example.com' );
  71. } );
  72. } );
  73. describe( 'template', () => {
  74. it( 'has url input view', () => {
  75. expect( view.template.children[ 0 ].get( 0 ) ).to.equal( view.urlInputView );
  76. } );
  77. it( 'has button views', () => {
  78. expect( view.template.children[ 0 ].get( 1 ) ).to.equal( view.saveButtonView );
  79. expect( view.template.children[ 0 ].get( 2 ) ).to.equal( view.separatorView );
  80. expect( view.template.children[ 0 ].get( 3 ) ).to.equal( view.cancelButtonView );
  81. } );
  82. } );
  83. } );
  84. describe( 'render()', () => {
  85. it( 'should register child views in #_focusables', () => {
  86. expect( view._focusables.map( f => f ) ).to.have.members( [
  87. view.urlInputView,
  88. view.saveButtonView,
  89. view.cancelButtonView
  90. ] );
  91. } );
  92. it( 'should register child views\' #element in #focusTracker', () => {
  93. const spy = testUtils.sinon.spy( FocusTracker.prototype, 'add' );
  94. view = new LinkFormView( { t: () => {} }, { manualDecorators: [] } );
  95. view.render();
  96. sinon.assert.calledWithExactly( spy.getCall( 0 ), view.urlInputView.element );
  97. sinon.assert.calledWithExactly( spy.getCall( 1 ), view.saveButtonView.element );
  98. sinon.assert.calledWithExactly( spy.getCall( 2 ), view.cancelButtonView.element );
  99. } );
  100. it( 'starts listening for #keystrokes coming from #element', () => {
  101. view = new LinkFormView( { t: () => {} }, { manualDecorators: [] } );
  102. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  103. view.render();
  104. sinon.assert.calledOnce( spy );
  105. sinon.assert.calledWithExactly( spy, view.element );
  106. } );
  107. describe( 'activates keyboard navigation for the toolbar', () => {
  108. it( 'so "tab" focuses the next focusable item', () => {
  109. const keyEvtData = {
  110. keyCode: keyCodes.tab,
  111. preventDefault: sinon.spy(),
  112. stopPropagation: sinon.spy()
  113. };
  114. // Mock the url input is focused.
  115. view.focusTracker.isFocused = true;
  116. view.focusTracker.focusedElement = view.urlInputView.element;
  117. const spy = sinon.spy( view.saveButtonView, 'focus' );
  118. view.keystrokes.press( keyEvtData );
  119. sinon.assert.calledOnce( keyEvtData.preventDefault );
  120. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  121. sinon.assert.calledOnce( spy );
  122. } );
  123. it( 'so "shift + tab" focuses the previous focusable item', () => {
  124. const keyEvtData = {
  125. keyCode: keyCodes.tab,
  126. shiftKey: true,
  127. preventDefault: sinon.spy(),
  128. stopPropagation: sinon.spy()
  129. };
  130. // Mock the cancel button is focused.
  131. view.focusTracker.isFocused = true;
  132. view.focusTracker.focusedElement = view.cancelButtonView.element;
  133. const spy = sinon.spy( view.saveButtonView, 'focus' );
  134. view.keystrokes.press( keyEvtData );
  135. sinon.assert.calledOnce( keyEvtData.preventDefault );
  136. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  137. sinon.assert.calledOnce( spy );
  138. } );
  139. } );
  140. } );
  141. describe( 'DOM bindings', () => {
  142. describe( 'submit event', () => {
  143. it( 'should trigger submit event', () => {
  144. const spy = sinon.spy();
  145. view.on( 'submit', spy );
  146. view.element.dispatchEvent( new Event( 'submit' ) );
  147. expect( spy.calledOnce ).to.true;
  148. } );
  149. } );
  150. } );
  151. describe( 'focus()', () => {
  152. it( 'focuses the #urlInputView', () => {
  153. const spy = sinon.spy( view.urlInputView, 'focus' );
  154. view.focus();
  155. sinon.assert.calledOnce( spy );
  156. } );
  157. } );
  158. describe( 'manual decorators', () => {
  159. let view, collection, linkCommand;
  160. beforeEach( () => {
  161. collection = new Collection();
  162. collection.add( new ManualDecorator( {
  163. id: 'decorator1',
  164. label: 'Foo',
  165. attributes: {
  166. foo: 'bar'
  167. }
  168. } ) );
  169. collection.add( new ManualDecorator( {
  170. id: 'decorator2',
  171. label: 'Download',
  172. attributes: {
  173. download: 'download'
  174. },
  175. defaultValue: true
  176. } ) );
  177. collection.add( new ManualDecorator( {
  178. id: 'decorator3',
  179. label: 'Multi',
  180. attributes: {
  181. class: 'fancy-class',
  182. target: '_blank',
  183. rel: 'noopener noreferrer'
  184. }
  185. } ) );
  186. class LinkCommandMock {
  187. constructor( manualDecorators ) {
  188. this.manualDecorators = manualDecorators;
  189. this.set( 'value' );
  190. }
  191. }
  192. mix( LinkCommandMock, ObservableMixin );
  193. linkCommand = new LinkCommandMock( collection );
  194. view = new LinkFormView( { t: val => val }, linkCommand );
  195. view.render();
  196. } );
  197. afterEach( () => {
  198. view.destroy();
  199. collection.clear();
  200. } );
  201. it( 'switch buttons reflects state of manual decorators', () => {
  202. expect( view._manualDecoratorSwitches.length ).to.equal( 3 );
  203. expect( view._manualDecoratorSwitches.get( 0 ) ).to.deep.include( {
  204. name: 'decorator1',
  205. label: 'Foo',
  206. isOn: undefined
  207. } );
  208. expect( view._manualDecoratorSwitches.get( 1 ) ).to.deep.include( {
  209. name: 'decorator2',
  210. label: 'Download',
  211. isOn: true
  212. } );
  213. expect( view._manualDecoratorSwitches.get( 2 ) ).to.deep.include( {
  214. name: 'decorator3',
  215. label: 'Multi',
  216. isOn: undefined
  217. } );
  218. } );
  219. it( 'reacts on switch button changes', () => {
  220. const modelItem = collection.first;
  221. const viewItem = view._manualDecoratorSwitches.first;
  222. expect( modelItem.value ).to.be.undefined;
  223. expect( viewItem.isOn ).to.be.undefined;
  224. viewItem.element.dispatchEvent( new Event( 'click' ) );
  225. expect( modelItem.value ).to.be.true;
  226. expect( viewItem.isOn ).to.be.true;
  227. viewItem.element.dispatchEvent( new Event( 'click' ) );
  228. expect( modelItem.value ).to.be.false;
  229. expect( viewItem.isOn ).to.be.false;
  230. } );
  231. it( 'reacts on switch button changes for the decorator with defaultValue', () => {
  232. const modelItem = collection.get( 1 );
  233. const viewItem = view._manualDecoratorSwitches.get( 1 );
  234. expect( modelItem.value ).to.be.undefined;
  235. expect( viewItem.isOn ).to.be.true;
  236. viewItem.element.dispatchEvent( new Event( 'click' ) );
  237. expect( modelItem.value ).to.be.false;
  238. expect( viewItem.isOn ).to.be.false;
  239. viewItem.element.dispatchEvent( new Event( 'click' ) );
  240. expect( modelItem.value ).to.be.true;
  241. expect( viewItem.isOn ).to.be.true;
  242. } );
  243. describe( 'getDecoratorSwitchesState()', () => {
  244. it( 'should provide object with decorators states', () => {
  245. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  246. decorator1: undefined,
  247. decorator2: true,
  248. decorator3: undefined
  249. } );
  250. view._manualDecoratorSwitches.map( item => {
  251. item.element.dispatchEvent( new Event( 'click' ) );
  252. } );
  253. view._manualDecoratorSwitches.get( 2 ).element.dispatchEvent( new Event( 'click' ) );
  254. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  255. decorator1: true,
  256. decorator2: false,
  257. decorator3: false
  258. } );
  259. } );
  260. it( 'should use decorator default value if command and decorator values are not set', () => {
  261. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  262. decorator1: undefined,
  263. decorator2: true,
  264. decorator3: undefined
  265. } );
  266. } );
  267. it( 'should use a decorator value if decorator value is set', () => {
  268. for ( const decorator of collection ) {
  269. decorator.value = true;
  270. }
  271. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  272. decorator1: true,
  273. decorator2: true,
  274. decorator3: true
  275. } );
  276. for ( const decorator of collection ) {
  277. decorator.value = false;
  278. }
  279. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  280. decorator1: false,
  281. decorator2: false,
  282. decorator3: false
  283. } );
  284. } );
  285. it( 'should use a decorator value if link command value is set', () => {
  286. linkCommand.value = '';
  287. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  288. decorator1: undefined,
  289. decorator2: undefined,
  290. decorator3: undefined
  291. } );
  292. for ( const decorator of collection ) {
  293. decorator.value = false;
  294. }
  295. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  296. decorator1: false,
  297. decorator2: false,
  298. decorator3: false
  299. } );
  300. for ( const decorator of collection ) {
  301. decorator.value = true;
  302. }
  303. expect( view.getDecoratorSwitchesState() ).to.deep.equal( {
  304. decorator1: true,
  305. decorator2: true,
  306. decorator3: true
  307. } );
  308. } );
  309. } );
  310. } );
  311. describe( 'localization of manual decorators', () => {
  312. before( () => {
  313. addTranslations( 'pl', {
  314. 'Open in a new tab': 'Otwórz w nowym oknie'
  315. } );
  316. } );
  317. after( () => {
  318. clearTranslations();
  319. } );
  320. let editor, editorElement, linkFormView;
  321. beforeEach( () => {
  322. editorElement = document.createElement( 'div' );
  323. document.body.appendChild( editorElement );
  324. return ClassicTestEditor
  325. .create( editorElement, {
  326. plugins: [ Link ],
  327. toolbar: [ 'link' ],
  328. language: 'pl',
  329. link: {
  330. decorators: {
  331. IsExternal: {
  332. mode: 'manual',
  333. label: 'Open in a new tab',
  334. attributes: {
  335. target: '_blank'
  336. }
  337. }
  338. }
  339. }
  340. } )
  341. .then( newEditor => {
  342. editor = newEditor;
  343. linkFormView = new LinkFormView( editor.locale, editor.commands.get( 'link' ) );
  344. } );
  345. } );
  346. afterEach( () => {
  347. editorElement.remove();
  348. return editor.destroy();
  349. } );
  350. it( 'translates labels of manual decorators UI', () => {
  351. expect( linkFormView._manualDecoratorSwitches.first.label ).to.equal( 'Otwórz w nowym oknie' );
  352. } );
  353. } );
  354. } );