linkui.js 25 KB

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