gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Manually resized images break image block when edited

Open WPopt opened this issue 3 years ago • 7 comments

Description

It appears that images blocks added to the block editor which have previously been manually resized (have a custom aspect ratio) will break the image block and return a "This block contains unexpected or invalid content." error when the post editor is reloaded.

Step-by-step reproduction instructions

  1. To reproduce this error, edit a post and identify an image block where:
  • The image aspect ratio is set to "custom"
  • Width and Height attributes are set to a custom value (not "Auto").
  1. Edit something about the image - for example the description.

  2. Save the post, refresh the editor.

  3. The block error will occur. Clicking "attempt block recovery" will resolve the issue and remove the custom width and height attributes.

Screenshots, screen recording, code snippet

No response

Environment info

  • WordPress 6.3.3
  • No plugins active
  • Tested using the Genesis framework and Ashe theme and the issue occurs on both.

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

WPopt avatar Oct 27 '23 00:10 WPopt

Thanks for reporting the issue, @WPopt.

Can you share the example markup?

Mamaduka avatar Oct 30 '23 05:10 Mamaduka

@Mamaduka Came to Git to report this same issue. Probably started in WP 6.3 when image aspect ratio feature was added to the core.

Have confirmed I see the issue using 2023 theme and no plugins.

I did some further testing:

When I load some old posts in the editor, I suddenly notice that image blocks that previously worked fine now switch – sometimes before my eyes – to say, “This block contains unexpected or invalid content.”

Pressing the “Attempt Recovery” button fixes the issue. These images have been fine for years.

I did a bit more digging by duplicating one of the image blocks, then recovering the duplicate, and comparing the HTML for the recovered block and the one that supposedly suddenly has a problem.

All of the blocks that were working fine but now show as broken contain custom image dimensions. In other words, in the HTML they have style=”width:XXXXpx;height:XXXXpx”

The fact that they have specific widths and heights means that these images are ones where I have, sometime in the past, used the image handles to drag them to create a different size on the page.

Another test I performed was to test what the HTML looks like now when you create a new image block in Gutenberg and use the handles to adjust the image size. I see now in WP6.32 it adds style=”aspect-ratio:1;width:XXXXpx;height:auto” .

I do not see the same issue with images that have been modified since WP6.3. I only see the issue with older images—presumably, ones modified before aspect ratio was added as a core feature. This suggests the issue is slightly different to the way the OP has described it. It doesn't seem to happen with all modified images. Only ones that have been modified before WP6.3 and therefore do not have aspect ratio in the style.

Here is an example of the HTML in an Image Block that now shows as broken and requiring recovery:

<figure class="wp-block-image aligncenter size-large is-resized"><img src="https://mysite.com/wp-content/uploads/2020/01/image.jpg" alt="" class="wp-image-52925" style="width:250px;height:250px"/><figcaption class="wp-element-caption">Caption.</figcaption></figure>

Also note that if you do use the Attempt Block Recovery button, it does not recover previous custom image dimensions. It simply deletes the style='width;height" stuff entirely.

And finally, all that's needed to suddenly change all previously working blocks to errored "contains unexpected or invalid content" is just to open a post in the post editor that contains images inserted and custom re-sized prior to 6.3, then just press the update post button. After the update, all images in the post that had custom dimensions will have switched to "contains unexpected or invalid content"

If your post had many custom re-sized images, and you then recover the blocks, you are forced to go through and resize them all over again. Not ideal.

shuttermuse avatar Nov 03 '23 17:11 shuttermuse

Help us move this issue forward. This issue is being marked stale since it has no activity after 15 days of requesting more information. Please add info requested so we can help move the issue forward. Note: The triage policy is to close stale issues that need more info and no response after 2 weeks.

github-actions[bot] avatar Nov 19 '23 00:11 github-actions[bot]

I can confirm the issue described by @shuttermuse still exists even with Core 6.4. The problem lies when old content created prior 6.3 is update to WP Core 6.3. You can reproduce this even in the WordPress Playground: https://playground.wordpress.net/

  1. Make sure the core version is 6.3/6.4
  2. paste the markup of image block created with Core 6.2. For example this:
<!-- wp:image {"width":321,"height":214,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large is-resized"><img src="https://images.unsplash.com/photo-1448375240586-882707db888b?q=80&amp;w=2670&amp;auto=format&amp;fit=crop&amp;ixlib=rb-4.0.3&amp;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D" alt="" width="321" height="214"/></figure>
<!-- /wp:image -->
  1. do some other change in the post (for example add text 'foo')
  2. Save post, View post, Edit post
  3. image block is broken with text "This block contains unexpected or invalid content"

I believe the problem is that "Updated Block: core/image" -feature only updates the html markup of image-block, but not attributes. This leads to situation where we have mixed types in width/height -values between attributes and markup, like this

<!-- wp:image {"width":321,"height":214,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large is-resized"><img src="https://images.unsplash.com/photo-1448375240586-882707db888b?q=80&amp;w=2670&amp;auto=format&amp;fit=crop&amp;ixlib=rb-4.0.3&amp;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D" alt="" style="width:321px;height:214px"/></figure>
<!-- /wp:image -->

When you change the width/height in 6.3/6.4 the markup is like this:

<!-- wp:image {"width":"321px","height":"214px","scale":"cover","sizeSlug":"large","className":"is-resized"} -->
<figure class="wp-block-image size-large is-resized"><img src="https://images.unsplash.com/photo-1448375240586-882707db888b?q=80&amp;w=2670&amp;auto=format&amp;fit=crop&amp;ixlib=rb-4.0.3&amp;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D" alt="" style="object-fit:cover;width:321px;height:214px"/></figure>
<!-- /wp:image -->

Notice the difference with block attributes:

  • <=6.2: "width":321,"height":214
  • >=6.3: "width":"321px","height":"214px"

I'm not entirely sure how the block version updates works, but I believe there is something wrong with the deprecated versions of image-block https://github.com/WordPress/gutenberg/blob/trunk/packages/block-library/src/image/deprecated.js

saulirajala avatar Jan 02 '24 15:01 saulirajala

Hmm, did some more digging and seems like this merged pr will also fix this issue. image_deprecated_v6 missed migrate() function and that's why attributes were not updated. And it seems that the fix is already cherry-picked for the next 6.4 point release. Nice! 🔥

I hope the maintenance release will happen soon, since this issue can be a roadblock for updating the core version in older WordPress sites to the latest one.

saulirajala avatar Jan 03 '24 08:01 saulirajala

@t-hamano, can you confirm @saulirajala discovery?

Mamaduka avatar Jan 03 '24 08:01 Mamaduka

Hmm, did some more digging and seems like this merged pr will also fix this issue. image_deprecated_v6 missed migrate() function and that's why attributes were not updated. And it seems that the fix is already cherry-picked for the next 6.4 point release. Nice! 🔥

This is correct. This issue has been fixed in #57063 and will be cherry-picked into the 6.4 point release. Therefore, I would like to close this issue.

t-hamano avatar Jan 04 '24 02:01 t-hamano