utils.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ViewElement from '@ckeditor/ckeditor5-engine/src/view/element';
  6. import ViewSelection from '@ckeditor/ckeditor5-engine/src/view/selection';
  7. import ViewDocumentFragment from '@ckeditor/ckeditor5-engine/src/view/documentfragment';
  8. import ViewRange from '@ckeditor/ckeditor5-engine/src/view/range';
  9. import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
  10. import { toImageWidget, isImageWidget, isImageWidgetSelected, isImage } from '../../src/image/utils';
  11. import { isWidget, getLabel } from '@ckeditor/ckeditor5-widget/src/utils';
  12. describe( 'image widget utils', () => {
  13. let element, image;
  14. beforeEach( () => {
  15. image = new ViewElement( 'img' );
  16. element = new ViewElement( 'figure', null, image );
  17. toImageWidget( element, 'image widget' );
  18. } );
  19. describe( 'toImageWidget()', () => {
  20. it( 'should be widgetized', () => {
  21. expect( isWidget( element ) ).to.be.true;
  22. } );
  23. it( 'should set element\'s label', () => {
  24. expect( getLabel( element ) ).to.equal( 'image widget' );
  25. } );
  26. it( 'should set element\'s label combined with alt attribute', () => {
  27. image.setAttribute( 'alt', 'foo bar baz' );
  28. expect( getLabel( element ) ).to.equal( 'foo bar baz image widget' );
  29. } );
  30. it( 'provided label creator should always return same label', () => {
  31. image.setAttribute( 'alt', 'foo bar baz' );
  32. expect( getLabel( element ) ).to.equal( 'foo bar baz image widget' );
  33. expect( getLabel( element ) ).to.equal( 'foo bar baz image widget' );
  34. } );
  35. } );
  36. describe( 'isImageWidget()', () => {
  37. it( 'should return true for elements marked with toImageWidget()', () => {
  38. expect( isImageWidget( element ) ).to.be.true;
  39. } );
  40. it( 'should return false for non-widgetized elements', () => {
  41. expect( isImageWidget( new ViewElement( 'p' ) ) ).to.be.false;
  42. } );
  43. } );
  44. describe( 'isImageWidgetSelected()', () => {
  45. let frag;
  46. it( 'should return true when image widget is the only element in the selection', () => {
  47. // We need to create a container for the element to be able to create a Range on this element.
  48. frag = new ViewDocumentFragment( [ element ] );
  49. const selection = new ViewSelection( [ ViewRange.createOn( element ) ] );
  50. expect( isImageWidgetSelected( selection ) ).to.be.true;
  51. } );
  52. it( 'should return false when non-widgetized elements is the only element in the selection', () => {
  53. const notWidgetizedElement = new ViewElement( 'p' );
  54. // We need to create a container for the element to be able to create a Range on this element.
  55. frag = new ViewDocumentFragment( [ notWidgetizedElement ] );
  56. const selection = new ViewSelection( [ ViewRange.createOn( notWidgetizedElement ) ] );
  57. expect( isImageWidgetSelected( selection ) ).to.be.false;
  58. } );
  59. it( 'should return false when widget element is not the only element in the selection', () => {
  60. const notWidgetizedElement = new ViewElement( 'p' );
  61. frag = new ViewDocumentFragment( [ element, notWidgetizedElement ] );
  62. const selection = new ViewSelection( [ ViewRange.createIn( frag ) ] );
  63. expect( isImageWidgetSelected( selection ) ).to.be.false;
  64. } );
  65. } );
  66. describe( 'isImage', () => {
  67. it( 'should return true for image element', () => {
  68. const image = new ModelElement( 'image' );
  69. expect( isImage( image ) ).to.be.true;
  70. } );
  71. it( 'should return true false for different elements', () => {
  72. const image = new ModelElement( 'foo' );
  73. expect( isImage( image ) ).to.be.false;
  74. } );
  75. it( 'should return true false for null and undefined', () => {
  76. expect( isImage( null ) ).to.be.false;
  77. expect( isImage( undefined ) ).to.be.false;
  78. } );
  79. } );
  80. } );