8
0

link.js 18 KB

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