link.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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', () => {
  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.hasValue = 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+L` keystroke', () => {
  117. const selectUrlInputSpy = testUtils.sinon.spy( balloonPanel.urlInput.view, 'select' );
  118. editor.keystrokes.press( { keyCode: keyCodes.l, 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 listen keyboard events when is open', () => {
  162. const escCloseSpy = testUtils.sinon.spy( linkFeature, '_closePanelOnEsc' );
  163. balloonPanel.view.model.isVisible = true;
  164. document.dispatchEvent( new Event( 'keydown' ) );
  165. expect( escCloseSpy.calledOnce ).to.true;
  166. } );
  167. it( 'should not listen keyboard events when is closed', () => {
  168. const escCloseSpy = testUtils.sinon.spy( linkFeature, '_closePanelOnEsc' );
  169. balloonPanel.view.model.isVisible = false;
  170. document.dispatchEvent( new Event( 'keydown' ) );
  171. expect( escCloseSpy.calledOnce ).to.false;
  172. } );
  173. it( 'should stop listening keyboard events after close', () => {
  174. const escCloseSpy = testUtils.sinon.spy( linkFeature, '_closePanelOnEsc' );
  175. balloonPanel.view.model.isVisible = true;
  176. balloonPanel.view.model.isVisible = false;
  177. document.dispatchEvent( new Event( 'keydown' ) );
  178. expect( escCloseSpy.notCalled ).to.true;
  179. } );
  180. } );
  181. describe( 'mouse', () => {
  182. it( 'should not close on click inside the panel', () => {
  183. balloonPanel.view.model.isVisible = true;
  184. balloonPanel.view.element.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
  185. expect( hidePanelSpy.notCalled ).to.true;
  186. } );
  187. it( 'should close and not focus editable on click outside the panel', () => {
  188. balloonPanel.view.model.isVisible = true;
  189. document.dispatchEvent( new Event( 'mouseup' ) );
  190. expect( hidePanelSpy.calledOnce ).to.true;
  191. expect( focusEditableSpy.notCalled ).to.true;
  192. } );
  193. it( 'should not listen mouse events before open', () => {
  194. balloonPanel.view.model.isVisible = false;
  195. document.dispatchEvent( new Event( 'mouseup' ) );
  196. expect( hidePanelSpy.notCalled ).to.true;
  197. } );
  198. it( 'should stop listening mouse events after closed', () => {
  199. balloonPanel.view.model.isVisible = true;
  200. balloonPanel.view.model.isVisible = false;
  201. document.dispatchEvent( new Event( 'mouseup' ) );
  202. expect( hidePanelSpy.notCalled ).to.true;
  203. } );
  204. } );
  205. } );
  206. describe( 'click on editable', () => {
  207. it( 'should open with not selected url input when collapsed selection is inside link element', () => {
  208. const selectUrlInputSpy = testUtils.sinon.spy( balloonPanel.urlInput.view, 'select' );
  209. const observer = editor.editing.view.getObserver( ClickObserver );
  210. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  211. setModelData( editor.document, '<$text linkHref="url">fo[]o</$text>' );
  212. observer.fire( 'click', { target: document.body } );
  213. expect( balloonPanel.view.model.isVisible ).to.true;
  214. expect( selectUrlInputSpy.notCalled ).to.true;
  215. } );
  216. it( 'should keep open and update position until collapsed selection stay inside the same link element', () => {
  217. const observer = editor.editing.view.getObserver( ClickObserver );
  218. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  219. setModelData( editor.document, '<$text linkHref="url">b[]ar</$text>' );
  220. const root = editor.editing.view.getRoot();
  221. const text = root.getChild( 0 ).getChild( 0 );
  222. observer.fire( 'click', { target: document.body } );
  223. expect( balloonPanel.view.model.isVisible ).to.true;
  224. const attachToSpy = testUtils.sinon.spy( balloonPanel.view, 'attachTo' );
  225. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 1 ) ], true );
  226. editor.editing.view.render();
  227. expect( balloonPanel.view.model.isVisible ).to.true;
  228. expect( attachToSpy.calledOnce ).to.true;
  229. } );
  230. it( 'should close when selection goes outside the link element', () => {
  231. const observer = editor.editing.view.getObserver( ClickObserver );
  232. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  233. setModelData( editor.document, 'foo <$text linkHref="url">b[]ar</$text>' );
  234. const root = editor.editing.view.getRoot();
  235. const text = root.getChild( 0 );
  236. observer.fire( 'click', { target: document.body } );
  237. expect( balloonPanel.view.model.isVisible ).to.true;
  238. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 3, text, 3 ) ], true );
  239. editor.editing.view.render();
  240. expect( balloonPanel.view.model.isVisible ).to.false;
  241. } );
  242. it( 'should close when selection goes to the other link element with the same href', () => {
  243. const observer = editor.editing.view.getObserver( ClickObserver );
  244. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  245. setModelData( editor.document, '<$text linkHref="url">f[]oo</$text> bar <$text linkHref="url">biz</$text>' );
  246. const root = editor.editing.view.getRoot();
  247. const text = root.getChild( 2 ).getChild( 0 );
  248. observer.fire( 'click', { target: document.body } );
  249. expect( balloonPanel.view.model.isVisible ).to.true;
  250. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 1 ) ], true );
  251. editor.editing.view.render();
  252. expect( balloonPanel.view.model.isVisible ).to.false;
  253. } );
  254. it( 'should close when selection becomes non-collapsed', () => {
  255. const observer = editor.editing.view.getObserver( ClickObserver );
  256. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  257. setModelData( editor.document, '<$text linkHref="url">f[]oo</$text>' );
  258. const root = editor.editing.view.getRoot();
  259. const text = root.getChild( 0 ).getChild( 0 );
  260. observer.fire( 'click', { target: {} } );
  261. expect( balloonPanel.view.model.isVisible ).to.true;
  262. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 2 ) ] );
  263. editor.editing.view.render();
  264. expect( balloonPanel.view.model.isVisible ).to.false;
  265. } );
  266. it( 'should stop updating position after close', () => {
  267. const observer = editor.editing.view.getObserver( ClickObserver );
  268. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  269. setModelData( editor.document, '<$text linkHref="url">b[]ar</$text>' );
  270. const root = editor.editing.view.getRoot();
  271. const text = root.getChild( 0 ).getChild( 0 );
  272. observer.fire( 'click', { target: {} } );
  273. expect( balloonPanel.view.model.isVisible ).to.true;
  274. balloonPanel.view.model.isVisible = false;
  275. const attachToSpy = testUtils.sinon.spy( balloonPanel.view, 'attachTo' );
  276. editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 2, text, 2 ) ], true );
  277. editor.editing.view.render();
  278. expect( attachToSpy.notCalled ).to.true;
  279. } );
  280. it( 'should not open when selection is not inside link element', () => {
  281. const observer = editor.editing.view.getObserver( ClickObserver );
  282. setModelData( editor.document, '[]' );
  283. observer.fire( 'click', { target: {} } );
  284. expect( balloonPanel.view.model.isVisible ).to.false;
  285. } );
  286. it( 'should not open when selection is non-collapsed', () => {
  287. const observer = editor.editing.view.getObserver( ClickObserver );
  288. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  289. setModelData( editor.document, '<$text linkHref="url">f[o]o</$text>' );
  290. observer.fire( 'click', { target: document.body } );
  291. expect( balloonPanel.view.model.isVisible ).to.false;
  292. } );
  293. } );
  294. describe( '_closePanelOnEsc', () => {
  295. it( 'should hide panel and focus editable on ESC press', () => {
  296. const eventInfo = {};
  297. const domEvent = { keyCode: 27 };
  298. linkFeature._closePanelOnEsc( eventInfo, domEvent );
  299. expect( hidePanelSpy.calledOnce ).to.true;
  300. expect( focusEditableSpy.calledOnce ).to.true;
  301. } );
  302. } );
  303. } );
  304. } );