Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #83 from ckeditor/t/78
Browse files Browse the repository at this point in the history
Fix: Caption will not be automatically added second time if it was already added before "caption fixer" got fired. Closes #78.
  • Loading branch information
scofalik authored Mar 31, 2017
2 parents 3818544 + 456bc00 commit e651b01
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/imagecaption/imagecaptionengine.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,11 @@ function insertMissingModelCaptionElement( evt, changeType, data, batch ) {

if ( value.type == 'elementStart' && isImage( item ) && !getCaptionFromImage( item ) ) {
batch.document.enqueueChanges( () => {
batch.insert( ModelPosition.createAt( item, 'end' ), new ModelElement( 'caption' ) );
// Make sure that the image does not have caption already.
// https://github.com/ckeditor/ckeditor5-image/issues/78
if ( !getCaptionFromImage( item ) ) {
batch.insert( ModelPosition.createAt( item, 'end' ), new ModelElement( 'caption' ) );
}
} );
}
}
Expand Down
26 changes: 26 additions & 0 deletions tests/imagecaption/imagecaptionengine.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,32 @@ describe( 'ImageCaptionEngine', () => {
);
} );

it( 'should not add caption element twice', () => {
const image = new ModelElement( 'image', { src: '', alt: '' } );
const caption = new ModelElement( 'caption' );
const batch = document.batch();

document.enqueueChanges( () => {
batch
// Since we are adding an empty image, this should trigger caption fixer.
.insert( ModelPosition.createAt( document.getRoot() ), image )
// Add caption just after the image is inserted, in same batch and enqueue changes block.
.insert( ModelPosition.createAt( image ), caption );
} );

// Check whether caption fixer added redundant caption.
expect( getModelData( document ) ).to.equal(
'[]<image alt="" src=""><caption></caption></image>'
);

expect( getViewData( viewDocument ) ).to.equal(
'[]<figure class="image ck-widget" contenteditable="false">' +
'<img alt="" src=""></img>' +
'<figcaption class="ck-editable ck-hidden" contenteditable="true"></figcaption>' +
'</figure>'
);
} );

it( 'should do nothing for other changes than insert', () => {
setModelData( document, '<image src=""><caption>foo bar</caption></image>' );
const image = document.getRoot().getChild( 0 );
Expand Down

0 comments on commit e651b01

Please sign in to comment.