linkui.js 26 KB

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