linkui.js 50 KB

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