|
|
@@ -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>' );
|
|
|
+ } );
|
|
|
+ } );
|
|
|
} );
|