link.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  13. import BalloonPanelView from '@ckeditor/ckeditor5-ui/src/balloonpanel/balloonpanelview';
  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, balloonPanelView, 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. balloonPanelView = linkFeature.balloonPanelView;
  32. formView = linkFeature.formView;
  33. } );
  34. } );
  35. afterEach( () => {
  36. return editor.destroy();
  37. } );
  38. it( 'should be loaded', () => {
  39. expect( linkFeature ).to.instanceOf( Link );
  40. } );
  41. it( 'should load LinkEngine', () => {
  42. expect( editor.plugins.get( LinkEngine ) ).to.instanceOf( LinkEngine );
  43. } );
  44. it( 'should register click observer', () => {
  45. expect( editor.editing.view.getObserver( ClickObserver ) ).to.instanceOf( ClickObserver );
  46. } );
  47. describe( 'link toolbar button', () => {
  48. it( 'should register link button', () => {
  49. expect( linkButton ).to.instanceOf( ButtonView );
  50. } );
  51. it( 'should bind linkButtonView to link command', () => {
  52. const command = editor.commands.get( 'link' );
  53. command.isEnabled = true;
  54. expect( linkButton.isEnabled ).to.be.true;
  55. command.isEnabled = false;
  56. expect( linkButton.isEnabled ).to.be.false;
  57. } );
  58. it( 'should open panel on linkButtonView execute event', () => {
  59. linkButton.fire( 'execute' );
  60. expect( linkFeature.balloonPanelView.isVisible ).to.true;
  61. } );
  62. it( 'should open panel attached to the link element, when collapsed selection is inside link element', () => {
  63. const attachToSpy = testUtils.sinon.spy( balloonPanelView, 'attachTo' );
  64. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  65. setModelData( editor.document, '<$text linkHref="url">some[] url</$text>' );
  66. editor.editing.view.isFocused = true;
  67. linkButton.fire( 'execute' );
  68. const linkElement = editorElement.querySelector( 'a' );
  69. sinon.assert.calledWithExactly( attachToSpy, sinon.match( {
  70. target: linkElement,
  71. limiter: editorElement
  72. } ) );
  73. } );
  74. it( 'should open panel attached to the selection, when there is non-collapsed selection', () => {
  75. const attachToSpy = testUtils.sinon.spy( balloonPanelView, 'attachTo' );
  76. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  77. setModelData( editor.document, 'so[me ur]l' );
  78. editor.editing.view.isFocused = true;
  79. linkButton.fire( 'execute' );
  80. const selectedRange = editorElement.ownerDocument.getSelection().getRangeAt( 0 );
  81. sinon.assert.calledWithExactly( attachToSpy, sinon.match( {
  82. target: selectedRange,
  83. limiter: editorElement
  84. } ) );
  85. } );
  86. it( 'should select panel input value when panel is opened', () => {
  87. const selectUrlInputSpy = testUtils.sinon.spy( linkFeature.formView.urlInputView, 'select' );
  88. editor.editing.view.isFocused = true;
  89. linkButton.fire( 'execute' );
  90. expect( selectUrlInputSpy.calledOnce ).to.true;
  91. } );
  92. } );
  93. describe( 'unlink toolbar button', () => {
  94. it( 'should register unlink button', () => {
  95. expect( unlinkButton ).to.instanceOf( ButtonView );
  96. } );
  97. it( 'should bind unlinkButtonView to unlink command', () => {
  98. const command = editor.commands.get( 'unlink' );
  99. command.isEnabled = true;
  100. expect( unlinkButton.isEnabled ).to.be.true;
  101. command.isEnabled = false;
  102. expect( unlinkButton.isEnabled ).to.be.false;
  103. } );
  104. it( 'should execute unlink command on unlinkButtonView execute event', () => {
  105. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  106. unlinkButton.fire( 'execute' );
  107. expect( executeSpy.calledOnce ).to.true;
  108. expect( executeSpy.calledWithExactly( 'unlink' ) ).to.true;
  109. } );
  110. } );
  111. describe( 'link balloon panel', () => {
  112. let hidePanelSpy, focusEditableSpy;
  113. beforeEach( () => {
  114. hidePanelSpy = testUtils.sinon.spy( balloonPanelView, 'hide' );
  115. focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  116. } );
  117. it( 'should be created', () => {
  118. expect( balloonPanelView ).to.instanceOf( BalloonPanelView );
  119. } );
  120. it( 'should be appended to the document body', () => {
  121. expect( document.body.contains( balloonPanelView.element ) );
  122. } );
  123. it( 'should open with selected url input on `CTRL+K` keystroke', () => {
  124. const selectUrlInputSpy = testUtils.sinon.spy( linkFeature.formView.urlInputView, 'select' );
  125. editor.keystrokes.press( { keyCode: keyCodes.k, ctrlKey: true } );
  126. expect( balloonPanelView.isVisible ).to.true;
  127. expect( selectUrlInputSpy.calledOnce ).to.true;
  128. } );
  129. it( 'should add balloon panel element to focus tracker', () => {
  130. editor.ui.focusTracker.isFocused = false;
  131. balloonPanelView.element.dispatchEvent( new Event( 'focus' ) );
  132. expect( editor.ui.focusTracker.isFocused ).to.true;
  133. } );
  134. it( 'should focus the link form on Tab key press', () => {
  135. const keyEvtData = {
  136. keyCode: keyCodes.tab,
  137. preventDefault: sinon.spy(),
  138. stopPropagation: sinon.spy()
  139. };
  140. // Mock balloon invisible, form not focused.
  141. balloonPanelView.isVisible = false;
  142. formView.focusTracker.isFocused = false;
  143. const spy = sinon.spy( formView, 'focus' );
  144. editor.keystrokes.press( keyEvtData );
  145. sinon.assert.notCalled( keyEvtData.preventDefault );
  146. sinon.assert.notCalled( keyEvtData.stopPropagation );
  147. sinon.assert.notCalled( spy );
  148. // Mock balloon visible, form focused.
  149. balloonPanelView.isVisible = true;
  150. formView.focusTracker.isFocused = true;
  151. editor.keystrokes.press( keyEvtData );
  152. sinon.assert.notCalled( keyEvtData.preventDefault );
  153. sinon.assert.notCalled( keyEvtData.stopPropagation );
  154. sinon.assert.notCalled( spy );
  155. // Mock balloon visible, form not focused.
  156. balloonPanelView.isVisible = true;
  157. formView.focusTracker.isFocused = false;
  158. editor.keystrokes.press( keyEvtData );
  159. sinon.assert.calledOnce( keyEvtData.preventDefault );
  160. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  161. sinon.assert.calledOnce( spy );
  162. } );
  163. describe( 'close listeners', () => {
  164. describe( 'keyboard', () => {
  165. it( 'should close after Esc key press (from editor)', () => {
  166. const keyEvtData = {
  167. keyCode: keyCodes.esc,
  168. preventDefault: sinon.spy(),
  169. stopPropagation: sinon.spy()
  170. };
  171. balloonPanelView.isVisible = false;
  172. editor.keystrokes.press( keyEvtData );
  173. sinon.assert.notCalled( hidePanelSpy );
  174. sinon.assert.notCalled( focusEditableSpy );
  175. balloonPanelView.isVisible = true;
  176. editor.keystrokes.press( keyEvtData );
  177. sinon.assert.calledOnce( hidePanelSpy );
  178. sinon.assert.calledOnce( focusEditableSpy );
  179. } );
  180. it( 'should close after Esc key press (from the form)', () => {
  181. const keyEvtData = {
  182. keyCode: keyCodes.esc,
  183. preventDefault: sinon.spy(),
  184. stopPropagation: sinon.spy()
  185. };
  186. formView.keystrokes.press( keyEvtData );
  187. sinon.assert.calledOnce( hidePanelSpy );
  188. sinon.assert.calledOnce( focusEditableSpy );
  189. } );
  190. } );
  191. describe( 'mouse', () => {
  192. it( 'should close and not focus editable on click outside the panel', () => {
  193. balloonPanelView.isVisible = true;
  194. document.body.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
  195. expect( hidePanelSpy.calledOnce ).to.true;
  196. expect( focusEditableSpy.notCalled ).to.true;
  197. } );
  198. it( 'should not close on click inside the panel', () => {
  199. balloonPanelView.isVisible = true;
  200. balloonPanelView.element.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
  201. expect( hidePanelSpy.notCalled ).to.true;
  202. } );
  203. } );
  204. } );
  205. describe( 'click on editable', () => {
  206. it( 'should open with not selected url input when collapsed selection is inside link element', () => {
  207. const selectUrlInputSpy = testUtils.sinon.spy( linkFeature.formView.urlInputView, 'select' );
  208. const observer = editor.editing.view.getObserver( ClickObserver );
  209. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  210. setModelData( editor.document, '<$text linkHref="url">fo[]o</$text>' );
  211. observer.fire( 'click', { target: document.body } );
  212. expect( balloonPanelView.isVisible ).to.true;
  213. expect( selectUrlInputSpy.notCalled ).to.true;
  214. } );
  215. it( 'should keep open and update position until collapsed selection stay inside the same link element', () => {
  216. const observer = editor.editing.view.getObserver( ClickObserver );
  217. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  218. setModelData( editor.document, '<$text linkHref="url">b[]ar</$text>' );
  219. const root = editor.editing.view.getRoot();
  220. const text = root.getChild( 0 ).getChild( 0 );
  221. observer.fire( 'click', { target: document.body } );
  222. expect( balloonPanelView.isVisible ).to.true;
  223. const attachToSpy = testUtils.sinon.spy( balloonPanelView, 'attachTo' );
  224. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 1 ) ], true );
  225. editor.editing.view.render();
  226. expect( balloonPanelView.isVisible ).to.true;
  227. expect( attachToSpy.calledOnce ).to.true;
  228. } );
  229. it( 'should close when selection goes outside the link element', () => {
  230. const observer = editor.editing.view.getObserver( ClickObserver );
  231. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  232. setModelData( editor.document, 'foo <$text linkHref="url">b[]ar</$text>' );
  233. const root = editor.editing.view.getRoot();
  234. const text = root.getChild( 0 );
  235. observer.fire( 'click', { target: document.body } );
  236. expect( balloonPanelView.isVisible ).to.true;
  237. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 3, text, 3 ) ], true );
  238. editor.editing.view.render();
  239. expect( balloonPanelView.isVisible ).to.false;
  240. } );
  241. it( 'should close when selection goes to the other link element with the same href', () => {
  242. const observer = editor.editing.view.getObserver( ClickObserver );
  243. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  244. setModelData( editor.document, '<$text linkHref="url">f[]oo</$text> bar <$text linkHref="url">biz</$text>' );
  245. const root = editor.editing.view.getRoot();
  246. const text = root.getChild( 2 ).getChild( 0 );
  247. observer.fire( 'click', { target: document.body } );
  248. expect( balloonPanelView.isVisible ).to.true;
  249. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 1 ) ], true );
  250. editor.editing.view.render();
  251. expect( balloonPanelView.isVisible ).to.false;
  252. } );
  253. it( 'should close when selection becomes non-collapsed', () => {
  254. const observer = editor.editing.view.getObserver( ClickObserver );
  255. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  256. setModelData( editor.document, '<$text linkHref="url">f[]oo</$text>' );
  257. const root = editor.editing.view.getRoot();
  258. const text = root.getChild( 0 ).getChild( 0 );
  259. observer.fire( 'click', { target: {} } );
  260. expect( balloonPanelView.isVisible ).to.true;
  261. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 2 ) ] );
  262. editor.editing.view.render();
  263. expect( balloonPanelView.isVisible ).to.false;
  264. } );
  265. it( 'should stop updating position after close', () => {
  266. const observer = editor.editing.view.getObserver( ClickObserver );
  267. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  268. setModelData( editor.document, '<$text linkHref="url">b[]ar</$text>' );
  269. const root = editor.editing.view.getRoot();
  270. const text = root.getChild( 0 ).getChild( 0 );
  271. observer.fire( 'click', { target: {} } );
  272. expect( balloonPanelView.isVisible ).to.true;
  273. balloonPanelView.isVisible = false;
  274. const attachToSpy = testUtils.sinon.spy( balloonPanelView, 'attachTo' );
  275. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 2, text, 2 ) ], true );
  276. editor.editing.view.render();
  277. expect( attachToSpy.notCalled ).to.true;
  278. } );
  279. it( 'should not open when selection is not inside link element', () => {
  280. const observer = editor.editing.view.getObserver( ClickObserver );
  281. setModelData( editor.document, '[]' );
  282. observer.fire( 'click', { target: {} } );
  283. expect( balloonPanelView.isVisible ).to.false;
  284. } );
  285. it( 'should not open when selection is non-collapsed', () => {
  286. const observer = editor.editing.view.getObserver( ClickObserver );
  287. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  288. setModelData( editor.document, '<$text linkHref="url">f[o]o</$text>' );
  289. observer.fire( 'click', { target: document.body } );
  290. expect( balloonPanelView.isVisible ).to.false;
  291. } );
  292. } );
  293. } );
  294. describe( 'link form', () => {
  295. let hidePanelSpy, focusEditableSpy;
  296. beforeEach( () => {
  297. hidePanelSpy = testUtils.sinon.spy( balloonPanelView, 'hide' );
  298. focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  299. } );
  300. describe( 'binding', () => {
  301. it( 'should bind formView.urlInputView#value to link command value', () => {
  302. const command = editor.commands.get( 'link' );
  303. expect( formView.urlInputView.value ).to.undefined;
  304. command.value = 'http://cksource.com';
  305. expect( formView.urlInputView.value ).to.equal( 'http://cksource.com' );
  306. } );
  307. it( 'should execute link command on formView#submit event', () => {
  308. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  309. formView.urlInputView.value = 'http://ckeditor.com';
  310. expect( formView.urlInputView.inputView.element.value ).to.equal( 'http://ckeditor.com' );
  311. formView.urlInputView.inputView.element.value = 'http://cksource.com';
  312. formView.fire( 'submit' );
  313. expect( executeSpy.calledOnce ).to.true;
  314. expect( executeSpy.calledWithExactly( 'link', 'http://cksource.com' ) ).to.true;
  315. } );
  316. it( 'should hide and focus editable on formView#submit event', () => {
  317. formView.fire( 'submit' );
  318. expect( hidePanelSpy.calledOnce ).to.true;
  319. expect( focusEditableSpy.calledOnce ).to.true;
  320. } );
  321. it( 'should execute unlink command on formView#unlink event', () => {
  322. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  323. formView.fire( 'unlink' );
  324. expect( executeSpy.calledOnce ).to.true;
  325. expect( executeSpy.calledWithExactly( 'unlink' ) ).to.true;
  326. } );
  327. it( 'should hide and focus editable on formView#unlink event', () => {
  328. formView.fire( 'unlink' );
  329. expect( hidePanelSpy.calledOnce ).to.true;
  330. expect( focusEditableSpy.calledOnce ).to.true;
  331. } );
  332. it( 'should hide and focus editable on formView#cancel event', () => {
  333. formView.fire( 'cancel' );
  334. expect( hidePanelSpy.calledOnce ).to.true;
  335. expect( focusEditableSpy.calledOnce ).to.true;
  336. } );
  337. } );
  338. } );
  339. } );