8
0

image.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 global from '@ckeditor/ckeditor5-utils/src/dom/global';
  14. describe( 'Image', () => {
  15. let editorElement, editor, document, viewDocument;
  16. beforeEach( () => {
  17. editorElement = global.document.createElement( 'div' );
  18. global.document.body.appendChild( editorElement );
  19. return ClassicTestEditor
  20. .create( editorElement, {
  21. plugins: [ Image ]
  22. } )
  23. .then( newEditor => {
  24. editor = newEditor;
  25. document = editor.document;
  26. viewDocument = editor.editing.view;
  27. } );
  28. } );
  29. afterEach( () => {
  30. editorElement.remove();
  31. return editor.destroy();
  32. } );
  33. it( 'should be loaded', () => {
  34. expect( editor.plugins.get( Image ) ).to.instanceOf( Image );
  35. } );
  36. it( 'should load ImageEngine plugin', () => {
  37. expect( editor.plugins.get( ImageEngine ) ).to.instanceOf( ImageEngine );
  38. } );
  39. it( 'should load Widget plugin', () => {
  40. expect( editor.plugins.get( Widget ) ).to.instanceOf( Widget );
  41. } );
  42. it( 'should load ImageTextAlternative plugin', () => {
  43. expect( editor.plugins.get( ImageTextAlternative ) ).to.instanceOf( ImageTextAlternative );
  44. } );
  45. describe( 'selection', () => {
  46. it( 'should create fake selection', () => {
  47. setModelData( document, '[<image alt="alt text" src="foo.png"></image>]' );
  48. expect( getViewData( viewDocument ) ).to.equal(
  49. '[<figure class="ck-widget ck-widget_selected image" contenteditable="false">' +
  50. '<img alt="alt text" src="foo.png"></img>' +
  51. '</figure>]'
  52. );
  53. expect( viewDocument.selection.isFake ).to.be.true;
  54. expect( viewDocument.selection.fakeSelectionLabel ).to.equal( 'alt text image widget' );
  55. } );
  56. it( 'should create proper fake selection label when alt attribute is empty', () => {
  57. setModelData( document, '[<image src="foo.png" alt=""></image>]' );
  58. expect( getViewData( viewDocument ) ).to.equal(
  59. '[<figure class="ck-widget ck-widget_selected image" contenteditable="false">' +
  60. '<img alt="" src="foo.png"></img>' +
  61. '</figure>]'
  62. );
  63. expect( viewDocument.selection.isFake ).to.be.true;
  64. expect( viewDocument.selection.fakeSelectionLabel ).to.equal( 'image widget' );
  65. } );
  66. it( 'should remove selected class from previously selected element', () => {
  67. setModelData( document,
  68. '[<image src="foo.png" alt="alt text"></image>]' +
  69. '<image src="foo.png" alt="alt text"></image>'
  70. );
  71. expect( getViewData( viewDocument ) ).to.equal(
  72. '[<figure class="ck-widget ck-widget_selected image" contenteditable="false">' +
  73. '<img alt="alt text" src="foo.png"></img>' +
  74. '</figure>]' +
  75. '<figure class="ck-widget image" contenteditable="false">' +
  76. '<img alt="alt text" src="foo.png"></img>' +
  77. '</figure>'
  78. );
  79. document.enqueueChanges( () => {
  80. const secondImage = document.getRoot().getChild( 1 );
  81. document.selection.setRanges( [ ModelRange.createOn( secondImage ) ] );
  82. } );
  83. expect( getViewData( viewDocument ) ).to.equal(
  84. '<figure class="ck-widget image" contenteditable="false">' +
  85. '<img alt="alt text" src="foo.png"></img>' +
  86. '</figure>' +
  87. '[<figure class="ck-widget ck-widget_selected image" contenteditable="false">' +
  88. '<img alt="alt text" src="foo.png"></img>' +
  89. '</figure>]'
  90. );
  91. } );
  92. } );
  93. } );