utils.js 3.4 KB

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