link.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 = 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 = 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. } );
  85. describe( 'unlink toolbar button', () => {
  86. it( 'should register unlink feature component', () => {
  87. expect( unlinkButton ).to.instanceOf( Button );
  88. } );
  89. it( 'should bind unlinkButton#model to unlink command', () => {
  90. const model = unlinkButton.model;
  91. const command = editor.commands.get( 'unlink' );
  92. expect( model.isEnabled ).to.false;
  93. command.hasValue = true;
  94. expect( model.isEnabled ).to.true;
  95. } );
  96. it( 'should execute unlink command on unlinkButton#model execute event', () => {
  97. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  98. unlinkButton.model.fire( 'execute' );
  99. expect( executeSpy.calledOnce ).to.true;
  100. expect( executeSpy.calledWithExactly( 'unlink' ) ).to.true;
  101. } );
  102. it( 'should hide panel on unlinkButton#model execute event', () => {
  103. balloonPanel.view.model.isVisible = true;
  104. unlinkButton.model.fire( 'execute' );
  105. expect( balloonPanel.view.model.isVisible ).to.false;
  106. } );
  107. } );
  108. describe( 'link balloon panel', () => {
  109. it( 'should create LinkBalloonPanel component', () => {
  110. expect( balloonPanel ).to.instanceOf( LinkBalloonPanel );
  111. } );
  112. it( 'should bind balloonPanel#model to link command', () => {
  113. const model = balloonPanel.model;
  114. const command = editor.commands.get( 'link' );
  115. expect( model.url ).to.undefined;
  116. command.value = 'http://cksource.com';
  117. expect( model.url ).to.equal( 'http://cksource.com' );
  118. } );
  119. it( 'should execute link command on balloonPanel#model executeLink event', () => {
  120. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  121. balloonPanel.model.url = 'http://cksource.com';
  122. balloonPanel.model.fire( 'executeLink' );
  123. expect( executeSpy.calledOnce ).to.true;
  124. expect( executeSpy.calledWithExactly( 'link', 'http://cksource.com' ) ).to.true;
  125. } );
  126. it( 'should hide balloon panel on balloonPanel#model execute event', () => {
  127. const hideSpy = testUtils.sinon.spy( balloonPanel.view, 'hide' );
  128. balloonPanel.model.fire( 'executeLink' );
  129. expect( hideSpy.calledOnce ).to.true;
  130. } );
  131. it( 'should execute unlink command on balloonPanel#model executeUnlink event', () => {
  132. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  133. balloonPanel.model.fire( 'executeUnlink' );
  134. expect( executeSpy.calledOnce ).to.true;
  135. expect( executeSpy.calledWithExactly( 'unlink' ) ).to.true;
  136. } );
  137. it( 'should hide balloon panel on balloonPanel#model executeUnlink event', () => {
  138. const hideSpy = testUtils.sinon.spy( balloonPanel.view, 'hide' );
  139. balloonPanel.model.fire( 'executeUnlink' );
  140. expect( hideSpy.calledOnce ).to.true;
  141. } );
  142. it( 'should append panel element to the body', () => {
  143. expect( document.body.contains( balloonPanel.view.element ) );
  144. } );
  145. it( 'should open panel on `CTRL+L` keystroke', () => {
  146. editor.keystrokes.press( { keyCode: keyCodes.l, ctrlKey: true } );
  147. expect( balloonPanel.view.model.isVisible ).to.true;
  148. } );
  149. it( 'should focus editor on balloonPanel hide', () => {
  150. const focusSpy = sinon.spy( editor.editing.view, 'focus' );
  151. balloonPanel.view.model.isVisible = true;
  152. balloonPanel.view.model.isVisible = false;
  153. expect( focusSpy.calledOnce ).to.true;
  154. } );
  155. it( 'should hide panel on editor focus event', () => {
  156. balloonPanel.view.model.isVisible = true;
  157. editor.editing.view.fire( 'focus' );
  158. expect( balloonPanel.view.model.isVisible ).to.false;
  159. } );
  160. it( 'should open panel on editor click, when selection is inside link element', () => {
  161. const observer = editor.editing.view.getObserver( ClickObserver );
  162. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  163. setModelData( editor.document, '<$text linkHref="url">some[] url</$text>' );
  164. observer.fire( 'click', { target: document.body } );
  165. expect( balloonPanel.view.model.isVisible ).to.true;
  166. } );
  167. it( 'should not open panel on editor click, when selection is not inside link element', () => {
  168. const observer = editor.editing.view.getObserver( ClickObserver );
  169. setModelData( editor.document, '[]' );
  170. observer.fire( 'click', { target: document.body } );
  171. expect( balloonPanel.view.model.isVisible ).to.false;
  172. } );
  173. } );
  174. } );