8
0

link.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import ClassicTestEditor from 'tests/core/_utils/classictesteditor.js';
  7. import testUtils from 'tests/core/_utils/utils.js';
  8. import { keyCodes } from 'ckeditor5/utils/keyboard.js';
  9. import { setData as setModelData } from 'ckeditor5/engine/dev-utils/model.js';
  10. import Link from 'ckeditor5/link/link.js';
  11. import LinkEngine from 'ckeditor5/link/linkengine.js';
  12. import ButtonView from 'ckeditor5/ui/button/buttonview.js';
  13. import BalloonPanelView from 'ckeditor5/ui/balloonpanel/balloonpanelview.js';
  14. import Range from 'ckeditor5/engine/view/range.js';
  15. import ClickObserver from 'ckeditor5/engine/view/observer/clickobserver.js';
  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. expect( balloonPanelView.limiter ).to.equal( editorElement );
  70. expect( attachToSpy.calledWithExactly( linkElement ) ).to.true;
  71. } );
  72. it( 'should open panel attached to the selection, when there is non-collapsed selection', () => {
  73. const attachToSpy = testUtils.sinon.spy( balloonPanelView, 'attachTo' );
  74. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  75. setModelData( editor.document, 'so[me ur]l' );
  76. editor.editing.view.isFocused = true;
  77. linkButton.fire( 'execute' );
  78. const selectedRange = editorElement.ownerDocument.getSelection().getRangeAt( 0 );
  79. expect( balloonPanelView.limiter ).to.equal( editorElement );
  80. expect( attachToSpy.calledWithExactly( selectedRange ) ).to.true;
  81. } );
  82. it( 'should select panel input value when panel is opened', () => {
  83. const selectUrlInputSpy = testUtils.sinon.spy( linkFeature.formView.urlInputView, 'select' );
  84. editor.editing.view.isFocused = true;
  85. linkButton.fire( 'execute' );
  86. expect( selectUrlInputSpy.calledOnce ).to.true;
  87. } );
  88. } );
  89. describe( 'unlink toolbar button', () => {
  90. it( 'should register unlink button', () => {
  91. expect( unlinkButton ).to.instanceOf( ButtonView );
  92. } );
  93. it( 'should bind unlinkButtonView to unlink command', () => {
  94. const command = editor.commands.get( 'unlink' );
  95. command.isEnabled = true;
  96. expect( unlinkButton.isEnabled ).to.be.true;
  97. command.isEnabled = false;
  98. expect( unlinkButton.isEnabled ).to.be.false;
  99. } );
  100. it( 'should execute unlink command on unlinkButtonView execute event', () => {
  101. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  102. unlinkButton.fire( 'execute' );
  103. expect( executeSpy.calledOnce ).to.true;
  104. expect( executeSpy.calledWithExactly( 'unlink' ) ).to.true;
  105. } );
  106. } );
  107. describe( 'link balloon panel', () => {
  108. let hidePanelSpy, focusEditableSpy;
  109. beforeEach( () => {
  110. hidePanelSpy = testUtils.sinon.spy( balloonPanelView, 'hide' );
  111. focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  112. } );
  113. it( 'should be created', () => {
  114. expect( balloonPanelView ).to.instanceOf( BalloonPanelView );
  115. } );
  116. it( 'should be appended to the document body', () => {
  117. expect( document.body.contains( balloonPanelView.element ) );
  118. } );
  119. it( 'should open with selected url input on `CTRL+K` keystroke', () => {
  120. const selectUrlInputSpy = testUtils.sinon.spy( linkFeature.formView.urlInputView, 'select' );
  121. editor.keystrokes.press( { keyCode: keyCodes.k, ctrlKey: true } );
  122. expect( balloonPanelView.isVisible ).to.true;
  123. expect( selectUrlInputSpy.calledOnce ).to.true;
  124. } );
  125. it( 'should add balloon panel element to focus tracker', () => {
  126. editor.ui.focusTracker.isFocused = false;
  127. balloonPanelView.element.dispatchEvent( new Event( 'focus' ) );
  128. expect( editor.ui.focusTracker.isFocused ).to.true;
  129. } );
  130. describe( 'close listeners', () => {
  131. describe( 'keyboard', () => {
  132. it( 'should close after `ESC` press', () => {
  133. balloonPanelView.isVisible = true;
  134. dispatchKeyboardEvent( document, 'keydown', keyCodes.esc );
  135. expect( hidePanelSpy.calledOnce ).to.true;
  136. expect( focusEditableSpy.calledOnce ).to.true;
  137. } );
  138. } );
  139. describe( 'mouse', () => {
  140. it( 'should close and not focus editable on click outside the panel', () => {
  141. balloonPanelView.isVisible = true;
  142. document.body.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
  143. expect( hidePanelSpy.calledOnce ).to.true;
  144. expect( focusEditableSpy.notCalled ).to.true;
  145. } );
  146. it( 'should not close on click inside the panel', () => {
  147. balloonPanelView.isVisible = true;
  148. balloonPanelView.element.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
  149. expect( hidePanelSpy.notCalled ).to.true;
  150. } );
  151. } );
  152. } );
  153. describe( 'click on editable', () => {
  154. it( 'should open with not selected url input when collapsed selection is inside link element', () => {
  155. const selectUrlInputSpy = testUtils.sinon.spy( linkFeature.formView.urlInputView, 'select' );
  156. const observer = editor.editing.view.getObserver( ClickObserver );
  157. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  158. setModelData( editor.document, '<$text linkHref="url">fo[]o</$text>' );
  159. observer.fire( 'click', { target: document.body } );
  160. expect( balloonPanelView.isVisible ).to.true;
  161. expect( selectUrlInputSpy.notCalled ).to.true;
  162. } );
  163. it( 'should keep open and update position until collapsed selection stay inside the same link element', () => {
  164. const observer = editor.editing.view.getObserver( ClickObserver );
  165. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  166. setModelData( editor.document, '<$text linkHref="url">b[]ar</$text>' );
  167. const root = editor.editing.view.getRoot();
  168. const text = root.getChild( 0 ).getChild( 0 );
  169. observer.fire( 'click', { target: document.body } );
  170. expect( balloonPanelView.isVisible ).to.true;
  171. const attachToSpy = testUtils.sinon.spy( balloonPanelView, 'attachTo' );
  172. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 1 ) ], true );
  173. editor.editing.view.render();
  174. expect( balloonPanelView.isVisible ).to.true;
  175. expect( attachToSpy.calledOnce ).to.true;
  176. } );
  177. it( 'should close when selection goes outside the link element', () => {
  178. const observer = editor.editing.view.getObserver( ClickObserver );
  179. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  180. setModelData( editor.document, 'foo <$text linkHref="url">b[]ar</$text>' );
  181. const root = editor.editing.view.getRoot();
  182. const text = root.getChild( 0 );
  183. observer.fire( 'click', { target: document.body } );
  184. expect( balloonPanelView.isVisible ).to.true;
  185. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 3, text, 3 ) ], true );
  186. editor.editing.view.render();
  187. expect( balloonPanelView.isVisible ).to.false;
  188. } );
  189. it( 'should close when selection goes to the other link element with the same href', () => {
  190. const observer = editor.editing.view.getObserver( ClickObserver );
  191. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  192. setModelData( editor.document, '<$text linkHref="url">f[]oo</$text> bar <$text linkHref="url">biz</$text>' );
  193. const root = editor.editing.view.getRoot();
  194. const text = root.getChild( 2 ).getChild( 0 );
  195. observer.fire( 'click', { target: document.body } );
  196. expect( balloonPanelView.isVisible ).to.true;
  197. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 1 ) ], true );
  198. editor.editing.view.render();
  199. expect( balloonPanelView.isVisible ).to.false;
  200. } );
  201. it( 'should close when selection becomes non-collapsed', () => {
  202. const observer = editor.editing.view.getObserver( ClickObserver );
  203. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  204. setModelData( editor.document, '<$text linkHref="url">f[]oo</$text>' );
  205. const root = editor.editing.view.getRoot();
  206. const text = root.getChild( 0 ).getChild( 0 );
  207. observer.fire( 'click', { target: {} } );
  208. expect( balloonPanelView.isVisible ).to.true;
  209. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 2 ) ] );
  210. editor.editing.view.render();
  211. expect( balloonPanelView.isVisible ).to.false;
  212. } );
  213. it( 'should stop updating position after close', () => {
  214. const observer = editor.editing.view.getObserver( ClickObserver );
  215. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  216. setModelData( editor.document, '<$text linkHref="url">b[]ar</$text>' );
  217. const root = editor.editing.view.getRoot();
  218. const text = root.getChild( 0 ).getChild( 0 );
  219. observer.fire( 'click', { target: {} } );
  220. expect( balloonPanelView.isVisible ).to.true;
  221. balloonPanelView.isVisible = false;
  222. const attachToSpy = testUtils.sinon.spy( balloonPanelView, 'attachTo' );
  223. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 2, text, 2 ) ], true );
  224. editor.editing.view.render();
  225. expect( attachToSpy.notCalled ).to.true;
  226. } );
  227. it( 'should not open when selection is not inside link element', () => {
  228. const observer = editor.editing.view.getObserver( ClickObserver );
  229. setModelData( editor.document, '[]' );
  230. observer.fire( 'click', { target: {} } );
  231. expect( balloonPanelView.isVisible ).to.false;
  232. } );
  233. it( 'should not open when selection is non-collapsed', () => {
  234. const observer = editor.editing.view.getObserver( ClickObserver );
  235. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  236. setModelData( editor.document, '<$text linkHref="url">f[o]o</$text>' );
  237. observer.fire( 'click', { target: document.body } );
  238. expect( balloonPanelView.isVisible ).to.false;
  239. } );
  240. } );
  241. } );
  242. describe( 'link form', () => {
  243. let hidePanelSpy, focusEditableSpy;
  244. beforeEach( () => {
  245. hidePanelSpy = testUtils.sinon.spy( balloonPanelView, 'hide' );
  246. focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  247. } );
  248. describe( 'binding', () => {
  249. it( 'should bind formView.urlInputView#value to link command value', () => {
  250. const command = editor.commands.get( 'link' );
  251. expect( formView.urlInputView.value ).to.undefined;
  252. command.value = 'http://cksource.com';
  253. expect( formView.urlInputView.value ).to.equal( 'http://cksource.com' );
  254. } );
  255. it( 'should execute link command on formView#submit event', () => {
  256. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  257. formView.urlInputView.value = 'http://ckeditor.com';
  258. expect( formView.urlInputView.inputView.element.value ).to.equal( 'http://ckeditor.com' );
  259. formView.urlInputView.inputView.element.value = 'http://cksource.com';
  260. formView.fire( 'submit' );
  261. expect( executeSpy.calledOnce ).to.true;
  262. expect( executeSpy.calledWithExactly( 'link', 'http://cksource.com' ) ).to.true;
  263. } );
  264. it( 'should hide and focus editable on formView#submit event', () => {
  265. formView.fire( 'submit' );
  266. expect( hidePanelSpy.calledOnce ).to.true;
  267. expect( focusEditableSpy.calledOnce ).to.true;
  268. } );
  269. it( 'should execute unlink command on formView#unlink event', () => {
  270. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  271. formView.fire( 'unlink' );
  272. expect( executeSpy.calledOnce ).to.true;
  273. expect( executeSpy.calledWithExactly( 'unlink' ) ).to.true;
  274. } );
  275. it( 'should hide and focus editable on formView#unlink event', () => {
  276. formView.fire( 'unlink' );
  277. expect( hidePanelSpy.calledOnce ).to.true;
  278. expect( focusEditableSpy.calledOnce ).to.true;
  279. } );
  280. it( 'should hide and focus editable on formView#cancel event', () => {
  281. formView.fire( 'cancel' );
  282. expect( hidePanelSpy.calledOnce ).to.true;
  283. expect( focusEditableSpy.calledOnce ).to.true;
  284. } );
  285. } );
  286. } );
  287. } );
  288. // Creates and dispatches keyboard event with specified keyCode.
  289. //
  290. // @private
  291. // @param {EventTarget} eventTarget
  292. // @param {String} eventName
  293. // @param {Number} keyCode
  294. function dispatchKeyboardEvent( element, eventName, keyCode ) {
  295. const event = document.createEvent( 'Events' );
  296. event.initEvent( eventName, true, true );
  297. event.which = keyCode;
  298. event.keyCode = keyCode;
  299. element.dispatchEvent( event );
  300. }