link.js 25 KB

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