linkui.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals document, Event */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  8. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  9. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  11. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  12. import LinkEditing from '../src/linkediting';
  13. import LinkUI from '../src/linkui';
  14. import LinkFormView from '../src/ui/linkformview';
  15. import LinkActionsView from '../src/ui/linkactionsview';
  16. import ContextualBalloon from '@ckeditor/ckeditor5-ui/src/panel/balloon/contextualballoon';
  17. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  18. import ClickObserver from '@ckeditor/ckeditor5-engine/src/view/observer/clickobserver';
  19. describe( 'LinkUI', () => {
  20. let editor, linkUIFeature, linkButton, balloon, formView, actionsView, editorElement;
  21. testUtils.createSinonSandbox();
  22. beforeEach( () => {
  23. editorElement = document.createElement( 'div' );
  24. document.body.appendChild( editorElement );
  25. return ClassicTestEditor
  26. .create( editorElement, {
  27. plugins: [ LinkEditing, LinkUI, Paragraph ]
  28. } )
  29. .then( newEditor => {
  30. editor = newEditor;
  31. linkUIFeature = editor.plugins.get( LinkUI );
  32. linkButton = editor.ui.componentFactory.create( 'link' );
  33. balloon = editor.plugins.get( ContextualBalloon );
  34. formView = linkUIFeature.formView;
  35. actionsView = linkUIFeature.actionsView;
  36. // There is no point to execute BalloonPanelView attachTo and pin methods so lets override it.
  37. testUtils.sinon.stub( balloon.view, 'attachTo' ).returns( {} );
  38. testUtils.sinon.stub( balloon.view, 'pin' ).returns( {} );
  39. formView.render();
  40. } );
  41. } );
  42. afterEach( () => {
  43. editorElement.remove();
  44. return editor.destroy();
  45. } );
  46. it( 'should be named', () => {
  47. expect( LinkUI.pluginName ).to.equal( 'LinkUI' );
  48. } );
  49. it( 'should load ContextualBalloon', () => {
  50. expect( editor.plugins.get( ContextualBalloon ) ).to.be.instanceOf( ContextualBalloon );
  51. } );
  52. describe( 'init', () => {
  53. it( 'should register click observer', () => {
  54. expect( editor.editing.view.getObserver( ClickObserver ) ).to.be.instanceOf( ClickObserver );
  55. } );
  56. it( 'should create #actionsView', () => {
  57. expect( actionsView ).to.be.instanceOf( LinkActionsView );
  58. } );
  59. it( 'should create #formView', () => {
  60. expect( formView ).to.be.instanceOf( LinkFormView );
  61. } );
  62. describe( 'link toolbar button', () => {
  63. it( 'should be registered', () => {
  64. expect( linkButton ).to.be.instanceOf( ButtonView );
  65. } );
  66. it( 'should be bound to the link command', () => {
  67. const command = editor.commands.get( 'link' );
  68. command.isEnabled = true;
  69. command.value = true;
  70. expect( linkButton.isOn ).to.be.true;
  71. expect( linkButton.isEnabled ).to.be.true;
  72. command.isEnabled = false;
  73. command.value = false;
  74. expect( linkButton.isOn ).to.be.false;
  75. expect( linkButton.isEnabled ).to.be.false;
  76. } );
  77. it( 'should call #_showUI upon #execute', () => {
  78. const spy = testUtils.sinon.stub( linkUIFeature, '_showUI' ).returns( {} );
  79. linkButton.fire( 'execute' );
  80. sinon.assert.calledWithExactly( spy );
  81. } );
  82. } );
  83. } );
  84. describe( '_showUI()', () => {
  85. let balloonAddSpy;
  86. beforeEach( () => {
  87. balloonAddSpy = testUtils.sinon.spy( balloon, 'add' );
  88. editor.editing.view.document.isFocused = true;
  89. } );
  90. it( 'should not work if the link command is disabled', () => {
  91. setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
  92. editor.commands.get( 'link' ).isEnabled = false;
  93. linkUIFeature._showUI();
  94. expect( balloon.visibleView ).to.be.null;
  95. } );
  96. it( 'should not throw if the UI is already visible', () => {
  97. setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
  98. linkUIFeature._showUI();
  99. expect( () => {
  100. linkUIFeature._showUI();
  101. } ).to.not.throw();
  102. } );
  103. it( 'should add #formView to the balloon and attach the balloon to the selection when text fragment is selected', () => {
  104. setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
  105. const selectedRange = editorElement.ownerDocument.getSelection().getRangeAt( 0 );
  106. linkUIFeature._showUI();
  107. expect( balloon.visibleView ).to.equal( formView );
  108. sinon.assert.calledWithExactly( balloonAddSpy, {
  109. view: formView,
  110. position: {
  111. target: selectedRange
  112. }
  113. } );
  114. } );
  115. it( 'should add #formView to the balloon and attach the balloon to the selection when selection is collapsed', () => {
  116. setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
  117. const selectedRange = editorElement.ownerDocument.getSelection().getRangeAt( 0 );
  118. linkUIFeature._showUI();
  119. expect( balloon.visibleView ).to.equal( formView );
  120. sinon.assert.calledWithExactly( balloonAddSpy, {
  121. view: formView,
  122. position: {
  123. target: selectedRange
  124. }
  125. } );
  126. } );
  127. it( 'should add #actionsView to the balloon and attach the balloon to the link element when collapsed selection is inside ' +
  128. 'that link',
  129. () => {
  130. setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
  131. const linkElement = editor.editing.view.getDomRoot().querySelector( 'a' );
  132. linkUIFeature._showUI();
  133. expect( balloon.visibleView ).to.equal( actionsView );
  134. sinon.assert.calledWithExactly( balloonAddSpy, {
  135. view: actionsView,
  136. position: {
  137. target: linkElement
  138. }
  139. } );
  140. } );
  141. // #https://github.com/ckeditor/ckeditor5-link/issues/181
  142. it( 'should add #formView to the balloon when collapsed selection is inside the link and #actionsView is already visible', () => {
  143. setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
  144. const linkElement = editor.editing.view.getDomRoot().querySelector( 'a' );
  145. linkUIFeature._showUI();
  146. expect( balloon.visibleView ).to.equal( actionsView );
  147. sinon.assert.calledWithExactly( balloonAddSpy, {
  148. view: actionsView,
  149. position: {
  150. target: linkElement
  151. }
  152. } );
  153. linkUIFeature._showUI();
  154. expect( balloon.visibleView ).to.equal( formView );
  155. sinon.assert.calledWithExactly( balloonAddSpy, {
  156. view: formView,
  157. position: {
  158. target: linkElement
  159. }
  160. } );
  161. } );
  162. it( 'should disable #formView and #actionsView elements when link and unlink commands are disabled', () => {
  163. setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
  164. linkUIFeature._showUI();
  165. editor.commands.get( 'link' ).isEnabled = true;
  166. editor.commands.get( 'unlink' ).isEnabled = true;
  167. expect( formView.urlInputView.isReadOnly ).to.be.false;
  168. expect( formView.saveButtonView.isEnabled ).to.be.true;
  169. expect( formView.cancelButtonView.isEnabled ).to.be.true;
  170. expect( actionsView.unlinkButtonView.isEnabled ).to.be.true;
  171. expect( actionsView.editButtonView.isEnabled ).to.be.true;
  172. editor.commands.get( 'link' ).isEnabled = false;
  173. editor.commands.get( 'unlink' ).isEnabled = false;
  174. expect( formView.urlInputView.isReadOnly ).to.be.true;
  175. expect( formView.saveButtonView.isEnabled ).to.be.false;
  176. expect( formView.cancelButtonView.isEnabled ).to.be.true;
  177. expect( actionsView.unlinkButtonView.isEnabled ).to.be.false;
  178. expect( actionsView.editButtonView.isEnabled ).to.be.false;
  179. } );
  180. // https://github.com/ckeditor/ckeditor5-link/issues/78
  181. it( 'should make sure the URL input in the #formView always stays in sync with the value of the command (selected link)', () => {
  182. setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
  183. // Mock some leftover value **in DOM**, e.g. after previous editing.
  184. formView.urlInputView.inputView.element.value = 'leftover';
  185. linkUIFeature._showUI();
  186. actionsView.fire( 'edit' );
  187. expect( formView.urlInputView.inputView.element.value ).to.equal( 'url' );
  188. } );
  189. // https://github.com/ckeditor/ckeditor5-link/issues/123
  190. it( 'should make sure the URL input in the #formView always stays in sync with the value of the command (no link selected)', () => {
  191. setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
  192. linkUIFeature._showUI();
  193. expect( formView.urlInputView.inputView.element.value ).to.equal( '' );
  194. } );
  195. describe( 'response to ui#update', () => {
  196. let view, viewDocument;
  197. beforeEach( () => {
  198. view = editor.editing.view;
  199. viewDocument = view.document;
  200. } );
  201. it( 'should not duplicate #update listeners', () => {
  202. setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
  203. const spy = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
  204. linkUIFeature._showUI();
  205. editor.ui.fire( 'update' );
  206. linkUIFeature._hideUI();
  207. linkUIFeature._showUI();
  208. editor.ui.fire( 'update' );
  209. sinon.assert.calledTwice( spy );
  210. } );
  211. // https://github.com/ckeditor/ckeditor5-link/issues/113
  212. it( 'updates the position of the panel – editing a link, then the selection remains in the link', () => {
  213. setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
  214. linkUIFeature._showUI();
  215. const spy = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
  216. expect( getViewData( view ) ).to.equal(
  217. '<p><a class="ck-link_selected" href="url">f{}oo</a></p>'
  218. );
  219. const root = viewDocument.getRoot();
  220. const linkElement = root.getChild( 0 ).getChild( 0 );
  221. const text = linkElement.getChild( 0 );
  222. // Move selection to foo[].
  223. view.change( writer => {
  224. writer.setSelection( text, 3, true );
  225. } );
  226. sinon.assert.calledOnce( spy );
  227. sinon.assert.calledWithExactly( spy, {
  228. target: view.domConverter.mapViewToDom( linkElement )
  229. } );
  230. } );
  231. // https://github.com/ckeditor/ckeditor5-link/issues/113
  232. it( 'updates the position of the panel – creating a new link, then the selection moved', () => {
  233. setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
  234. linkUIFeature._showUI();
  235. const spy = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
  236. const root = viewDocument.getRoot();
  237. const text = root.getChild( 0 ).getChild( 0 );
  238. view.change( writer => {
  239. writer.setSelection( text, 3, true );
  240. } );
  241. sinon.assert.calledOnce( spy );
  242. sinon.assert.calledWithExactly( spy, {
  243. target: editorElement.ownerDocument.getSelection().getRangeAt( 0 )
  244. } );
  245. } );
  246. // https://github.com/ckeditor/ckeditor5-link/issues/113
  247. it( 'hides of the panel – editing a link, then the selection moved out of the link', () => {
  248. setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text>bar</paragraph>' );
  249. linkUIFeature._showUI();
  250. const spyUpdate = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
  251. const spyHide = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  252. const root = viewDocument.getRoot();
  253. const text = root.getChild( 0 ).getChild( 1 );
  254. // Move selection to b[]ar.
  255. view.change( writer => {
  256. writer.setSelection( text, 1, true );
  257. } );
  258. sinon.assert.calledOnce( spyHide );
  259. sinon.assert.notCalled( spyUpdate );
  260. } );
  261. // https://github.com/ckeditor/ckeditor5-link/issues/113
  262. it( 'hides the panel – editing a link, then the selection expands', () => {
  263. setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
  264. linkUIFeature._showUI();
  265. const spyUpdate = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
  266. const spyHide = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  267. expect( getViewData( view ) ).to.equal( '<p><a class="ck-link_selected" href="url">f{}oo</a></p>' );
  268. const root = viewDocument.getRoot();
  269. const text = root.getChild( 0 ).getChild( 0 ).getChild( 0 );
  270. // Move selection to f[o]o.
  271. view.change( writer => {
  272. writer.setSelection( writer.createRange(
  273. writer.createPositionAt( text, 1 ),
  274. writer.createPositionAt( text, 2 )
  275. ), true );
  276. } );
  277. sinon.assert.calledOnce( spyHide );
  278. sinon.assert.notCalled( spyUpdate );
  279. } );
  280. // https://github.com/ckeditor/ckeditor5-link/issues/113
  281. it( 'hides the panel – creating a new link, then the selection moved to another parent', () => {
  282. setModelData( editor.model, '<paragraph>f[]oo</paragraph><paragraph>bar</paragraph>' );
  283. linkUIFeature._showUI();
  284. const spyUpdate = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
  285. const spyHide = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  286. const root = viewDocument.getRoot();
  287. const text = root.getChild( 1 ).getChild( 0 );
  288. // Move selection to f[o]o.
  289. view.change( writer => {
  290. writer.setSelection( writer.createRange(
  291. writer.createPositionAt( text, 1 ),
  292. writer.createPositionAt( text, 2 )
  293. ), true );
  294. } );
  295. sinon.assert.calledOnce( spyHide );
  296. sinon.assert.notCalled( spyUpdate );
  297. } );
  298. } );
  299. } );
  300. describe( '_hideUI()', () => {
  301. beforeEach( () => {
  302. linkUIFeature._showUI();
  303. } );
  304. it( 'should remove the UI from the balloon', () => {
  305. expect( balloon.hasView( formView ) ).to.be.true;
  306. expect( balloon.hasView( actionsView ) ).to.be.true;
  307. linkUIFeature._hideUI();
  308. expect( balloon.hasView( formView ) ).to.be.false;
  309. expect( balloon.hasView( actionsView ) ).to.be.false;
  310. } );
  311. it( 'should focus the `editable` by default', () => {
  312. const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  313. linkUIFeature._hideUI();
  314. // First call is from _removeFormView.
  315. sinon.assert.calledTwice( spy );
  316. } );
  317. // https://github.com/ckeditor/ckeditor5-link/issues/193
  318. it( 'should focus the `editable` before before removing elements from the balloon', () => {
  319. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  320. const removeSpy = testUtils.sinon.spy( balloon, 'remove' );
  321. linkUIFeature._hideUI();
  322. expect( focusSpy.calledBefore( removeSpy ) ).to.equal( true );
  323. } );
  324. it( 'should not throw an error when views are not in the `balloon`', () => {
  325. linkUIFeature._hideUI();
  326. expect( () => {
  327. linkUIFeature._hideUI();
  328. } ).to.not.throw();
  329. } );
  330. it( 'should clear ui#update listener from the ViewDocument', () => {
  331. const spy = sinon.spy();
  332. linkUIFeature.listenTo( editor.ui, 'update', spy );
  333. linkUIFeature._hideUI();
  334. editor.ui.fire( 'update' );
  335. sinon.assert.notCalled( spy );
  336. } );
  337. } );
  338. describe( 'keyboard support', () => {
  339. it( 'should show the UI on Ctrl+K keystroke', () => {
  340. const spy = testUtils.sinon.stub( linkUIFeature, '_showUI' ).returns( {} );
  341. const command = editor.commands.get( 'link' );
  342. command.isEnabled = false;
  343. editor.keystrokes.press( {
  344. keyCode: keyCodes.k,
  345. ctrlKey: true,
  346. preventDefault: sinon.spy(),
  347. stopPropagation: sinon.spy()
  348. } );
  349. sinon.assert.notCalled( spy );
  350. command.isEnabled = true;
  351. editor.keystrokes.press( {
  352. keyCode: keyCodes.k,
  353. ctrlKey: true,
  354. preventDefault: sinon.spy(),
  355. stopPropagation: sinon.spy()
  356. } );
  357. sinon.assert.calledWithExactly( spy );
  358. } );
  359. it( 'should prevent default action on Ctrl+K keystroke', () => {
  360. const preventDefaultSpy = sinon.spy();
  361. const stopPropagationSpy = sinon.spy();
  362. editor.keystrokes.press( {
  363. keyCode: keyCodes.k,
  364. ctrlKey: true,
  365. preventDefault: preventDefaultSpy,
  366. stopPropagation: stopPropagationSpy
  367. } );
  368. sinon.assert.calledOnce( preventDefaultSpy );
  369. sinon.assert.calledOnce( stopPropagationSpy );
  370. } );
  371. it( 'should focus the the #actionsView on `Tab` key press when #actionsView is visible', () => {
  372. const keyEvtData = {
  373. keyCode: keyCodes.tab,
  374. preventDefault: sinon.spy(),
  375. stopPropagation: sinon.spy()
  376. };
  377. const normalPriorityTabCallbackSpy = sinon.spy();
  378. const highestPriorityTabCallbackSpy = sinon.spy();
  379. editor.keystrokes.set( 'Tab', normalPriorityTabCallbackSpy );
  380. editor.keystrokes.set( 'Tab', highestPriorityTabCallbackSpy, { priority: 'highest' } );
  381. // Balloon is invisible, form not focused.
  382. actionsView.focusTracker.isFocused = false;
  383. const spy = sinon.spy( actionsView, 'focus' );
  384. editor.keystrokes.press( keyEvtData );
  385. sinon.assert.notCalled( keyEvtData.preventDefault );
  386. sinon.assert.notCalled( keyEvtData.stopPropagation );
  387. sinon.assert.notCalled( spy );
  388. sinon.assert.calledOnce( normalPriorityTabCallbackSpy );
  389. sinon.assert.calledOnce( highestPriorityTabCallbackSpy );
  390. // Balloon is visible, form focused.
  391. linkUIFeature._showUI();
  392. testUtils.sinon.stub( linkUIFeature, '_areActionsVisible' ).value( true );
  393. actionsView.focusTracker.isFocused = true;
  394. editor.keystrokes.press( keyEvtData );
  395. sinon.assert.notCalled( keyEvtData.preventDefault );
  396. sinon.assert.notCalled( keyEvtData.stopPropagation );
  397. sinon.assert.notCalled( spy );
  398. sinon.assert.calledTwice( normalPriorityTabCallbackSpy );
  399. sinon.assert.calledTwice( highestPriorityTabCallbackSpy );
  400. // Balloon is still visible, form not focused.
  401. actionsView.focusTracker.isFocused = false;
  402. editor.keystrokes.press( keyEvtData );
  403. sinon.assert.calledOnce( keyEvtData.preventDefault );
  404. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  405. sinon.assert.calledOnce( spy );
  406. sinon.assert.calledTwice( normalPriorityTabCallbackSpy );
  407. sinon.assert.calledThrice( highestPriorityTabCallbackSpy );
  408. } );
  409. it( 'should hide the UI after Esc key press (from editor) and not focus the editable', () => {
  410. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  411. const keyEvtData = {
  412. keyCode: keyCodes.esc,
  413. preventDefault: sinon.spy(),
  414. stopPropagation: sinon.spy()
  415. };
  416. // Balloon is visible.
  417. linkUIFeature._showUI();
  418. editor.keystrokes.press( keyEvtData );
  419. sinon.assert.calledWithExactly( spy );
  420. } );
  421. it( 'should not hide the UI after Esc key press (from editor) when UI is open but is not visible', () => {
  422. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  423. const keyEvtData = {
  424. keyCode: keyCodes.esc,
  425. preventDefault: () => {},
  426. stopPropagation: () => {}
  427. };
  428. const viewMock = {
  429. ready: true,
  430. render: () => {},
  431. destroy: () => {}
  432. };
  433. linkUIFeature._showUI();
  434. // Some view precedes the link UI in the balloon.
  435. balloon.add( { view: viewMock } );
  436. editor.keystrokes.press( keyEvtData );
  437. sinon.assert.notCalled( spy );
  438. } );
  439. } );
  440. describe( 'mouse support', () => {
  441. it( 'should hide the UI and not focus editable upon clicking outside the UI', () => {
  442. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  443. linkUIFeature._showUI( true );
  444. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  445. sinon.assert.calledWithExactly( spy );
  446. } );
  447. it( 'should not hide the UI upon clicking inside the the UI', () => {
  448. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  449. linkUIFeature._showUI( true );
  450. balloon.view.element.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  451. sinon.assert.notCalled( spy );
  452. } );
  453. describe( 'clicking on editable', () => {
  454. let observer, spy;
  455. beforeEach( () => {
  456. observer = editor.editing.view.getObserver( ClickObserver );
  457. editor.model.schema.extend( '$text', { allowIn: '$root' } );
  458. spy = testUtils.sinon.stub( linkUIFeature, '_showUI' ).returns( {} );
  459. } );
  460. it( 'should show the UI when collapsed selection is inside link element', () => {
  461. setModelData( editor.model, '<$text linkHref="url">fo[]o</$text>' );
  462. observer.fire( 'click', { target: document.body } );
  463. sinon.assert.calledWithExactly( spy );
  464. } );
  465. it( 'should show the UI when selection exclusively encloses a link element (#1)', () => {
  466. setModelData( editor.model, '[<$text linkHref="url">foo</$text>]' );
  467. observer.fire( 'click', { target: {} } );
  468. sinon.assert.calledWithExactly( spy );
  469. } );
  470. it( 'should show the UI when selection exclusively encloses a link element (#2)', () => {
  471. setModelData( editor.model, '<$text linkHref="url">[foo]</$text>' );
  472. observer.fire( 'click', { target: {} } );
  473. sinon.assert.calledWithExactly( spy );
  474. } );
  475. it( 'should do nothing when selection is not inside link element', () => {
  476. setModelData( editor.model, '[]' );
  477. observer.fire( 'click', { target: {} } );
  478. sinon.assert.notCalled( spy );
  479. } );
  480. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#1)', () => {
  481. setModelData( editor.model, '<$text linkHref="url">f[o]o</$text>' );
  482. observer.fire( 'click', { target: {} } );
  483. sinon.assert.notCalled( spy );
  484. } );
  485. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#2)', () => {
  486. setModelData( editor.model, '<$text linkHref="url">[fo]o</$text>' );
  487. observer.fire( 'click', { target: {} } );
  488. sinon.assert.notCalled( spy );
  489. } );
  490. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#3)', () => {
  491. setModelData( editor.model, '<$text linkHref="url">f[oo]</$text>' );
  492. observer.fire( 'click', { target: {} } );
  493. sinon.assert.notCalled( spy );
  494. } );
  495. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#4)', () => {
  496. setModelData( editor.model, 'ba[r<$text linkHref="url">foo]</$text>' );
  497. observer.fire( 'click', { target: {} } );
  498. sinon.assert.notCalled( spy );
  499. } );
  500. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#5)', () => {
  501. setModelData( editor.model, 'ba[r<$text linkHref="url">foo</$text>]' );
  502. observer.fire( 'click', { target: {} } );
  503. sinon.assert.notCalled( spy );
  504. } );
  505. } );
  506. } );
  507. describe( 'actions view', () => {
  508. let focusEditableSpy;
  509. beforeEach( () => {
  510. focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  511. } );
  512. it( 'should mark the editor UI as focused when the #actionsView is focused', () => {
  513. linkUIFeature._showUI();
  514. linkUIFeature._removeFormView();
  515. expect( balloon.visibleView ).to.equal( actionsView );
  516. editor.ui.focusTracker.isFocused = false;
  517. actionsView.element.dispatchEvent( new Event( 'focus' ) );
  518. expect( editor.ui.focusTracker.isFocused ).to.be.true;
  519. } );
  520. describe( 'binding', () => {
  521. it( 'should show the #formView on #edit event and select the URL input field', () => {
  522. linkUIFeature._showUI();
  523. linkUIFeature._removeFormView();
  524. const selectSpy = testUtils.sinon.spy( formView.urlInputView, 'select' );
  525. actionsView.fire( 'edit' );
  526. expect( balloon.visibleView ).to.equal( formView );
  527. sinon.assert.calledOnce( selectSpy );
  528. } );
  529. it( 'should execute unlink command on actionsView#unlink event', () => {
  530. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  531. actionsView.fire( 'unlink' );
  532. expect( executeSpy.calledOnce ).to.be.true;
  533. expect( executeSpy.calledWithExactly( 'unlink' ) ).to.be.true;
  534. } );
  535. it( 'should hide and focus editable on actionsView#unlink event', () => {
  536. linkUIFeature._showUI();
  537. linkUIFeature._removeFormView();
  538. // Removing the form would call the focus spy.
  539. focusEditableSpy.resetHistory();
  540. actionsView.fire( 'unlink' );
  541. expect( balloon.visibleView ).to.be.null;
  542. expect( focusEditableSpy.calledOnce ).to.be.true;
  543. } );
  544. it( 'should hide after Esc key press', () => {
  545. const keyEvtData = {
  546. keyCode: keyCodes.esc,
  547. preventDefault: sinon.spy(),
  548. stopPropagation: sinon.spy()
  549. };
  550. linkUIFeature._showUI();
  551. linkUIFeature._removeFormView();
  552. // Removing the form would call the focus spy.
  553. focusEditableSpy.resetHistory();
  554. actionsView.keystrokes.press( keyEvtData );
  555. expect( balloon.visibleView ).to.equal( null );
  556. expect( focusEditableSpy.calledOnce ).to.be.true;
  557. } );
  558. // #https://github.com/ckeditor/ckeditor5-link/issues/181
  559. it( 'should add the #formView upon Ctrl+K keystroke press', () => {
  560. const keyEvtData = {
  561. keyCode: keyCodes.k,
  562. ctrlKey: true,
  563. preventDefault: sinon.spy(),
  564. stopPropagation: sinon.spy()
  565. };
  566. linkUIFeature._showUI();
  567. linkUIFeature._removeFormView();
  568. expect( balloon.visibleView ).to.equal( actionsView );
  569. actionsView.keystrokes.press( keyEvtData );
  570. expect( balloon.visibleView ).to.equal( formView );
  571. } );
  572. } );
  573. } );
  574. describe( 'link form view', () => {
  575. let focusEditableSpy;
  576. beforeEach( () => {
  577. focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  578. } );
  579. it( 'should mark the editor UI as focused when the #formView is focused', () => {
  580. linkUIFeature._showUI();
  581. expect( balloon.visibleView ).to.equal( formView );
  582. editor.ui.focusTracker.isFocused = false;
  583. formView.element.dispatchEvent( new Event( 'focus' ) );
  584. expect( editor.ui.focusTracker.isFocused ).to.be.true;
  585. } );
  586. describe( 'binding', () => {
  587. beforeEach( () => {
  588. setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
  589. } );
  590. it( 'should bind formView.urlInputView#value to link command value', () => {
  591. const command = editor.commands.get( 'link' );
  592. expect( formView.urlInputView.value ).to.undefined;
  593. command.value = 'http://cksource.com';
  594. expect( formView.urlInputView.value ).to.equal( 'http://cksource.com' );
  595. } );
  596. it( 'should execute link command on formView#submit event', () => {
  597. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  598. formView.urlInputView.value = 'http://ckeditor.com';
  599. expect( formView.urlInputView.inputView.element.value ).to.equal( 'http://ckeditor.com' );
  600. formView.urlInputView.inputView.element.value = 'http://cksource.com';
  601. formView.fire( 'submit' );
  602. expect( executeSpy.calledOnce ).to.be.true;
  603. expect( executeSpy.calledWithExactly( 'link', 'http://cksource.com' ) ).to.be.true;
  604. } );
  605. it( 'should hide and reveal the #actionsView on formView#submit event', () => {
  606. linkUIFeature._showUI();
  607. formView.fire( 'submit' );
  608. expect( balloon.visibleView ).to.equal( actionsView );
  609. expect( focusEditableSpy.calledOnce ).to.be.true;
  610. } );
  611. it( 'should hide and reveal the #actionsView on formView#cancel event', () => {
  612. linkUIFeature._showUI();
  613. formView.fire( 'cancel' );
  614. expect( balloon.visibleView ).to.equal( actionsView );
  615. expect( focusEditableSpy.calledOnce ).to.be.true;
  616. } );
  617. it( 'should hide after Esc key press', () => {
  618. const keyEvtData = {
  619. keyCode: keyCodes.esc,
  620. preventDefault: sinon.spy(),
  621. stopPropagation: sinon.spy()
  622. };
  623. linkUIFeature._showUI();
  624. formView.keystrokes.press( keyEvtData );
  625. expect( balloon.visibleView ).to.equal( actionsView );
  626. expect( focusEditableSpy.calledOnce ).to.be.true;
  627. } );
  628. // https://github.com/ckeditor/ckeditor5/issues/1501
  629. it( 'should blur url input element before hiding the view', () => {
  630. linkUIFeature._showUI();
  631. const focusSpy = testUtils.sinon.spy( formView.saveButtonView, 'focus' );
  632. const removeSpy = testUtils.sinon.spy( balloon, 'remove' );
  633. formView.fire( 'cancel' );
  634. expect( focusSpy.calledBefore( removeSpy ) ).to.equal( true );
  635. } );
  636. } );
  637. } );
  638. } );