link.js 24 KB

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