8
0

linkui.js 24 KB

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