link.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 select panel input value when panel is opened', () => {
  62. const selectSpy = sinon.spy( linkFeature.balloonPanel.urlInput.view, 'select' );
  63. editor.editing.view.isFocused = true;
  64. linkButton.model.fire( 'execute' );
  65. expect( selectSpy.calledOnce ).to.true;
  66. selectSpy.restore();
  67. } );
  68. it( 'should not open panel on linkButton#model execute event, when editor is not focused', () => {
  69. editor.editing.view.isFocused = false;
  70. linkButton.model.fire( 'execute' );
  71. expect( linkFeature.balloonPanel.view.isVisible ).to.false;
  72. } );
  73. it( 'should open panel attached to the link element, when collapsed selection is inside link element', () => {
  74. const attachToSpy = sinon.spy( balloonPanel.view, 'attachTo' );
  75. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  76. setModelData( editor.document, '<$text linkHref="url">some[] url</$text>' );
  77. editor.editing.view.isFocused = true;
  78. linkButton.model.fire( 'execute' );
  79. const linkElement = editorElement.querySelector( 'a' );
  80. expect( attachToSpy.calledWithExactly( linkElement, editorElement ) ).to.true;
  81. } );
  82. it( 'should open panel attached to the selection, when there is non-collapsed selection', () => {
  83. const attachToSpy = sinon.spy( balloonPanel.view, 'attachTo' );
  84. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  85. setModelData( editor.document, 'so[me ur]l' );
  86. editor.editing.view.isFocused = true;
  87. linkButton.model.fire( 'execute' );
  88. const selectedRange = editorElement.ownerDocument.getSelection().getRangeAt( 0 );
  89. expect( attachToSpy.calledWithExactly( selectedRange, editorElement ) ).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. it( 'should hide panel on unlinkButton#model execute event', () => {
  110. balloonPanel.view.model.isVisible = true;
  111. unlinkButton.model.fire( 'execute' );
  112. expect( balloonPanel.view.model.isVisible ).to.false;
  113. } );
  114. } );
  115. describe( 'link balloon panel', () => {
  116. it( 'should create LinkBalloonPanel component', () => {
  117. expect( balloonPanel ).to.instanceOf( LinkBalloonPanel );
  118. } );
  119. it( 'should bind balloonPanel#model to link command', () => {
  120. const model = balloonPanel.model;
  121. const command = editor.commands.get( 'link' );
  122. expect( model.url ).to.undefined;
  123. command.value = 'http://cksource.com';
  124. expect( model.url ).to.equal( 'http://cksource.com' );
  125. } );
  126. it( 'should execute link command on balloonPanel#model executeLink event', () => {
  127. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  128. balloonPanel.model.url = 'http://cksource.com';
  129. balloonPanel.model.fire( 'executeLink' );
  130. expect( executeSpy.calledOnce ).to.true;
  131. expect( executeSpy.calledWithExactly( 'link', 'http://cksource.com' ) ).to.true;
  132. } );
  133. it( 'should hide balloon panel on balloonPanel#model execute event', () => {
  134. const hideSpy = testUtils.sinon.spy( balloonPanel.view, 'hide' );
  135. balloonPanel.model.fire( 'executeLink' );
  136. expect( hideSpy.calledOnce ).to.true;
  137. } );
  138. it( 'should execute unlink command on balloonPanel#model executeUnlink event', () => {
  139. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  140. balloonPanel.model.fire( 'executeUnlink' );
  141. expect( executeSpy.calledOnce ).to.true;
  142. expect( executeSpy.calledWithExactly( 'unlink' ) ).to.true;
  143. } );
  144. it( 'should hide balloon panel on balloonPanel#model executeUnlink event', () => {
  145. const hideSpy = testUtils.sinon.spy( balloonPanel.view, 'hide' );
  146. balloonPanel.model.fire( 'executeUnlink' );
  147. expect( hideSpy.calledOnce ).to.true;
  148. } );
  149. it( 'should append panel element to the body', () => {
  150. expect( document.body.contains( balloonPanel.view.element ) );
  151. } );
  152. it( 'should open panel on `CTRL+L` keystroke', () => {
  153. editor.keystrokes.press( { keyCode: keyCodes.l, ctrlKey: true } );
  154. expect( balloonPanel.view.model.isVisible ).to.true;
  155. } );
  156. it( 'should focus editor on balloonPanel hide', () => {
  157. const focusSpy = sinon.spy( editor.editing.view, 'focus' );
  158. balloonPanel.view.model.isVisible = true;
  159. balloonPanel.view.model.isVisible = false;
  160. expect( focusSpy.calledOnce ).to.true;
  161. } );
  162. it( 'should hide panel on editor focus event', () => {
  163. balloonPanel.view.model.isVisible = true;
  164. editor.editing.view.fire( 'focus' );
  165. expect( balloonPanel.view.model.isVisible ).to.false;
  166. } );
  167. it( 'should open panel on editor click, when selection is inside link element', () => {
  168. const observer = editor.editing.view.getObserver( ClickObserver );
  169. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  170. setModelData( editor.document, '<$text linkHref="url">some[] url</$text>' );
  171. observer.fire( 'click', { target: document.body } );
  172. expect( balloonPanel.view.model.isVisible ).to.true;
  173. } );
  174. it( 'should not open panel on editor click, 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. } );