8
0

linkimageui.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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, linkButtonComponent, 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. linkButtonComponent = editor.ui.componentFactory.create( 'linkImage' );
  29. plugin = editor.plugins.get( 'LinkImageUI' );
  30. linkButton = plugin.linkButtonView;
  31. } );
  32. } );
  33. afterEach( () => {
  34. editorElement.remove();
  35. return editor.destroy();
  36. } );
  37. it( 'should be named"', () => {
  38. expect( LinkImageUI.pluginName ).to.equal( 'LinkImageUI' );
  39. } );
  40. describe( 'init()', () => {
  41. it( 'should listen to the click event on the images', () => {
  42. const listenToSpy = sinon.stub( plugin, 'listenTo' );
  43. listenToSpy( viewDocument, 'click' );
  44. viewDocument.fire( 'click' );
  45. sinon.assert.calledOnce( listenToSpy );
  46. } );
  47. } );
  48. describe( 'link toolbar UI component', () => {
  49. it( 'should be registered', () => {
  50. expect( linkButtonComponent ).to.be.instanceOf( ButtonView );
  51. } );
  52. describe( 'link button', () => {
  53. it( 'should have a toggleable button', () => {
  54. expect( linkButton.isToggleable ).to.be.true;
  55. } );
  56. it( 'should be bound to the link command', () => {
  57. const command = editor.commands.get( 'link' );
  58. command.isEnabled = true;
  59. command.value = 'http://ckeditor.com';
  60. expect( linkButton.isOn ).to.be.true;
  61. expect( linkButton.isEnabled ).to.be.true;
  62. command.isEnabled = false;
  63. command.value = undefined;
  64. expect( linkButton.isOn ).to.be.false;
  65. expect( linkButton.isEnabled ).to.be.false;
  66. } );
  67. it( 'should call #_showUI upon #execute', () => {
  68. const spy = testUtils.sinon.stub( editor.plugins.get( 'LinkUI' ), '_showUI' );
  69. linkButton.fire( 'execute' );
  70. sinon.assert.calledWithExactly( spy, true );
  71. } );
  72. } );
  73. } );
  74. describe( 'click', () => {
  75. it( 'should prevent default behavior if image is wrapped with a link', () => {
  76. editor.setData( '<figure class="image"><a href="https://example.com"><img src="" /></a></figure>' );
  77. editor.editing.view.change( writer => {
  78. writer.setSelection( viewDocument.getRoot().getChild( 0 ), 'on' );
  79. } );
  80. const img = viewDocument.selection.getSelectedElement();
  81. const data = fakeEventData();
  82. const eventInfo = new EventInfo( img, 'click' );
  83. const domEventDataMock = new DomEventData( viewDocument, eventInfo, data );
  84. viewDocument.fire( 'click', domEventDataMock );
  85. expect( img.getChild( 0 ).name ).to.equal( 'a' );
  86. expect( data.preventDefault.called ).to.be.true;
  87. } );
  88. } );
  89. describe( 'event handling', () => {
  90. it( 'should show plugin#actionsView after "execute" if image is already linked', () => {
  91. const linkUIPlugin = editor.plugins.get( 'LinkUI' );
  92. editor.setData( '<figure class="image"><a href="https://example.com"><img src="" /></a></figure>' );
  93. editor.editing.view.change( writer => {
  94. writer.setSelection( viewDocument.getRoot().getChild( 0 ), 'on' );
  95. } );
  96. linkButton.fire( 'execute' );
  97. expect( linkUIPlugin._balloon.visibleView ).to.equals( linkUIPlugin.actionsView );
  98. } );
  99. it( 'should show plugin#formView after "execute" if image is not linked', () => {
  100. const linkUIPlugin = editor.plugins.get( 'LinkUI' );
  101. editor.setData( '<figure class="image"><img src="" /></a>' );
  102. editor.editing.view.change( writer => {
  103. writer.setSelection( viewDocument.getRoot().getChild( 0 ), 'on' );
  104. } );
  105. linkButton.fire( 'execute' );
  106. expect( linkUIPlugin._balloon.visibleView ).to.equals( linkUIPlugin.formView );
  107. } );
  108. } );
  109. } );
  110. function fakeEventData() {
  111. return {
  112. preventDefault: sinon.spy()
  113. };
  114. }