8
0

link.js 25 KB

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