link.js 25 KB

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