8
0

link.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. /* bender-tags: link, browser-only */
  7. import ClassicTestEditor from '/tests/core/_utils/classictesteditor.js';
  8. import testUtils from '/tests/core/_utils/utils.js';
  9. import { keyCodes } from '/ckeditor5/utils/keyboard.js';
  10. import { setData as setModelData } from '/tests/engine/_utils/model.js';
  11. import Link from '/ckeditor5/link/link.js';
  12. import LinkEngine from '/ckeditor5/link/linkengine.js';
  13. import Button from '/ckeditor5/ui/button/button.js';
  14. import LinkBalloonPanel from '/ckeditor5/link/ui/linkballoonpanel.js';
  15. import ClickObserver from '/ckeditor5/engine/view/observer/clickobserver.js';
  16. testUtils.createSinonSandbox();
  17. describe( 'Link', () => {
  18. let editor, linkFeature, linkButton, unlinkButton, balloonPanel;
  19. beforeEach( () => {
  20. const editorElement = document.createElement( 'div' );
  21. document.body.appendChild( editorElement );
  22. return ClassicTestEditor.create( editorElement, {
  23. features: [ Link ]
  24. } )
  25. .then( newEditor => {
  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. linkButton.model.fire( 'execute' );
  63. expect( linkFeature.balloonPanel.view.isVisible ).to.false;
  64. } );
  65. } );
  66. describe( 'unlink toolbar button', () => {
  67. it( 'should register unlink feature component', () => {
  68. expect( unlinkButton ).to.instanceOf( Button );
  69. } );
  70. it( 'should bind unlinkButton#model to unlink command', () => {
  71. const model = unlinkButton.model;
  72. const command = editor.commands.get( 'unlink' );
  73. expect( model.isEnabled ).to.false;
  74. command.hasValue = true;
  75. expect( model.isEnabled ).to.true;
  76. } );
  77. it( 'should execute unlink command on unlinkButton#model execute event', () => {
  78. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  79. unlinkButton.model.fire( 'execute' );
  80. expect( executeSpy.calledOnce ).to.true;
  81. expect( executeSpy.calledWithExactly( 'unlink' ) ).to.true;
  82. } );
  83. it( 'should hide panel on unlinkButton#model execute event', () => {
  84. balloonPanel.view.model.isVisible = true;
  85. unlinkButton.model.fire( 'execute' );
  86. expect( balloonPanel.view.model.isVisible ).to.false;
  87. } );
  88. } );
  89. describe( 'link panel', () => {
  90. it( 'should create LinkBalloonPanel component', () => {
  91. expect( balloonPanel ).to.instanceOf( LinkBalloonPanel );
  92. } );
  93. it( 'should bind balloonPanel#model to link command', () => {
  94. const model = balloonPanel.model;
  95. const command = editor.commands.get( 'link' );
  96. expect( model.url ).to.undefined;
  97. command.value = 'http://cksource.com';
  98. expect( model.url ).to.equal( 'http://cksource.com' );
  99. } );
  100. it( 'should execute link command on balloonPanel#model execute event', () => {
  101. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  102. balloonPanel.model.url = 'http://cksource.com';
  103. balloonPanel.model.fire( 'execute' );
  104. expect( executeSpy.calledOnce ).to.true;
  105. expect( executeSpy.calledWithExactly( 'link', 'http://cksource.com' ) ).to.true;
  106. } );
  107. it( 'should append panel element to the body', () => {
  108. expect( document.body.contains( balloonPanel.view.element ) );
  109. } );
  110. it( 'should open panel on `CTRL+L` keystroke', () => {
  111. editor.keystrokes.press( { keyCode: keyCodes.l, ctrlKey: true } );
  112. expect( balloonPanel.view.model.isVisible ).to.true;
  113. } );
  114. it( 'should focus editor on balloonPanel#model hide event', () => {
  115. balloonPanel.model.fire( 'hide' );
  116. expect( editor.editing.view.isFocused ).to.true;
  117. } );
  118. it( 'should hide panel on editor focus event', () => {
  119. balloonPanel.view.model.isVisible = true;
  120. editor.editing.view.focus();
  121. expect( balloonPanel.view.model.isVisible ).to.false;
  122. } );
  123. it( 'should show panel on editor click, when selection is inside link element', () => {
  124. const observer = editor.editing.view.getObserver( ClickObserver );
  125. const linkCommand = editor.commands.get( 'link' );
  126. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  127. setModelData( editor.document, '<$text linkHref="url">some[] url</$text>' );
  128. linkCommand.value = 'url';
  129. observer.fire( 'click', { target: document.body } );
  130. expect( balloonPanel.view.model.isVisible ).to.true;
  131. } );
  132. it( 'should not show panel on editor click, when selection is not inside link element', () => {
  133. const observer = editor.editing.view.getObserver( ClickObserver );
  134. setModelData( editor.document, '[]' );
  135. observer.fire( 'click', { target: document.body } );
  136. expect( balloonPanel.view.model.isVisible ).to.false;
  137. } );
  138. } );
  139. } );