linkimageui.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  8. import EventInfo from '@ckeditor/ckeditor5-utils/src/eventinfo';
  9. import DomEventData from '@ckeditor/ckeditor5-engine/src/view/observer/domeventdata';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  12. import LinkImage from '../src/linkimage';
  13. import LinkImageUI from '../src/linkimageui';
  14. describe( 'LinkImageUI', () => {
  15. let editor, viewDocument, editorElement;
  16. let plugin, linkButton;
  17. testUtils.createSinonSandbox();
  18. beforeEach( () => {
  19. editorElement = document.createElement( 'div' );
  20. document.body.appendChild( editorElement );
  21. return ClassicTestEditor
  22. .create( editorElement, {
  23. plugins: [ LinkImageUI, LinkImage, Paragraph ]
  24. } )
  25. .then( newEditor => {
  26. editor = newEditor;
  27. viewDocument = editor.editing.view.document;
  28. linkButton = editor.ui.componentFactory.create( 'linkImage' );
  29. plugin = editor.plugins.get( 'LinkImageUI' );
  30. } );
  31. } );
  32. afterEach( () => {
  33. editorElement.remove();
  34. return editor.destroy();
  35. } );
  36. it( 'should be named"', () => {
  37. expect( LinkImageUI.pluginName ).to.equal( 'LinkImageUI' );
  38. } );
  39. describe( 'init()', () => {
  40. it( 'should listen to the click event on the images', () => {
  41. const listenToSpy = sinon.stub( plugin, 'listenTo' );
  42. listenToSpy( viewDocument, 'click' );
  43. viewDocument.fire( 'click' );
  44. sinon.assert.calledOnce( listenToSpy );
  45. } );
  46. } );
  47. describe( 'link toolbar UI component', () => {
  48. it( 'should be registered', () => {
  49. expect( linkButton ).to.be.instanceOf( ButtonView );
  50. } );
  51. describe( 'link button', () => {
  52. it( 'should have a toggleable button', () => {
  53. expect( linkButton.isToggleable ).to.be.true;
  54. } );
  55. it( 'should be bound to the link command', () => {
  56. const command = editor.commands.get( 'link' );
  57. command.isEnabled = true;
  58. command.value = 'http://ckeditor.com';
  59. expect( linkButton.isOn ).to.be.true;
  60. expect( linkButton.isEnabled ).to.be.true;
  61. command.isEnabled = false;
  62. command.value = undefined;
  63. expect( linkButton.isOn ).to.be.false;
  64. expect( linkButton.isEnabled ).to.be.false;
  65. } );
  66. it( 'should call #_showUI upon #execute', () => {
  67. const spy = testUtils.sinon.stub( editor.plugins.get( 'LinkUI' ), '_showUI' );
  68. linkButton.fire( 'execute' );
  69. sinon.assert.calledWithExactly( spy, true );
  70. } );
  71. } );
  72. } );
  73. describe( 'click', () => {
  74. it( 'should prevent default behavior if image is wrapped with a link', () => {
  75. editor.setData( '<figure class="image"><a href="https://example.com"><img src="" /></a></figure>' );
  76. editor.editing.view.change( writer => {
  77. writer.setSelection( viewDocument.getRoot().getChild( 0 ), 'on' );
  78. } );
  79. const img = viewDocument.selection.getSelectedElement();
  80. const data = fakeEventData();
  81. const eventInfo = new EventInfo( img, 'click' );
  82. const domEventDataMock = new DomEventData( viewDocument, eventInfo, data );
  83. viewDocument.fire( 'click', domEventDataMock );
  84. expect( img.getChild( 0 ).name ).to.equal( 'a' );
  85. expect( data.preventDefault.called ).to.be.true;
  86. } );
  87. } );
  88. describe( 'event handling', () => {
  89. it( 'should show plugin#actionsView after "execute" if image is already linked', () => {
  90. const linkUIPlugin = editor.plugins.get( 'LinkUI' );
  91. editor.setData( '<figure class="image"><a href="https://example.com"><img src="" /></a></figure>' );
  92. editor.editing.view.change( writer => {
  93. writer.setSelection( viewDocument.getRoot().getChild( 0 ), 'on' );
  94. } );
  95. linkButton.fire( 'execute' );
  96. expect( linkUIPlugin._balloon.visibleView ).to.equals( linkUIPlugin.actionsView );
  97. } );
  98. it( 'should show plugin#formView after "execute" if image is not linked', () => {
  99. const linkUIPlugin = editor.plugins.get( 'LinkUI' );
  100. editor.setData( '<figure class="image"><img src="" /></a>' );
  101. editor.editing.view.change( writer => {
  102. writer.setSelection( viewDocument.getRoot().getChild( 0 ), 'on' );
  103. } );
  104. linkButton.fire( 'execute' );
  105. expect( linkUIPlugin._balloon.visibleView ).to.equals( linkUIPlugin.formView );
  106. } );
  107. } );
  108. } );
  109. function fakeEventData() {
  110. return {
  111. preventDefault: sinon.spy()
  112. };
  113. }