8
0

linkui.js 30 KB

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