link.js 23 KB

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