link.js 24 KB

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