8
0

utils.js 3.8 KB

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