8
0

link.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  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 Link from '../src/link';
  11. import LinkEngine from '../src/linkengine';
  12. import ContextualBalloon from '@ckeditor/ckeditor5-ui/src/panel/balloon/contextualballoon';
  13. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  14. import Range from '@ckeditor/ckeditor5-engine/src/view/range';
  15. import ClickObserver from '@ckeditor/ckeditor5-engine/src/view/observer/clickobserver';
  16. testUtils.createSinonSandbox();
  17. describe( 'Link', () => {
  18. let editor, linkFeature, linkButton, unlinkButton, balloon, formView, editorElement;
  19. beforeEach( () => {
  20. editorElement = document.createElement( 'div' );
  21. document.body.appendChild( editorElement );
  22. return ClassicTestEditor.create( editorElement, {
  23. plugins: [ Link ]
  24. } )
  25. .then( newEditor => {
  26. newEditor.editing.view.attachDomRoot( editorElement );
  27. editor = newEditor;
  28. linkFeature = editor.plugins.get( Link );
  29. linkButton = editor.ui.componentFactory.create( 'link' );
  30. unlinkButton = editor.ui.componentFactory.create( 'unlink' );
  31. balloon = editor.plugins.get( ContextualBalloon );
  32. formView = linkFeature.formView;
  33. // There is no point to execute `BalloonPanelView#attachTo` so override it.
  34. testUtils.sinon.stub( balloon.view, 'attachTo', () => {} );
  35. return formView.init();
  36. } );
  37. } );
  38. afterEach( () => {
  39. return editor.destroy();
  40. } );
  41. it( 'should be loaded', () => {
  42. expect( linkFeature ).to.instanceOf( Link );
  43. } );
  44. it( 'should load LinkEngine', () => {
  45. expect( editor.plugins.get( LinkEngine ) ).to.instanceOf( LinkEngine );
  46. } );
  47. it( 'should load ContextualBalloon', () => {
  48. expect( editor.plugins.get( ContextualBalloon ) ).to.instanceOf( ContextualBalloon );
  49. } );
  50. it( 'should register click observer', () => {
  51. expect( editor.editing.view.getObserver( ClickObserver ) ).to.instanceOf( ClickObserver );
  52. } );
  53. describe( 'link toolbar button', () => {
  54. it( 'should register link button', () => {
  55. expect( linkButton ).to.instanceOf( ButtonView );
  56. } );
  57. it( 'should bind linkButtonView to link command', () => {
  58. const command = editor.commands.get( 'link' );
  59. command.isEnabled = true;
  60. expect( linkButton.isEnabled ).to.be.true;
  61. command.isEnabled = false;
  62. expect( linkButton.isEnabled ).to.be.false;
  63. } );
  64. it( 'should add link form to the ContextualBalloon on execute event', () => {
  65. linkButton.fire( 'execute' );
  66. expect( balloon.visibleView ).to.equal( formView );
  67. } );
  68. it( 'should add link form to the ContextualBalloon and attach balloon to the link element ' +
  69. 'when collapsed selection is inside link element',
  70. () => {
  71. const balloonAddSpy = testUtils.sinon.spy( balloon, 'add' );
  72. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  73. setModelData( editor.document, '<$text linkHref="url">some[] url</$text>' );
  74. editor.editing.view.isFocused = true;
  75. linkButton.fire( 'execute' );
  76. const linkElement = editorElement.querySelector( 'a' );
  77. sinon.assert.calledWithExactly( balloonAddSpy, {
  78. view: formView,
  79. position: {
  80. target: linkElement,
  81. limiter: editorElement
  82. }
  83. } );
  84. } );
  85. it( 'should add link form to the ContextualBalloon and attach balloon to the selection, when selection is non-collapsed', () => {
  86. const balloonAddSpy = testUtils.sinon.spy( balloon, 'add' );
  87. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  88. setModelData( editor.document, 'so[me ur]l' );
  89. editor.editing.view.isFocused = true;
  90. linkButton.fire( 'execute' );
  91. const selectedRange = editorElement.ownerDocument.getSelection().getRangeAt( 0 );
  92. sinon.assert.calledWithExactly( balloonAddSpy, {
  93. view: formView,
  94. position: {
  95. target: selectedRange,
  96. limiter: editorElement
  97. }
  98. } );
  99. } );
  100. it( 'should select link input value when link balloon is opened', () => {
  101. const selectUrlInputSpy = testUtils.sinon.spy( linkFeature.formView.urlInputView, 'select' );
  102. editor.editing.view.isFocused = true;
  103. linkButton.fire( 'execute' );
  104. expect( selectUrlInputSpy.calledOnce ).to.true;
  105. } );
  106. } );
  107. describe( 'unlink toolbar button', () => {
  108. it( 'should register unlink button', () => {
  109. expect( unlinkButton ).to.instanceOf( ButtonView );
  110. } );
  111. it( 'should bind unlinkButtonView to unlink command', () => {
  112. const command = editor.commands.get( 'unlink' );
  113. command.isEnabled = true;
  114. expect( unlinkButton.isEnabled ).to.be.true;
  115. command.isEnabled = false;
  116. expect( unlinkButton.isEnabled ).to.be.false;
  117. } );
  118. it( 'should execute unlink command on unlinkButtonView execute event', () => {
  119. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  120. unlinkButton.fire( 'execute' );
  121. expect( executeSpy.calledOnce ).to.true;
  122. expect( executeSpy.calledWithExactly( 'unlink' ) ).to.true;
  123. } );
  124. } );
  125. describe( 'ContextualBalloon', () => {
  126. let focusEditableSpy;
  127. beforeEach( () => {
  128. focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  129. } );
  130. it( 'should not be added to ContextualBalloon at default', () => {
  131. expect( balloon.visibleView ).to.null;
  132. } );
  133. it( 'should be added to ContextualBalloon and form should be selected on `CTRL+K` keystroke', () => {
  134. const selectUrlInputSpy = testUtils.sinon.spy( formView.urlInputView, 'select' );
  135. editor.keystrokes.press( { keyCode: keyCodes.k, ctrlKey: true } );
  136. expect( balloon.visibleView ).to.equal( formView );
  137. expect( selectUrlInputSpy.calledOnce ).to.true;
  138. } );
  139. it( 'should not add panel to ContextualBalloon more than once', () => {
  140. // Add panel to balloon by pressing toolbar button.
  141. linkButton.fire( 'execute' );
  142. // Press button once again.
  143. expect( () => {
  144. linkButton.fire( 'execute' );
  145. } ).to.not.throw();
  146. } );
  147. it( 'should focus the link form on Tab key press', () => {
  148. const keyEvtData = {
  149. keyCode: keyCodes.tab,
  150. preventDefault: sinon.spy(),
  151. stopPropagation: sinon.spy()
  152. };
  153. // Balloon is invisible, form not focused.
  154. formView.focusTracker.isFocused = false;
  155. const spy = sinon.spy( formView, 'focus' );
  156. editor.keystrokes.press( keyEvtData );
  157. sinon.assert.notCalled( keyEvtData.preventDefault );
  158. sinon.assert.notCalled( keyEvtData.stopPropagation );
  159. sinon.assert.notCalled( spy );
  160. // Balloon is visible, form focused.
  161. return balloon.add( { view: formView } )
  162. .then( () => {
  163. formView.focusTracker.isFocused = true;
  164. editor.keystrokes.press( keyEvtData );
  165. sinon.assert.notCalled( keyEvtData.preventDefault );
  166. sinon.assert.notCalled( keyEvtData.stopPropagation );
  167. sinon.assert.notCalled( spy );
  168. // Balloon is still visible, form not focused.
  169. formView.focusTracker.isFocused = false;
  170. editor.keystrokes.press( keyEvtData );
  171. sinon.assert.calledOnce( keyEvtData.preventDefault );
  172. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  173. sinon.assert.calledOnce( spy );
  174. } );
  175. } );
  176. describe( 'close listeners', () => {
  177. describe( 'keyboard', () => {
  178. it( 'should close after Esc key press (from editor) and not focus editable', () => {
  179. const keyEvtData = {
  180. keyCode: keyCodes.esc,
  181. preventDefault: sinon.spy(),
  182. stopPropagation: sinon.spy()
  183. };
  184. // Balloon is visible.
  185. return balloon.add( { view: formView } ).then( () => {
  186. editor.keystrokes.press( keyEvtData );
  187. expect( balloon.visibleView ).to.null;
  188. sinon.assert.notCalled( focusEditableSpy );
  189. } );
  190. } );
  191. it( 'should not close after Esc key press (from editor) when panel is in stack but not visible', () => {
  192. const keyEvtData = {
  193. keyCode: keyCodes.esc,
  194. preventDefault: () => {},
  195. stopPropagation: () => {}
  196. };
  197. const viewMock = {
  198. init: () => {},
  199. destroy: () => {}
  200. };
  201. return balloon.add( { view: formView } )
  202. .then( () => {
  203. return balloon.add( { view: viewMock } );
  204. } )
  205. .then( () => {
  206. editor.keystrokes.press( keyEvtData );
  207. expect( balloon.visibleView ).to.equal( viewMock );
  208. expect( balloon.hasView( formView ) ).to.true;
  209. sinon.assert.notCalled( focusEditableSpy );
  210. } );
  211. } );
  212. it( 'should close after Esc key press (from the form) and focus editable', () => {
  213. const keyEvtData = {
  214. keyCode: keyCodes.esc,
  215. preventDefault: sinon.spy(),
  216. stopPropagation: sinon.spy()
  217. };
  218. return balloon.add( { view: formView } )
  219. .then( () => {
  220. formView.keystrokes.press( keyEvtData );
  221. expect( balloon.visibleView ).to.null;
  222. sinon.assert.calledOnce( focusEditableSpy );
  223. } );
  224. } );
  225. } );
  226. describe( 'mouse', () => {
  227. it( 'should close and not focus editable on click outside the panel', () => {
  228. return balloon.add( { view: formView } )
  229. .then( () => {
  230. document.body.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
  231. expect( balloon.visibleView ).to.null;
  232. expect( focusEditableSpy.notCalled ).to.true;
  233. } );
  234. } );
  235. it( 'should not close on click inside the panel', () => {
  236. return balloon.add( { view: formView } )
  237. .then( () => {
  238. balloon.view.element.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
  239. expect( balloon.visibleView ).to.equal( formView );
  240. } );
  241. } );
  242. } );
  243. } );
  244. describe( 'click on editable', () => {
  245. it( 'should open with not selected url input when collapsed selection is inside link element', () => {
  246. const selectUrlInputSpy = testUtils.sinon.spy( formView.urlInputView, 'select' );
  247. const observer = editor.editing.view.getObserver( ClickObserver );
  248. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  249. setModelData( editor.document, '<$text linkHref="url">fo[]o</$text>' );
  250. observer.fire( 'click', { target: document.body } );
  251. expect( balloon.visibleView ).to.equal( formView );
  252. expect( selectUrlInputSpy.notCalled ).to.true;
  253. } );
  254. it( 'should keep open and update position until collapsed selection stay inside the same link element', () => {
  255. const balloonUpdatePositionSpy = testUtils.sinon.spy( balloon, 'updatePosition' );
  256. const observer = editor.editing.view.getObserver( ClickObserver );
  257. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  258. setModelData( editor.document, '<$text linkHref="url">b[]ar</$text>' );
  259. const root = editor.editing.view.getRoot();
  260. const text = root.getChild( 0 ).getChild( 0 );
  261. observer.fire( 'click', { target: document.body } );
  262. expect( balloon.visibleView ).to.equal( formView );
  263. // Move selection.
  264. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 1 ) ], true );
  265. editor.editing.view.render();
  266. // Check if balloon is still open and position was updated.
  267. expect( balloon.visibleView ).to.equal( formView );
  268. expect( balloonUpdatePositionSpy.calledOnce ).to.true;
  269. } );
  270. it( 'should not duplicate `render` listener on `ViewDocument`', () => {
  271. const observer = editor.editing.view.getObserver( ClickObserver );
  272. const updatePositionSpy = testUtils.sinon.spy( balloon, 'updatePosition' );
  273. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  274. setModelData( editor.document, '<$text linkHref="url">b[]ar</$text>' );
  275. // Click at the same link more than once.
  276. observer.fire( 'click', { target: document.body } );
  277. observer.fire( 'click', { target: document.body } );
  278. observer.fire( 'click', { target: document.body } );
  279. sinon.assert.notCalled( updatePositionSpy );
  280. const root = editor.editing.view.getRoot();
  281. const text = root.getChild( 0 ).getChild( 0 );
  282. // Move selection.
  283. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 1 ) ], true );
  284. editor.editing.view.render();
  285. // Position should be updated only once.
  286. sinon.assert.calledOnce( updatePositionSpy );
  287. } );
  288. it( 'should close when selection goes outside the link element', () => {
  289. const observer = editor.editing.view.getObserver( ClickObserver );
  290. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  291. setModelData( editor.document, 'foo <$text linkHref="url">b[]ar</$text>' );
  292. const root = editor.editing.view.getRoot();
  293. const text = root.getChild( 0 );
  294. observer.fire( 'click', { target: document.body } );
  295. expect( balloon.visibleView ).to.equal( formView );
  296. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 3, text, 3 ) ], true );
  297. editor.editing.view.render();
  298. expect( balloon.visibleView ).to.null;
  299. } );
  300. it( 'should close when selection goes to the other link element with the same href', () => {
  301. const observer = editor.editing.view.getObserver( ClickObserver );
  302. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  303. setModelData( editor.document, '<$text linkHref="url">f[]oo</$text> bar <$text linkHref="url">biz</$text>' );
  304. const root = editor.editing.view.getRoot();
  305. const text = root.getChild( 2 ).getChild( 0 );
  306. observer.fire( 'click', { target: document.body } );
  307. expect( balloon.visibleView ).to.equal( formView );
  308. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 1 ) ], true );
  309. editor.editing.view.render();
  310. expect( balloon.visibleView ).to.null;
  311. } );
  312. it( 'should close when selection becomes non-collapsed', () => {
  313. const observer = editor.editing.view.getObserver( ClickObserver );
  314. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  315. setModelData( editor.document, '<$text linkHref="url">f[]oo</$text>' );
  316. const root = editor.editing.view.getRoot();
  317. const text = root.getChild( 0 ).getChild( 0 );
  318. observer.fire( 'click', { target: {} } );
  319. expect( balloon.visibleView ).to.equal( formView );
  320. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 2 ) ] );
  321. editor.editing.view.render();
  322. expect( balloon.visibleView ).to.null;
  323. } );
  324. it( 'should stop updating position after close', () => {
  325. const balloonUpdatePositionSpy = testUtils.sinon.spy( balloon, 'updatePosition' );
  326. const observer = editor.editing.view.getObserver( ClickObserver );
  327. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  328. setModelData( editor.document, '<$text linkHref="url">b[]ar</$text>' );
  329. const root = editor.editing.view.getRoot();
  330. const text = root.getChild( 0 ).getChild( 0 );
  331. observer.fire( 'click', { target: {} } );
  332. expect( balloon.visibleView ).to.equal( formView );
  333. // Close balloon by dispatching `cancel` event on formView.
  334. formView.fire( 'cancel' );
  335. // Move selection inside link element.
  336. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 2, text, 2 ) ], true );
  337. editor.editing.view.render();
  338. expect( balloonUpdatePositionSpy.notCalled ).to.true;
  339. } );
  340. it( 'should not open when selection is not inside link element', () => {
  341. const observer = editor.editing.view.getObserver( ClickObserver );
  342. setModelData( editor.document, '[]' );
  343. observer.fire( 'click', { target: {} } );
  344. expect( balloon.visibleView ).to.null;
  345. } );
  346. it( 'should not open when selection is non-collapsed', () => {
  347. const observer = editor.editing.view.getObserver( ClickObserver );
  348. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  349. setModelData( editor.document, '<$text linkHref="url">f[o]o</$text>' );
  350. observer.fire( 'click', { target: document.body } );
  351. expect( balloon.visibleView ).to.null;
  352. } );
  353. } );
  354. } );
  355. describe( 'link form', () => {
  356. let focusEditableSpy;
  357. beforeEach( () => {
  358. focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  359. } );
  360. describe( 'binding', () => {
  361. it( 'should bind formView.urlInputView#value to link command value', () => {
  362. const command = editor.commands.get( 'link' );
  363. expect( formView.urlInputView.value ).to.undefined;
  364. command.value = 'http://cksource.com';
  365. expect( formView.urlInputView.value ).to.equal( 'http://cksource.com' );
  366. } );
  367. it( 'should execute link command on formView#submit event', () => {
  368. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  369. formView.urlInputView.value = 'http://ckeditor.com';
  370. expect( formView.urlInputView.inputView.element.value ).to.equal( 'http://ckeditor.com' );
  371. formView.urlInputView.inputView.element.value = 'http://cksource.com';
  372. formView.fire( 'submit' );
  373. expect( executeSpy.calledOnce ).to.true;
  374. expect( executeSpy.calledWithExactly( 'link', 'http://cksource.com' ) ).to.true;
  375. } );
  376. it( 'should hide and focus editable on formView#submit event', () => {
  377. return balloon.add( { view: formView } )
  378. .then( () => {
  379. formView.fire( 'submit' );
  380. expect( balloon.visibleView ).to.null;
  381. expect( focusEditableSpy.calledOnce ).to.true;
  382. } );
  383. } );
  384. it( 'should execute unlink command on formView#unlink event', () => {
  385. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  386. formView.fire( 'unlink' );
  387. expect( executeSpy.calledOnce ).to.true;
  388. expect( executeSpy.calledWithExactly( 'unlink' ) ).to.true;
  389. } );
  390. it( 'should hide and focus editable on formView#unlink event', () => {
  391. return balloon.add( { view: formView } )
  392. .then( () => {
  393. formView.fire( 'unlink' );
  394. expect( balloon.visibleView ).to.null;
  395. expect( focusEditableSpy.calledOnce ).to.true;
  396. } );
  397. } );
  398. it( 'should hide and focus editable on formView#cancel event', () => {
  399. return balloon.add( { view: formView } )
  400. .then( () => {
  401. formView.fire( 'cancel' );
  402. expect( balloon.visibleView ).to.null;
  403. expect( focusEditableSpy.calledOnce ).to.true;
  404. } );
  405. } );
  406. } );
  407. } );
  408. } );