8
0

link.js 14 KB

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