link.js 14 KB

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