link.js 24 KB

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