linkui.js 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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 View from '@ckeditor/ckeditor5-ui/src/view';
  19. import ClickObserver from '@ckeditor/ckeditor5-engine/src/view/observer/clickobserver';
  20. describe( 'LinkUI', () => {
  21. let editor, linkUIFeature, linkButton, balloon, formView, actionsView, editorElement;
  22. testUtils.createSinonSandbox();
  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 be named', () => {
  48. expect( LinkUI.pluginName ).to.equal( 'LinkUI' );
  49. } );
  50. it( 'should load ContextualBalloon', () => {
  51. expect( editor.plugins.get( ContextualBalloon ) ).to.be.instanceOf( ContextualBalloon );
  52. } );
  53. describe( 'init', () => {
  54. it( 'should register click observer', () => {
  55. expect( editor.editing.view.getObserver( ClickObserver ) ).to.be.instanceOf( ClickObserver );
  56. } );
  57. it( 'should create #actionsView', () => {
  58. expect( actionsView ).to.be.instanceOf( LinkActionsView );
  59. } );
  60. it( 'should create #formView', () => {
  61. expect( formView ).to.be.instanceOf( LinkFormView );
  62. } );
  63. describe( 'link toolbar button', () => {
  64. it( 'should be registered', () => {
  65. expect( linkButton ).to.be.instanceOf( ButtonView );
  66. } );
  67. it( 'should be bound to the link command', () => {
  68. const command = editor.commands.get( 'link' );
  69. command.isEnabled = true;
  70. command.value = true;
  71. expect( linkButton.isOn ).to.be.true;
  72. expect( linkButton.isEnabled ).to.be.true;
  73. command.isEnabled = false;
  74. command.value = false;
  75. expect( linkButton.isOn ).to.be.false;
  76. expect( linkButton.isEnabled ).to.be.false;
  77. } );
  78. it( 'should call #_showUI upon #execute', () => {
  79. const spy = testUtils.sinon.stub( linkUIFeature, '_showUI' ).returns( {} );
  80. linkButton.fire( 'execute' );
  81. sinon.assert.calledWithExactly( spy, true );
  82. } );
  83. } );
  84. } );
  85. describe( '_showUI()', () => {
  86. let balloonAddSpy;
  87. beforeEach( () => {
  88. balloonAddSpy = testUtils.sinon.spy( balloon, 'add' );
  89. editor.editing.view.document.isFocused = true;
  90. } );
  91. it( 'should not work if the link command is disabled', () => {
  92. setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
  93. editor.commands.get( 'link' ).isEnabled = false;
  94. linkUIFeature._showUI();
  95. expect( balloon.visibleView ).to.be.null;
  96. } );
  97. it( 'should not throw if the UI is already visible', () => {
  98. setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
  99. linkUIFeature._showUI();
  100. expect( () => {
  101. linkUIFeature._showUI();
  102. } ).to.not.throw();
  103. } );
  104. it( 'should add #formView to the balloon and attach the balloon to the selection when text fragment is selected', () => {
  105. setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
  106. const selectedRange = editorElement.ownerDocument.getSelection().getRangeAt( 0 );
  107. linkUIFeature._showUI();
  108. expect( balloon.visibleView ).to.equal( formView );
  109. sinon.assert.calledWithExactly( balloonAddSpy, {
  110. view: formView,
  111. position: {
  112. target: selectedRange
  113. }
  114. } );
  115. } );
  116. it( 'should add #formView to the balloon and attach the balloon to the selection when selection is collapsed', () => {
  117. setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
  118. const selectedRange = editorElement.ownerDocument.getSelection().getRangeAt( 0 );
  119. linkUIFeature._showUI();
  120. expect( balloon.visibleView ).to.equal( formView );
  121. sinon.assert.calledWithExactly( balloonAddSpy, {
  122. view: formView,
  123. position: {
  124. target: selectedRange
  125. }
  126. } );
  127. } );
  128. it( 'should add #actionsView to the balloon and attach the balloon to the link element when collapsed selection is inside ' +
  129. 'that link',
  130. () => {
  131. setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
  132. const linkElement = editor.editing.view.getDomRoot().querySelector( 'a' );
  133. linkUIFeature._showUI();
  134. expect( balloon.visibleView ).to.equal( actionsView );
  135. sinon.assert.calledWithExactly( balloonAddSpy, {
  136. view: actionsView,
  137. position: {
  138. target: linkElement
  139. }
  140. } );
  141. } );
  142. // #https://github.com/ckeditor/ckeditor5-link/issues/181
  143. it( 'should add #formView to the balloon when collapsed selection is inside the link and #actionsView is already visible', () => {
  144. setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
  145. const linkElement = editor.editing.view.getDomRoot().querySelector( 'a' );
  146. linkUIFeature._showUI();
  147. expect( balloon.visibleView ).to.equal( actionsView );
  148. sinon.assert.calledWithExactly( balloonAddSpy, {
  149. view: actionsView,
  150. position: {
  151. target: linkElement
  152. }
  153. } );
  154. linkUIFeature._showUI();
  155. expect( balloon.visibleView ).to.equal( formView );
  156. sinon.assert.calledWithExactly( balloonAddSpy, {
  157. view: formView,
  158. position: {
  159. target: linkElement
  160. }
  161. } );
  162. } );
  163. it( 'should disable #formView and #actionsView elements when link and unlink commands are disabled', () => {
  164. setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
  165. linkUIFeature._showUI();
  166. editor.commands.get( 'link' ).isEnabled = true;
  167. editor.commands.get( 'unlink' ).isEnabled = true;
  168. expect( formView.urlInputView.isReadOnly ).to.be.false;
  169. expect( formView.saveButtonView.isEnabled ).to.be.true;
  170. expect( formView.cancelButtonView.isEnabled ).to.be.true;
  171. expect( actionsView.unlinkButtonView.isEnabled ).to.be.true;
  172. expect( actionsView.editButtonView.isEnabled ).to.be.true;
  173. editor.commands.get( 'link' ).isEnabled = false;
  174. editor.commands.get( 'unlink' ).isEnabled = false;
  175. expect( formView.urlInputView.isReadOnly ).to.be.true;
  176. expect( formView.saveButtonView.isEnabled ).to.be.false;
  177. expect( formView.cancelButtonView.isEnabled ).to.be.true;
  178. expect( actionsView.unlinkButtonView.isEnabled ).to.be.false;
  179. expect( actionsView.editButtonView.isEnabled ).to.be.false;
  180. } );
  181. // https://github.com/ckeditor/ckeditor5-link/issues/78
  182. it( 'should make sure the URL input in the #formView always stays in sync with the value of the command (selected link)', () => {
  183. setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
  184. // Mock some leftover value **in DOM**, e.g. after previous editing.
  185. formView.urlInputView.inputView.element.value = 'leftover';
  186. linkUIFeature._showUI();
  187. actionsView.fire( 'edit' );
  188. expect( formView.urlInputView.inputView.element.value ).to.equal( 'url' );
  189. } );
  190. // https://github.com/ckeditor/ckeditor5-link/issues/123
  191. it( 'should make sure the URL input in the #formView always stays in sync with the value of the command (no link selected)', () => {
  192. setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
  193. linkUIFeature._showUI();
  194. expect( formView.urlInputView.inputView.element.value ).to.equal( '' );
  195. } );
  196. it( 'should optionally force `main` stack to be visible', () => {
  197. setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
  198. balloon.add( {
  199. view: new View(),
  200. stackId: 'secondary'
  201. } );
  202. linkUIFeature._showUI( true );
  203. expect( balloon.visibleView ).to.equal( formView );
  204. } );
  205. describe( 'response to ui#update', () => {
  206. let view, viewDocument;
  207. beforeEach( () => {
  208. view = editor.editing.view;
  209. viewDocument = view.document;
  210. } );
  211. it( 'should not duplicate #update listeners', () => {
  212. setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
  213. const spy = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
  214. linkUIFeature._showUI();
  215. editor.ui.fire( 'update' );
  216. linkUIFeature._hideUI();
  217. linkUIFeature._showUI();
  218. editor.ui.fire( 'update' );
  219. sinon.assert.calledTwice( spy );
  220. } );
  221. // https://github.com/ckeditor/ckeditor5-link/issues/113
  222. it( 'updates the position of the panel – editing a link, then the selection remains in the link', () => {
  223. setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
  224. linkUIFeature._showUI();
  225. const spy = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
  226. expect( getViewData( view ) ).to.equal(
  227. '<p><a class="ck-link_selected" href="url">f{}oo</a></p>'
  228. );
  229. const root = viewDocument.getRoot();
  230. const linkElement = root.getChild( 0 ).getChild( 0 );
  231. const text = linkElement.getChild( 0 );
  232. // Move selection to foo[].
  233. view.change( writer => {
  234. writer.setSelection( text, 3, true );
  235. } );
  236. sinon.assert.calledOnce( spy );
  237. sinon.assert.calledWithExactly( spy, {
  238. target: view.domConverter.mapViewToDom( linkElement )
  239. } );
  240. } );
  241. // https://github.com/ckeditor/ckeditor5-link/issues/113
  242. it( 'updates the position of the panel – creating a new link, then the selection moved', () => {
  243. setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
  244. linkUIFeature._showUI();
  245. const spy = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
  246. const root = viewDocument.getRoot();
  247. const text = root.getChild( 0 ).getChild( 0 );
  248. view.change( writer => {
  249. writer.setSelection( text, 3, true );
  250. } );
  251. sinon.assert.calledOnce( spy );
  252. sinon.assert.calledWithExactly( spy, {
  253. target: editorElement.ownerDocument.getSelection().getRangeAt( 0 )
  254. } );
  255. } );
  256. // https://github.com/ckeditor/ckeditor5-link/issues/113
  257. it( 'hides of the panel – editing a link, then the selection moved out of the link', () => {
  258. setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text>bar</paragraph>' );
  259. linkUIFeature._showUI();
  260. const spyUpdate = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
  261. const spyHide = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  262. const root = viewDocument.getRoot();
  263. const text = root.getChild( 0 ).getChild( 1 );
  264. // Move selection to b[]ar.
  265. view.change( writer => {
  266. writer.setSelection( text, 1, true );
  267. } );
  268. sinon.assert.calledOnce( spyHide );
  269. sinon.assert.notCalled( spyUpdate );
  270. } );
  271. // https://github.com/ckeditor/ckeditor5-link/issues/113
  272. it( 'hides the panel – editing a link, then the selection expands', () => {
  273. setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
  274. linkUIFeature._showUI();
  275. const spyUpdate = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
  276. const spyHide = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  277. expect( getViewData( view ) ).to.equal( '<p><a class="ck-link_selected" href="url">f{}oo</a></p>' );
  278. const root = viewDocument.getRoot();
  279. const text = root.getChild( 0 ).getChild( 0 ).getChild( 0 );
  280. // Move selection to f[o]o.
  281. view.change( writer => {
  282. writer.setSelection( writer.createRange(
  283. writer.createPositionAt( text, 1 ),
  284. writer.createPositionAt( text, 2 )
  285. ), true );
  286. } );
  287. sinon.assert.calledOnce( spyHide );
  288. sinon.assert.notCalled( spyUpdate );
  289. } );
  290. // https://github.com/ckeditor/ckeditor5-link/issues/113
  291. it( 'hides the panel – creating a new link, then the selection moved to another parent', () => {
  292. setModelData( editor.model, '<paragraph>f[]oo</paragraph><paragraph>bar</paragraph>' );
  293. linkUIFeature._showUI();
  294. const spyUpdate = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
  295. const spyHide = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  296. const root = viewDocument.getRoot();
  297. const text = root.getChild( 1 ).getChild( 0 );
  298. // Move selection to f[o]o.
  299. view.change( writer => {
  300. writer.setSelection( writer.createRange(
  301. writer.createPositionAt( text, 1 ),
  302. writer.createPositionAt( text, 2 )
  303. ), true );
  304. } );
  305. sinon.assert.calledOnce( spyHide );
  306. sinon.assert.notCalled( spyUpdate );
  307. } );
  308. } );
  309. } );
  310. describe( '_hideUI()', () => {
  311. beforeEach( () => {
  312. linkUIFeature._showUI();
  313. } );
  314. it( 'should remove the UI from the balloon', () => {
  315. expect( balloon.hasView( formView ) ).to.be.true;
  316. expect( balloon.hasView( actionsView ) ).to.be.true;
  317. linkUIFeature._hideUI();
  318. expect( balloon.hasView( formView ) ).to.be.false;
  319. expect( balloon.hasView( actionsView ) ).to.be.false;
  320. } );
  321. it( 'should focus the `editable` by default', () => {
  322. const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  323. linkUIFeature._hideUI();
  324. // First call is from _removeFormView.
  325. sinon.assert.calledTwice( spy );
  326. } );
  327. // https://github.com/ckeditor/ckeditor5-link/issues/193
  328. it( 'should focus the `editable` before before removing elements from the balloon', () => {
  329. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  330. const removeSpy = testUtils.sinon.spy( balloon, 'remove' );
  331. linkUIFeature._hideUI();
  332. expect( focusSpy.calledBefore( removeSpy ) ).to.equal( true );
  333. } );
  334. it( 'should not throw an error when views are not in the `balloon`', () => {
  335. linkUIFeature._hideUI();
  336. expect( () => {
  337. linkUIFeature._hideUI();
  338. } ).to.not.throw();
  339. } );
  340. it( 'should clear ui#update listener from the ViewDocument', () => {
  341. const spy = sinon.spy();
  342. linkUIFeature.listenTo( editor.ui, 'update', spy );
  343. linkUIFeature._hideUI();
  344. editor.ui.fire( 'update' );
  345. sinon.assert.notCalled( spy );
  346. } );
  347. } );
  348. describe( 'keyboard support', () => {
  349. it( 'should show the UI on Ctrl+K keystroke', () => {
  350. const spy = testUtils.sinon.stub( linkUIFeature, '_showUI' ).returns( {} );
  351. const command = editor.commands.get( 'link' );
  352. command.isEnabled = false;
  353. editor.keystrokes.press( {
  354. keyCode: keyCodes.k,
  355. ctrlKey: true,
  356. preventDefault: sinon.spy(),
  357. stopPropagation: sinon.spy()
  358. } );
  359. sinon.assert.notCalled( spy );
  360. command.isEnabled = true;
  361. editor.keystrokes.press( {
  362. keyCode: keyCodes.k,
  363. ctrlKey: true,
  364. preventDefault: sinon.spy(),
  365. stopPropagation: sinon.spy()
  366. } );
  367. sinon.assert.calledWithExactly( spy, true );
  368. } );
  369. it( 'should prevent default action on Ctrl+K keystroke', () => {
  370. const preventDefaultSpy = sinon.spy();
  371. const stopPropagationSpy = sinon.spy();
  372. editor.keystrokes.press( {
  373. keyCode: keyCodes.k,
  374. ctrlKey: true,
  375. preventDefault: preventDefaultSpy,
  376. stopPropagation: stopPropagationSpy
  377. } );
  378. sinon.assert.calledOnce( preventDefaultSpy );
  379. sinon.assert.calledOnce( stopPropagationSpy );
  380. } );
  381. it( 'should make stack with link visible on Ctrl+K keystroke - no link', () => {
  382. const command = editor.commands.get( 'link' );
  383. command.isEnabled = true;
  384. balloon.add( {
  385. view: new View(),
  386. stackId: 'custom'
  387. } );
  388. editor.keystrokes.press( {
  389. keyCode: keyCodes.k,
  390. ctrlKey: true,
  391. preventDefault: sinon.spy(),
  392. stopPropagation: sinon.spy()
  393. } );
  394. expect( balloon.visibleView ).to.equal( formView );
  395. } );
  396. it( 'should make stack with link visible on Ctrl+K keystroke - link', () => {
  397. setModelData( editor.model, '<paragraph><$text linkHref="foo.html">f[]oo</$text></paragraph>' );
  398. const customView = new View();
  399. balloon.add( {
  400. view: customView,
  401. stackId: 'custom'
  402. } );
  403. expect( balloon.visibleView ).to.equal( customView );
  404. editor.keystrokes.press( {
  405. keyCode: keyCodes.k,
  406. ctrlKey: true,
  407. preventDefault: sinon.spy(),
  408. stopPropagation: sinon.spy()
  409. } );
  410. expect( balloon.visibleView ).to.equal( actionsView );
  411. editor.keystrokes.press( {
  412. keyCode: keyCodes.k,
  413. ctrlKey: true,
  414. preventDefault: sinon.spy(),
  415. stopPropagation: sinon.spy()
  416. } );
  417. expect( balloon.visibleView ).to.equal( formView );
  418. } );
  419. it( 'should focus the the #actionsView on `Tab` key press when #actionsView is visible', () => {
  420. const keyEvtData = {
  421. keyCode: keyCodes.tab,
  422. preventDefault: sinon.spy(),
  423. stopPropagation: sinon.spy()
  424. };
  425. const normalPriorityTabCallbackSpy = sinon.spy();
  426. const highestPriorityTabCallbackSpy = sinon.spy();
  427. editor.keystrokes.set( 'Tab', normalPriorityTabCallbackSpy );
  428. editor.keystrokes.set( 'Tab', highestPriorityTabCallbackSpy, { priority: 'highest' } );
  429. // Balloon is invisible, form not focused.
  430. actionsView.focusTracker.isFocused = false;
  431. const spy = sinon.spy( actionsView, 'focus' );
  432. editor.keystrokes.press( keyEvtData );
  433. sinon.assert.notCalled( keyEvtData.preventDefault );
  434. sinon.assert.notCalled( keyEvtData.stopPropagation );
  435. sinon.assert.notCalled( spy );
  436. sinon.assert.calledOnce( normalPriorityTabCallbackSpy );
  437. sinon.assert.calledOnce( highestPriorityTabCallbackSpy );
  438. // Balloon is visible, form focused.
  439. linkUIFeature._showUI();
  440. testUtils.sinon.stub( linkUIFeature, '_areActionsVisible' ).value( true );
  441. actionsView.focusTracker.isFocused = true;
  442. editor.keystrokes.press( keyEvtData );
  443. sinon.assert.notCalled( keyEvtData.preventDefault );
  444. sinon.assert.notCalled( keyEvtData.stopPropagation );
  445. sinon.assert.notCalled( spy );
  446. sinon.assert.calledTwice( normalPriorityTabCallbackSpy );
  447. sinon.assert.calledTwice( highestPriorityTabCallbackSpy );
  448. // Balloon is still visible, form not focused.
  449. actionsView.focusTracker.isFocused = false;
  450. editor.keystrokes.press( keyEvtData );
  451. sinon.assert.calledOnce( keyEvtData.preventDefault );
  452. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  453. sinon.assert.calledOnce( spy );
  454. sinon.assert.calledTwice( normalPriorityTabCallbackSpy );
  455. sinon.assert.calledThrice( highestPriorityTabCallbackSpy );
  456. } );
  457. it( 'should hide the UI after Esc key press (from editor) and not focus the editable', () => {
  458. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  459. const keyEvtData = {
  460. keyCode: keyCodes.esc,
  461. preventDefault: sinon.spy(),
  462. stopPropagation: sinon.spy()
  463. };
  464. // Balloon is visible.
  465. linkUIFeature._showUI();
  466. editor.keystrokes.press( keyEvtData );
  467. sinon.assert.calledWithExactly( spy );
  468. } );
  469. it( 'should not hide the UI after Esc key press (from editor) when UI is open but is not visible', () => {
  470. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  471. const keyEvtData = {
  472. keyCode: keyCodes.esc,
  473. preventDefault: () => {},
  474. stopPropagation: () => {}
  475. };
  476. const viewMock = {
  477. ready: true,
  478. render: () => {},
  479. destroy: () => {}
  480. };
  481. linkUIFeature._showUI();
  482. // Some view precedes the link UI in the balloon.
  483. balloon.add( { view: viewMock } );
  484. editor.keystrokes.press( keyEvtData );
  485. sinon.assert.notCalled( spy );
  486. } );
  487. } );
  488. describe( 'mouse support', () => {
  489. it( 'should hide the UI and not focus editable upon clicking outside the UI', () => {
  490. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  491. linkUIFeature._showUI();
  492. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  493. sinon.assert.calledWithExactly( spy );
  494. } );
  495. it( 'should hide the UI when link is in not currently visible stack', () => {
  496. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  497. balloon.add( {
  498. view: new View(),
  499. stackId: 'secondary'
  500. } );
  501. linkUIFeature._showUI();
  502. // Be sure any of link view is not currently visible/
  503. expect( balloon.visibleView ).to.not.equal( actionsView );
  504. expect( balloon.visibleView ).to.not.equal( formView );
  505. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  506. sinon.assert.calledWithExactly( spy );
  507. } );
  508. it( 'should not hide the UI upon clicking inside the the UI', () => {
  509. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  510. linkUIFeature._showUI();
  511. balloon.view.element.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  512. sinon.assert.notCalled( spy );
  513. } );
  514. describe( 'clicking on editable', () => {
  515. let observer, spy;
  516. beforeEach( () => {
  517. observer = editor.editing.view.getObserver( ClickObserver );
  518. editor.model.schema.extend( '$text', { allowIn: '$root' } );
  519. spy = testUtils.sinon.stub( linkUIFeature, '_showUI' ).returns( {} );
  520. } );
  521. it( 'should show the UI when collapsed selection is inside link element', () => {
  522. setModelData( editor.model, '<$text linkHref="url">fo[]o</$text>' );
  523. observer.fire( 'click', { target: document.body } );
  524. sinon.assert.calledWithExactly( spy );
  525. } );
  526. it( 'should show the UI when selection exclusively encloses a link element (#1)', () => {
  527. setModelData( editor.model, '[<$text linkHref="url">foo</$text>]' );
  528. observer.fire( 'click', { target: {} } );
  529. sinon.assert.calledWithExactly( spy );
  530. } );
  531. it( 'should show the UI when selection exclusively encloses a link element (#2)', () => {
  532. setModelData( editor.model, '<$text linkHref="url">[foo]</$text>' );
  533. observer.fire( 'click', { target: {} } );
  534. sinon.assert.calledWithExactly( spy );
  535. } );
  536. it( 'should do nothing when selection is not inside link element', () => {
  537. setModelData( editor.model, '[]' );
  538. observer.fire( 'click', { target: {} } );
  539. sinon.assert.notCalled( spy );
  540. } );
  541. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#1)', () => {
  542. setModelData( editor.model, '<$text linkHref="url">f[o]o</$text>' );
  543. observer.fire( 'click', { target: {} } );
  544. sinon.assert.notCalled( spy );
  545. } );
  546. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#2)', () => {
  547. setModelData( editor.model, '<$text linkHref="url">[fo]o</$text>' );
  548. observer.fire( 'click', { target: {} } );
  549. sinon.assert.notCalled( spy );
  550. } );
  551. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#3)', () => {
  552. setModelData( editor.model, '<$text linkHref="url">f[oo]</$text>' );
  553. observer.fire( 'click', { target: {} } );
  554. sinon.assert.notCalled( spy );
  555. } );
  556. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#4)', () => {
  557. setModelData( editor.model, 'ba[r<$text linkHref="url">foo]</$text>' );
  558. observer.fire( 'click', { target: {} } );
  559. sinon.assert.notCalled( spy );
  560. } );
  561. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#5)', () => {
  562. setModelData( editor.model, 'ba[r<$text linkHref="url">foo</$text>]' );
  563. observer.fire( 'click', { target: {} } );
  564. sinon.assert.notCalled( spy );
  565. } );
  566. } );
  567. } );
  568. describe( 'actions view', () => {
  569. let focusEditableSpy;
  570. beforeEach( () => {
  571. focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  572. } );
  573. it( 'should mark the editor UI as focused when the #actionsView is focused', () => {
  574. linkUIFeature._showUI();
  575. linkUIFeature._removeFormView();
  576. expect( balloon.visibleView ).to.equal( actionsView );
  577. editor.ui.focusTracker.isFocused = false;
  578. actionsView.element.dispatchEvent( new Event( 'focus' ) );
  579. expect( editor.ui.focusTracker.isFocused ).to.be.true;
  580. } );
  581. describe( 'binding', () => {
  582. it( 'should show the #formView on #edit event and select the URL input field', () => {
  583. linkUIFeature._showUI();
  584. linkUIFeature._removeFormView();
  585. const selectSpy = testUtils.sinon.spy( formView.urlInputView, 'select' );
  586. actionsView.fire( 'edit' );
  587. expect( balloon.visibleView ).to.equal( formView );
  588. sinon.assert.calledOnce( selectSpy );
  589. } );
  590. it( 'should execute unlink command on actionsView#unlink event', () => {
  591. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  592. actionsView.fire( 'unlink' );
  593. expect( executeSpy.calledOnce ).to.be.true;
  594. expect( executeSpy.calledWithExactly( 'unlink' ) ).to.be.true;
  595. } );
  596. it( 'should hide and focus editable on actionsView#unlink event', () => {
  597. linkUIFeature._showUI();
  598. linkUIFeature._removeFormView();
  599. // Removing the form would call the focus spy.
  600. focusEditableSpy.resetHistory();
  601. actionsView.fire( 'unlink' );
  602. expect( balloon.visibleView ).to.be.null;
  603. expect( focusEditableSpy.calledOnce ).to.be.true;
  604. } );
  605. it( 'should hide after Esc key press', () => {
  606. const keyEvtData = {
  607. keyCode: keyCodes.esc,
  608. preventDefault: sinon.spy(),
  609. stopPropagation: sinon.spy()
  610. };
  611. linkUIFeature._showUI();
  612. linkUIFeature._removeFormView();
  613. // Removing the form would call the focus spy.
  614. focusEditableSpy.resetHistory();
  615. actionsView.keystrokes.press( keyEvtData );
  616. expect( balloon.visibleView ).to.equal( null );
  617. expect( focusEditableSpy.calledOnce ).to.be.true;
  618. } );
  619. // #https://github.com/ckeditor/ckeditor5-link/issues/181
  620. it( 'should add the #formView upon Ctrl+K keystroke press', () => {
  621. const keyEvtData = {
  622. keyCode: keyCodes.k,
  623. ctrlKey: true,
  624. preventDefault: sinon.spy(),
  625. stopPropagation: sinon.spy()
  626. };
  627. linkUIFeature._showUI();
  628. linkUIFeature._removeFormView();
  629. expect( balloon.visibleView ).to.equal( actionsView );
  630. actionsView.keystrokes.press( keyEvtData );
  631. expect( balloon.visibleView ).to.equal( formView );
  632. } );
  633. } );
  634. } );
  635. describe( 'link form view', () => {
  636. let focusEditableSpy;
  637. beforeEach( () => {
  638. focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  639. } );
  640. it( 'should mark the editor UI as focused when the #formView is focused', () => {
  641. linkUIFeature._showUI();
  642. expect( balloon.visibleView ).to.equal( formView );
  643. editor.ui.focusTracker.isFocused = false;
  644. formView.element.dispatchEvent( new Event( 'focus' ) );
  645. expect( editor.ui.focusTracker.isFocused ).to.be.true;
  646. } );
  647. describe( 'binding', () => {
  648. beforeEach( () => {
  649. setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
  650. } );
  651. it( 'should bind formView.urlInputView#value to link command value', () => {
  652. const command = editor.commands.get( 'link' );
  653. expect( formView.urlInputView.value ).to.undefined;
  654. command.value = 'http://cksource.com';
  655. expect( formView.urlInputView.value ).to.equal( 'http://cksource.com' );
  656. } );
  657. it( 'should execute link command on formView#submit event', () => {
  658. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  659. formView.urlInputView.value = 'http://ckeditor.com';
  660. expect( formView.urlInputView.inputView.element.value ).to.equal( 'http://ckeditor.com' );
  661. formView.urlInputView.inputView.element.value = 'http://cksource.com';
  662. formView.fire( 'submit' );
  663. expect( executeSpy.calledOnce ).to.be.true;
  664. expect( executeSpy.calledWithExactly( 'link', 'http://cksource.com', {} ) ).to.be.true;
  665. } );
  666. it( 'should hide and reveal the #actionsView on formView#submit event', () => {
  667. linkUIFeature._showUI();
  668. formView.fire( 'submit' );
  669. expect( balloon.visibleView ).to.equal( actionsView );
  670. expect( focusEditableSpy.calledOnce ).to.be.true;
  671. } );
  672. it( 'should hide and reveal the #actionsView on formView#cancel event if link command has a value', () => {
  673. linkUIFeature._showUI();
  674. const command = editor.commands.get( 'link' );
  675. command.value = 'http://foo.com';
  676. formView.fire( 'cancel' );
  677. expect( balloon.visibleView ).to.equal( actionsView );
  678. expect( focusEditableSpy.calledOnce ).to.be.true;
  679. } );
  680. it( 'should hide the balloon on formView#cancel if link command does not have a value', () => {
  681. linkUIFeature._showUI();
  682. formView.fire( 'cancel' );
  683. expect( balloon.visibleView ).to.be.null;
  684. } );
  685. it( 'should hide and reveal the #actionsView after Esc key press if link command has a value', () => {
  686. const keyEvtData = {
  687. keyCode: keyCodes.esc,
  688. preventDefault: sinon.spy(),
  689. stopPropagation: sinon.spy()
  690. };
  691. linkUIFeature._showUI();
  692. const command = editor.commands.get( 'link' );
  693. command.value = 'http://foo.com';
  694. formView.keystrokes.press( keyEvtData );
  695. expect( balloon.visibleView ).to.equal( actionsView );
  696. expect( focusEditableSpy.calledOnce ).to.be.true;
  697. } );
  698. it( 'should hide the balloon after Esc key press if link command does not have a value', () => {
  699. const keyEvtData = {
  700. keyCode: keyCodes.esc,
  701. preventDefault: sinon.spy(),
  702. stopPropagation: sinon.spy()
  703. };
  704. linkUIFeature._showUI();
  705. formView.keystrokes.press( keyEvtData );
  706. expect( balloon.visibleView ).to.be.null;
  707. } );
  708. // https://github.com/ckeditor/ckeditor5/issues/1501
  709. it( 'should blur url input element before hiding the view', () => {
  710. linkUIFeature._showUI();
  711. const focusSpy = testUtils.sinon.spy( formView.saveButtonView, 'focus' );
  712. const removeSpy = testUtils.sinon.spy( balloon, 'remove' );
  713. formView.fire( 'cancel' );
  714. expect( focusSpy.calledBefore( removeSpy ) ).to.equal( true );
  715. } );
  716. it( 'should gather information about manual decorators', () => {
  717. let editor, model, formView;
  718. const editorElement = document.createElement( 'div' );
  719. document.body.appendChild( editorElement );
  720. return ClassicTestEditor
  721. .create( editorElement, {
  722. plugins: [ LinkEditing, LinkUI, Paragraph ],
  723. link: {
  724. decorators: [
  725. {
  726. mode: 'manual',
  727. label: 'Foo',
  728. attributes: {
  729. foo: 'bar'
  730. }
  731. }
  732. ]
  733. }
  734. } )
  735. .then( newEditor => {
  736. editor = newEditor;
  737. model = editor.model;
  738. model.schema.extend( '$text', {
  739. allowIn: '$root',
  740. allowAttributes: 'linkHref'
  741. } );
  742. const linkUIFeature = editor.plugins.get( LinkUI );
  743. const balloon = editor.plugins.get( ContextualBalloon );
  744. formView = linkUIFeature.formView;
  745. // There is no point to execute BalloonPanelView attachTo and pin methods so lets override it.
  746. testUtils.sinon.stub( balloon.view, 'attachTo' ).returns( {} );
  747. testUtils.sinon.stub( balloon.view, 'pin' ).returns( {} );
  748. formView.render();
  749. } )
  750. .then( () => {
  751. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  752. setModelData( model, 'f[<$text linkHref="url" linkManualDecorator0="true">ooba</$text>]r' );
  753. expect( formView.urlInputView.inputView.element.value ).to.equal( 'url' );
  754. expect( formView.customAttributesView.length ).to.equal( 1 );
  755. expect( formView.customAttributesView.first.isOn ).to.be.true;
  756. formView.fire( 'submit' );
  757. expect( executeSpy.calledOnce ).to.be.true;
  758. expect( executeSpy.calledWithExactly( 'link', 'url', { linkManualDecorator0: true } ) ).to.be.true;
  759. } );
  760. } );
  761. } );
  762. } );
  763. } );