link.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 ClickObserver from '/ckeditor5/engine/view/observer/clickobserver.js';
  15. testUtils.createSinonSandbox();
  16. describe( 'Link', () => {
  17. let editor, linkFeature, linkButton, unlinkButton, balloonPanel, editorElement;
  18. beforeEach( () => {
  19. editorElement = document.createElement( 'div' );
  20. document.body.appendChild( editorElement );
  21. return ClassicTestEditor.create( editorElement, {
  22. features: [ Link ]
  23. } )
  24. .then( newEditor => {
  25. newEditor.editing.view.attachDomRoot( editorElement );
  26. editor = newEditor;
  27. linkFeature = editor.plugins.get( Link );
  28. linkButton = editor.ui.featureComponents.create( 'link' );
  29. unlinkButton = editor.ui.featureComponents.create( 'unlink' );
  30. balloonPanel = linkFeature.balloonPanel;
  31. } );
  32. } );
  33. afterEach( () => {
  34. return editor.destroy();
  35. } );
  36. it( 'should be loaded', () => {
  37. expect( linkFeature ).to.instanceOf( Link );
  38. } );
  39. it( 'should load LinkEngine', () => {
  40. expect( editor.plugins.get( LinkEngine ) ).to.instanceOf( LinkEngine );
  41. } );
  42. it( 'should register click observer', () => {
  43. expect( editor.editing.view.getObserver( ClickObserver ) ).to.instanceOf( ClickObserver );
  44. } );
  45. describe( 'link toolbar button', () => {
  46. it( 'should register link feature component', () => {
  47. expect( linkButton ).to.instanceOf( Button );
  48. } );
  49. it( 'should bind linkButton#model to link command', () => {
  50. const model = linkButton.model;
  51. const command = editor.commands.get( 'link' );
  52. expect( model.isEnabled ).to.be.true;
  53. command.isEnabled = false;
  54. expect( model.isEnabled ).to.be.false;
  55. } );
  56. it( 'should open panel on linkButton#model execute event, when editor is focused', () => {
  57. editor.editing.view.isFocused = true;
  58. linkButton.model.fire( 'execute' );
  59. expect( linkFeature.balloonPanel.view.isVisible ).to.true;
  60. } );
  61. it( 'should not open panel on linkButton#model execute event, when editor is not focused', () => {
  62. editor.editing.view.isFocused = false;
  63. linkButton.model.fire( 'execute' );
  64. expect( linkFeature.balloonPanel.view.isVisible ).to.false;
  65. } );
  66. it( 'should open panel attached to the link element, when collapsed selection is inside link element', () => {
  67. const attachToSpy = testUtils.sinon.spy( balloonPanel.view, 'attachTo' );
  68. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  69. setModelData( editor.document, '<$text linkHref="url">some[] url</$text>' );
  70. editor.editing.view.isFocused = true;
  71. linkButton.model.fire( 'execute' );
  72. const linkElement = editorElement.querySelector( 'a' );
  73. expect( attachToSpy.calledWithExactly( linkElement, editorElement ) ).to.true;
  74. } );
  75. it( 'should open panel attached to the selection, when there is non-collapsed selection', () => {
  76. const attachToSpy = testUtils.sinon.spy( balloonPanel.view, 'attachTo' );
  77. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  78. setModelData( editor.document, 'so[me ur]l' );
  79. editor.editing.view.isFocused = true;
  80. linkButton.model.fire( 'execute' );
  81. const selectedRange = editorElement.ownerDocument.getSelection().getRangeAt( 0 );
  82. expect( attachToSpy.calledWithExactly( selectedRange, editorElement ) ).to.true;
  83. } );
  84. it( 'should select panel input value when panel is opened', () => {
  85. const selectUrlInputSpy = testUtils.sinon.spy( balloonPanel.urlInput.view, 'select' );
  86. editor.editing.view.isFocused = true;
  87. linkButton.model.fire( 'execute' );
  88. expect( selectUrlInputSpy.calledOnce ).to.true;
  89. } );
  90. } );
  91. describe( 'unlink toolbar button', () => {
  92. it( 'should register unlink feature component', () => {
  93. expect( unlinkButton ).to.instanceOf( Button );
  94. } );
  95. it( 'should bind unlinkButton#model to unlink command', () => {
  96. const model = unlinkButton.model;
  97. const command = editor.commands.get( 'unlink' );
  98. expect( model.isEnabled ).to.false;
  99. command.hasValue = true;
  100. expect( model.isEnabled ).to.true;
  101. } );
  102. it( 'should execute unlink command on unlinkButton#model execute event', () => {
  103. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  104. unlinkButton.model.fire( 'execute' );
  105. expect( executeSpy.calledOnce ).to.true;
  106. expect( executeSpy.calledWithExactly( 'unlink' ) ).to.true;
  107. } );
  108. } );
  109. describe( 'link balloon panel', () => {
  110. let hidePanelSpy, focusEditableSpy;
  111. beforeEach( () => {
  112. hidePanelSpy = testUtils.sinon.spy( balloonPanel.view, 'hide' );
  113. focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  114. } );
  115. it( 'should be created', () => {
  116. expect( balloonPanel ).to.instanceOf( LinkBalloonPanel );
  117. } );
  118. it( 'should be appended to the document body', () => {
  119. expect( document.body.contains( balloonPanel.view.element ) );
  120. } );
  121. it( 'should open with selected url input on `CTRL+L` keystroke', () => {
  122. const selectUrlInputSpy = testUtils.sinon.spy( balloonPanel.urlInput.view, 'select' );
  123. editor.keystrokes.press( { keyCode: keyCodes.l, ctrlKey: true } );
  124. expect( balloonPanel.view.model.isVisible ).to.true;
  125. expect( selectUrlInputSpy.calledOnce ).to.true;
  126. } );
  127. describe( 'binding', () => {
  128. it( 'should bind balloonPanel#model to link command', () => {
  129. const model = balloonPanel.model;
  130. const command = editor.commands.get( 'link' );
  131. expect( model.url ).to.undefined;
  132. command.value = 'http://cksource.com';
  133. expect( model.url ).to.equal( 'http://cksource.com' );
  134. } );
  135. it( 'should execute link command on balloonPanel#model executeLink event', () => {
  136. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  137. balloonPanel.model.url = 'http://cksource.com';
  138. balloonPanel.model.fire( 'executeLink' );
  139. expect( executeSpy.calledOnce ).to.true;
  140. expect( executeSpy.calledWithExactly( 'link', 'http://cksource.com' ) ).to.true;
  141. } );
  142. it( 'should hide and focus editable on balloonPanel#model executeLink event', () => {
  143. balloonPanel.model.fire( 'executeLink' );
  144. expect( hidePanelSpy.calledOnce ).to.true;
  145. expect( focusEditableSpy.calledOnce ).to.true;
  146. } );
  147. it( 'should execute unlink command on balloonPanel#model executeUnlink event', () => {
  148. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  149. balloonPanel.model.fire( 'executeUnlink' );
  150. expect( executeSpy.calledOnce ).to.true;
  151. expect( executeSpy.calledWithExactly( 'unlink' ) ).to.true;
  152. } );
  153. it( 'should hide and focus editable on balloonPanel#model executeUnlink event', () => {
  154. balloonPanel.model.fire( 'executeUnlink' );
  155. expect( hidePanelSpy.calledOnce ).to.true;
  156. expect( focusEditableSpy.calledOnce ).to.true;
  157. } );
  158. it( 'should hide and focus editable on balloonPanel#model executeCancel event', () => {
  159. balloonPanel.model.fire( 'executeCancel' );
  160. expect( hidePanelSpy.calledOnce ).to.true;
  161. expect( focusEditableSpy.calledOnce ).to.true;
  162. } );
  163. } );
  164. describe( 'click on editable', () => {
  165. it( 'should open with not selected url input when selection is inside link element', () => {
  166. const selectUrlInputSpy = testUtils.sinon.spy( balloonPanel.urlInput.view, 'select' );
  167. const observer = editor.editing.view.getObserver( ClickObserver );
  168. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  169. setModelData( editor.document, '<$text linkHref="url">some[] url</$text>' );
  170. observer.fire( 'click', { target: document.body } );
  171. expect( balloonPanel.view.model.isVisible ).to.true;
  172. expect( selectUrlInputSpy.notCalled ).to.true;
  173. } );
  174. it( 'should not open panel when selection is not inside link element', () => {
  175. const observer = editor.editing.view.getObserver( ClickObserver );
  176. setModelData( editor.document, '[]' );
  177. observer.fire( 'click', { target: document.body } );
  178. expect( balloonPanel.view.model.isVisible ).to.false;
  179. } );
  180. } );
  181. } );
  182. } );