8
0

utils.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * @license Copyright (c) 2003-2019, 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( 'figcaption', { contenteditable: true } );
  41. editable._document = document;
  42. expect( isCaption( editable ) ).to.be.false;
  43. } );
  44. } );
  45. describe( 'getCaptionFromImage', () => {
  46. it( 'should return caption element from image element', () => {
  47. const dummy = new ModelElement( 'dummy' );
  48. const caption = new ModelElement( 'caption' );
  49. const image = new ModelElement( 'image', null, [ dummy, caption ] );
  50. expect( getCaptionFromImage( image ) ).to.equal( caption );
  51. } );
  52. it( 'should return null when caption element is not present', () => {
  53. const image = new ModelElement( 'image' );
  54. expect( getCaptionFromImage( image ) ).to.be.null;
  55. } );
  56. } );
  57. describe( 'matchImageCaption', () => {
  58. it( 'should return null for element that is not a figcaption', () => {
  59. const element = new ViewElement( 'div' );
  60. expect( matchImageCaption( element ) ).to.be.null;
  61. } );
  62. it( 'should return null if figcaption has no parent', () => {
  63. const element = new ViewElement( 'figcaption' );
  64. expect( matchImageCaption( element ) ).to.be.null;
  65. } );
  66. it( 'should return null if figcaption\'s parent is not a figure', () => {
  67. const element = new ViewElement( 'figcaption' );
  68. new ViewElement( 'div', null, element ); // eslint-disable-line no-new
  69. expect( matchImageCaption( element ) ).to.be.null;
  70. } );
  71. it( 'should return null if parent has no image class', () => {
  72. const element = new ViewElement( 'figcaption' );
  73. new ViewElement( 'figure', null, element ); // eslint-disable-line no-new
  74. expect( matchImageCaption( element ) ).to.be.null;
  75. } );
  76. it( 'should return object if element is a valid caption', () => {
  77. const element = new ViewElement( 'figcaption' );
  78. new ViewElement( 'figure', { class: 'image' }, element ); // eslint-disable-line no-new
  79. expect( matchImageCaption( element ) ).to.deep.equal( { name: true } );
  80. } );
  81. } );
  82. } );