Browse Source

Keep an empty "alt" attribute in the data.

Kamil Piechaczek 5 years ago
parent
commit
9451c97a30

+ 3 - 0
packages/ckeditor5-image/src/image/converters.js

@@ -127,6 +127,9 @@ export function modelToViewAttributeConverter( attributeKey ) {
 
 		if ( data.attributeNewValue !== null ) {
 			viewWriter.setAttribute( data.attributeKey, data.attributeNewValue, img );
+		} else if ( attributeKey === 'alt' && !data.attributeNewValue ) {
+			// The `alt` attribute should always be added. Even empty. See #5033.
+			viewWriter.setAttribute( data.attributeKey, '', img );
 		} else {
 			viewWriter.removeAttribute( data.attributeKey, img );
 		}

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

@@ -218,7 +218,7 @@ describe( 'Image converters', () => {
 			);
 		} );
 
-		it( 'should convert removing attribute from image', () => {
+		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 +227,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', () => {