link.js 17 KB

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