8
0
Просмотр исходного кода

Add tests for image/utils methods.

Maciej Gołaszewski 7 лет назад
Родитель
Сommit
d574942ba3

+ 3 - 3
packages/ckeditor5-image/src/image/imageinsertcommand.js

@@ -31,13 +31,13 @@ export default class ImageInsertCommand extends Command {
 	 * @param {String|Array.<String>} options.sources The image source or an array of image sources to insert.
 	 */
 	execute( options ) {
-		const editor = this.editor;
+		const model = this.editor.model;
 
-		editor.model.change( writer => {
+		model.change( writer => {
 			const sources = Array.isArray( options.sources ) ? options.sources : [ options.sources ];
 
 			for ( const src of sources ) {
-				insertImage( writer, editor, { src } );
+				insertImage( writer, model, { src } );
 			}
 		} );
 	}

+ 9 - 8
packages/ckeditor5-image/src/image/utils.js

@@ -67,19 +67,20 @@ export function isImage( modelElement ) {
 }
 
 /**
- * Handles inserting single file.
+ * Handles inserting single file. This method unifies image insertion using {@link module:widget/utils~findOptimalInsertionPosition} method.
+ *
+ *		model.change( writer => {
+ *			insertImage( writer, model, { src: 'path/to/image.jpg' } );
+ *		} );
  *
  * @param {module:engine/model/writer~writer} writer
- * @param {module:core/editor/editor~Editor} editor
- * @param {Object} attributes
+ * @param {module:engine/model/model~Model} model
+ * @param {Object} [attributes={}] Attributes of inserted image
  */
-export function insertImage( writer, editor, attributes ) {
-	const model = editor.model;
-	const doc = model.document;
-
+export function insertImage( writer, model, attributes = {} ) {
 	const imageElement = writer.createElement( 'image', attributes );
 
-	const insertAtSelection = findOptimalInsertionPosition( doc.selection, model );
+	const insertAtSelection = findOptimalInsertionPosition( model.document.selection, model );
 
 	model.insertContent( imageElement, insertAtSelection );
 

+ 6 - 5
packages/ckeditor5-image/src/imageupload/imageuploadcommand.js

@@ -33,14 +33,15 @@ export default class ImageUploadCommand extends Command {
 	 */
 	execute( options ) {
 		const editor = this.editor;
+		const model = editor.model;
 
 		const fileRepository = editor.plugins.get( FileRepository );
 
-		editor.model.change( writer => {
+		model.change( writer => {
 			const filesToUpload = Array.isArray( options.files ) ? options.files : [ options.files ];
 
 			for ( const file of filesToUpload ) {
-				uploadImage( writer, editor, fileRepository, file );
+				uploadImage( writer, model, fileRepository, file );
 			}
 		} );
 	}
@@ -49,9 +50,9 @@ export default class ImageUploadCommand extends Command {
 // Handles uploading single file.
 //
 // @param {module:engine/model/writer~writer} writer
-// @param {module:core/editor/editor~Editor} editor
+// @param {module:engine/model/model~Model} model
 // @param {File} file
-function uploadImage( writer, editor, fileRepository, file ) {
+function uploadImage( writer, model, fileRepository, file ) {
 	const loader = fileRepository.createLoader( file );
 
 	// Do not throw when upload adapter is not set. FileRepository will log an error anyway.
@@ -59,5 +60,5 @@ function uploadImage( writer, editor, fileRepository, file ) {
 		return;
 	}
 
-	insertImage( writer, editor, { uploadId: loader.id } );
+	insertImage( writer, model, { uploadId: loader.id } );
 }

+ 158 - 2
packages/ckeditor5-image/tests/image/utils.js

@@ -7,8 +7,13 @@ import ViewDocumentFragment from '@ckeditor/ckeditor5-engine/src/view/documentfr
 import ViewDowncastWriter from '@ckeditor/ckeditor5-engine/src/view/downcastwriter';
 import ViewDocument from '@ckeditor/ckeditor5-engine/src/view/document';
 import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
-import { toImageWidget, isImageWidget, isImageWidgetSelected, isImage } from '../../src/image/utils';
+import { toImageWidget, isImageWidget, isImageWidgetSelected, isImage, isImageAllowed, insertImage } from '../../src/image/utils';
 import { isWidget, getLabel } from '@ckeditor/ckeditor5-widget/src/utils';
+import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
+import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
+import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
+import { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
+import Image from '../../src/image/imageediting';
 
 describe( 'image widget utils', () => {
 	let element, image, writer;
@@ -87,7 +92,7 @@ describe( 'image widget utils', () => {
 		} );
 	} );
 
-	describe( 'isImage', () => {
+	describe( 'isImage()', () => {
 		it( 'should return true for image element', () => {
 			const image = new ModelElement( 'image' );
 
@@ -105,4 +110,155 @@ describe( 'image widget utils', () => {
 			expect( isImage( undefined ) ).to.be.false;
 		} );
 	} );
+
+	describe( 'isImageAllowed()', () => {
+		let editor, model;
+
+		beforeEach( () => {
+			return VirtualTestEditor
+				.create( {
+					plugins: [ Image, Paragraph ]
+				} )
+				.then( newEditor => {
+					editor = newEditor;
+					model = editor.model;
+
+					const schema = model.schema;
+					schema.extend( 'image', { allowAttributes: 'uploadId' } );
+				} );
+		} );
+
+		it( 'should return true when the selection directly in the root', () => {
+			model.enqueueChange( 'transparent', () => {
+				setModelData( model, '[]' );
+
+				expect( isImageAllowed( model ) ).to.be.true;
+			} );
+		} );
+
+		it( 'should return true when the selection is in empty block', () => {
+			setModelData( model, '<paragraph>[]</paragraph>' );
+
+			expect( isImageAllowed( model ) ).to.be.true;
+		} );
+
+		it( 'should return true when the selection directly in a paragraph', () => {
+			setModelData( model, '<paragraph>foo[]</paragraph>' );
+			expect( isImageAllowed( model ) ).to.be.true;
+		} );
+
+		it( 'should return true when the selection directly in a block', () => {
+			model.schema.register( 'block', { inheritAllFrom: '$block' } );
+			model.schema.extend( '$text', { allowIn: 'block' } );
+			editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'block', view: 'block' } ) );
+
+			setModelData( model, '<block>foo[]</block>' );
+			expect( isImageAllowed( model ) ).to.be.true;
+		} );
+
+		it( 'should return false when the selection is on other image', () => {
+			setModelData( model, '[<image></image>]' );
+			expect( isImageAllowed( model ) ).to.be.false;
+		} );
+
+		it( 'should return false when the selection is inside other image', () => {
+			model.schema.register( 'caption', {
+				allowIn: 'image',
+				allowContentOf: '$block',
+				isLimit: true
+			} );
+			editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'caption', view: 'figcaption' } ) );
+			setModelData( model, '<image><caption>[]</caption></image>' );
+			expect( isImageAllowed( model ) ).to.be.false;
+		} );
+
+		it( 'should return false when the selection is on other object', () => {
+			model.schema.register( 'object', { isObject: true, allowIn: '$root' } );
+			editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'object', view: 'object' } ) );
+			setModelData( model, '[<object></object>]' );
+
+			expect( isImageAllowed( model ) ).to.be.false;
+		} );
+
+		it( 'should return false when the selection is inside other object', () => {
+			model.schema.register( 'object', { isObject: true, allowIn: '$root' } );
+			model.schema.extend( '$text', { allowIn: 'object' } );
+			editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'object', view: 'object' } ) );
+			setModelData( model, '<object>[]</object>' );
+
+			expect( isImageAllowed( model ) ).to.be.false;
+		} );
+
+		it( 'should return false when schema disallows image', () => {
+			model.schema.register( 'block', { inheritAllFrom: '$block' } );
+			model.schema.extend( 'paragraph', { allowIn: 'block' } );
+			// Block image in block.
+			model.schema.addChildCheck( ( context, childDefinition ) => {
+				if ( childDefinition.name === 'image' && context.last.name === 'block' ) {
+					return false;
+				}
+			} );
+			editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'block', view: 'block' } ) );
+
+			setModelData( model, '<block><paragraph>[]</paragraph></block>' );
+
+			expect( isImageAllowed( model ) ).to.be.false;
+		} );
+	} );
+
+	describe( 'insertImage()', () => {
+		let editor, model;
+
+		beforeEach( () => {
+			return VirtualTestEditor
+				.create( {
+					plugins: [ Image, Paragraph ]
+				} )
+				.then( newEditor => {
+					editor = newEditor;
+					model = editor.model;
+
+					const schema = model.schema;
+					schema.extend( 'image', { allowAttributes: 'uploadId' } );
+				} );
+		} );
+
+		it( 'should insert image at selection position as other widgets', () => {
+			setModelData( model, '<paragraph>f[o]o</paragraph>' );
+
+			model.change( writer => {
+				insertImage( writer, model );
+			} );
+
+			expect( getModelData( model ) ).to.equal( '[<image></image>]<paragraph>foo</paragraph>' );
+		} );
+
+		it( 'should insert image with given attributes', () => {
+			setModelData( model, '<paragraph>f[o]o</paragraph>' );
+
+			model.change( writer => {
+				insertImage( writer, model, { src: 'bar' } );
+			} );
+
+			expect( getModelData( model ) ).to.equal( '[<image src="bar"></image>]<paragraph>foo</paragraph>' );
+		} );
+
+		it( 'should not insert image nor crash when image could not be inserted', () => {
+			model.schema.register( 'other', {
+				allowIn: '$root',
+				isLimit: true
+			} );
+			model.schema.extend( '$text', { allowIn: 'other' } );
+
+			editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'other', view: 'p' } ) );
+
+			setModelData( model, '<other>[]</other>' );
+
+			model.change( writer => {
+				insertImage( writer, model );
+			} );
+
+			expect( getModelData( model ) ).to.equal( '<other>[]</other>' );
+		} );
+	} );
 } );