linkformview.js 12 KB

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