Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mati365 committed Nov 18, 2024
1 parent 94a51d3 commit 67e5b30
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
13 changes: 7 additions & 6 deletions packages/ckeditor5-image/src/imageupload/imageuploadediting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,30 +121,31 @@ export default class ImageUploadEditing extends Plugin {
// Handle the case when the image is not fully uploaded yet but it's being moved.
// See more: https://github.com/ckeditor/ckeditor5/pull/17327
.add( dispatcher => dispatcher.on<UpcastElementEvent>( 'element:img', ( evt, data, conversionApi ) => {
if ( !conversionApi.consumable.test( data.viewItem, { attributes: [ 'data-ck-upload-id' ] } ) ) {
return;
}

const uploadId = data.viewItem.getAttribute( 'data-ck-upload-id' );

if ( !uploadId ) {
return;
}

if ( !data.modelRange ) {
Object.assign( data, conversionApi.convertChildren( data.viewItem, data.modelCursor ) );
}

const [ modelElement ] = Array.from( data.modelRange!.getItems() );
const [ modelElement ] = Array.from( data.modelRange!.getItems( { shallow: true } ) );
const loader = fileRepository.loaders.get( uploadId as string );

if ( modelElement ) {
// Handle case when `uploadId` is set on the image element but the loader is not present in the registry.
// It may happen when the image was successfully uploaded and the loader was removed from the registry.
// It's still present in the `_uploadedImages` map though. It's why we do not place this line in the condition below.
conversionApi.writer.setAttribute( 'uploadId', uploadId, modelElement );
conversionApi.consumable.consume( data.viewItem, { attributes: [ 'data-ck-upload-id' ] } );

if ( loader && loader.data ) {
conversionApi.writer.setAttribute( 'uploadStatus', loader.status, modelElement );
}
}
} ) );
}, { priority: 'low' } ) );

// Handle pasted images.
// For every image file, a new file loader is created and a placeholder image is
Expand Down
22 changes: 22 additions & 0 deletions packages/ckeditor5-image/tests/imageupload/imageuploadediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -1694,6 +1694,28 @@ describe( 'ImageUploadEditing', () => {
);
} );

it( 'should not upcast empty `data-ck-upload-id` attribute', () => {
editor.setData( '<p><img data-ck-upload-id=""></p>' );

expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
'<paragraph><imageInline></imageInline></paragraph>'
);
} );

it( 'should not upcast already consumed element', () => {
editor.conversion.for( 'upcast' ).add( dispatcher =>
dispatcher.on( 'element:img', ( evt, data, conversionApi ) => {
conversionApi.consumable.consume( data.viewItem, { attributes: [ 'data-ck-upload-id' ] } );
}, { priority: 'high' } )
);

editor.setData( '<p><img data-ck-upload-id="123"></p>' );

expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
'<paragraph><imageInline></imageInline></paragraph>'
);
} );

it( 'should upcast `uploadStatus` if image is present in registry', () => {
sinon.stub( fileRepository.loaders, 'get' ).withArgs( '123' ).returns( {
status: 'uploading',
Expand Down

0 comments on commit 67e5b30

Please sign in to comment.