link.js 15 KB

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