Skip to content

Commit

Permalink
Merge pull request #7405 from ckeditor/i/5033-empty-alt
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jodator authored Jun 10, 2020
2 parents 64e5315 + 2b1edf4 commit e81cbbb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
6 changes: 1 addition & 5 deletions packages/ckeditor5-image/src/image/converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
17 changes: 15 additions & 2 deletions packages/ckeditor5-image/tests/image/converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

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

Expand Down
7 changes: 4 additions & 3 deletions packages/ckeditor5-image/tests/image/imageediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,16 +458,17 @@ 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 );

model.change( writer => {
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', () => {
Expand Down

0 comments on commit e81cbbb

Please sign in to comment.