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 Button from '/ckeditor5/ui/button/button.js';
  13. import BalloonPanel from '/ckeditor5/ui/balloonpanel/balloonpanel.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, balloonPanel, form, 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. balloonPanel = linkFeature.balloonPanel;
  32. form = linkFeature.form;
  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( Button );
  50. } );
  51. it( 'should bind linkButton#model to link command', () => {
  52. const model = linkButton.model;
  53. const command = editor.commands.get( 'link' );
  54. command.isEnabled = true;
  55. expect( model.isEnabled ).to.be.true;
  56. command.isEnabled = false;
  57. expect( model.isEnabled ).to.be.false;
  58. } );
  59. it( 'should open panel on linkButton#model execute event', () => {
  60. linkButton.model.fire( 'execute' );
  61. expect( linkFeature.balloonPanel.view.isVisible ).to.true;
  62. } );
  63. it( 'should open panel attached to the link element, when collapsed selection is inside link element', () => {
  64. const attachToSpy = testUtils.sinon.spy( balloonPanel.view, 'attachTo' );
  65. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  66. setModelData( editor.document, '<$text linkHref="url">some[] url</$text>' );
  67. editor.editing.view.isFocused = true;
  68. linkButton.model.fire( 'execute' );
  69. const linkElement = editorElement.querySelector( 'a' );
  70. expect( attachToSpy.calledWithExactly( linkElement, editorElement ) ).to.true;
  71. } );
  72. it( 'should open panel attached to the selection, when there is non-collapsed selection', () => {
  73. const attachToSpy = testUtils.sinon.spy( balloonPanel.view, '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.model.fire( 'execute' );
  78. const selectedRange = editorElement.ownerDocument.getSelection().getRangeAt( 0 );
  79. expect( attachToSpy.calledWithExactly( selectedRange, editorElement ) ).to.true;
  80. } );
  81. it( 'should select panel input value when panel is opened', () => {
  82. const selectUrlInputSpy = testUtils.sinon.spy( linkFeature.form.urlInput.view, 'select' );
  83. editor.editing.view.isFocused = true;
  84. linkButton.model.fire( 'execute' );
  85. expect( selectUrlInputSpy.calledOnce ).to.true;
  86. } );
  87. } );
  88. describe( 'unlink toolbar button', () => {
  89. it( 'should register unlink feature component', () => {
  90. expect( unlinkButton ).to.instanceOf( Button );
  91. } );
  92. it( 'should bind unlinkButton#model to unlink command', () => {
  93. const model = unlinkButton.model;
  94. const command = editor.commands.get( 'unlink' );
  95. command.isEnabled = true;
  96. expect( model.isEnabled ).to.be.true;
  97. command.isEnabled = false;
  98. expect( model.isEnabled ).to.be.false;
  99. } );
  100. it( 'should execute unlink command on unlinkButton#model execute event', () => {
  101. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  102. unlinkButton.model.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( balloonPanel.view, 'hide' );
  111. focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  112. } );
  113. it( 'should be created', () => {
  114. expect( balloonPanel ).to.instanceOf( BalloonPanel );
  115. } );
  116. it( 'should be appended to the document body', () => {
  117. expect( document.body.contains( balloonPanel.view.element ) );
  118. } );
  119. it( 'should open with selected url input on `CTRL+K` keystroke', () => {
  120. const selectUrlInputSpy = testUtils.sinon.spy( linkFeature.form.urlInput.view, 'select' );
  121. editor.keystrokes.press( { keyCode: keyCodes.k, ctrlKey: true } );
  122. expect( balloonPanel.view.isVisible ).to.true;
  123. expect( selectUrlInputSpy.calledOnce ).to.true;
  124. } );
  125. it( 'should add balloon panel element to focus tracker', () => {
  126. editor.focusTracker.isFocused = false;
  127. balloonPanel.view.element.dispatchEvent( new Event( 'focus' ) );
  128. expect( editor.focusTracker.isFocused ).to.true;
  129. } );
  130. describe( 'close listeners', () => {
  131. describe( 'keyboard', () => {
  132. it( 'should close after `ESC` press', () => {
  133. balloonPanel.view.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. balloonPanel.view.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. balloonPanel.view.isVisible = true;
  148. balloonPanel.view.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.form.urlInput.view, '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( balloonPanel.view.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( balloonPanel.view.isVisible ).to.true;
  171. const attachToSpy = testUtils.sinon.spy( balloonPanel.view, 'attachTo' );
  172. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 1 ) ], true );
  173. editor.editing.view.render();
  174. expect( balloonPanel.view.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( balloonPanel.view.isVisible ).to.true;
  185. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 3, text, 3 ) ], true );
  186. editor.editing.view.render();
  187. expect( balloonPanel.view.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( balloonPanel.view.isVisible ).to.true;
  197. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 1 ) ], true );
  198. editor.editing.view.render();
  199. expect( balloonPanel.view.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( balloonPanel.view.isVisible ).to.true;
  209. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 2 ) ] );
  210. editor.editing.view.render();
  211. expect( balloonPanel.view.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( balloonPanel.view.isVisible ).to.true;
  221. balloonPanel.view.isVisible = false;
  222. const attachToSpy = testUtils.sinon.spy( balloonPanel.view, '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( balloonPanel.view.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( balloonPanel.view.isVisible ).to.false;
  239. } );
  240. } );
  241. } );
  242. describe( 'link form', () => {
  243. let hidePanelSpy, focusEditableSpy;
  244. beforeEach( () => {
  245. hidePanelSpy = testUtils.sinon.spy( balloonPanel.view, 'hide' );
  246. focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  247. } );
  248. describe( 'binding', () => {
  249. it( 'should bind form.model#url to link command value', () => {
  250. const model = linkFeature.form.model;
  251. const command = editor.commands.get( 'link' );
  252. expect( model.value ).to.undefined;
  253. command.value = 'http://cksource.com';
  254. expect( model.url ).to.equal( 'http://cksource.com' );
  255. } );
  256. it( 'should execute link command on form.model#submit event', () => {
  257. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  258. form.model.url = 'http://cksource.com';
  259. form.model.fire( 'submit' );
  260. expect( executeSpy.calledOnce ).to.true;
  261. expect( executeSpy.calledWithExactly( 'link', 'http://cksource.com' ) ).to.true;
  262. } );
  263. it( 'should hide and focus editable on form.model#submit event', () => {
  264. form.model.fire( 'submit' );
  265. expect( hidePanelSpy.calledOnce ).to.true;
  266. expect( focusEditableSpy.calledOnce ).to.true;
  267. } );
  268. it( 'should execute unlink command on form.model#unlink event', () => {
  269. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  270. form.model.fire( 'unlink' );
  271. expect( executeSpy.calledOnce ).to.true;
  272. expect( executeSpy.calledWithExactly( 'unlink' ) ).to.true;
  273. } );
  274. it( 'should hide and focus editable on form.model#unlink event', () => {
  275. form.model.fire( 'unlink' );
  276. expect( hidePanelSpy.calledOnce ).to.true;
  277. expect( focusEditableSpy.calledOnce ).to.true;
  278. } );
  279. it( 'should hide and focus editable on form.model#cancel event', () => {
  280. form.model.fire( 'cancel' );
  281. expect( hidePanelSpy.calledOnce ).to.true;
  282. expect( focusEditableSpy.calledOnce ).to.true;
  283. } );
  284. } );
  285. } );
  286. } );
  287. // Creates and dispatches keyboard event with specified keyCode.
  288. //
  289. // @private
  290. // @param {EventTarget} eventTarget
  291. // @param {String} eventName
  292. // @param {Number} keyCode
  293. function dispatchKeyboardEvent( element, eventName, keyCode ) {
  294. const event = document.createEvent( 'Events' );
  295. event.initEvent( eventName, true, true );
  296. event.which = keyCode;
  297. event.keyCode = keyCode;
  298. element.dispatchEvent( event );
  299. }