Ver código fonte

Merge pull request #83 from ckeditor/t/78

Fix: Caption will not be automatically added second time if it was already added before "caption fixer" got fired. Closes #78.
Szymon Cofalik 8 anos atrás
pai
commit
183953f076

+ 5 - 1
packages/ckeditor5-image/src/imagecaption/imagecaptionengine.js

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

+ 26 - 0
packages/ckeditor5-image/tests/imagecaption/imagecaptionengine.js

@@ -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 );