8
0

link.js 25 KB

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