linkui.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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 { 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 LinkEditing from '../src/linkediting';
  13. import LinkUI from '../src/linkui';
  14. import LinkFormView from '../src/ui/linkformview';
  15. import LinkActionsView from '../src/ui/linkactionsview';
  16. import ContextualBalloon from '@ckeditor/ckeditor5-ui/src/panel/balloon/contextualballoon';
  17. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  18. import ClickObserver from '@ckeditor/ckeditor5-engine/src/view/observer/clickobserver';
  19. describe( 'LinkUI', () => {
  20. let editor, linkUIFeature, linkButton, balloon, formView, actionsView, editorElement;
  21. testUtils.createSinonSandbox();
  22. beforeEach( () => {
  23. editorElement = document.createElement( 'div' );
  24. document.body.appendChild( editorElement );
  25. return ClassicTestEditor
  26. .create( editorElement, {
  27. plugins: [ LinkEditing, LinkUI, Paragraph ]
  28. } )
  29. .then( newEditor => {
  30. editor = newEditor;
  31. linkUIFeature = editor.plugins.get( LinkUI );
  32. linkButton = editor.ui.componentFactory.create( 'link' );
  33. balloon = editor.plugins.get( ContextualBalloon );
  34. formView = linkUIFeature.formView;
  35. actionsView = linkUIFeature.actionsView;
  36. // There is no point to execute BalloonPanelView attachTo and pin methods so lets override it.
  37. testUtils.sinon.stub( balloon.view, 'attachTo' ).returns( {} );
  38. testUtils.sinon.stub( balloon.view, 'pin' ).returns( {} );
  39. formView.render();
  40. } );
  41. } );
  42. afterEach( () => {
  43. editorElement.remove();
  44. return editor.destroy();
  45. } );
  46. it( 'should load ContextualBalloon', () => {
  47. expect( editor.plugins.get( ContextualBalloon ) ).to.be.instanceOf( ContextualBalloon );
  48. } );
  49. describe( 'init', () => {
  50. it( 'should register click observer', () => {
  51. expect( editor.editing.view.getObserver( ClickObserver ) ).to.be.instanceOf( ClickObserver );
  52. } );
  53. it( 'should create #actionsView', () => {
  54. expect( actionsView ).to.be.instanceOf( LinkActionsView );
  55. } );
  56. it( 'should create #formView', () => {
  57. expect( formView ).to.be.instanceOf( LinkFormView );
  58. } );
  59. describe( 'link toolbar button', () => {
  60. it( 'should be registered', () => {
  61. expect( linkButton ).to.be.instanceOf( ButtonView );
  62. } );
  63. it( 'should be bound to the link command', () => {
  64. const command = editor.commands.get( 'link' );
  65. command.isEnabled = true;
  66. command.value = true;
  67. expect( linkButton.isOn ).to.be.true;
  68. expect( linkButton.isEnabled ).to.be.true;
  69. command.isEnabled = false;
  70. command.value = false;
  71. expect( linkButton.isOn ).to.be.false;
  72. expect( linkButton.isEnabled ).to.be.false;
  73. } );
  74. it( 'should call #_showUI upon #execute', () => {
  75. const spy = testUtils.sinon.stub( linkUIFeature, '_showUI' ).returns( {} );
  76. linkButton.fire( 'execute' );
  77. sinon.assert.calledWithExactly( spy );
  78. } );
  79. } );
  80. } );
  81. describe( '_showUI()', () => {
  82. let balloonAddSpy;
  83. beforeEach( () => {
  84. balloonAddSpy = testUtils.sinon.spy( balloon, 'add' );
  85. editor.editing.view.document.isFocused = true;
  86. } );
  87. it( 'should not work if the link command is disabled', () => {
  88. setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
  89. editor.commands.get( 'link' ).isEnabled = false;
  90. linkUIFeature._showUI();
  91. expect( balloon.visibleView ).to.be.null;
  92. } );
  93. it( 'should not throw if the UI is already visible', () => {
  94. setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
  95. linkUIFeature._showUI();
  96. expect( () => {
  97. linkUIFeature._showUI();
  98. } ).to.not.throw();
  99. } );
  100. it( 'should add #formView to the balloon and attach the balloon to the selection when text fragment is selected', () => {
  101. setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
  102. const selectedRange = editorElement.ownerDocument.getSelection().getRangeAt( 0 );
  103. linkUIFeature._showUI();
  104. expect( balloon.visibleView ).to.equal( formView );
  105. sinon.assert.calledWithExactly( balloonAddSpy, {
  106. view: formView,
  107. position: {
  108. target: selectedRange
  109. }
  110. } );
  111. } );
  112. it( 'should add #formView to the balloon and attach the balloon to the selection when selection is collapsed', () => {
  113. setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
  114. const selectedRange = editorElement.ownerDocument.getSelection().getRangeAt( 0 );
  115. linkUIFeature._showUI();
  116. expect( balloon.visibleView ).to.equal( formView );
  117. sinon.assert.calledWithExactly( balloonAddSpy, {
  118. view: formView,
  119. position: {
  120. target: selectedRange
  121. }
  122. } );
  123. } );
  124. it( 'should add #actionsView to the balloon and attach the balloon to the link element when collapsed selection is inside ' +
  125. 'that link',
  126. () => {
  127. setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
  128. const linkElement = editor.editing.view.getDomRoot().querySelector( 'a' );
  129. linkUIFeature._showUI();
  130. expect( balloon.visibleView ).to.equal( actionsView );
  131. sinon.assert.calledWithExactly( balloonAddSpy, {
  132. view: actionsView,
  133. position: {
  134. target: linkElement
  135. }
  136. } );
  137. } );
  138. // #https://github.com/ckeditor/ckeditor5-link/issues/181
  139. it( 'should add #formView to the balloon when collapsed selection is inside the link and #actionsView is already visible', () => {
  140. setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
  141. const linkElement = editor.editing.view.getDomRoot().querySelector( 'a' );
  142. linkUIFeature._showUI();
  143. expect( balloon.visibleView ).to.equal( actionsView );
  144. sinon.assert.calledWithExactly( balloonAddSpy, {
  145. view: actionsView,
  146. position: {
  147. target: linkElement
  148. }
  149. } );
  150. linkUIFeature._showUI();
  151. expect( balloon.visibleView ).to.equal( formView );
  152. sinon.assert.calledWithExactly( balloonAddSpy, {
  153. view: formView,
  154. position: {
  155. target: linkElement
  156. }
  157. } );
  158. } );
  159. it( 'should disable #formView and #actionsView elements when link and unlink commands are disabled', () => {
  160. setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
  161. linkUIFeature._showUI();
  162. editor.commands.get( 'link' ).isEnabled = true;
  163. editor.commands.get( 'unlink' ).isEnabled = true;
  164. expect( formView.urlInputView.isReadOnly ).to.be.false;
  165. expect( formView.saveButtonView.isEnabled ).to.be.true;
  166. expect( formView.cancelButtonView.isEnabled ).to.be.true;
  167. expect( actionsView.unlinkButtonView.isEnabled ).to.be.true;
  168. expect( actionsView.editButtonView.isEnabled ).to.be.true;
  169. editor.commands.get( 'link' ).isEnabled = false;
  170. editor.commands.get( 'unlink' ).isEnabled = false;
  171. expect( formView.urlInputView.isReadOnly ).to.be.true;
  172. expect( formView.saveButtonView.isEnabled ).to.be.false;
  173. expect( formView.cancelButtonView.isEnabled ).to.be.true;
  174. expect( actionsView.unlinkButtonView.isEnabled ).to.be.false;
  175. expect( actionsView.editButtonView.isEnabled ).to.be.false;
  176. } );
  177. // https://github.com/ckeditor/ckeditor5-link/issues/78
  178. it( 'should make sure the URL input in the #formView always stays in sync with the value of the command (selected link)', () => {
  179. setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
  180. // Mock some leftover value **in DOM**, e.g. after previous editing.
  181. formView.urlInputView.inputView.element.value = 'leftover';
  182. linkUIFeature._showUI();
  183. actionsView.fire( 'edit' );
  184. expect( formView.urlInputView.inputView.element.value ).to.equal( 'url' );
  185. } );
  186. // https://github.com/ckeditor/ckeditor5-link/issues/123
  187. it( 'should make sure the URL input in the #formView always stays in sync with the value of the command (no link selected)', () => {
  188. setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
  189. linkUIFeature._showUI();
  190. expect( formView.urlInputView.inputView.element.value ).to.equal( '' );
  191. } );
  192. describe( 'response to ui#update', () => {
  193. let view, viewDocument;
  194. beforeEach( () => {
  195. view = editor.editing.view;
  196. viewDocument = view.document;
  197. } );
  198. it( 'should not duplicate #update listeners', () => {
  199. setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
  200. const spy = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
  201. linkUIFeature._showUI();
  202. editor.ui.fire( 'update' );
  203. linkUIFeature._hideUI();
  204. linkUIFeature._showUI();
  205. editor.ui.fire( 'update' );
  206. sinon.assert.calledTwice( spy );
  207. } );
  208. // https://github.com/ckeditor/ckeditor5-link/issues/113
  209. it( 'updates the position of the panel – editing a link, then the selection remains in the link', () => {
  210. setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
  211. linkUIFeature._showUI();
  212. const spy = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
  213. expect( getViewData( view ) ).to.equal(
  214. '<p><a class="ck-link_selected" href="url">f{}oo</a></p>'
  215. );
  216. const root = viewDocument.getRoot();
  217. const linkElement = root.getChild( 0 ).getChild( 0 );
  218. const text = linkElement.getChild( 0 );
  219. // Move selection to foo[].
  220. view.change( writer => {
  221. writer.setSelection( writer.createRange( writer.createPositionAt( text, 3 ) ), true );
  222. } );
  223. sinon.assert.calledOnce( spy );
  224. sinon.assert.calledWithExactly( spy, {
  225. target: view.domConverter.mapViewToDom( linkElement )
  226. } );
  227. } );
  228. // https://github.com/ckeditor/ckeditor5-link/issues/113
  229. it( 'updates the position of the panel – creating a new link, then the selection moved', () => {
  230. setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
  231. linkUIFeature._showUI();
  232. const spy = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
  233. const root = viewDocument.getRoot();
  234. const text = root.getChild( 0 ).getChild( 0 );
  235. view.change( writer => {
  236. writer.setSelection( writer.createRange( writer.createPositionAt( text, 3 ) ), true );
  237. } );
  238. sinon.assert.calledOnce( spy );
  239. sinon.assert.calledWithExactly( spy, {
  240. target: editorElement.ownerDocument.getSelection().getRangeAt( 0 )
  241. } );
  242. } );
  243. // https://github.com/ckeditor/ckeditor5-link/issues/113
  244. it( 'hides of the panel – editing a link, then the selection moved out of the link', () => {
  245. setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text>bar</paragraph>' );
  246. linkUIFeature._showUI();
  247. const spyUpdate = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
  248. const spyHide = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  249. const root = viewDocument.getRoot();
  250. const text = root.getChild( 0 ).getChild( 1 );
  251. // Move selection to b[]ar.
  252. view.change( writer => {
  253. writer.setSelection( writer.createRange( writer.createPositionAt( text, 1 ) ), true );
  254. } );
  255. sinon.assert.calledOnce( spyHide );
  256. sinon.assert.notCalled( spyUpdate );
  257. } );
  258. // https://github.com/ckeditor/ckeditor5-link/issues/113
  259. it( 'hides the panel – editing a link, then the selection expands', () => {
  260. setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
  261. linkUIFeature._showUI();
  262. const spyUpdate = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
  263. const spyHide = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  264. expect( getViewData( view ) ).to.equal( '<p><a class="ck-link_selected" href="url">f{}oo</a></p>' );
  265. const root = viewDocument.getRoot();
  266. const text = root.getChild( 0 ).getChild( 0 ).getChild( 0 );
  267. // Move selection to f[o]o.
  268. view.change( writer => {
  269. writer.setSelection( writer.createRange(
  270. writer.createPositionAt( text, 1 ),
  271. writer.createPositionAt( text, 2 )
  272. ), true );
  273. } );
  274. sinon.assert.calledOnce( spyHide );
  275. sinon.assert.notCalled( spyUpdate );
  276. } );
  277. // https://github.com/ckeditor/ckeditor5-link/issues/113
  278. it( 'hides the panel – creating a new link, then the selection moved to another parent', () => {
  279. setModelData( editor.model, '<paragraph>f[]oo</paragraph><paragraph>bar</paragraph>' );
  280. linkUIFeature._showUI();
  281. const spyUpdate = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
  282. const spyHide = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  283. const root = viewDocument.getRoot();
  284. const text = root.getChild( 1 ).getChild( 0 );
  285. // Move selection to f[o]o.
  286. view.change( writer => {
  287. writer.setSelection( writer.createRange(
  288. writer.createPositionAt( text, 1 ),
  289. writer.createPositionAt( text, 2 )
  290. ), true );
  291. } );
  292. sinon.assert.calledOnce( spyHide );
  293. sinon.assert.notCalled( spyUpdate );
  294. } );
  295. } );
  296. } );
  297. describe( '_hideUI()', () => {
  298. beforeEach( () => {
  299. linkUIFeature._showUI();
  300. } );
  301. it( 'should remove the UI from the balloon', () => {
  302. expect( balloon.hasView( formView ) ).to.be.true;
  303. expect( balloon.hasView( actionsView ) ).to.be.true;
  304. linkUIFeature._hideUI();
  305. expect( balloon.hasView( formView ) ).to.be.false;
  306. expect( balloon.hasView( actionsView ) ).to.be.false;
  307. } );
  308. it( 'should focus the `editable` by default', () => {
  309. const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  310. linkUIFeature._hideUI();
  311. // First call is from _removeFormView.
  312. sinon.assert.calledTwice( spy );
  313. } );
  314. it( 'should not throw an error when views are not in the `balloon`', () => {
  315. linkUIFeature._hideUI();
  316. expect( () => {
  317. linkUIFeature._hideUI();
  318. } ).to.not.throw();
  319. } );
  320. it( 'should clear ui#update listener from the ViewDocument', () => {
  321. const spy = sinon.spy();
  322. linkUIFeature.listenTo( editor.ui, 'update', spy );
  323. linkUIFeature._hideUI();
  324. editor.ui.fire( 'update' );
  325. sinon.assert.notCalled( spy );
  326. } );
  327. } );
  328. describe( 'keyboard support', () => {
  329. it( 'should show the UI on Ctrl+K keystroke', () => {
  330. const spy = testUtils.sinon.stub( linkUIFeature, '_showUI' ).returns( {} );
  331. const command = editor.commands.get( 'link' );
  332. command.isEnabled = false;
  333. editor.keystrokes.press( {
  334. keyCode: keyCodes.k,
  335. ctrlKey: true,
  336. preventDefault: sinon.spy(),
  337. stopPropagation: sinon.spy()
  338. } );
  339. sinon.assert.notCalled( spy );
  340. command.isEnabled = true;
  341. editor.keystrokes.press( {
  342. keyCode: keyCodes.k,
  343. ctrlKey: true,
  344. preventDefault: sinon.spy(),
  345. stopPropagation: sinon.spy()
  346. } );
  347. sinon.assert.calledWithExactly( spy );
  348. } );
  349. it( 'should prevent default action on Ctrl+K keystroke', () => {
  350. const preventDefaultSpy = sinon.spy();
  351. const stopPropagationSpy = sinon.spy();
  352. editor.keystrokes.press( {
  353. keyCode: keyCodes.k,
  354. ctrlKey: true,
  355. preventDefault: preventDefaultSpy,
  356. stopPropagation: stopPropagationSpy
  357. } );
  358. sinon.assert.calledOnce( preventDefaultSpy );
  359. sinon.assert.calledOnce( stopPropagationSpy );
  360. } );
  361. it( 'should focus the the #actionsView on `Tab` key press when #actionsView is visible', () => {
  362. const keyEvtData = {
  363. keyCode: keyCodes.tab,
  364. preventDefault: sinon.spy(),
  365. stopPropagation: sinon.spy()
  366. };
  367. const normalPriorityTabCallbackSpy = sinon.spy();
  368. const highestPriorityTabCallbackSpy = sinon.spy();
  369. editor.keystrokes.set( 'Tab', normalPriorityTabCallbackSpy );
  370. editor.keystrokes.set( 'Tab', highestPriorityTabCallbackSpy, { priority: 'highest' } );
  371. // Balloon is invisible, form not focused.
  372. actionsView.focusTracker.isFocused = false;
  373. const spy = sinon.spy( actionsView, 'focus' );
  374. editor.keystrokes.press( keyEvtData );
  375. sinon.assert.notCalled( keyEvtData.preventDefault );
  376. sinon.assert.notCalled( keyEvtData.stopPropagation );
  377. sinon.assert.notCalled( spy );
  378. sinon.assert.calledOnce( normalPriorityTabCallbackSpy );
  379. sinon.assert.calledOnce( highestPriorityTabCallbackSpy );
  380. // Balloon is visible, form focused.
  381. linkUIFeature._showUI();
  382. testUtils.sinon.stub( linkUIFeature, '_areActionsVisible' ).value( true );
  383. actionsView.focusTracker.isFocused = true;
  384. editor.keystrokes.press( keyEvtData );
  385. sinon.assert.notCalled( keyEvtData.preventDefault );
  386. sinon.assert.notCalled( keyEvtData.stopPropagation );
  387. sinon.assert.notCalled( spy );
  388. sinon.assert.calledTwice( normalPriorityTabCallbackSpy );
  389. sinon.assert.calledTwice( highestPriorityTabCallbackSpy );
  390. // Balloon is still visible, form not focused.
  391. actionsView.focusTracker.isFocused = false;
  392. editor.keystrokes.press( keyEvtData );
  393. sinon.assert.calledOnce( keyEvtData.preventDefault );
  394. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  395. sinon.assert.calledOnce( spy );
  396. sinon.assert.calledTwice( normalPriorityTabCallbackSpy );
  397. sinon.assert.calledThrice( highestPriorityTabCallbackSpy );
  398. } );
  399. it( 'should hide the UI after Esc key press (from editor) and not focus the editable', () => {
  400. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  401. const keyEvtData = {
  402. keyCode: keyCodes.esc,
  403. preventDefault: sinon.spy(),
  404. stopPropagation: sinon.spy()
  405. };
  406. // Balloon is visible.
  407. linkUIFeature._showUI();
  408. editor.keystrokes.press( keyEvtData );
  409. sinon.assert.calledWithExactly( spy );
  410. } );
  411. it( 'should not hide the UI after Esc key press (from editor) when UI is open but is not visible', () => {
  412. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  413. const keyEvtData = {
  414. keyCode: keyCodes.esc,
  415. preventDefault: () => {},
  416. stopPropagation: () => {}
  417. };
  418. const viewMock = {
  419. ready: true,
  420. render: () => {},
  421. destroy: () => {}
  422. };
  423. linkUIFeature._showUI();
  424. // Some view precedes the link UI in the balloon.
  425. balloon.add( { view: viewMock } );
  426. editor.keystrokes.press( keyEvtData );
  427. sinon.assert.notCalled( spy );
  428. } );
  429. } );
  430. describe( 'mouse support', () => {
  431. it( 'should hide the UI and not focus editable upon clicking outside the UI', () => {
  432. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  433. linkUIFeature._showUI( true );
  434. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  435. sinon.assert.calledWithExactly( spy );
  436. } );
  437. it( 'should not hide the UI upon clicking inside the the UI', () => {
  438. const spy = testUtils.sinon.spy( linkUIFeature, '_hideUI' );
  439. linkUIFeature._showUI( true );
  440. balloon.view.element.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  441. sinon.assert.notCalled( spy );
  442. } );
  443. describe( 'clicking on editable', () => {
  444. let observer, spy;
  445. beforeEach( () => {
  446. observer = editor.editing.view.getObserver( ClickObserver );
  447. editor.model.schema.extend( '$text', { allowIn: '$root' } );
  448. spy = testUtils.sinon.stub( linkUIFeature, '_showUI' ).returns( {} );
  449. } );
  450. it( 'should show the UI when collapsed selection is inside link element', () => {
  451. setModelData( editor.model, '<$text linkHref="url">fo[]o</$text>' );
  452. observer.fire( 'click', { target: document.body } );
  453. sinon.assert.calledWithExactly( spy );
  454. } );
  455. it( 'should show the UI when selection exclusively encloses a link element (#1)', () => {
  456. setModelData( editor.model, '[<$text linkHref="url">foo</$text>]' );
  457. observer.fire( 'click', { target: {} } );
  458. sinon.assert.calledWithExactly( spy );
  459. } );
  460. it( 'should show the UI when selection exclusively encloses a link element (#2)', () => {
  461. setModelData( editor.model, '<$text linkHref="url">[foo]</$text>' );
  462. observer.fire( 'click', { target: {} } );
  463. sinon.assert.calledWithExactly( spy );
  464. } );
  465. it( 'should do nothing when selection is not inside link element', () => {
  466. setModelData( editor.model, '[]' );
  467. observer.fire( 'click', { target: {} } );
  468. sinon.assert.notCalled( spy );
  469. } );
  470. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#1)', () => {
  471. setModelData( editor.model, '<$text linkHref="url">f[o]o</$text>' );
  472. observer.fire( 'click', { target: {} } );
  473. sinon.assert.notCalled( spy );
  474. } );
  475. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#2)', () => {
  476. setModelData( editor.model, '<$text linkHref="url">[fo]o</$text>' );
  477. observer.fire( 'click', { target: {} } );
  478. sinon.assert.notCalled( spy );
  479. } );
  480. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#3)', () => {
  481. setModelData( editor.model, '<$text linkHref="url">f[oo]</$text>' );
  482. observer.fire( 'click', { target: {} } );
  483. sinon.assert.notCalled( spy );
  484. } );
  485. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#4)', () => {
  486. setModelData( editor.model, 'ba[r<$text linkHref="url">foo]</$text>' );
  487. observer.fire( 'click', { target: {} } );
  488. sinon.assert.notCalled( spy );
  489. } );
  490. it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#5)', () => {
  491. setModelData( editor.model, 'ba[r<$text linkHref="url">foo</$text>]' );
  492. observer.fire( 'click', { target: {} } );
  493. sinon.assert.notCalled( spy );
  494. } );
  495. } );
  496. } );
  497. describe( 'actions view', () => {
  498. let focusEditableSpy;
  499. beforeEach( () => {
  500. focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  501. } );
  502. it( 'should mark the editor UI as focused when the #actionsView is focused', () => {
  503. linkUIFeature._showUI();
  504. linkUIFeature._removeFormView();
  505. expect( balloon.visibleView ).to.equal( actionsView );
  506. editor.ui.focusTracker.isFocused = false;
  507. actionsView.element.dispatchEvent( new Event( 'focus' ) );
  508. expect( editor.ui.focusTracker.isFocused ).to.be.true;
  509. } );
  510. describe( 'binding', () => {
  511. it( 'should show the #formView on #edit event and select the URL input field', () => {
  512. linkUIFeature._showUI();
  513. linkUIFeature._removeFormView();
  514. const selectSpy = testUtils.sinon.spy( formView.urlInputView, 'select' );
  515. actionsView.fire( 'edit' );
  516. expect( balloon.visibleView ).to.equal( formView );
  517. sinon.assert.calledOnce( selectSpy );
  518. } );
  519. it( 'should execute unlink command on actionsView#unlink event', () => {
  520. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  521. actionsView.fire( 'unlink' );
  522. expect( executeSpy.calledOnce ).to.be.true;
  523. expect( executeSpy.calledWithExactly( 'unlink' ) ).to.be.true;
  524. } );
  525. it( 'should hide and focus editable on actionsView#unlink event', () => {
  526. linkUIFeature._showUI();
  527. linkUIFeature._removeFormView();
  528. // Removing the form would call the focus spy.
  529. focusEditableSpy.resetHistory();
  530. actionsView.fire( 'unlink' );
  531. expect( balloon.visibleView ).to.be.null;
  532. expect( focusEditableSpy.calledOnce ).to.be.true;
  533. } );
  534. it( 'should hide after Esc key press', () => {
  535. const keyEvtData = {
  536. keyCode: keyCodes.esc,
  537. preventDefault: sinon.spy(),
  538. stopPropagation: sinon.spy()
  539. };
  540. linkUIFeature._showUI();
  541. linkUIFeature._removeFormView();
  542. // Removing the form would call the focus spy.
  543. focusEditableSpy.resetHistory();
  544. actionsView.keystrokes.press( keyEvtData );
  545. expect( balloon.visibleView ).to.equal( null );
  546. expect( focusEditableSpy.calledOnce ).to.be.true;
  547. } );
  548. // #https://github.com/ckeditor/ckeditor5-link/issues/181
  549. it( 'should add the #formView upon Ctrl+K keystroke press', () => {
  550. const keyEvtData = {
  551. keyCode: keyCodes.k,
  552. ctrlKey: true,
  553. preventDefault: sinon.spy(),
  554. stopPropagation: sinon.spy()
  555. };
  556. linkUIFeature._showUI();
  557. linkUIFeature._removeFormView();
  558. expect( balloon.visibleView ).to.equal( actionsView );
  559. actionsView.keystrokes.press( keyEvtData );
  560. expect( balloon.visibleView ).to.equal( formView );
  561. } );
  562. } );
  563. } );
  564. describe( 'link form view', () => {
  565. let focusEditableSpy;
  566. beforeEach( () => {
  567. focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  568. } );
  569. it( 'should mark the editor UI as focused when the #formView is focused', () => {
  570. linkUIFeature._showUI();
  571. expect( balloon.visibleView ).to.equal( formView );
  572. editor.ui.focusTracker.isFocused = false;
  573. formView.element.dispatchEvent( new Event( 'focus' ) );
  574. expect( editor.ui.focusTracker.isFocused ).to.be.true;
  575. } );
  576. describe( 'binding', () => {
  577. beforeEach( () => {
  578. setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
  579. } );
  580. it( 'should bind formView.urlInputView#value to link command value', () => {
  581. const command = editor.commands.get( 'link' );
  582. expect( formView.urlInputView.value ).to.undefined;
  583. command.value = 'http://cksource.com';
  584. expect( formView.urlInputView.value ).to.equal( 'http://cksource.com' );
  585. } );
  586. it( 'should execute link command on formView#submit event', () => {
  587. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  588. formView.urlInputView.value = 'http://ckeditor.com';
  589. expect( formView.urlInputView.inputView.element.value ).to.equal( 'http://ckeditor.com' );
  590. formView.urlInputView.inputView.element.value = 'http://cksource.com';
  591. formView.fire( 'submit' );
  592. expect( executeSpy.calledOnce ).to.be.true;
  593. expect( executeSpy.calledWithExactly( 'link', 'http://cksource.com' ) ).to.be.true;
  594. } );
  595. it( 'should hide and reveal the #actionsView on formView#submit event', () => {
  596. linkUIFeature._showUI();
  597. formView.fire( 'submit' );
  598. expect( balloon.visibleView ).to.equal( actionsView );
  599. expect( focusEditableSpy.calledOnce ).to.be.true;
  600. } );
  601. it( 'should hide and reveal the #actionsView on formView#cancel event', () => {
  602. linkUIFeature._showUI();
  603. formView.fire( 'cancel' );
  604. expect( balloon.visibleView ).to.equal( actionsView );
  605. expect( focusEditableSpy.calledOnce ).to.be.true;
  606. } );
  607. it( 'should hide after Esc key press', () => {
  608. const keyEvtData = {
  609. keyCode: keyCodes.esc,
  610. preventDefault: sinon.spy(),
  611. stopPropagation: sinon.spy()
  612. };
  613. linkUIFeature._showUI();
  614. formView.keystrokes.press( keyEvtData );
  615. expect( balloon.visibleView ).to.equal( actionsView );
  616. expect( focusEditableSpy.calledOnce ).to.be.true;
  617. } );
  618. } );
  619. } );
  620. } );