link.js 24 KB

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