linkui.js 42 KB

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