linkui.js 42 KB

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