8
0

image.js 5.1 KB

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