utils.js 3.3 KB

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