linkui.js 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  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 not show the UI on Ctrl+K keystroke on content with LinkCommand disabled', () => {
  593. const spy = testUtils.sinon.stub( linkUIFeature, '_showUI' ).returns( {} );
  594. const command = editor.commands.get( 'link' );
  595. command.isEnabled = false;
  596. editor.keystrokes.press( {
  597. keyCode: keyCodes.k,
  598. ctrlKey: true,
  599. preventDefault: sinon.spy(),
  600. stopPropagation: sinon.spy()
  601. } );
  602. sinon.assert.notCalled( spy );
  603. } );
  604. it( 'should prevent default action on Ctrl+K keystroke', () => {
  605. const preventDefaultSpy = sinon.spy();
  606. const stopPropagationSpy = sinon.spy();
  607. editor.keystrokes.press( {
  608. keyCode: keyCodes.k,
  609. ctrlKey: true,
  610. preventDefault: preventDefaultSpy,
  611. stopPropagation: stopPropagationSpy
  612. } );
  613. sinon.assert.calledOnce( preventDefaultSpy );
  614. sinon.assert.calledOnce( stopPropagationSpy );
  615. } );
  616. it( 'should make stack with link visible on Ctrl+K keystroke - no link', () => {
  617. const command = editor.commands.get( 'link' );
  618. command.isEnabled = true;
  619. balloon.add( {
  620. view: new View(),
  621. stackId: 'custom'
  622. } );
  623. editor.keystrokes.press( {
  624. keyCode: keyCodes.k,
  625. ctrlKey: true,
  626. preventDefault: sinon.spy(),
  627. stopPropagation: sinon.spy()
  628. } );
  629. expect( balloon.visibleView ).to.equal( formView );
  630. } );
  631. it( 'should make stack with link visible on Ctrl+K keystroke - link', () => {
  632. setModelData( editor.model, '<paragraph><$text linkHref="foo.html">f[]oo</$text></paragraph>' );
  633. const customView = new View();
  634. balloon.add( {
  635. view: customView,
  636. stackId: 'custom'
  637. } );
  638. expect( balloon.visibleView ).to.equal( customView );
  639. editor.keystrokes.press( {
  640. keyCode: keyCodes.k,
  641. ctrlKey: true,
  642. preventDefault: sinon.spy(),
  643. stopPropagation: sinon.spy()
  644. } );
  645. expect( balloon.visibleView ).to.equal( actionsView );
  646. editor.keystrokes.press( {
  647. keyCode: keyCodes.k,
  648. ctrlKey: true,
  649. preventDefault: sinon.spy(),
  650. stopPropagation: sinon.spy()
  651. } );
  652. expect( balloon.visibleView ).to.equal( formView );
  653. } );
  654. it( 'should focus the the #actionsView on `Tab` key press when #actionsView is visible', () => {
  655. const keyEvtData = {
  656. keyCode: keyCodes.tab,
  657. preventDefault: sinon.spy(),
  658. stopPropagation: sinon.spy()
  659. };
  660. const normalPriorityTabCallbackSpy = sinon.spy();
  661. const highestPriorityTabCallbackSpy = sinon.spy();
  662. editor.keystrokes.set( 'Tab', normalPriorityTabCallbackSpy );
  663. editor.keystrokes.set( 'Tab', highestPriorityTabCallbackSpy, { priority: 'highest' } );
  664. // Balloon is invisible, form not focused.
  665. actionsView.focusTracker.isFocused = false;
  666. const spy = sinon.spy( actionsView, 'focus' );
  667. editor.keystrokes.press( keyEvtData );
  668. sinon.assert.notCalled( keyEvtData.preventDefault );
  669. sinon.assert.notCalled( keyEvtData.stopPropagation );
  670. sinon.assert.notCalled( spy );
  671. sinon.assert.calledOnce( normalPriorityTabCallbackSpy );
  672. sinon.assert.calledOnce( highestPriorityTabCallbackSpy );
  673. // Balloon is visible, form focused.
  674. linkUIFeature._showUI();
  675. testUtils.sinon.stub( linkUIFeature, '_areActionsVisible' ).value( true );
  676. actionsView.focusTracker.isFocused = true;
  677. editor.keystrokes.press( keyEvtData );
  678. sinon.assert.notCalled( keyEvtData.preventDefault );
  679. sinon.assert.notCalled( keyEvtData.stopPropagation );
  680. sinon.assert.notCalled( spy );
  681. sinon.assert.calledTwice( normalPriorityTabCallbackSpy );
  682. sinon.assert.calledTwice( highestPriorityTabCallbackSpy );
  683. // Balloon is still visible, form not focused.
  684. actionsView.focusTracker.isFocused = false;
  685. editor.keystrokes.press( keyEvtData );
  686. sinon.assert.calledOnce( keyEvtData.preventDefault );
  687. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  688. sinon.assert.calledOnce( spy );
  689. sinon.assert.calledTwice( normalPriorityTabCallbackSpy );
  690. sinon.assert.calledThrice( highestPriorityTabCallbackSpy );
  691. } );
  692. it( 'should hide the UI after Esc key press (from editor) and not focus the editable', () => {
  693. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  694. const keyEvtData = {
  695. keyCode: keyCodes.esc,
  696. preventDefault: sinon.spy(),
  697. stopPropagation: sinon.spy()
  698. };
  699. // Balloon is visible.
  700. linkUIFeature._showUI();
  701. editor.keystrokes.press( keyEvtData );
  702. sinon.assert.calledWithExactly( spy );
  703. } );
  704. it( 'should not hide the UI after Esc key press (from editor) when UI is open but is not visible', () => {
  705. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  706. const keyEvtData = {
  707. keyCode: keyCodes.esc,
  708. preventDefault: () => {},
  709. stopPropagation: () => {}
  710. };
  711. const viewMock = {
  712. ready: true,
  713. render: () => {},
  714. destroy: () => {}
  715. };
  716. linkUIFeature._showUI();
  717. // Some view precedes the link UI in the balloon.
  718. balloon.add( { view: viewMock } );
  719. editor.keystrokes.press( keyEvtData );
  720. sinon.assert.notCalled( spy );
  721. } );
  722. } );
  723. describe( 'mouse support', () => {
  724. it( 'should hide the UI and not focus editable upon clicking outside the UI', () => {
  725. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  726. linkUIFeature._showUI();
  727. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  728. sinon.assert.calledWithExactly( spy );
  729. } );
  730. it( 'should hide the UI when link is in not currently visible stack', () => {
  731. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  732. balloon.add( {
  733. view: new View(),
  734. stackId: 'secondary'
  735. } );
  736. linkUIFeature._showUI();
  737. // Be sure any of link view is not currently visible/
  738. expect( balloon.visibleView ).to.not.equal( actionsView );
  739. expect( balloon.visibleView ).to.not.equal( formView );
  740. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  741. sinon.assert.calledWithExactly( spy );
  742. } );
  743. it( 'should not hide the UI upon clicking inside the the UI', () => {
  744. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  745. linkUIFeature._showUI();
  746. balloon.view.element.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  747. sinon.assert.notCalled( spy );
  748. } );
  749. describe( 'clicking on editable', () => {
  750. let observer, spy;
  751. beforeEach( () => {
  752. observer = editor.editing.view.getObserver( ClickObserver );
  753. editor.model.schema.extend( '$text', { allowIn: '$root' } );
  754. spy = testUtils.sinon.stub( linkUIFeature, '_showUI' ).returns( {} );
  755. } );
  756. it( 'should show the UI when collapsed selection is inside link element', () => {
  757. setModelData( editor.model, '<$text linkHref="url">fo[]o</$text>' );
  758. observer.fire( 'click', { target: document.body } );
  759. sinon.assert.calledWithExactly( spy );
  760. } );
  761. it( 'should show the UI when selection exclusively encloses a link element (#1)', () => {
  762. setModelData( editor.model, '[<$text linkHref="url">foo</$text>]' );
  763. observer.fire( 'click', { target: {} } );
  764. sinon.assert.calledWithExactly( spy );
  765. } );
  766. it( 'should show the UI when selection exclusively encloses a link element (#2)', () => {
  767. setModelData( editor.model, '<$text linkHref="url">[foo]</$text>' );
  768. observer.fire( 'click', { target: {} } );
  769. sinon.assert.calledWithExactly( spy );
  770. } );
  771. it( 'should do nothing when selection is not inside link element', () => {
  772. setModelData( editor.model, '[]' );
  773. observer.fire( 'click', { target: {} } );
  774. sinon.assert.notCalled( spy );
  775. } );
  776. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#1)', () => {
  777. setModelData( editor.model, '<$text linkHref="url">f[o]o</$text>' );
  778. observer.fire( 'click', { target: {} } );
  779. sinon.assert.notCalled( spy );
  780. } );
  781. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#2)', () => {
  782. setModelData( editor.model, '<$text linkHref="url">[fo]o</$text>' );
  783. observer.fire( 'click', { target: {} } );
  784. sinon.assert.notCalled( spy );
  785. } );
  786. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#3)', () => {
  787. setModelData( editor.model, '<$text linkHref="url">f[oo]</$text>' );
  788. observer.fire( 'click', { target: {} } );
  789. sinon.assert.notCalled( spy );
  790. } );
  791. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#4)', () => {
  792. setModelData( editor.model, 'ba[r<$text linkHref="url">foo]</$text>' );
  793. observer.fire( 'click', { target: {} } );
  794. sinon.assert.notCalled( spy );
  795. } );
  796. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#5)', () => {
  797. setModelData( editor.model, 'ba[r<$text linkHref="url">foo</$text>]' );
  798. observer.fire( 'click', { target: {} } );
  799. sinon.assert.notCalled( spy );
  800. } );
  801. } );
  802. } );
  803. describe( 'actions view', () => {
  804. let focusEditableSpy;
  805. beforeEach( () => {
  806. focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  807. } );
  808. it( 'should mark the editor UI as focused when the #actionsView is focused', () => {
  809. linkUIFeature._showUI();
  810. linkUIFeature._removeFormView();
  811. expect( balloon.visibleView ).to.equal( actionsView );
  812. editor.ui.focusTracker.isFocused = false;
  813. actionsView.element.dispatchEvent( new Event( 'focus' ) );
  814. expect( editor.ui.focusTracker.isFocused ).to.be.true;
  815. } );
  816. describe( 'binding', () => {
  817. it( 'should show the #formView on #edit event and select the URL input field', () => {
  818. linkUIFeature._showUI();
  819. linkUIFeature._removeFormView();
  820. const selectSpy = testUtils.sinon.spy( formView.urlInputView.fieldView, 'select' );
  821. actionsView.fire( 'edit' );
  822. expect( balloon.visibleView ).to.equal( formView );
  823. sinon.assert.calledOnce( selectSpy );
  824. } );
  825. it( 'should execute unlink command on actionsView#unlink event', () => {
  826. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  827. actionsView.fire( 'unlink' );
  828. expect( executeSpy.calledOnce ).to.be.true;
  829. expect( executeSpy.calledWithExactly( 'unlink' ) ).to.be.true;
  830. } );
  831. it( 'should hide and focus editable on actionsView#unlink event', () => {
  832. linkUIFeature._showUI();
  833. linkUIFeature._removeFormView();
  834. // Removing the form would call the focus spy.
  835. focusEditableSpy.resetHistory();
  836. actionsView.fire( 'unlink' );
  837. expect( balloon.visibleView ).to.be.null;
  838. expect( focusEditableSpy.calledOnce ).to.be.true;
  839. } );
  840. it( 'should hide after Esc key press', () => {
  841. const keyEvtData = {
  842. keyCode: keyCodes.esc,
  843. preventDefault: sinon.spy(),
  844. stopPropagation: sinon.spy()
  845. };
  846. linkUIFeature._showUI();
  847. linkUIFeature._removeFormView();
  848. // Removing the form would call the focus spy.
  849. focusEditableSpy.resetHistory();
  850. actionsView.keystrokes.press( keyEvtData );
  851. expect( balloon.visibleView ).to.equal( null );
  852. expect( focusEditableSpy.calledOnce ).to.be.true;
  853. } );
  854. // #https://github.com/ckeditor/ckeditor5-link/issues/181
  855. it( 'should add the #formView upon Ctrl+K keystroke press', () => {
  856. const keyEvtData = {
  857. keyCode: keyCodes.k,
  858. ctrlKey: true,
  859. preventDefault: sinon.spy(),
  860. stopPropagation: sinon.spy()
  861. };
  862. linkUIFeature._showUI();
  863. linkUIFeature._removeFormView();
  864. expect( balloon.visibleView ).to.equal( actionsView );
  865. actionsView.keystrokes.press( keyEvtData );
  866. expect( balloon.visibleView ).to.equal( formView );
  867. } );
  868. } );
  869. } );
  870. describe( 'link form view', () => {
  871. let focusEditableSpy;
  872. const createEditorWithDefaultProtocol = defaultProtocol => {
  873. return ClassicTestEditor
  874. .create( editorElement, {
  875. plugins: [ LinkEditing, LinkUI, Paragraph, BlockQuote ],
  876. link: { defaultProtocol }
  877. } )
  878. .then( editor => {
  879. const linkUIFeature = editor.plugins.get( LinkUI );
  880. const formView = linkUIFeature.formView;
  881. formView.render();
  882. editor.model.schema.extend( '$text', {
  883. allowIn: '$root',
  884. allowAttributes: 'linkHref'
  885. } );
  886. return { editor, formView };
  887. } );
  888. };
  889. beforeEach( () => {
  890. focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  891. } );
  892. it( 'should mark the editor UI as focused when the #formView is focused', () => {
  893. linkUIFeature._showUI();
  894. expect( balloon.visibleView ).to.equal( formView );
  895. editor.ui.focusTracker.isFocused = false;
  896. formView.element.dispatchEvent( new Event( 'focus' ) );
  897. expect( editor.ui.focusTracker.isFocused ).to.be.true;
  898. } );
  899. describe( 'link protocol', () => {
  900. it( 'should use a default link protocol from the `config.link.defaultProtocol` when provided', () => {
  901. return ClassicTestEditor
  902. .create( editorElement, {
  903. link: {
  904. defaultProtocol: 'https://'
  905. }
  906. } )
  907. .then( editor => {
  908. const defaultProtocol = editor.config.get( 'link.defaultProtocol' );
  909. expect( defaultProtocol ).to.equal( 'https://' );
  910. return editor.destroy();
  911. } );
  912. } );
  913. it( 'should not add a protocol without the configuration', () => {
  914. formView.urlInputView.fieldView.value = 'ckeditor.com';
  915. formView.fire( 'submit' );
  916. expect( formView.urlInputView.fieldView.value ).to.equal( 'ckeditor.com' );
  917. } );
  918. it( 'should not add a protocol to the local links even when `config.link.defaultProtocol` configured', () => {
  919. return createEditorWithDefaultProtocol( 'http://' ).then( ( { editor, formView } ) => {
  920. const linkCommandSpy = sinon.spy( editor.commands.get( 'link' ), 'execute' );
  921. formView.urlInputView.fieldView.value = '#test';
  922. formView.fire( 'submit' );
  923. sinon.assert.calledWith( linkCommandSpy, '#test', sinon.match.any );
  924. return editor.destroy();
  925. } );
  926. } );
  927. it( 'should not add a protocol to the relative links even when `config.link.defaultProtocol` configured', () => {
  928. return createEditorWithDefaultProtocol( 'http://' ).then( ( { editor, formView } ) => {
  929. const linkCommandSpy = sinon.spy( editor.commands.get( 'link' ), 'execute' );
  930. formView.urlInputView.fieldView.value = '/test.html';
  931. formView.fire( 'submit' );
  932. sinon.assert.calledWith( linkCommandSpy, '/test.html', sinon.match.any );
  933. return editor.destroy();
  934. } );
  935. } );
  936. it( 'should not add a protocol when given provided within the value even when `config.link.defaultProtocol` configured', () => {
  937. return createEditorWithDefaultProtocol( 'http://' ).then( ( { editor, formView } ) => {
  938. const linkCommandSpy = sinon.spy( editor.commands.get( 'link' ), 'execute' );
  939. formView.urlInputView.fieldView.value = 'http://example.com';
  940. formView.fire( 'submit' );
  941. expect( formView.urlInputView.fieldView.value ).to.equal( 'http://example.com' );
  942. sinon.assert.calledWith( linkCommandSpy, 'http://example.com', sinon.match.any );
  943. return editor.destroy();
  944. } );
  945. } );
  946. it( 'should use the "http://" protocol when it\'s configured', () => {
  947. return createEditorWithDefaultProtocol( 'http://' ).then( ( { editor, formView } ) => {
  948. const linkCommandSpy = sinon.spy( editor.commands.get( 'link' ), 'execute' );
  949. formView.urlInputView.fieldView.value = 'ckeditor.com';
  950. formView.fire( 'submit' );
  951. sinon.assert.calledWith( linkCommandSpy, 'http://ckeditor.com', sinon.match.any );
  952. return editor.destroy();
  953. } );
  954. } );
  955. it( 'should use the "http://" protocol when it\'s configured and form input value contains "www."', () => {
  956. return createEditorWithDefaultProtocol( 'http://' ).then( ( { editor, formView } ) => {
  957. const linkCommandSpy = sinon.spy( editor.commands.get( 'link' ), 'execute' );
  958. formView.urlInputView.fieldView.value = 'www.ckeditor.com';
  959. formView.fire( 'submit' );
  960. sinon.assert.calledWith( linkCommandSpy, 'http://www.ckeditor.com', sinon.match.any );
  961. return editor.destroy();
  962. } );
  963. } );
  964. it( 'should propagate the protocol to the link\'s `linkHref` attribute in model', () => {
  965. return createEditorWithDefaultProtocol( 'http://' ).then( ( { editor, formView } ) => {
  966. setModelData( editor.model, '[ckeditor.com]' );
  967. formView.urlInputView.fieldView.value = 'ckeditor.com';
  968. formView.fire( 'submit' );
  969. expect( getModelData( editor.model ) ).to.equal(
  970. '[<$text linkHref="http://ckeditor.com">ckeditor.com</$text>]'
  971. );
  972. return editor.destroy();
  973. } );
  974. } );
  975. it( 'should detect an email on submitting the form and add "mailto:" protocol automatically to the provided value', () => {
  976. return createEditorWithDefaultProtocol( 'http://' ).then( ( { editor, formView } ) => {
  977. setModelData( editor.model, '[email@example.com]' );
  978. formView.urlInputView.fieldView.value = 'email@example.com';
  979. formView.fire( 'submit' );
  980. expect( formView.urlInputView.fieldView.value ).to.equal( 'mailto:email@example.com' );
  981. expect( getModelData( editor.model ) ).to.equal(
  982. '[<$text linkHref="mailto:email@example.com">email@example.com</$text>]'
  983. );
  984. return editor.destroy();
  985. } );
  986. } );
  987. it( 'should detect an email on submitting the form and add "mailto:" protocol automatically to the provided value ' +
  988. 'even when defaultProtocol is undefined', () => {
  989. setModelData( editor.model, '<paragraph>[email@example.com]</paragraph>' );
  990. formView.urlInputView.fieldView.value = 'email@example.com';
  991. formView.fire( 'submit' );
  992. expect( formView.urlInputView.fieldView.value ).to.equal( 'mailto:email@example.com' );
  993. expect( getModelData( editor.model ) ).to.equal(
  994. '<paragraph>[<$text linkHref="mailto:email@example.com">email@example.com</$text>]</paragraph>'
  995. );
  996. } );
  997. it( 'should not add an email protocol when given provided within the value' +
  998. 'even when `config.link.defaultProtocol` configured', () => {
  999. return createEditorWithDefaultProtocol( 'mailto:' ).then( ( { editor, formView } ) => {
  1000. formView.urlInputView.fieldView.value = 'mailto:test@example.com';
  1001. formView.fire( 'submit' );
  1002. expect( formView.urlInputView.fieldView.value ).to.equal( 'mailto:test@example.com' );
  1003. return editor.destroy();
  1004. } );
  1005. } );
  1006. } );
  1007. describe( 'binding', () => {
  1008. beforeEach( () => {
  1009. setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
  1010. } );
  1011. it( 'should bind formView.urlInputView#value to link command value', () => {
  1012. const command = editor.commands.get( 'link' );
  1013. expect( formView.urlInputView.fieldView.value ).to.be.undefined;
  1014. command.value = 'http://cksource.com';
  1015. expect( formView.urlInputView.fieldView.value ).to.equal( 'http://cksource.com' );
  1016. } );
  1017. it( 'should execute link command on formView#submit event', () => {
  1018. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  1019. formView.urlInputView.fieldView.value = 'http://ckeditor.com';
  1020. expect( formView.urlInputView.fieldView.value ).to.equal( 'http://ckeditor.com' );
  1021. formView.urlInputView.fieldView.value = 'http://cksource.com';
  1022. formView.fire( 'submit' );
  1023. expect( executeSpy.calledOnce ).to.be.true;
  1024. expect( executeSpy.calledWithExactly( 'link', 'http://cksource.com', {} ) ).to.be.true;
  1025. } );
  1026. it( 'should should clear the fake visual selection on formView#submit event', () => {
  1027. linkUIFeature._showUI();
  1028. expect( editor.model.markers.has( 'link-ui' ) ).to.be.true;
  1029. formView.urlInputView.fieldView.value = 'http://cksource.com';
  1030. formView.fire( 'submit' );
  1031. expect( editor.model.markers.has( 'link-ui' ) ).to.be.false;
  1032. } );
  1033. it( 'should hide and reveal the #actionsView on formView#submit event', () => {
  1034. linkUIFeature._showUI();
  1035. formView.fire( 'submit' );
  1036. expect( balloon.visibleView ).to.equal( actionsView );
  1037. expect( focusEditableSpy.calledOnce ).to.be.true;
  1038. } );
  1039. it( 'should hide and reveal the #actionsView on formView#cancel event if link command has a value', () => {
  1040. linkUIFeature._showUI();
  1041. const command = editor.commands.get( 'link' );
  1042. command.value = 'http://foo.com';
  1043. formView.fire( 'cancel' );
  1044. expect( balloon.visibleView ).to.equal( actionsView );
  1045. expect( focusEditableSpy.calledOnce ).to.be.true;
  1046. } );
  1047. it( 'should hide the balloon on formView#cancel if link command does not have a value', () => {
  1048. linkUIFeature._showUI();
  1049. formView.fire( 'cancel' );
  1050. expect( balloon.visibleView ).to.be.null;
  1051. } );
  1052. it( 'should hide and reveal the #actionsView after Esc key press if link command has a value', () => {
  1053. const keyEvtData = {
  1054. keyCode: keyCodes.esc,
  1055. preventDefault: sinon.spy(),
  1056. stopPropagation: sinon.spy()
  1057. };
  1058. linkUIFeature._showUI();
  1059. const command = editor.commands.get( 'link' );
  1060. command.value = 'http://foo.com';
  1061. formView.keystrokes.press( keyEvtData );
  1062. expect( balloon.visibleView ).to.equal( actionsView );
  1063. expect( focusEditableSpy.calledOnce ).to.be.true;
  1064. } );
  1065. it( 'should hide the balloon after Esc key press if link command does not have a value', () => {
  1066. const keyEvtData = {
  1067. keyCode: keyCodes.esc,
  1068. preventDefault: sinon.spy(),
  1069. stopPropagation: sinon.spy()
  1070. };
  1071. linkUIFeature._showUI();
  1072. formView.keystrokes.press( keyEvtData );
  1073. expect( balloon.visibleView ).to.be.null;
  1074. } );
  1075. // https://github.com/ckeditor/ckeditor5/issues/1501
  1076. it( 'should blur url input element before hiding the view', () => {
  1077. linkUIFeature._showUI();
  1078. const focusSpy = testUtils.sinon.spy( formView.saveButtonView, 'focus' );
  1079. const removeSpy = testUtils.sinon.spy( balloon, 'remove' );
  1080. formView.fire( 'cancel' );
  1081. expect( focusSpy.calledBefore( removeSpy ) ).to.equal( true );
  1082. } );
  1083. describe( 'support manual decorators', () => {
  1084. let editorElement, editor, model, formView, linkUIFeature;
  1085. beforeEach( () => {
  1086. editorElement = document.createElement( 'div' );
  1087. document.body.appendChild( editorElement );
  1088. return ClassicTestEditor
  1089. .create( editorElement, {
  1090. plugins: [ LinkEditing, LinkUI, Paragraph ],
  1091. link: {
  1092. decorators: {
  1093. isFoo: {
  1094. mode: 'manual',
  1095. label: 'Foo',
  1096. attributes: {
  1097. foo: 'bar'
  1098. }
  1099. }
  1100. }
  1101. }
  1102. } )
  1103. .then( newEditor => {
  1104. editor = newEditor;
  1105. model = editor.model;
  1106. model.schema.extend( '$text', {
  1107. allowIn: '$root',
  1108. allowAttributes: 'linkHref'
  1109. } );
  1110. linkUIFeature = editor.plugins.get( LinkUI );
  1111. const balloon = editor.plugins.get( ContextualBalloon );
  1112. formView = linkUIFeature.formView;
  1113. // There is no point to execute BalloonPanelView attachTo and pin methods so lets override it.
  1114. testUtils.sinon.stub( balloon.view, 'attachTo' ).returns( {} );
  1115. testUtils.sinon.stub( balloon.view, 'pin' ).returns( {} );
  1116. formView.render();
  1117. } );
  1118. } );
  1119. afterEach( () => {
  1120. editorElement.remove();
  1121. return editor.destroy();
  1122. } );
  1123. it( 'should gather information about manual decorators', () => {
  1124. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  1125. setModelData( model, 'f[<$text linkHref="url" linkIsFoo="true">ooba</$text>]r' );
  1126. expect( formView.urlInputView.fieldView.element.value ).to.equal( 'url' );
  1127. expect( formView.getDecoratorSwitchesState() ).to.deep.equal( { linkIsFoo: true } );
  1128. formView.fire( 'submit' );
  1129. expect( executeSpy.calledOnce ).to.be.true;
  1130. expect( executeSpy.calledWithExactly( 'link', 'url', { linkIsFoo: true } ) ).to.be.true;
  1131. } );
  1132. it( 'should reset switch state when form view is closed', () => {
  1133. setModelData( model, 'f[<$text linkHref="url" linkIsFoo="true">ooba</$text>]r' );
  1134. const manualDecorators = editor.commands.get( 'link' ).manualDecorators;
  1135. const firstDecoratorModel = manualDecorators.first;
  1136. const firstDecoratorSwitch = formView._manualDecoratorSwitches.first;
  1137. expect( firstDecoratorModel.value, 'Initial value should be read from the model (true)' ).to.be.true;
  1138. expect( firstDecoratorSwitch.isOn, 'Initial value should be read from the model (true)' ).to.be.true;
  1139. firstDecoratorSwitch.fire( 'execute' );
  1140. expect( firstDecoratorModel.value, 'Pressing button toggles value' ).to.be.false;
  1141. expect( firstDecoratorSwitch.isOn, 'Pressing button toggles value' ).to.be.false;
  1142. linkUIFeature._closeFormView();
  1143. expect( firstDecoratorModel.value, 'Close form view without submit resets value to initial state' ).to.be.true;
  1144. expect( firstDecoratorSwitch.isOn, 'Close form view without submit resets value to initial state' ).to.be.true;
  1145. } );
  1146. } );
  1147. } );
  1148. } );
  1149. } );