link.js 23 KB

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