utils.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ViewDocument from '@ckeditor/ckeditor5-engine/src/view/document';
  6. import ViewEditableElement from '@ckeditor/ckeditor5-engine/src/view/editableelement';
  7. import ViewElement from '@ckeditor/ckeditor5-engine/src/view/element';
  8. import {
  9. captionElementCreator,
  10. isCaption,
  11. getCaptionFromImage,
  12. matchImageCaption
  13. } from '../../src/imagecaption/utils';
  14. import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
  15. describe( 'image captioning utils', () => {
  16. let element, document;
  17. beforeEach( () => {
  18. document = new ViewDocument();
  19. const creator = captionElementCreator( document );
  20. element = creator();
  21. } );
  22. describe( 'editableCaptionCreator', () => {
  23. it( 'should create figcatpion editable element', () => {
  24. expect( element ).to.be.instanceOf( ViewEditableElement );
  25. expect( element.name ).to.equal( 'figcaption' );
  26. expect( isCaption( element ) ).to.be.true;
  27. } );
  28. } );
  29. describe( 'isCaptionEditable', () => {
  30. it( 'should return true for elements created with creator', () => {
  31. expect( isCaption( element ) ).to.be.true;
  32. } );
  33. it( 'should return false for other elements', () => {
  34. const editable = new ViewEditableElement( 'figcaption', { contenteditable: true } ) ;
  35. editable.document = document;
  36. expect( isCaption( editable ) ).to.be.false;
  37. } );
  38. } );
  39. describe( 'getCaptionFromImage', () => {
  40. it( 'should return caption element from image element', () => {
  41. const dummy = new ModelElement( 'dummy' );
  42. const caption = new ModelElement( 'caption' );
  43. const image = new ModelElement( 'image', null, [ dummy, caption ] );
  44. expect( getCaptionFromImage( image ) ).to.equal( caption );
  45. } );
  46. it( 'should return null when caption element is not present', () => {
  47. const image = new ModelElement( 'image' );
  48. expect( getCaptionFromImage( image ) ).to.be.null;
  49. } );
  50. } );
  51. describe( 'matchImageCaption', () => {
  52. it( 'should return null for element that is not a figcaption', () => {
  53. const element = new ViewElement( 'div' );
  54. expect( matchImageCaption( element ) ).to.be.null;
  55. } );
  56. it( 'should return null if figcaption has no parent', () => {
  57. const element = new ViewElement( 'figcaption' );
  58. expect( matchImageCaption( element ) ).to.be.null;
  59. } );
  60. it( 'should return null if figcaption\'s parent is not a figure', () => {
  61. const element = new ViewElement( 'figcaption' );
  62. new ViewElement( 'div', null, element );
  63. expect( matchImageCaption( element ) ).to.be.null;
  64. } );
  65. it( 'should return null if parent has no image class', () => {
  66. const element = new ViewElement( 'figcaption' );
  67. new ViewElement( 'figure', null, element );
  68. expect( matchImageCaption( element ) ).to.be.null;
  69. } );
  70. it( 'should return object if element is a valid caption', () => {
  71. const element = new ViewElement( 'figcaption' );
  72. new ViewElement( 'figure', { class: 'image' }, element );
  73. expect( matchImageCaption( element ) ).to.deep.equal( { name: true } );
  74. } );
  75. } );
  76. } );