8
0
Просмотр исходного кода

Merge pull request #7405 from ckeditor/i/5033-empty-alt

Fix (image): The src and alt attributes for the image element will be always added to the editor's data. Even if they are empty. Closes #5033.
Maciej 5 лет назад
Родитель
Сommit
e81cbbba4b

+ 1 - 5
packages/ckeditor5-image/src/image/converters.js

@@ -125,10 +125,6 @@ export function modelToViewAttributeConverter( attributeKey ) {
 		const figure = conversionApi.mapper.toViewElement( data.item );
 		const img = getViewImgFromWidget( figure );
 
-		if ( data.attributeNewValue !== null ) {
-			viewWriter.setAttribute( data.attributeKey, data.attributeNewValue, img );
-		} else {
-			viewWriter.removeAttribute( data.attributeKey, img );
-		}
+		viewWriter.setAttribute( data.attributeKey, data.attributeNewValue || '', img );
 	}
 }

+ 15 - 2
packages/ckeditor5-image/tests/image/converters.js

@@ -218,7 +218,20 @@ describe( 'Image converters', () => {
 			);
 		} );
 
-		it( 'should convert removing attribute from image', () => {
+		it( 'should convert an empty "src" attribute from image even if removed', () => {
+			setModelData( model, '<image src="" alt="foo bar"></image>' );
+			const image = document.getRoot().getChild( 0 );
+
+			model.change( writer => {
+				writer.removeAttribute( 'src', image );
+			} );
+
+			expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
+				'<figure class="ck-widget image" contenteditable="false"><img alt="foo bar" src=""></img></figure>'
+			);
+		} );
+
+		it( 'should convert an empty "alt" attribute from image even if removed', () => {
 			setModelData( model, '<image src="" alt="foo bar"></image>' );
 			const image = document.getRoot().getChild( 0 );
 
@@ -227,7 +240,7 @@ describe( 'Image converters', () => {
 			} );
 
 			expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
-				'<figure class="ck-widget image" contenteditable="false"><img src=""></img></figure>'
+				'<figure class="ck-widget image" contenteditable="false"><img alt="" src=""></img></figure>'
 			);
 		} );
 

+ 4 - 3
packages/ckeditor5-image/tests/image/imageediting.js

@@ -458,7 +458,7 @@ describe( 'ImageEditing', () => {
 				);
 			} );
 
-			it( 'should convert attribute removal', () => {
+			it( 'should convert attribute removal (but keeps an empty "alt" to the data)', () => {
 				setModelData( model, '<image src="/assets/sample.png" alt="alt text"></image>' );
 				const image = doc.getRoot().getChild( 0 );
 
@@ -466,8 +466,9 @@ describe( 'ImageEditing', () => {
 					writer.removeAttribute( 'alt', image );
 				} );
 
-				expect( getViewData( view, { withoutSelection: true } ) )
-					.to.equal( '<figure class="ck-widget image" contenteditable="false"><img src="/assets/sample.png"></img></figure>' );
+				expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
+					'<figure class="ck-widget image" contenteditable="false"><img alt="" src="/assets/sample.png"></img></figure>'
+				);
 			} );
 
 			it( 'should not convert change if is already consumed', () => {