linkui.js 43 KB

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