linkui.js 43 KB

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