linkui.js 26 KB

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