8
0

utils.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. it( 'should be created in context of proper document', () => {
  29. expect( element.document ).to.equal( document );
  30. } );
  31. it( 'should add proper class when element is focused', () => {
  32. element.isFocused = true;
  33. expect( element.hasClass( 'focused' ) ).to.be.true;
  34. element.isFocused = false;
  35. expect( element.hasClass( 'focused' ) ).to.be.false;
  36. } );
  37. } );
  38. describe( 'isCaptionEditable', () => {
  39. it( 'should return true for elements created with creator', () => {
  40. expect( isCaption( element ) ).to.be.true;
  41. } );
  42. it( 'should return false for other elements', () => {
  43. const editable = new ViewEditableElement( 'figcaption', { contenteditable: true } ) ;
  44. editable.document = document;
  45. expect( isCaption( editable ) ).to.be.false;
  46. } );
  47. } );
  48. describe( 'getCaptionFromImage', () => {
  49. it( 'should return caption element from image element', () => {
  50. const dummy = new ModelElement( 'dummy' );
  51. const caption = new ModelElement( 'caption' );
  52. const image = new ModelElement( 'image', null, [ dummy, caption ] );
  53. expect( getCaptionFromImage( image ) ).to.equal( caption );
  54. } );
  55. it( 'should return null when caption element is not present', () => {
  56. const image = new ModelElement( 'image' );
  57. expect( getCaptionFromImage( image ) ).to.be.null;
  58. } );
  59. } );
  60. describe( 'matchImageCaption', () => {
  61. it( 'should return null for element that is not a figcaption', () => {
  62. const element = new ViewElement( 'div' );
  63. expect( matchImageCaption( element ) ).to.be.null;
  64. } );
  65. it( 'should return null if figcaption has no parent', () => {
  66. const element = new ViewElement( 'figcaption' );
  67. expect( matchImageCaption( element ) ).to.be.null;
  68. } );
  69. it( 'should return null if figcaption\'s parent is not a figure', () => {
  70. const element = new ViewElement( 'figcaption' );
  71. new ViewElement( 'div', null, element );
  72. expect( matchImageCaption( element ) ).to.be.null;
  73. } );
  74. it( 'should return null if parent has no image class', () => {
  75. const element = new ViewElement( 'figcaption' );
  76. new ViewElement( 'figure', null, element );
  77. expect( matchImageCaption( element ) ).to.be.null;
  78. } );
  79. it( 'should return object if element is a valid caption', () => {
  80. const element = new ViewElement( 'figcaption' );
  81. new ViewElement( 'figure', { class: 'image' }, element );
  82. expect( matchImageCaption( element ) ).to.deep.equal( { name: true } );
  83. } );
  84. } );
  85. } );