utils.js 3.9 KB

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