8
0

link.js 25 KB

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