瀏覽代碼

Fixed issue with duble inserting caption to image.

Szymon Kupś 8 年之前
父節點
當前提交
72cfe1203a

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

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

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

@@ -189,6 +189,34 @@ 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();
+
+			// When first change to the document will be made - manually add caption to the image element.
+			document.once( 'change', () => {
+				document.enqueueChanges( () => {
+					batch.insert( ModelPosition.createAt( image ), caption );
+				} );
+			}, { priority: 'high' } );
+
+			document.enqueueChanges( () => {
+				batch.insert( ModelPosition.createAt( document.getRoot() ), image );
+			} );
+
+			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 );