8
0

linkui.js 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  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 not add an email protocol when given provided within the value' +
  988. 'even when `config.link.defaultProtocol` configured', () => {
  989. return createEditorWithDefaultProtocol( 'mailto:' ).then( ( { editor, formView } ) => {
  990. formView.urlInputView.fieldView.value = 'mailto:test@example.com';
  991. formView.fire( 'submit' );
  992. expect( formView.urlInputView.fieldView.value ).to.equal( 'mailto:test@example.com' );
  993. return editor.destroy();
  994. } );
  995. } );
  996. } );
  997. describe( 'binding', () => {
  998. beforeEach( () => {
  999. setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
  1000. } );
  1001. it( 'should bind formView.urlInputView#value to link command value', () => {
  1002. const command = editor.commands.get( 'link' );
  1003. expect( formView.urlInputView.fieldView.value ).to.be.undefined;
  1004. command.value = 'http://cksource.com';
  1005. expect( formView.urlInputView.fieldView.value ).to.equal( 'http://cksource.com' );
  1006. } );
  1007. it( 'should execute link command on formView#submit event', () => {
  1008. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  1009. formView.urlInputView.fieldView.value = 'http://ckeditor.com';
  1010. expect( formView.urlInputView.fieldView.value ).to.equal( 'http://ckeditor.com' );
  1011. formView.urlInputView.fieldView.value = 'http://cksource.com';
  1012. formView.fire( 'submit' );
  1013. expect( executeSpy.calledOnce ).to.be.true;
  1014. expect( executeSpy.calledWithExactly( 'link', 'http://cksource.com', {} ) ).to.be.true;
  1015. } );
  1016. it( 'should should clear the fake visual selection on formView#submit event', () => {
  1017. linkUIFeature._showUI();
  1018. expect( editor.model.markers.has( 'link-ui' ) ).to.be.true;
  1019. formView.urlInputView.fieldView.value = 'http://cksource.com';
  1020. formView.fire( 'submit' );
  1021. expect( editor.model.markers.has( 'link-ui' ) ).to.be.false;
  1022. } );
  1023. it( 'should hide and reveal the #actionsView on formView#submit event', () => {
  1024. linkUIFeature._showUI();
  1025. formView.fire( 'submit' );
  1026. expect( balloon.visibleView ).to.equal( actionsView );
  1027. expect( focusEditableSpy.calledOnce ).to.be.true;
  1028. } );
  1029. it( 'should hide and reveal the #actionsView on formView#cancel event if link command has a value', () => {
  1030. linkUIFeature._showUI();
  1031. const command = editor.commands.get( 'link' );
  1032. command.value = 'http://foo.com';
  1033. formView.fire( 'cancel' );
  1034. expect( balloon.visibleView ).to.equal( actionsView );
  1035. expect( focusEditableSpy.calledOnce ).to.be.true;
  1036. } );
  1037. it( 'should hide the balloon on formView#cancel if link command does not have a value', () => {
  1038. linkUIFeature._showUI();
  1039. formView.fire( 'cancel' );
  1040. expect( balloon.visibleView ).to.be.null;
  1041. } );
  1042. it( 'should hide and reveal the #actionsView after Esc key press if link command has a value', () => {
  1043. const keyEvtData = {
  1044. keyCode: keyCodes.esc,
  1045. preventDefault: sinon.spy(),
  1046. stopPropagation: sinon.spy()
  1047. };
  1048. linkUIFeature._showUI();
  1049. const command = editor.commands.get( 'link' );
  1050. command.value = 'http://foo.com';
  1051. formView.keystrokes.press( keyEvtData );
  1052. expect( balloon.visibleView ).to.equal( actionsView );
  1053. expect( focusEditableSpy.calledOnce ).to.be.true;
  1054. } );
  1055. it( 'should hide the balloon after Esc key press if link command does not have a value', () => {
  1056. const keyEvtData = {
  1057. keyCode: keyCodes.esc,
  1058. preventDefault: sinon.spy(),
  1059. stopPropagation: sinon.spy()
  1060. };
  1061. linkUIFeature._showUI();
  1062. formView.keystrokes.press( keyEvtData );
  1063. expect( balloon.visibleView ).to.be.null;
  1064. } );
  1065. // https://github.com/ckeditor/ckeditor5/issues/1501
  1066. it( 'should blur url input element before hiding the view', () => {
  1067. linkUIFeature._showUI();
  1068. const focusSpy = testUtils.sinon.spy( formView.saveButtonView, 'focus' );
  1069. const removeSpy = testUtils.sinon.spy( balloon, 'remove' );
  1070. formView.fire( 'cancel' );
  1071. expect( focusSpy.calledBefore( removeSpy ) ).to.equal( true );
  1072. } );
  1073. describe( 'support manual decorators', () => {
  1074. let editorElement, editor, model, formView, linkUIFeature;
  1075. beforeEach( () => {
  1076. editorElement = document.createElement( 'div' );
  1077. document.body.appendChild( editorElement );
  1078. return ClassicTestEditor
  1079. .create( editorElement, {
  1080. plugins: [ LinkEditing, LinkUI, Paragraph ],
  1081. link: {
  1082. decorators: {
  1083. isFoo: {
  1084. mode: 'manual',
  1085. label: 'Foo',
  1086. attributes: {
  1087. foo: 'bar'
  1088. }
  1089. }
  1090. }
  1091. }
  1092. } )
  1093. .then( newEditor => {
  1094. editor = newEditor;
  1095. model = editor.model;
  1096. model.schema.extend( '$text', {
  1097. allowIn: '$root',
  1098. allowAttributes: 'linkHref'
  1099. } );
  1100. linkUIFeature = editor.plugins.get( LinkUI );
  1101. const balloon = editor.plugins.get( ContextualBalloon );
  1102. formView = linkUIFeature.formView;
  1103. // There is no point to execute BalloonPanelView attachTo and pin methods so lets override it.
  1104. testUtils.sinon.stub( balloon.view, 'attachTo' ).returns( {} );
  1105. testUtils.sinon.stub( balloon.view, 'pin' ).returns( {} );
  1106. formView.render();
  1107. } );
  1108. } );
  1109. afterEach( () => {
  1110. editorElement.remove();
  1111. return editor.destroy();
  1112. } );
  1113. it( 'should gather information about manual decorators', () => {
  1114. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  1115. setModelData( model, 'f[<$text linkHref="url" linkIsFoo="true">ooba</$text>]r' );
  1116. expect( formView.urlInputView.fieldView.element.value ).to.equal( 'url' );
  1117. expect( formView.getDecoratorSwitchesState() ).to.deep.equal( { linkIsFoo: true } );
  1118. formView.fire( 'submit' );
  1119. expect( executeSpy.calledOnce ).to.be.true;
  1120. expect( executeSpy.calledWithExactly( 'link', 'url', { linkIsFoo: true } ) ).to.be.true;
  1121. } );
  1122. it( 'should reset switch state when form view is closed', () => {
  1123. setModelData( model, 'f[<$text linkHref="url" linkIsFoo="true">ooba</$text>]r' );
  1124. const manualDecorators = editor.commands.get( 'link' ).manualDecorators;
  1125. const firstDecoratorModel = manualDecorators.first;
  1126. const firstDecoratorSwitch = formView._manualDecoratorSwitches.first;
  1127. expect( firstDecoratorModel.value, 'Initial value should be read from the model (true)' ).to.be.true;
  1128. expect( firstDecoratorSwitch.isOn, 'Initial value should be read from the model (true)' ).to.be.true;
  1129. firstDecoratorSwitch.fire( 'execute' );
  1130. expect( firstDecoratorModel.value, 'Pressing button toggles value' ).to.be.false;
  1131. expect( firstDecoratorSwitch.isOn, 'Pressing button toggles value' ).to.be.false;
  1132. linkUIFeature._closeFormView();
  1133. expect( firstDecoratorModel.value, 'Close form view without submit resets value to initial state' ).to.be.true;
  1134. expect( firstDecoratorSwitch.isOn, 'Close form view without submit resets value to initial state' ).to.be.true;
  1135. } );
  1136. } );
  1137. } );
  1138. } );
  1139. } );