link.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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. describe( 'when the document is rendering', () => {
  171. it( 'should not duplicate #render listeners', () => {
  172. const viewDocument = editor.editing.view;
  173. setModelData( editor.document, '<paragraph>f[]oo</paragraph>' );
  174. const spy = testUtils.sinon.stub( balloon, 'updatePosition', () => {} );
  175. return linkFeature._showPanel()
  176. .then( () => {
  177. viewDocument.render();
  178. linkFeature._hidePanel();
  179. return linkFeature._showPanel()
  180. .then( () => {
  181. viewDocument.render();
  182. sinon.assert.calledTwice( spy );
  183. } );
  184. } );
  185. } );
  186. //https://github.com/ckeditor/ckeditor5-link/issues/113
  187. it( 'updates the position of the panel – editing a link, then the selection remains in the link upon #render', () => {
  188. const viewDocument = editor.editing.view;
  189. setModelData( editor.document, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
  190. return linkFeature._showPanel()
  191. .then( () => {
  192. const spy = testUtils.sinon.stub( balloon, 'updatePosition', () => {} );
  193. const root = viewDocument.getRoot();
  194. const text = root.getChild( 0 ).getChild( 0 ).getChild( 0 );
  195. // Move selection to foo[].
  196. viewDocument.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 3, text, 3 ) ], true );
  197. viewDocument.render();
  198. sinon.assert.calledOnce( spy );
  199. sinon.assert.calledWithExactly( spy );
  200. } );
  201. } );
  202. //https://github.com/ckeditor/ckeditor5-link/issues/113
  203. it( 'updates the position of the panel – creating a new link, then the selection moved upon #render', () => {
  204. const viewDocument = editor.editing.view;
  205. setModelData( editor.document, '<paragraph>f[]oo</paragraph>' );
  206. return linkFeature._showPanel()
  207. .then( () => {
  208. const spy = testUtils.sinon.stub( balloon, 'updatePosition', () => {} );
  209. // Fires #render.
  210. const root = viewDocument.getRoot();
  211. const text = root.getChild( 0 ).getChild( 0 );
  212. viewDocument.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 3, text, 3 ) ], true );
  213. viewDocument.render();
  214. sinon.assert.calledOnce( spy );
  215. sinon.assert.calledWithExactly( spy, {
  216. target: editorElement.ownerDocument.getSelection().getRangeAt( 0 ),
  217. limiter: editorElement
  218. } );
  219. } );
  220. } );
  221. //https://github.com/ckeditor/ckeditor5-link/issues/113
  222. it( 'hides of the panel – editing a link, then the selection moved out of the link upon #render', () => {
  223. const viewDocument = editor.editing.view;
  224. setModelData( editor.document, '<paragraph><$text linkHref="url">f[]oo</$text>bar</paragraph>' );
  225. return linkFeature._showPanel()
  226. .then( () => {
  227. const spyUpdate = testUtils.sinon.stub( balloon, 'updatePosition', () => {} );
  228. const spyHide = testUtils.sinon.spy( linkFeature, '_hidePanel' );
  229. const root = viewDocument.getRoot();
  230. const text = root.getChild( 0 ).getChild( 1 );
  231. // Move selection to b[]ar.
  232. viewDocument.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 1 ) ], true );
  233. viewDocument.render();
  234. sinon.assert.calledOnce( spyHide );
  235. sinon.assert.notCalled( spyUpdate );
  236. } );
  237. } );
  238. //https://github.com/ckeditor/ckeditor5-link/issues/113
  239. it( 'hides of the panel – editing a link, then the selection moved to another link upon #render', () => {
  240. const viewDocument = editor.editing.view;
  241. setModelData( editor.document, '<paragraph><$text linkHref="url">f[]oo</$text>bar<$text linkHref="url">b[]az</$text></paragraph>' );
  242. return linkFeature._showPanel()
  243. .then( () => {
  244. const spyUpdate = testUtils.sinon.stub( balloon, 'updatePosition', () => {} );
  245. const spyHide = testUtils.sinon.spy( linkFeature, '_hidePanel' );
  246. const root = viewDocument.getRoot();
  247. const text = root.getChild( 0 ).getChild( 2 ).getChild( 0 );
  248. // Move selection to b[]az.
  249. viewDocument.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 1 ) ], true );
  250. viewDocument.render();
  251. sinon.assert.calledOnce( spyHide );
  252. sinon.assert.notCalled( spyUpdate );
  253. } );
  254. } );
  255. //https://github.com/ckeditor/ckeditor5-link/issues/113
  256. it( 'hides the panel – editing a link, then the selection expands upon #render', () => {
  257. const viewDocument = editor.editing.view;
  258. setModelData( editor.document, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
  259. return linkFeature._showPanel()
  260. .then( () => {
  261. const spyUpdate = testUtils.sinon.stub( balloon, 'updatePosition', () => {} );
  262. const spyHide = testUtils.sinon.spy( linkFeature, '_hidePanel' );
  263. const root = viewDocument.getRoot();
  264. const text = root.getChild( 0 ).getChild( 0 ).getChild( 0 );
  265. // Move selection to f[o]o.
  266. viewDocument.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 2 ) ], true );
  267. viewDocument.render();
  268. sinon.assert.calledOnce( spyHide );
  269. sinon.assert.notCalled( spyUpdate );
  270. } );
  271. } );
  272. } );
  273. } );
  274. describe( '_hidePanel()', () => {
  275. beforeEach( () => {
  276. return balloon.add( { view: formView } );
  277. } );
  278. it( 'should remove #formView from the #_balloon', () => {
  279. linkFeature._hidePanel();
  280. expect( balloon.hasView( formView ) ).to.false;
  281. } );
  282. it( 'should not focus the `editable` by default', () => {
  283. const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  284. linkFeature._hidePanel();
  285. sinon.assert.notCalled( spy );
  286. } );
  287. it( 'should not focus the `editable` when called with a `false` parameter', () => {
  288. const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  289. linkFeature._hidePanel( false );
  290. sinon.assert.notCalled( spy );
  291. } );
  292. it( 'should focus the `editable` when called with a `true` parameter', () => {
  293. const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  294. linkFeature._hidePanel( true );
  295. sinon.assert.calledOnce( spy );
  296. } );
  297. it( 'should not throw an error when #formView is not added to the `balloon`', () => {
  298. linkFeature._hidePanel( true );
  299. expect( () => {
  300. linkFeature._hidePanel( true );
  301. } ).to.not.throw();
  302. } );
  303. it( 'should clear `render` listener from ViewDocument', () => {
  304. const spy = sinon.spy();
  305. linkFeature.listenTo( editor.editing.view, 'render', spy );
  306. linkFeature._hidePanel();
  307. editor.editing.view.render();
  308. sinon.assert.notCalled( spy );
  309. } );
  310. } );
  311. describe( 'link toolbar button', () => {
  312. it( 'should register link button', () => {
  313. expect( linkButton ).to.instanceOf( ButtonView );
  314. } );
  315. it( 'should bind linkButtonView to link command', () => {
  316. const command = editor.commands.get( 'link' );
  317. command.isEnabled = true;
  318. expect( linkButton.isEnabled ).to.be.true;
  319. command.isEnabled = false;
  320. expect( linkButton.isEnabled ).to.be.false;
  321. } );
  322. it( 'should show the #_balloon on execute event with the selected #formView', () => {
  323. // Method is stubbed because it returns internal promise which can't be returned in test.
  324. const spy = testUtils.sinon.stub( linkFeature, '_showPanel', () => {} );
  325. linkButton.fire( 'execute' );
  326. sinon.assert.calledWithExactly( spy, true );
  327. } );
  328. } );
  329. describe( 'unlink toolbar button', () => {
  330. it( 'should register unlink button', () => {
  331. expect( unlinkButton ).to.instanceOf( ButtonView );
  332. } );
  333. it( 'should bind unlinkButtonView to unlink command', () => {
  334. const command = editor.commands.get( 'unlink' );
  335. command.isEnabled = true;
  336. expect( unlinkButton.isEnabled ).to.be.true;
  337. command.isEnabled = false;
  338. expect( unlinkButton.isEnabled ).to.be.false;
  339. } );
  340. it( 'should execute unlink command on unlinkButtonView execute event', () => {
  341. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  342. unlinkButton.fire( 'execute' );
  343. expect( executeSpy.calledOnce ).to.true;
  344. expect( executeSpy.calledWithExactly( 'unlink' ) ).to.true;
  345. } );
  346. } );
  347. describe( 'keyboard support', () => {
  348. it( 'should show the #_balloon with selected #formView on `CTRL+K` keystroke', () => {
  349. // Method is stubbed because it returns internal promise which can't be returned in test.
  350. const spy = testUtils.sinon.stub( linkFeature, '_showPanel', () => {} );
  351. editor.keystrokes.press( { keyCode: keyCodes.k, ctrlKey: true } );
  352. sinon.assert.calledWithExactly( spy, true );
  353. } );
  354. it( 'should focus the the #formView on `Tab` key press when the #_balloon is open', () => {
  355. const keyEvtData = {
  356. keyCode: keyCodes.tab,
  357. preventDefault: sinon.spy(),
  358. stopPropagation: sinon.spy()
  359. };
  360. // Balloon is invisible, form not focused.
  361. formView.focusTracker.isFocused = false;
  362. const spy = sinon.spy( formView, 'focus' );
  363. editor.keystrokes.press( keyEvtData );
  364. sinon.assert.notCalled( keyEvtData.preventDefault );
  365. sinon.assert.notCalled( keyEvtData.stopPropagation );
  366. sinon.assert.notCalled( spy );
  367. // Balloon is visible, form focused.
  368. return linkFeature._showPanel( true )
  369. .then( () => {
  370. formView.focusTracker.isFocused = true;
  371. editor.keystrokes.press( keyEvtData );
  372. sinon.assert.notCalled( keyEvtData.preventDefault );
  373. sinon.assert.notCalled( keyEvtData.stopPropagation );
  374. sinon.assert.notCalled( spy );
  375. // Balloon is still visible, form not focused.
  376. formView.focusTracker.isFocused = false;
  377. editor.keystrokes.press( keyEvtData );
  378. sinon.assert.calledOnce( keyEvtData.preventDefault );
  379. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  380. sinon.assert.calledOnce( spy );
  381. } );
  382. } );
  383. it( 'should hide the #_balloon after Esc key press (from editor) and not focus the editable', () => {
  384. const spy = testUtils.sinon.spy( linkFeature, '_hidePanel' );
  385. const keyEvtData = {
  386. keyCode: keyCodes.esc,
  387. preventDefault: sinon.spy(),
  388. stopPropagation: sinon.spy()
  389. };
  390. // Balloon is visible.
  391. return linkFeature._showPanel( false ).then( () => {
  392. editor.keystrokes.press( keyEvtData );
  393. sinon.assert.calledWithExactly( spy );
  394. } );
  395. } );
  396. it( 'should not hide #_balloon after Esc key press (from editor) when #_balloon is open but is not visible', () => {
  397. const spy = testUtils.sinon.spy( linkFeature, '_hidePanel' );
  398. const keyEvtData = {
  399. keyCode: keyCodes.esc,
  400. preventDefault: () => {},
  401. stopPropagation: () => {}
  402. };
  403. const viewMock = {
  404. ready: true,
  405. init: () => {},
  406. destroy: () => {}
  407. };
  408. return linkFeature._showPanel( false )
  409. .then( () => balloon.add( { view: viewMock } ) )
  410. .then( () => {
  411. editor.keystrokes.press( keyEvtData );
  412. sinon.assert.notCalled( spy );
  413. } );
  414. } );
  415. it( 'should hide the #_balloon after Esc key press (from the form) and focus the editable', () => {
  416. const spy = testUtils.sinon.spy( linkFeature, '_hidePanel' );
  417. const keyEvtData = {
  418. keyCode: keyCodes.esc,
  419. preventDefault: sinon.spy(),
  420. stopPropagation: sinon.spy()
  421. };
  422. return linkFeature._showPanel( true )
  423. .then( () => {
  424. formView.keystrokes.press( keyEvtData );
  425. sinon.assert.calledWithExactly( spy, true );
  426. } );
  427. } );
  428. } );
  429. describe( 'mouse support', () => {
  430. it( 'should hide #_balloon and not focus editable on click outside the #_balloon', () => {
  431. const spy = testUtils.sinon.spy( linkFeature, '_hidePanel' );
  432. return linkFeature._showPanel( true )
  433. .then( () => {
  434. document.body.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
  435. sinon.assert.calledWithExactly( spy );
  436. } );
  437. } );
  438. it( 'should not hide #_balloon on click inside the #_balloon', () => {
  439. const spy = testUtils.sinon.spy( linkFeature, '_hidePanel' );
  440. return linkFeature._showPanel( true )
  441. .then( () => {
  442. balloon.view.element.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
  443. sinon.assert.notCalled( spy );
  444. } );
  445. } );
  446. describe( 'clicking on editable', () => {
  447. let observer, spy;
  448. beforeEach( () => {
  449. observer = editor.editing.view.getObserver( ClickObserver );
  450. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  451. // Method is stubbed because it returns internal promise which can't be returned in test.
  452. spy = testUtils.sinon.stub( linkFeature, '_showPanel', () => {} );
  453. } );
  454. it( 'should open with not selected formView when collapsed selection is inside link element', () => {
  455. setModelData( editor.document, '<$text linkHref="url">fo[]o</$text>' );
  456. observer.fire( 'click', { target: document.body } );
  457. sinon.assert.calledWithExactly( spy );
  458. } );
  459. it( 'should open when selection exclusively encloses a LinkElement (#1)', () => {
  460. setModelData( editor.document, '[<$text linkHref="url">foo</$text>]' );
  461. observer.fire( 'click', { target: {} } );
  462. sinon.assert.calledWithExactly( spy );
  463. } );
  464. it( 'should open when selection exclusively encloses a LinkElement (#2)', () => {
  465. setModelData( editor.document, '<$text linkHref="url">[foo]</$text>' );
  466. observer.fire( 'click', { target: {} } );
  467. sinon.assert.calledWithExactly( spy );
  468. } );
  469. it( 'should not open when selection is not inside link element', () => {
  470. setModelData( editor.document, '[]' );
  471. observer.fire( 'click', { target: {} } );
  472. sinon.assert.notCalled( spy );
  473. } );
  474. it( 'should not open when selection is non-collapsed and doesn\'t enclose a LinkElement (#1)', () => {
  475. setModelData( editor.document, '<$text linkHref="url">f[o]o</$text>' );
  476. observer.fire( 'click', { target: {} } );
  477. sinon.assert.notCalled( spy );
  478. } );
  479. it( 'should not open when selection is non-collapsed and doesn\'t enclose a LinkElement (#2)', () => {
  480. setModelData( editor.document, '<$text linkHref="url">[fo]o</$text>' );
  481. observer.fire( 'click', { target: {} } );
  482. sinon.assert.notCalled( spy );
  483. } );
  484. it( 'should not open when selection is non-collapsed and doesn\'t enclose a LinkElement (#3)', () => {
  485. setModelData( editor.document, '<$text linkHref="url">f[oo]</$text>' );
  486. observer.fire( 'click', { target: {} } );
  487. sinon.assert.notCalled( spy );
  488. } );
  489. it( 'should not open when selection is non-collapsed and doesn\'t enclose a LinkElement (#4)', () => {
  490. setModelData( editor.document, 'ba[r<$text linkHref="url">foo]</$text>' );
  491. observer.fire( 'click', { target: {} } );
  492. sinon.assert.notCalled( spy );
  493. } );
  494. it( 'should not open when selection is non-collapsed and doesn\'t enclose a LinkElement (#5)', () => {
  495. setModelData( editor.document, 'ba[r<$text linkHref="url">foo</$text>]' );
  496. observer.fire( 'click', { target: {} } );
  497. sinon.assert.notCalled( spy );
  498. } );
  499. } );
  500. } );
  501. describe( 'link form', () => {
  502. let focusEditableSpy;
  503. beforeEach( () => {
  504. focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  505. } );
  506. it( 'should mark the editor ui as focused when the #formView is focused', () => {
  507. return linkFeature._showPanel()
  508. .then( () => {
  509. editor.ui.focusTracker.isFocused = false;
  510. formView.element.dispatchEvent( new Event( 'focus' ) );
  511. expect( editor.ui.focusTracker.isFocused ).to.true;
  512. } );
  513. } );
  514. describe( 'binding', () => {
  515. it( 'should bind formView.urlInputView#value to link command value', () => {
  516. const command = editor.commands.get( 'link' );
  517. expect( formView.urlInputView.value ).to.undefined;
  518. command.value = 'http://cksource.com';
  519. expect( formView.urlInputView.value ).to.equal( 'http://cksource.com' );
  520. } );
  521. it( 'should execute link command on formView#submit event', () => {
  522. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  523. formView.urlInputView.value = 'http://ckeditor.com';
  524. expect( formView.urlInputView.inputView.element.value ).to.equal( 'http://ckeditor.com' );
  525. formView.urlInputView.inputView.element.value = 'http://cksource.com';
  526. formView.fire( 'submit' );
  527. expect( executeSpy.calledOnce ).to.true;
  528. expect( executeSpy.calledWithExactly( 'link', 'http://cksource.com' ) ).to.true;
  529. } );
  530. it( 'should hide and focus editable on formView#submit event', () => {
  531. return linkFeature._showPanel()
  532. .then( () => {
  533. formView.fire( 'submit' );
  534. expect( balloon.visibleView ).to.null;
  535. expect( focusEditableSpy.calledOnce ).to.true;
  536. } );
  537. } );
  538. it( 'should execute unlink command on formView#unlink event', () => {
  539. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  540. formView.fire( 'unlink' );
  541. expect( executeSpy.calledOnce ).to.true;
  542. expect( executeSpy.calledWithExactly( 'unlink' ) ).to.true;
  543. } );
  544. it( 'should hide and focus editable on formView#unlink event', () => {
  545. return linkFeature._showPanel()
  546. .then( () => {
  547. formView.fire( 'unlink' );
  548. expect( balloon.visibleView ).to.null;
  549. expect( focusEditableSpy.calledOnce ).to.true;
  550. } );
  551. } );
  552. it( 'should hide and focus editable on formView#cancel event', () => {
  553. return linkFeature._showPanel()
  554. .then( () => {
  555. formView.fire( 'cancel' );
  556. expect( balloon.visibleView ).to.null;
  557. expect( focusEditableSpy.calledOnce ).to.true;
  558. } );
  559. } );
  560. } );
  561. } );
  562. } );