utils.js 3.3 KB

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