8
0

image.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  6. import Image from '../src/image';
  7. import ImageEngine from '../src/image/imageengine';
  8. import Widget from '@ckeditor/ckeditor5-widget/src/widget';
  9. import ImageTextAlternative from '../src/imagetextalternative';
  10. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  11. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  12. import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
  13. import ContextualToolbar from '@ckeditor/ckeditor5-ui/src/toolbar/contextual/contextualtoolbar';
  14. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  15. import View from '@ckeditor/ckeditor5-ui/src/view';
  16. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  17. describe( 'Image', () => {
  18. let editorElement, editor, document, viewDocument;
  19. beforeEach( () => {
  20. editorElement = global.document.createElement( 'div' );
  21. global.document.body.appendChild( editorElement );
  22. return ClassicTestEditor.create( editorElement, {
  23. plugins: [ Image ]
  24. } )
  25. .then( newEditor => {
  26. editor = newEditor;
  27. document = editor.document;
  28. viewDocument = editor.editing.view;
  29. } );
  30. } );
  31. afterEach( () => {
  32. editorElement.remove();
  33. return editor.destroy();
  34. } );
  35. it( 'should be loaded', () => {
  36. expect( editor.plugins.get( Image ) ).to.instanceOf( Image );
  37. } );
  38. it( 'should load ImageEngine plugin', () => {
  39. expect( editor.plugins.get( ImageEngine ) ).to.instanceOf( ImageEngine );
  40. } );
  41. it( 'should load Widget plugin', () => {
  42. expect( editor.plugins.get( Widget ) ).to.instanceOf( Widget );
  43. } );
  44. it( 'should load ImageTextAlternative plugin', () => {
  45. expect( editor.plugins.get( ImageTextAlternative ) ).to.instanceOf( ImageTextAlternative );
  46. } );
  47. describe( 'ContextualToolbar integration', () => {
  48. let balloon, contextualToolbar, newEditor;
  49. beforeEach( () => {
  50. return ClassicTestEditor.create( editorElement, {
  51. plugins: [ Image, ContextualToolbar, Paragraph ]
  52. } )
  53. .then( editor => {
  54. newEditor = editor;
  55. balloon = newEditor.plugins.get( 'ContextualBalloon' );
  56. contextualToolbar = newEditor.plugins.get( 'ContextualToolbar' );
  57. const button = new View();
  58. button.element = global.document.createElement( 'div' );
  59. // There must be at least one toolbar items which is not disabled to show it.
  60. // https://github.com/ckeditor/ckeditor5-ui/issues/269
  61. contextualToolbar.toolbarView.items.add( button );
  62. newEditor.editing.view.isFocused = true;
  63. } );
  64. } );
  65. afterEach( () => {
  66. editorElement.remove();
  67. return newEditor.destroy();
  68. } );
  69. it( 'should prevent the ContextualToolbar from being displayed when an image is selected', () => {
  70. // When image is selected along with text.
  71. setModelData( newEditor.document, '<paragraph>fo[o</paragraph><image alt="alt text" src="foo.png"></image>]' );
  72. contextualToolbar.show();
  73. // ContextualToolbar should be visible.
  74. expect( balloon.visibleView ).to.equal( contextualToolbar.toolbarView );
  75. // When only image is selected.
  76. setModelData( newEditor.document, '<paragraph>foo</paragraph>[<image alt="alt text" src="foo.png"></image>]' );
  77. contextualToolbar.show();
  78. // ContextualToolbar should not be visible.
  79. expect( balloon.visibleView ).to.be.null;
  80. } );
  81. it( 'should listen to ContextualToolbar#show event with high priority', () => {
  82. const highestPrioritySpy = sinon.spy();
  83. const normalPrioritySpy = sinon.spy();
  84. // Select an image
  85. setModelData( newEditor.document, '<paragraph>foo</paragraph>[<image alt="alt text" src="foo.png"></image>]' );
  86. newEditor.listenTo( contextualToolbar, 'show', highestPrioritySpy, { priority: 'highest' } );
  87. newEditor.listenTo( contextualToolbar, 'show', normalPrioritySpy, { priority: 'normal' } );
  88. contextualToolbar.show();
  89. sinon.assert.calledOnce( highestPrioritySpy );
  90. sinon.assert.notCalled( normalPrioritySpy );
  91. } );
  92. } );
  93. describe( 'selection', () => {
  94. it( 'should create fake selection', () => {
  95. setModelData( document, '[<image alt="alt text" src="foo.png"></image>]' );
  96. expect( getViewData( viewDocument ) ).to.equal(
  97. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  98. '<img alt="alt text" src="foo.png"></img>' +
  99. '</figure>]'
  100. );
  101. expect( viewDocument.selection.isFake ).to.be.true;
  102. expect( viewDocument.selection.fakeSelectionLabel ).to.equal( 'alt text image widget' );
  103. } );
  104. it( 'should create proper fake selection label when alt attribute is empty', () => {
  105. setModelData( document, '[<image src="foo.png" alt=""></image>]' );
  106. expect( getViewData( viewDocument ) ).to.equal(
  107. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  108. '<img alt="" src="foo.png"></img>' +
  109. '</figure>]'
  110. );
  111. expect( viewDocument.selection.isFake ).to.be.true;
  112. expect( viewDocument.selection.fakeSelectionLabel ).to.equal( 'image widget' );
  113. } );
  114. it( 'should remove selected class from previously selected element', () => {
  115. setModelData( document,
  116. '[<image src="foo.png" alt="alt text"></image>]' +
  117. '<image src="foo.png" alt="alt text"></image>'
  118. );
  119. expect( getViewData( viewDocument ) ).to.equal(
  120. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  121. '<img alt="alt text" src="foo.png"></img>' +
  122. '</figure>]' +
  123. '<figure class="image ck-widget" contenteditable="false">' +
  124. '<img alt="alt text" src="foo.png"></img>' +
  125. '</figure>'
  126. );
  127. document.enqueueChanges( () => {
  128. const secondImage = document.getRoot().getChild( 1 );
  129. document.selection.setRanges( [ ModelRange.createOn( secondImage ) ] );
  130. } );
  131. expect( getViewData( viewDocument ) ).to.equal(
  132. '<figure class="image ck-widget" contenteditable="false">' +
  133. '<img alt="alt text" src="foo.png"></img>' +
  134. '</figure>' +
  135. '[<figure class="image ck-widget ck-widget_selected" contenteditable="false">' +
  136. '<img alt="alt text" src="foo.png"></img>' +
  137. '</figure>]'
  138. );
  139. } );
  140. } );
  141. } );