8
0

link.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. expect( model.isEnabled ).to.be.true;
  55. command.isEnabled = false;
  56. expect( model.isEnabled ).to.be.false;
  57. } );
  58. it( 'should open panel on linkButton#model execute event', () => {
  59. linkButton.model.fire( 'execute' );
  60. expect( linkFeature.balloonPanel.view.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( balloonPanel.view, '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.model.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( balloonPanel.view, '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.model.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._urlInput.view, 'select' );
  82. editor.editing.view.isFocused = true;
  83. linkButton.model.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( Button );
  90. } );
  91. it( 'should bind unlinkButton#model to unlink command', () => {
  92. const model = unlinkButton.model;
  93. const command = editor.commands.get( 'unlink' );
  94. expect( model.isEnabled ).to.false;
  95. command.isEnabled = true;
  96. expect( model.isEnabled ).to.true;
  97. } );
  98. it( 'should execute unlink command on unlinkButton#model execute event', () => {
  99. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  100. unlinkButton.model.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( balloonPanel.view, 'hide' );
  109. focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  110. } );
  111. it( 'should be created', () => {
  112. expect( balloonPanel ).to.instanceOf( BalloonPanel );
  113. } );
  114. it( 'should be appended to the document body', () => {
  115. expect( document.body.contains( balloonPanel.view.element ) );
  116. } );
  117. it( 'should open with selected url input on `CTRL+K` keystroke', () => {
  118. const selectUrlInputSpy = testUtils.sinon.spy( linkFeature._urlInput.view, 'select' );
  119. editor.keystrokes.press( { keyCode: keyCodes.k, ctrlKey: true } );
  120. expect( balloonPanel.view.isVisible ).to.true;
  121. expect( selectUrlInputSpy.calledOnce ).to.true;
  122. } );
  123. it( 'should add balloon panel element to focus tracker', () => {
  124. editor.focusTracker.isFocused = false;
  125. balloonPanel.view.element.dispatchEvent( new Event( 'focus' ) );
  126. expect( editor.focusTracker.isFocused ).to.true;
  127. } );
  128. describe( 'close listeners', () => {
  129. describe( 'keyboard', () => {
  130. it( 'should close after `ESC` press', () => {
  131. balloonPanel.view.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. balloonPanel.view.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. balloonPanel.view.isVisible = true;
  146. balloonPanel.view.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._urlInput.view, '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( balloonPanel.view.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( balloonPanel.view.isVisible ).to.true;
  169. const attachToSpy = testUtils.sinon.spy( balloonPanel.view, 'attachTo' );
  170. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 1 ) ], true );
  171. editor.editing.view.render();
  172. expect( balloonPanel.view.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( balloonPanel.view.isVisible ).to.true;
  183. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 3, text, 3 ) ], true );
  184. editor.editing.view.render();
  185. expect( balloonPanel.view.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( balloonPanel.view.isVisible ).to.true;
  195. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 1 ) ], true );
  196. editor.editing.view.render();
  197. expect( balloonPanel.view.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( balloonPanel.view.isVisible ).to.true;
  207. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 2 ) ] );
  208. editor.editing.view.render();
  209. expect( balloonPanel.view.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( balloonPanel.view.isVisible ).to.true;
  219. balloonPanel.view.isVisible = false;
  220. const attachToSpy = testUtils.sinon.spy( balloonPanel.view, '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( balloonPanel.view.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( balloonPanel.view.isVisible ).to.false;
  237. } );
  238. } );
  239. } );
  240. describe( 'link form', () => {
  241. let hidePanelSpy, focusEditableSpy;
  242. beforeEach( () => {
  243. hidePanelSpy = testUtils.sinon.spy( balloonPanel.view, 'hide' );
  244. focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  245. } );
  246. describe( 'binding', () => {
  247. it( 'should bind _urlInput#model to link command', () => {
  248. const model = linkFeature._urlInput.model;
  249. const command = editor.commands.get( 'link' );
  250. expect( model.value ).to.undefined;
  251. command.value = 'http://cksource.com';
  252. expect( model.value ).to.equal( 'http://cksource.com' );
  253. } );
  254. it( 'should execute link command on _saveButton#model submit event', () => {
  255. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  256. linkFeature._urlInput.model.value = 'http://cksource.com';
  257. form.model.fire( 'submit' );
  258. expect( executeSpy.calledOnce ).to.true;
  259. expect( executeSpy.calledWithExactly( 'link', 'http://cksource.com' ) ).to.true;
  260. } );
  261. it( 'should hide and focus editable on form#model submit event', () => {
  262. form.model.fire( 'submit' );
  263. expect( hidePanelSpy.calledOnce ).to.true;
  264. expect( focusEditableSpy.calledOnce ).to.true;
  265. } );
  266. it( 'should execute unlink command on _unlinkButton#model execute event', () => {
  267. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  268. linkFeature._unlinkButton.model.fire( 'execute' );
  269. expect( executeSpy.calledOnce ).to.true;
  270. expect( executeSpy.calledWithExactly( 'unlink' ) ).to.true;
  271. } );
  272. it( 'should hide and focus editable on _unlinkButton#model execute event', () => {
  273. linkFeature._unlinkButton.model.fire( 'execute' );
  274. expect( hidePanelSpy.calledOnce ).to.true;
  275. expect( focusEditableSpy.calledOnce ).to.true;
  276. } );
  277. it( 'should hide and focus editable on _cancelButton#model cancel event', () => {
  278. linkFeature._cancelButton.model.fire( 'execute' );
  279. expect( hidePanelSpy.calledOnce ).to.true;
  280. expect( focusEditableSpy.calledOnce ).to.true;
  281. } );
  282. } );
  283. } );
  284. } );
  285. // Creates and dispatches keyboard event with specified keyCode.
  286. //
  287. // @private
  288. // @param {EventTarget} eventTarget
  289. // @param {String} eventName
  290. // @param {Number} keyCode
  291. function dispatchKeyboardEvent( element, eventName, keyCode ) {
  292. const event = document.createEvent( 'Events' );
  293. event.initEvent( eventName, true, true );
  294. event.which = keyCode;
  295. event.keyCode = keyCode;
  296. element.dispatchEvent( event );
  297. }