linkui.js 43 KB

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