linkui.js 39 KB

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