utils.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 ViewContainerElement from '@ckeditor/ckeditor5-engine/src/view/containerelement';
  9. import {
  10. captionElementCreator,
  11. isCaption,
  12. getCaptionFromImage,
  13. isInsideCaption,
  14. matchImageCaption,
  15. insertViewCaptionAndBind
  16. } from '../../src/imagecaption/utils';
  17. import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
  18. import Mapper from '@ckeditor/ckeditor5-engine/src/conversion/mapper';
  19. describe( 'image captioning utils', () => {
  20. let element, document;
  21. beforeEach( () => {
  22. document = new ViewDocument();
  23. const creator = captionElementCreator( document );
  24. element = creator();
  25. } );
  26. describe( 'editableCaptionCreator', () => {
  27. it( 'should create figcatpion editable element', () => {
  28. expect( element ).to.be.instanceOf( ViewEditableElement );
  29. expect( element.name ).to.equal( 'figcaption' );
  30. expect( isCaption( element ) ).to.be.true;
  31. } );
  32. it( 'should be created in context of proper document', () => {
  33. expect( element.document ).to.equal( document );
  34. } );
  35. it( 'should add proper class when element is focused', () => {
  36. element.isFocused = true;
  37. expect( element.hasClass( 'focused' ) ).to.be.true;
  38. element.isFocused = false;
  39. expect( element.hasClass( 'focused' ) ).to.be.false;
  40. } );
  41. } );
  42. describe( 'isCaptionEditable', () => {
  43. it( 'should return true for elements created with creator', () => {
  44. expect( isCaption( element ) ).to.be.true;
  45. } );
  46. it( 'should return false for other elements', () => {
  47. const editable = new ViewEditableElement( 'figcaption', { contenteditable: true } ) ;
  48. editable.document = document;
  49. expect( isCaption( editable ) ).to.be.false;
  50. } );
  51. } );
  52. describe( 'getCaptionFromImage', () => {
  53. it( 'should return caption element from image element', () => {
  54. const dummy = new ModelElement( 'dummy' );
  55. const caption = new ModelElement( 'caption' );
  56. const image = new ModelElement( 'image', null, [ dummy, caption ] );
  57. expect( getCaptionFromImage( image ) ).to.equal( caption );
  58. } );
  59. it( 'should return null when caption element is not present', () => {
  60. const image = new ModelElement( 'image' );
  61. expect( getCaptionFromImage( image ) ).to.be.null;
  62. } );
  63. } );
  64. describe( 'isInsideCaption', () => {
  65. it( 'should return false if node has no parent', () => {
  66. const el = new ModelElement( 'test' );
  67. expect( isInsideCaption( el ) ).to.be.false;
  68. } );
  69. it( 'should return false if node\'s parent is not caption', () => {
  70. const el = new ModelElement( 'test' );
  71. new ModelElement( 'test', null, el );
  72. expect( isInsideCaption( el ) ).to.be.false;
  73. } );
  74. it( 'should return false if parent`s parent node is not defined', () => {
  75. const el = new ModelElement( 'test' );
  76. new ModelElement( 'caption', null, el );
  77. expect( isInsideCaption( el ) ).to.be.false;
  78. } );
  79. it( 'should return false if parent\'s parent node is not an image', () => {
  80. const el = new ModelElement( 'test' );
  81. const parent = new ModelElement( 'caption', null, el );
  82. new ModelElement( 'not-image', null, parent );
  83. expect( isInsideCaption( el ) ).to.be.false;
  84. } );
  85. it( 'should return true if node is placed inside image\'s caption', () => {
  86. const el = new ModelElement( 'test' );
  87. const parent = new ModelElement( 'caption', null, el );
  88. new ModelElement( 'image', null, parent );
  89. expect( isInsideCaption( el ) ).to.be.true;
  90. } );
  91. } );
  92. describe( 'matchImageCaption', () => {
  93. it( 'should return null for element that is not a figcaption', () => {
  94. const element = new ViewElement( 'div' );
  95. expect( matchImageCaption( element ) ).to.be.null;
  96. } );
  97. it( 'should return null if figcaption has no parent', () => {
  98. const element = new ViewElement( 'figcaption' );
  99. expect( matchImageCaption( element ) ).to.be.null;
  100. } );
  101. it( 'should return null if figcaption\'s parent is not a figure', () => {
  102. const element = new ViewElement( 'figcaption' );
  103. new ViewElement( 'div', null, element );
  104. expect( matchImageCaption( element ) ).to.be.null;
  105. } );
  106. it( 'should return null if parent has no image class', () => {
  107. const element = new ViewElement( 'figcaption' );
  108. new ViewElement( 'figure', null, element );
  109. expect( matchImageCaption( element ) ).to.be.null;
  110. } );
  111. it( 'should return object if element is a valid caption', () => {
  112. const element = new ViewElement( 'figcaption' );
  113. new ViewElement( 'figure', { class: 'image' }, element );
  114. expect( matchImageCaption( element ) ).to.deep.equal( { name: true } );
  115. } );
  116. } );
  117. describe( 'insertViewCaptionAndBind', () => {
  118. let viewCaption, modelCaption, viewImage, mapper;
  119. beforeEach( () => {
  120. viewCaption = new ViewContainerElement( 'figcaption' );
  121. modelCaption = new ModelElement( 'caption' );
  122. viewImage = new ViewContainerElement( 'figure', { class: 'image' } );
  123. mapper = new Mapper();
  124. } );
  125. it( 'should insert provided caption to provided image', () => {
  126. insertViewCaptionAndBind( viewCaption, modelCaption, viewImage, mapper );
  127. expect( viewImage.getChild( 0 ) ).to.equal( viewCaption );
  128. } );
  129. it( 'should insert provided caption at the end of provided image', () => {
  130. const dummyElement = new ViewElement( 'dummy' );
  131. viewImage.appendChildren( dummyElement );
  132. insertViewCaptionAndBind( viewCaption, modelCaption, viewImage, mapper );
  133. expect( viewImage.getChild( 1 ) ).to.equal( viewCaption );
  134. } );
  135. it( 'should bind view caption to model caption using provided mapper', () => {
  136. insertViewCaptionAndBind( viewCaption, modelCaption, viewImage, mapper );
  137. expect( mapper.toModelElement( viewCaption ) ).to.equal( modelCaption );
  138. } );
  139. } );
  140. } );