linkui.js 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  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. } );
  361. it( 'should display a fake visual selection on a collapsed selection', () => {
  362. setModelData( editor.model, '<paragraph>f[]o</paragraph>' );
  363. linkUIFeature._showUI();
  364. expect( editor.model.markers.has( 'link-ui' ) ).to.be.true;
  365. const paragraph = editor.model.document.getRoot().getChild( 0 );
  366. const expectedRange = editor.model.createRange(
  367. editor.model.createPositionAt( paragraph, 1 ),
  368. editor.model.createPositionAt( paragraph, 1 )
  369. );
  370. const markerRange = editor.model.markers.get( 'link-ui' ).getRange();
  371. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  372. } );
  373. } );
  374. describe( '_hideUI()', () => {
  375. beforeEach( () => {
  376. linkUIFeature._showUI();
  377. } );
  378. it( 'should remove the UI from the balloon', () => {
  379. expect( balloon.hasView( formView ) ).to.be.true;
  380. expect( balloon.hasView( actionsView ) ).to.be.true;
  381. linkUIFeature._hideUI();
  382. expect( balloon.hasView( formView ) ).to.be.false;
  383. expect( balloon.hasView( actionsView ) ).to.be.false;
  384. } );
  385. it( 'should focus the `editable` by default', () => {
  386. const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  387. linkUIFeature._hideUI();
  388. // First call is from _removeFormView.
  389. sinon.assert.calledTwice( spy );
  390. } );
  391. // https://github.com/ckeditor/ckeditor5-link/issues/193
  392. it( 'should focus the `editable` before before removing elements from the balloon', () => {
  393. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  394. const removeSpy = testUtils.sinon.spy( balloon, 'remove' );
  395. linkUIFeature._hideUI();
  396. expect( focusSpy.calledBefore( removeSpy ) ).to.equal( true );
  397. } );
  398. it( 'should not throw an error when views are not in the `balloon`', () => {
  399. linkUIFeature._hideUI();
  400. expect( () => {
  401. linkUIFeature._hideUI();
  402. } ).to.not.throw();
  403. } );
  404. it( 'should clear ui#update listener from the ViewDocument', () => {
  405. const spy = sinon.spy();
  406. linkUIFeature.listenTo( editor.ui, 'update', spy );
  407. linkUIFeature._hideUI();
  408. editor.ui.fire( 'update' );
  409. sinon.assert.notCalled( spy );
  410. } );
  411. it( 'should clear the fake visual selection from a selected text fragment', () => {
  412. expect( editor.model.markers.has( 'link-ui' ) ).to.be.true;
  413. linkUIFeature._hideUI();
  414. expect( editor.model.markers.has( 'link-ui' ) ).to.be.false;
  415. } );
  416. } );
  417. describe( 'keyboard support', () => {
  418. it( 'should show the UI on Ctrl+K keystroke', () => {
  419. const spy = testUtils.sinon.stub( linkUIFeature, '_showUI' ).returns( {} );
  420. editor.keystrokes.press( {
  421. keyCode: keyCodes.k,
  422. ctrlKey: true,
  423. preventDefault: sinon.spy(),
  424. stopPropagation: sinon.spy()
  425. } );
  426. sinon.assert.calledWithExactly( spy, true );
  427. } );
  428. it( 'should prevent default action on Ctrl+K keystroke', () => {
  429. const preventDefaultSpy = sinon.spy();
  430. const stopPropagationSpy = sinon.spy();
  431. editor.keystrokes.press( {
  432. keyCode: keyCodes.k,
  433. ctrlKey: true,
  434. preventDefault: preventDefaultSpy,
  435. stopPropagation: stopPropagationSpy
  436. } );
  437. sinon.assert.calledOnce( preventDefaultSpy );
  438. sinon.assert.calledOnce( stopPropagationSpy );
  439. } );
  440. it( 'should make stack with link visible on Ctrl+K keystroke - no link', () => {
  441. const command = editor.commands.get( 'link' );
  442. command.isEnabled = true;
  443. balloon.add( {
  444. view: new View(),
  445. stackId: 'custom'
  446. } );
  447. editor.keystrokes.press( {
  448. keyCode: keyCodes.k,
  449. ctrlKey: true,
  450. preventDefault: sinon.spy(),
  451. stopPropagation: sinon.spy()
  452. } );
  453. expect( balloon.visibleView ).to.equal( formView );
  454. } );
  455. it( 'should make stack with link visible on Ctrl+K keystroke - link', () => {
  456. setModelData( editor.model, '<paragraph><$text linkHref="foo.html">f[]oo</$text></paragraph>' );
  457. const customView = new View();
  458. balloon.add( {
  459. view: customView,
  460. stackId: 'custom'
  461. } );
  462. expect( balloon.visibleView ).to.equal( customView );
  463. editor.keystrokes.press( {
  464. keyCode: keyCodes.k,
  465. ctrlKey: true,
  466. preventDefault: sinon.spy(),
  467. stopPropagation: sinon.spy()
  468. } );
  469. expect( balloon.visibleView ).to.equal( actionsView );
  470. editor.keystrokes.press( {
  471. keyCode: keyCodes.k,
  472. ctrlKey: true,
  473. preventDefault: sinon.spy(),
  474. stopPropagation: sinon.spy()
  475. } );
  476. expect( balloon.visibleView ).to.equal( formView );
  477. } );
  478. it( 'should focus the the #actionsView on `Tab` key press when #actionsView is visible', () => {
  479. const keyEvtData = {
  480. keyCode: keyCodes.tab,
  481. preventDefault: sinon.spy(),
  482. stopPropagation: sinon.spy()
  483. };
  484. const normalPriorityTabCallbackSpy = sinon.spy();
  485. const highestPriorityTabCallbackSpy = sinon.spy();
  486. editor.keystrokes.set( 'Tab', normalPriorityTabCallbackSpy );
  487. editor.keystrokes.set( 'Tab', highestPriorityTabCallbackSpy, { priority: 'highest' } );
  488. // Balloon is invisible, form not focused.
  489. actionsView.focusTracker.isFocused = false;
  490. const spy = sinon.spy( actionsView, 'focus' );
  491. editor.keystrokes.press( keyEvtData );
  492. sinon.assert.notCalled( keyEvtData.preventDefault );
  493. sinon.assert.notCalled( keyEvtData.stopPropagation );
  494. sinon.assert.notCalled( spy );
  495. sinon.assert.calledOnce( normalPriorityTabCallbackSpy );
  496. sinon.assert.calledOnce( highestPriorityTabCallbackSpy );
  497. // Balloon is visible, form focused.
  498. linkUIFeature._showUI();
  499. testUtils.sinon.stub( linkUIFeature, '_areActionsVisible' ).value( true );
  500. actionsView.focusTracker.isFocused = true;
  501. editor.keystrokes.press( keyEvtData );
  502. sinon.assert.notCalled( keyEvtData.preventDefault );
  503. sinon.assert.notCalled( keyEvtData.stopPropagation );
  504. sinon.assert.notCalled( spy );
  505. sinon.assert.calledTwice( normalPriorityTabCallbackSpy );
  506. sinon.assert.calledTwice( highestPriorityTabCallbackSpy );
  507. // Balloon is still visible, form not focused.
  508. actionsView.focusTracker.isFocused = false;
  509. editor.keystrokes.press( keyEvtData );
  510. sinon.assert.calledOnce( keyEvtData.preventDefault );
  511. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  512. sinon.assert.calledOnce( spy );
  513. sinon.assert.calledTwice( normalPriorityTabCallbackSpy );
  514. sinon.assert.calledThrice( highestPriorityTabCallbackSpy );
  515. } );
  516. it( 'should hide the UI after Esc key press (from editor) and not focus the editable', () => {
  517. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  518. const keyEvtData = {
  519. keyCode: keyCodes.esc,
  520. preventDefault: sinon.spy(),
  521. stopPropagation: sinon.spy()
  522. };
  523. // Balloon is visible.
  524. linkUIFeature._showUI();
  525. editor.keystrokes.press( keyEvtData );
  526. sinon.assert.calledWithExactly( spy );
  527. } );
  528. it( 'should not hide the UI after Esc key press (from editor) when UI is open but is not visible', () => {
  529. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  530. const keyEvtData = {
  531. keyCode: keyCodes.esc,
  532. preventDefault: () => {},
  533. stopPropagation: () => {}
  534. };
  535. const viewMock = {
  536. ready: true,
  537. render: () => {},
  538. destroy: () => {}
  539. };
  540. linkUIFeature._showUI();
  541. // Some view precedes the link UI in the balloon.
  542. balloon.add( { view: viewMock } );
  543. editor.keystrokes.press( keyEvtData );
  544. sinon.assert.notCalled( spy );
  545. } );
  546. } );
  547. describe( 'mouse support', () => {
  548. it( 'should hide the UI and not focus editable upon clicking outside the UI', () => {
  549. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  550. linkUIFeature._showUI();
  551. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  552. sinon.assert.calledWithExactly( spy );
  553. } );
  554. it( 'should hide the UI when link is in not currently visible stack', () => {
  555. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  556. balloon.add( {
  557. view: new View(),
  558. stackId: 'secondary'
  559. } );
  560. linkUIFeature._showUI();
  561. // Be sure any of link view is not currently visible/
  562. expect( balloon.visibleView ).to.not.equal( actionsView );
  563. expect( balloon.visibleView ).to.not.equal( formView );
  564. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  565. sinon.assert.calledWithExactly( spy );
  566. } );
  567. it( 'should not hide the UI upon clicking inside the the UI', () => {
  568. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  569. linkUIFeature._showUI();
  570. balloon.view.element.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  571. sinon.assert.notCalled( spy );
  572. } );
  573. describe( 'clicking on editable', () => {
  574. let observer, spy;
  575. beforeEach( () => {
  576. observer = editor.editing.view.getObserver( ClickObserver );
  577. editor.model.schema.extend( '$text', { allowIn: '$root' } );
  578. spy = testUtils.sinon.stub( linkUIFeature, '_showUI' ).returns( {} );
  579. } );
  580. it( 'should show the UI when collapsed selection is inside link element', () => {
  581. setModelData( editor.model, '<$text linkHref="url">fo[]o</$text>' );
  582. observer.fire( 'click', { target: document.body } );
  583. sinon.assert.calledWithExactly( spy );
  584. } );
  585. it( 'should show the UI when selection exclusively encloses a link element (#1)', () => {
  586. setModelData( editor.model, '[<$text linkHref="url">foo</$text>]' );
  587. observer.fire( 'click', { target: {} } );
  588. sinon.assert.calledWithExactly( spy );
  589. } );
  590. it( 'should show the UI when selection exclusively encloses a link element (#2)', () => {
  591. setModelData( editor.model, '<$text linkHref="url">[foo]</$text>' );
  592. observer.fire( 'click', { target: {} } );
  593. sinon.assert.calledWithExactly( spy );
  594. } );
  595. it( 'should do nothing when selection is not inside link element', () => {
  596. setModelData( editor.model, '[]' );
  597. observer.fire( 'click', { target: {} } );
  598. sinon.assert.notCalled( spy );
  599. } );
  600. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#1)', () => {
  601. setModelData( editor.model, '<$text linkHref="url">f[o]o</$text>' );
  602. observer.fire( 'click', { target: {} } );
  603. sinon.assert.notCalled( spy );
  604. } );
  605. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#2)', () => {
  606. setModelData( editor.model, '<$text linkHref="url">[fo]o</$text>' );
  607. observer.fire( 'click', { target: {} } );
  608. sinon.assert.notCalled( spy );
  609. } );
  610. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#3)', () => {
  611. setModelData( editor.model, '<$text linkHref="url">f[oo]</$text>' );
  612. observer.fire( 'click', { target: {} } );
  613. sinon.assert.notCalled( spy );
  614. } );
  615. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#4)', () => {
  616. setModelData( editor.model, 'ba[r<$text linkHref="url">foo]</$text>' );
  617. observer.fire( 'click', { target: {} } );
  618. sinon.assert.notCalled( spy );
  619. } );
  620. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#5)', () => {
  621. setModelData( editor.model, 'ba[r<$text linkHref="url">foo</$text>]' );
  622. observer.fire( 'click', { target: {} } );
  623. sinon.assert.notCalled( spy );
  624. } );
  625. } );
  626. } );
  627. describe( 'actions view', () => {
  628. let focusEditableSpy;
  629. beforeEach( () => {
  630. focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  631. } );
  632. it( 'should mark the editor UI as focused when the #actionsView is focused', () => {
  633. linkUIFeature._showUI();
  634. linkUIFeature._removeFormView();
  635. expect( balloon.visibleView ).to.equal( actionsView );
  636. editor.ui.focusTracker.isFocused = false;
  637. actionsView.element.dispatchEvent( new Event( 'focus' ) );
  638. expect( editor.ui.focusTracker.isFocused ).to.be.true;
  639. } );
  640. describe( 'binding', () => {
  641. it( 'should show the #formView on #edit event and select the URL input field', () => {
  642. linkUIFeature._showUI();
  643. linkUIFeature._removeFormView();
  644. const selectSpy = testUtils.sinon.spy( formView.urlInputView.fieldView, 'select' );
  645. actionsView.fire( 'edit' );
  646. expect( balloon.visibleView ).to.equal( formView );
  647. sinon.assert.calledOnce( selectSpy );
  648. } );
  649. it( 'should execute unlink command on actionsView#unlink event', () => {
  650. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  651. actionsView.fire( 'unlink' );
  652. expect( executeSpy.calledOnce ).to.be.true;
  653. expect( executeSpy.calledWithExactly( 'unlink' ) ).to.be.true;
  654. } );
  655. it( 'should hide and focus editable on actionsView#unlink event', () => {
  656. linkUIFeature._showUI();
  657. linkUIFeature._removeFormView();
  658. // Removing the form would call the focus spy.
  659. focusEditableSpy.resetHistory();
  660. actionsView.fire( 'unlink' );
  661. expect( balloon.visibleView ).to.be.null;
  662. expect( focusEditableSpy.calledOnce ).to.be.true;
  663. } );
  664. it( 'should hide after Esc key press', () => {
  665. const keyEvtData = {
  666. keyCode: keyCodes.esc,
  667. preventDefault: sinon.spy(),
  668. stopPropagation: sinon.spy()
  669. };
  670. linkUIFeature._showUI();
  671. linkUIFeature._removeFormView();
  672. // Removing the form would call the focus spy.
  673. focusEditableSpy.resetHistory();
  674. actionsView.keystrokes.press( keyEvtData );
  675. expect( balloon.visibleView ).to.equal( null );
  676. expect( focusEditableSpy.calledOnce ).to.be.true;
  677. } );
  678. // #https://github.com/ckeditor/ckeditor5-link/issues/181
  679. it( 'should add the #formView upon Ctrl+K keystroke press', () => {
  680. const keyEvtData = {
  681. keyCode: keyCodes.k,
  682. ctrlKey: true,
  683. preventDefault: sinon.spy(),
  684. stopPropagation: sinon.spy()
  685. };
  686. linkUIFeature._showUI();
  687. linkUIFeature._removeFormView();
  688. expect( balloon.visibleView ).to.equal( actionsView );
  689. actionsView.keystrokes.press( keyEvtData );
  690. expect( balloon.visibleView ).to.equal( formView );
  691. } );
  692. } );
  693. } );
  694. describe( 'link form view', () => {
  695. let focusEditableSpy;
  696. const createEditorWithDefaultProtocol = defaultProtocol => {
  697. return ClassicTestEditor
  698. .create( editorElement, {
  699. plugins: [ LinkEditing, LinkUI, Paragraph, BlockQuote ],
  700. link: { defaultProtocol }
  701. } )
  702. .then( editor => {
  703. const linkUIFeature = editor.plugins.get( LinkUI );
  704. const formView = linkUIFeature.formView;
  705. formView.render();
  706. editor.model.schema.extend( '$text', {
  707. allowIn: '$root',
  708. allowAttributes: 'linkHref'
  709. } );
  710. return { editor, formView };
  711. } );
  712. };
  713. beforeEach( () => {
  714. focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  715. } );
  716. it( 'should mark the editor UI as focused when the #formView is focused', () => {
  717. linkUIFeature._showUI();
  718. expect( balloon.visibleView ).to.equal( formView );
  719. editor.ui.focusTracker.isFocused = false;
  720. formView.element.dispatchEvent( new Event( 'focus' ) );
  721. expect( editor.ui.focusTracker.isFocused ).to.be.true;
  722. } );
  723. describe( 'link protocol', () => {
  724. it( 'should use a default link protocol from the `config.link.defaultProtocol` when provided', () => {
  725. return ClassicTestEditor
  726. .create( editorElement, {
  727. link: {
  728. defaultProtocol: 'https://'
  729. }
  730. } )
  731. .then( editor => {
  732. const defaultProtocol = editor.config.get( 'link.defaultProtocol' );
  733. expect( defaultProtocol ).to.equal( 'https://' );
  734. editor.destroy();
  735. } );
  736. } );
  737. it( 'should not add a protocol without the configuration', () => {
  738. formView.urlInputView.fieldView.value = 'ckeditor.com';
  739. formView.fire( 'submit' );
  740. expect( formView.urlInputView.fieldView.value ).to.equal( 'ckeditor.com' );
  741. } );
  742. it( 'should not add a protocol to the local links even when `config.link.defaultProtocol` configured', () => {
  743. return createEditorWithDefaultProtocol( 'http://' ).then( ( { editor, formView } ) => {
  744. formView.urlInputView.fieldView.value = '#test';
  745. formView.fire( 'submit' );
  746. expect( formView.urlInputView.fieldView.value ).to.equal( '#test' );
  747. editor.destroy();
  748. } );
  749. } );
  750. it( 'should not add a protocol to the relative links even when `config.link.defaultProtocol` configured', () => {
  751. return createEditorWithDefaultProtocol( 'http://' ).then( ( { editor, formView } ) => {
  752. formView.urlInputView.fieldView.value = '/test.html';
  753. formView.fire( 'submit' );
  754. expect( formView.urlInputView.fieldView.value ).to.equal( '/test.html' );
  755. editor.destroy();
  756. } );
  757. } );
  758. it( 'should not add a protocol when given provided within the value even when `config.link.defaultProtocol` configured', () => {
  759. return createEditorWithDefaultProtocol( 'http://' ).then( ( { editor, formView } ) => {
  760. formView.urlInputView.fieldView.value = 'http://example.com';
  761. formView.fire( 'submit' );
  762. expect( formView.urlInputView.fieldView.value ).to.equal( 'http://example.com' );
  763. editor.destroy();
  764. } );
  765. } );
  766. it( 'should use the "http://" protocol when it\'s configured', () => {
  767. return createEditorWithDefaultProtocol( 'http://' ).then( ( { editor, formView } ) => {
  768. formView.urlInputView.fieldView.value = 'ckeditor.com';
  769. formView.fire( 'submit' );
  770. expect( formView.urlInputView.fieldView.value ).to.equal( 'http://ckeditor.com' );
  771. editor.destroy();
  772. } );
  773. } );
  774. it( 'should use the "http://" protocol when it\'s configured and form input value contains "www."', () => {
  775. return createEditorWithDefaultProtocol( 'http://' ).then( ( { editor, formView } ) => {
  776. formView.urlInputView.fieldView.value = 'www.ckeditor.com';
  777. formView.fire( 'submit' );
  778. expect( formView.urlInputView.fieldView.value ).to.equal( 'http://www.ckeditor.com' );
  779. editor.destroy();
  780. } );
  781. } );
  782. it( 'should propagate the protocol to the link\'s `linkHref` attribute in model', () => {
  783. return createEditorWithDefaultProtocol( 'http://' ).then( ( { editor, formView } ) => {
  784. setModelData( editor.model, '[ckeditor.com]' );
  785. formView.urlInputView.fieldView.value = 'ckeditor.com';
  786. formView.fire( 'submit' );
  787. expect( getModelData( editor.model ) ).to.equal(
  788. '[<$text linkHref="http://ckeditor.com">ckeditor.com</$text>]'
  789. );
  790. editor.destroy();
  791. } );
  792. } );
  793. it( 'should detect an email on submitting the form and add "mailto:" protocol automatically to the provided value', () => {
  794. return createEditorWithDefaultProtocol( 'http://' ).then( ( { editor, formView } ) => {
  795. setModelData( editor.model, '[email@example.com]' );
  796. formView.urlInputView.fieldView.value = 'email@example.com';
  797. formView.fire( 'submit' );
  798. expect( formView.urlInputView.fieldView.value ).to.equal( 'mailto:email@example.com' );
  799. expect( getModelData( editor.model ) ).to.equal(
  800. '[<$text linkHref="mailto:email@example.com">email@example.com</$text>]'
  801. );
  802. editor.destroy();
  803. } );
  804. } );
  805. it( 'should not add an email protocol when given provided within the value' +
  806. 'even when `config.link.defaultProtocol` configured', () => {
  807. return createEditorWithDefaultProtocol( 'mailto:' ).then( ( { editor, formView } ) => {
  808. formView.urlInputView.fieldView.value = 'mailto:test@example.com';
  809. formView.fire( 'submit' );
  810. expect( formView.urlInputView.fieldView.value ).to.equal( 'mailto:test@example.com' );
  811. editor.destroy();
  812. } );
  813. } );
  814. } );
  815. describe( 'binding', () => {
  816. beforeEach( () => {
  817. setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
  818. } );
  819. it( 'should bind formView.urlInputView#value to link command value', () => {
  820. const command = editor.commands.get( 'link' );
  821. expect( formView.urlInputView.fieldView.value ).to.be.undefined;
  822. command.value = 'http://cksource.com';
  823. expect( formView.urlInputView.fieldView.value ).to.equal( 'http://cksource.com' );
  824. } );
  825. it( 'should execute link command on formView#submit event', () => {
  826. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  827. formView.urlInputView.fieldView.value = 'http://ckeditor.com';
  828. expect( formView.urlInputView.fieldView.value ).to.equal( 'http://ckeditor.com' );
  829. formView.urlInputView.fieldView.value = 'http://cksource.com';
  830. formView.fire( 'submit' );
  831. expect( executeSpy.calledOnce ).to.be.true;
  832. expect( executeSpy.calledWithExactly( 'link', 'http://cksource.com', {} ) ).to.be.true;
  833. } );
  834. it( 'should should clear the fake visual selection on formView#submit event', () => {
  835. linkUIFeature._showUI();
  836. expect( editor.model.markers.has( 'link-ui' ) ).to.be.true;
  837. formView.urlInputView.fieldView.value = 'http://cksource.com';
  838. formView.fire( 'submit' );
  839. expect( editor.model.markers.has( 'link-ui' ) ).to.be.false;
  840. } );
  841. it( 'should hide and reveal the #actionsView on formView#submit event', () => {
  842. linkUIFeature._showUI();
  843. formView.fire( 'submit' );
  844. expect( balloon.visibleView ).to.equal( actionsView );
  845. expect( focusEditableSpy.calledOnce ).to.be.true;
  846. } );
  847. it( 'should hide and reveal the #actionsView on formView#cancel event if link command has a value', () => {
  848. linkUIFeature._showUI();
  849. const command = editor.commands.get( 'link' );
  850. command.value = 'http://foo.com';
  851. formView.fire( 'cancel' );
  852. expect( balloon.visibleView ).to.equal( actionsView );
  853. expect( focusEditableSpy.calledOnce ).to.be.true;
  854. } );
  855. it( 'should hide the balloon on formView#cancel if link command does not have a value', () => {
  856. linkUIFeature._showUI();
  857. formView.fire( 'cancel' );
  858. expect( balloon.visibleView ).to.be.null;
  859. } );
  860. it( 'should hide and reveal the #actionsView after Esc key press if link command has a value', () => {
  861. const keyEvtData = {
  862. keyCode: keyCodes.esc,
  863. preventDefault: sinon.spy(),
  864. stopPropagation: sinon.spy()
  865. };
  866. linkUIFeature._showUI();
  867. const command = editor.commands.get( 'link' );
  868. command.value = 'http://foo.com';
  869. formView.keystrokes.press( keyEvtData );
  870. expect( balloon.visibleView ).to.equal( actionsView );
  871. expect( focusEditableSpy.calledOnce ).to.be.true;
  872. } );
  873. it( 'should hide the balloon after Esc key press if link command does not have a value', () => {
  874. const keyEvtData = {
  875. keyCode: keyCodes.esc,
  876. preventDefault: sinon.spy(),
  877. stopPropagation: sinon.spy()
  878. };
  879. linkUIFeature._showUI();
  880. formView.keystrokes.press( keyEvtData );
  881. expect( balloon.visibleView ).to.be.null;
  882. } );
  883. // https://github.com/ckeditor/ckeditor5/issues/1501
  884. it( 'should blur url input element before hiding the view', () => {
  885. linkUIFeature._showUI();
  886. const focusSpy = testUtils.sinon.spy( formView.saveButtonView, 'focus' );
  887. const removeSpy = testUtils.sinon.spy( balloon, 'remove' );
  888. formView.fire( 'cancel' );
  889. expect( focusSpy.calledBefore( removeSpy ) ).to.equal( true );
  890. } );
  891. describe( 'support manual decorators', () => {
  892. let editorElement, editor, model, formView, linkUIFeature;
  893. beforeEach( () => {
  894. editorElement = document.createElement( 'div' );
  895. document.body.appendChild( editorElement );
  896. return ClassicTestEditor
  897. .create( editorElement, {
  898. plugins: [ LinkEditing, LinkUI, Paragraph ],
  899. link: {
  900. decorators: {
  901. isFoo: {
  902. mode: 'manual',
  903. label: 'Foo',
  904. attributes: {
  905. foo: 'bar'
  906. }
  907. }
  908. }
  909. }
  910. } )
  911. .then( newEditor => {
  912. editor = newEditor;
  913. model = editor.model;
  914. model.schema.extend( '$text', {
  915. allowIn: '$root',
  916. allowAttributes: 'linkHref'
  917. } );
  918. linkUIFeature = editor.plugins.get( LinkUI );
  919. const balloon = editor.plugins.get( ContextualBalloon );
  920. formView = linkUIFeature.formView;
  921. // There is no point to execute BalloonPanelView attachTo and pin methods so lets override it.
  922. testUtils.sinon.stub( balloon.view, 'attachTo' ).returns( {} );
  923. testUtils.sinon.stub( balloon.view, 'pin' ).returns( {} );
  924. formView.render();
  925. } );
  926. } );
  927. afterEach( () => {
  928. editorElement.remove();
  929. return editor.destroy();
  930. } );
  931. it( 'should gather information about manual decorators', () => {
  932. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  933. setModelData( model, 'f[<$text linkHref="url" linkIsFoo="true">ooba</$text>]r' );
  934. expect( formView.urlInputView.fieldView.element.value ).to.equal( 'url' );
  935. expect( formView.getDecoratorSwitchesState() ).to.deep.equal( { linkIsFoo: true } );
  936. formView.fire( 'submit' );
  937. expect( executeSpy.calledOnce ).to.be.true;
  938. expect( executeSpy.calledWithExactly( 'link', 'url', { linkIsFoo: true } ) ).to.be.true;
  939. } );
  940. it( 'should reset switch state when form view is closed', () => {
  941. setModelData( model, 'f[<$text linkHref="url" linkIsFoo="true">ooba</$text>]r' );
  942. const manualDecorators = editor.commands.get( 'link' ).manualDecorators;
  943. const firstDecoratorModel = manualDecorators.first;
  944. const firstDecoratorSwitch = formView._manualDecoratorSwitches.first;
  945. expect( firstDecoratorModel.value, 'Initial value should be read from the model (true)' ).to.be.true;
  946. expect( firstDecoratorSwitch.isOn, 'Initial value should be read from the model (true)' ).to.be.true;
  947. firstDecoratorSwitch.fire( 'execute' );
  948. expect( firstDecoratorModel.value, 'Pressing button toggles value' ).to.be.false;
  949. expect( firstDecoratorSwitch.isOn, 'Pressing button toggles value' ).to.be.false;
  950. linkUIFeature._closeFormView();
  951. expect( firstDecoratorModel.value, 'Close form view without submit resets value to initial state' ).to.be.true;
  952. expect( firstDecoratorSwitch.isOn, 'Close form view without submit resets value to initial state' ).to.be.true;
  953. } );
  954. } );
  955. } );
  956. } );
  957. } );