Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer to pick width style from figure element instead of img if possible. #17460

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/ckeditor5-image/src/imageresize/imageresizeediting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,21 @@ export default class ImageResizeEditing extends Plugin {
return null;
}

// If this is an image inside a figure with width style, skip setting the width.
// See: https://github.com/ckeditor/ckeditor5/issues/17441
if ( viewElement.name !== 'figure' ) {
let parent = viewElement.parent;

// Traverse up through parents until we find a figure or run out of ancestors
while ( parent ) {
if ( parent.is( 'element' ) && imageUtils.isBlockImageView( parent ) && parent.getStyle( 'width' ) ) {
return null;
}

parent = parent.parent;
}
}

return viewElement.getStyle( 'width' );
}
}
Expand Down
10 changes: 10 additions & 0 deletions packages/ckeditor5-image/tests/imageresize/imageresizeediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ describe( 'ImageResizeEditing', () => {
expect( editor.model.document.getRoot().getChild( 0 ).getAttribute( 'resizedWidth' ) ).to.equal( '100px' );
} );

it( 'it should prefer the width from the figure element over the width from the image element', () => {
editor.setData(
'<figure class="image" style="width:100px;">' +
`<img src="${ IMAGE_SRC_FIXTURE }" style="width:200px;">` +
'</figure>'
);

expect( editor.model.document.getRoot().getChild( 0 ).getAttribute( 'resizedWidth' ) ).to.equal( '100px' );
} );

it( 'upcasts 50% width correctly', () => {
editor.setData( `<figure class="image" style="width:50%;"><img src="${ IMAGE_SRC_FIXTURE }"></figure>` );

Expand Down