8
0

link.js 24 KB

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