linkformview.js 12 KB

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