utils.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 ViewDocumentFragment from '@ckeditor/ckeditor5-engine/src/view/documentfragment';
  6. import ViewDowncastWriter from '@ckeditor/ckeditor5-engine/src/view/downcastwriter';
  7. import ViewDocument from '@ckeditor/ckeditor5-engine/src/view/document';
  8. import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
  9. import { toImageWidget, isImageWidget, getSelectedImageWidget, isImage, isImageAllowed, insertImage } from '../../src/image/utils';
  10. import { isWidget, getLabel } from '@ckeditor/ckeditor5-widget/src/utils';
  11. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  12. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  13. import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. import Image from '../../src/image/imageediting';
  15. describe( 'image widget utils', () => {
  16. let element, image, writer, viewDocument;
  17. beforeEach( () => {
  18. viewDocument = new ViewDocument();
  19. writer = new ViewDowncastWriter( viewDocument );
  20. image = writer.createContainerElement( 'img' );
  21. element = writer.createContainerElement( 'figure' );
  22. writer.insert( writer.createPositionAt( element, 0 ), image );
  23. toImageWidget( element, writer, 'image widget' );
  24. } );
  25. describe( 'toImageWidget()', () => {
  26. it( 'should be widgetized', () => {
  27. expect( isWidget( element ) ).to.be.true;
  28. } );
  29. it( 'should set element\'s label', () => {
  30. expect( getLabel( element ) ).to.equal( 'image widget' );
  31. } );
  32. it( 'should set element\'s label combined with alt attribute', () => {
  33. writer.setAttribute( 'alt', 'foo bar baz', image );
  34. expect( getLabel( element ) ).to.equal( 'foo bar baz image widget' );
  35. } );
  36. it( 'provided label creator should always return same label', () => {
  37. writer.setAttribute( 'alt', 'foo bar baz', image );
  38. expect( getLabel( element ) ).to.equal( 'foo bar baz image widget' );
  39. expect( getLabel( element ) ).to.equal( 'foo bar baz image widget' );
  40. } );
  41. } );
  42. describe( 'isImageWidget()', () => {
  43. it( 'should return true for elements marked with toImageWidget()', () => {
  44. expect( isImageWidget( element ) ).to.be.true;
  45. } );
  46. it( 'should return false for non-widgetized elements', () => {
  47. expect( isImageWidget( writer.createContainerElement( 'p' ) ) ).to.be.false;
  48. } );
  49. } );
  50. describe( 'getSelectedImageWidget()', () => {
  51. let frag;
  52. it( 'should return true when image widget is the only element in the selection', () => {
  53. // We need to create a container for the element to be able to create a Range on this element.
  54. frag = new ViewDocumentFragment( viewDocument, [ element ] );
  55. const selection = writer.createSelection( element, 'on' );
  56. expect( getSelectedImageWidget( selection ) ).to.equal( element );
  57. } );
  58. it( 'should return false when non-widgetized elements is the only element in the selection', () => {
  59. const notWidgetizedElement = writer.createContainerElement( 'p' );
  60. // We need to create a container for the element to be able to create a Range on this element.
  61. frag = new ViewDocumentFragment( viewDocument, [ notWidgetizedElement ] );
  62. const selection = writer.createSelection( notWidgetizedElement, 'on' );
  63. expect( getSelectedImageWidget( selection ) ).to.be.null;
  64. } );
  65. it( 'should return false when widget element is not the only element in the selection', () => {
  66. const notWidgetizedElement = writer.createContainerElement( 'p' );
  67. frag = new ViewDocumentFragment( viewDocument, [ element, notWidgetizedElement ] );
  68. const selection = writer.createSelection( writer.createRangeIn( frag ) );
  69. expect( getSelectedImageWidget( selection ) ).to.be.null;
  70. } );
  71. } );
  72. describe( 'isImage()', () => {
  73. it( 'should return true for image element', () => {
  74. const image = new ModelElement( 'image' );
  75. expect( isImage( image ) ).to.be.true;
  76. } );
  77. it( 'should return true false for different elements', () => {
  78. const image = new ModelElement( 'foo' );
  79. expect( isImage( image ) ).to.be.false;
  80. } );
  81. it( 'should return true false for null and undefined', () => {
  82. expect( isImage( null ) ).to.be.false;
  83. expect( isImage( undefined ) ).to.be.false;
  84. } );
  85. } );
  86. describe( 'isImageAllowed()', () => {
  87. let editor, model;
  88. beforeEach( () => {
  89. return VirtualTestEditor
  90. .create( {
  91. plugins: [ Image, Paragraph ]
  92. } )
  93. .then( newEditor => {
  94. editor = newEditor;
  95. model = editor.model;
  96. const schema = model.schema;
  97. schema.extend( 'image', { allowAttributes: 'uploadId' } );
  98. } );
  99. } );
  100. it( 'should return true when the selection directly in the root', () => {
  101. model.enqueueChange( 'transparent', () => {
  102. setModelData( model, '[]' );
  103. expect( isImageAllowed( model ) ).to.be.true;
  104. } );
  105. } );
  106. it( 'should return true when the selection is in empty block', () => {
  107. setModelData( model, '<paragraph>[]</paragraph>' );
  108. expect( isImageAllowed( model ) ).to.be.true;
  109. } );
  110. it( 'should return true when the selection directly in a paragraph', () => {
  111. setModelData( model, '<paragraph>foo[]</paragraph>' );
  112. expect( isImageAllowed( model ) ).to.be.true;
  113. } );
  114. it( 'should return true when the selection directly in a block', () => {
  115. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  116. model.schema.extend( '$text', { allowIn: 'block' } );
  117. editor.conversion.for( 'downcast' ).elementToElement( { model: 'block', view: 'block' } );
  118. setModelData( model, '<block>foo[]</block>' );
  119. expect( isImageAllowed( model ) ).to.be.true;
  120. } );
  121. it( 'should return false when the selection is on other image', () => {
  122. setModelData( model, '[<image></image>]' );
  123. expect( isImageAllowed( model ) ).to.be.false;
  124. } );
  125. it( 'should return false when the selection is inside other image', () => {
  126. model.schema.register( 'caption', {
  127. allowIn: 'image',
  128. allowContentOf: '$block',
  129. isLimit: true
  130. } );
  131. editor.conversion.for( 'downcast' ).elementToElement( { model: 'caption', view: 'figcaption' } );
  132. setModelData( model, '<image><caption>[]</caption></image>' );
  133. expect( isImageAllowed( model ) ).to.be.false;
  134. } );
  135. it( 'should return false when the selection is on other object', () => {
  136. model.schema.register( 'object', { isObject: true, allowIn: '$root' } );
  137. editor.conversion.for( 'downcast' ).elementToElement( { model: 'object', view: 'object' } );
  138. setModelData( model, '[<object></object>]' );
  139. expect( isImageAllowed( model ) ).to.be.false;
  140. } );
  141. it( 'should be true when the selection is inside isLimit element which allows image', () => {
  142. model.schema.register( 'table', { allowWhere: '$block', isLimit: true, isObject: true, isBlock: true } );
  143. model.schema.register( 'tableRow', { allowIn: 'table', isLimit: true } );
  144. model.schema.register( 'tableCell', { allowIn: 'tableRow', isLimit: true } );
  145. model.schema.extend( '$block', { allowIn: 'tableCell' } );
  146. editor.conversion.for( 'downcast' ).elementToElement( { model: 'table', view: 'table' } );
  147. editor.conversion.for( 'downcast' ).elementToElement( { model: 'tableRow', view: 'tableRow' } );
  148. editor.conversion.for( 'downcast' ).elementToElement( { model: 'tableCell', view: 'tableCell' } );
  149. setModelData( model, '<table><tableRow><tableCell><paragraph>foo[]</paragraph></tableCell></tableRow></table>' );
  150. expect( isImageAllowed( model ) ).to.be.true;
  151. } );
  152. it( 'should return false when schema disallows image', () => {
  153. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  154. model.schema.extend( 'paragraph', { allowIn: 'block' } );
  155. // Block image in block.
  156. model.schema.addChildCheck( ( context, childDefinition ) => {
  157. if ( childDefinition.name === 'image' && context.last.name === 'block' ) {
  158. return false;
  159. }
  160. } );
  161. editor.conversion.for( 'downcast' ).elementToElement( { model: 'block', view: 'block' } );
  162. setModelData( model, '<block><paragraph>[]</paragraph></block>' );
  163. expect( isImageAllowed( model ) ).to.be.false;
  164. } );
  165. } );
  166. describe( 'insertImage()', () => {
  167. let editor, model;
  168. beforeEach( () => {
  169. return VirtualTestEditor
  170. .create( {
  171. plugins: [ Image, Paragraph ]
  172. } )
  173. .then( newEditor => {
  174. editor = newEditor;
  175. model = editor.model;
  176. const schema = model.schema;
  177. schema.extend( 'image', { allowAttributes: 'uploadId' } );
  178. } );
  179. } );
  180. it( 'should insert image at selection position as other widgets', () => {
  181. setModelData( model, '<paragraph>f[o]o</paragraph>' );
  182. model.change( writer => {
  183. insertImage( writer, model );
  184. } );
  185. expect( getModelData( model ) ).to.equal( '[<image></image>]<paragraph>foo</paragraph>' );
  186. } );
  187. it( 'should insert image with given attributes', () => {
  188. setModelData( model, '<paragraph>f[o]o</paragraph>' );
  189. model.change( writer => {
  190. insertImage( writer, model, { src: 'bar' } );
  191. } );
  192. expect( getModelData( model ) ).to.equal( '[<image src="bar"></image>]<paragraph>foo</paragraph>' );
  193. } );
  194. it( 'should not insert image nor crash when image could not be inserted', () => {
  195. model.schema.register( 'other', {
  196. allowIn: '$root',
  197. isLimit: true
  198. } );
  199. model.schema.extend( '$text', { allowIn: 'other' } );
  200. editor.conversion.for( 'downcast' ).elementToElement( { model: 'other', view: 'p' } );
  201. setModelData( model, '<other>[]</other>' );
  202. model.change( writer => {
  203. insertImage( writer, model );
  204. } );
  205. expect( getModelData( model ) ).to.equal( '<other>[]</other>' );
  206. } );
  207. } );
  208. } );