Skip to content

Conversation

@sagarnasit
Copy link
Contributor

@sagarnasit sagarnasit commented Aug 8, 2024

Summary

This PR accounts following:

  • Skip wrapping the image in the picture element if the jpeg fallback is not available.

Fixes #1447

@sagarnasit sagarnasit marked this pull request as ready for review August 9, 2024 14:44
@github-actions
Copy link

github-actions bot commented Aug 9, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @sagarnasit.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: sagarnasit.

Co-authored-by: adamsilverstein <[email protected]>
Co-authored-by: westonruter <[email protected]>
Co-authored-by: mukeshpanchal27 <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! Just a couple tweaks, and then it seems the only thing left is to add a test.

@westonruter westonruter added this to the webp-uploads n.e.x.t milestone Aug 9, 2024
@westonruter westonruter added [Type] Bug An existing feature is broken [Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) labels Aug 9, 2024
@sagarnasit
Copy link
Contributor Author

@westonruter I updated the check for $original_file_mime_type with $sub_size_mime_types and made other changes as per your suggestions.

Additionally, I've included a separate test file for the fallback image as the "Output JPEG" setting needs to be disabled initially, which is not viable in the existing test file.

</label>
<p class="description" id="webp_uploads_use_picture_element_description"><?php esc_html_e( 'The picture element serves a modern image format with a fallback to JPEG. Warning: Make sure you test your theme and plugins for compatibility. In particular, CSS selectors will not match images when using the child combinator (e.g. figure > img).', 'webp-uploads' ); ?></p>
<div id="webp_uploads_jpeg_fallback_notice" class="notice notice-info inline" <?php echo $picture_element_enabled ? '' : 'hidden'; ?>>
<p><?php esc_html_e( 'Picture elements will only be used when JPEG fallback images are available. So this will not apply to any images you uploaded while the "Also generate JPEG" setting was disabled.', 'webp-uploads' ); ?></p>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another slight copy improvement:

Suggested change
<p><?php esc_html_e( 'Picture elements will only be used when JPEG fallback images are available. So this will not apply to any images you uploaded while the "Also generate JPEG" setting was disabled.', 'webp-uploads' ); ?></p>
<p><?php esc_html_e( 'Picture elements will only be used when JPEG fallback images are available. So this will not apply to any images you may have uploaded while the "Also generate JPEG" setting was disabled.', 'webp-uploads' ); ?></p>

@mukeshpanchal27
Copy link
Member

@sagarnasit Have you replicate https://github.com/WordPress/performance/actions/runs/10364966081/job/28691196072?pr=1450#step:9:290 unit test failed in PHP 8.3 in your local? I didn't replicate it 🤔

Screenshot 2024-08-13 at 3 32 40 PM

@sagarnasit
Copy link
Contributor Author

@mukeshpanchal27 The issue is not with PHP 8.3 but with the WordPress trunk. I tried to load the WordPress trunk on a local setup and was able to replicate the issue with the following.

WP_ENV_CORE=WordPress/WordPress WP_ENV_PHP_VERSION=8.3 npx wp-env start

I also attempted to debug further, but I was unsuccessful in finding out why next_tag is returning true even if there is no PICTURE tag in the markup.

@westonruter
Copy link
Member

Probably a regression in the HTML Tag Processor in WP Core trunk since a lot has been going into that lately. We should do a git bisect on trunk to pinpoint the problematic commit.

@westonruter
Copy link
Member

Doing so now...

@westonruter
Copy link
Member

westonruter commented Aug 13, 2024

OK, I found that the regression was introduced in r58613 (in WordPress/WordPress this is commit df598e1d9). If I check out the parent commit, then I get no failure here.

@westonruter
Copy link
Member

For reference, the unit test assertion is failing here:

$picture_processor = new WP_HTML_Tag_Processor( $picture_markup );
$this->assertFalse( $picture_processor->next_tag( array( 'tag_name' => 'picture' ) ), 'There should not be a PICTURE tag.' );

The markup contained in $picture_markup is the following:

<p><img width="1024" height="683" src="http://localhost:8889/wp-content/uploads/2024/08/leaves-232-1024x683.webp" class="wp-image-9" alt="Green Leaves" decoding="async" loading="lazy" srcset="http://localhost:8889/wp-content/uploads/2024/08/leaves-232-1024x683.webp 1024w, http://localhost:8889/wp-content/uploads/2024/08/leaves-232-300x200.webp 300w, http://localhost:8889/wp-content/uploads/2024/08/leaves-232-768x512.webp 768w, http://localhost:8889/wp-content/uploads/2024/08/leaves-232-jpg.webp 1080w" sizes="(max-width: 1024px) 100vw, 1024px" /></p>

With formatting:

<p>
  <img
    width="1024"
    height="683"
    src="http://localhost:8889/wp-content/uploads/2024/08/leaves-232-1024x683.webp"
    class="wp-image-9"
    alt="Green Leaves"
    decoding="async"
    loading="lazy"
    srcset="
      http://localhost:8889/wp-content/uploads/2024/08/leaves-232-1024x683.webp 1024w,
      http://localhost:8889/wp-content/uploads/2024/08/leaves-232-300x200.webp   300w,
      http://localhost:8889/wp-content/uploads/2024/08/leaves-232-768x512.webp   768w,
      http://localhost:8889/wp-content/uploads/2024/08/leaves-232-jpg.webp      1080w
    "
    sizes="(max-width: 1024px) 100vw, 1024px"
  />
</p>

There is definitely no PICTURE tag here.

And yet, for some reason, $picture_processor->next_tag( array( 'tag_name' => 'picture' ) ) is returning true.

@mukeshpanchal27
Copy link
Member

It's WP Trunk error not related to PHP version. I have open WordPress/wordpress-develop#7185 to verify that.

@westonruter
Copy link
Member

Given this code:

$picture_markup = '<p><img width="1024" height="683" src="http://localhost:8889/wp-content/uploads/2024/08/leaves-232-1024x683.webp" class="wp-image-9" alt="Green Leaves" decoding="async" loading="lazy" srcset="http://localhost:8889/wp-content/uploads/2024/08/leaves-232-1024x683.webp 1024w, http://localhost:8889/wp-content/uploads/2024/08/leaves-232-300x200.webp 300w, http://localhost:8889/wp-content/uploads/2024/08/leaves-232-768x512.webp 768w, http://localhost:8889/wp-content/uploads/2024/08/leaves-232-jpg.webp 1080w" sizes="(max-width: 1024px) 100vw, 1024px" /></p>';
$picture_processor = new WP_HTML_Tag_Processor( $picture_markup );
var_dump( $picture_processor->next_tag( array( 'tag_name' => 'picture' ) ) );
var_dump( $picture_processor->get_tag() );

It outputs:

bool(true)
string(1) "P"

If I remove the <p> from the markup, then I get:

bool(false)
NULL

So it seems that the Tag Processor is only looking at the first character of the tag name now, which is a bug.

Quick workaround to get the tests to pass: remove the <p> tag.

@westonruter
Copy link
Member

Reported upstream: https://core.trac.wordpress.org/ticket/61545#comment:6

@westonruter
Copy link
Member

✅ Behavior looks good.

Before:

<picture
  class="not-transparent wp-picture-155"
  style="--dominant-color: #594e4a; display: contents"
  ><source
    type="image/webp"
    srcset="
      http://localhost:8888/wp-content/uploads/2024/08/image-1-jpeg.webp     1300w,
      http://localhost:8888/wp-content/uploads/2024/08/image-1-300x201.webp   300w,
      http://localhost:8888/wp-content/uploads/2024/08/image-1-1024x688.webp 1024w,
      http://localhost:8888/wp-content/uploads/2024/08/image-1-768x516.webp   768w
    "
    sizes="(max-width: 650px) 100vw, 650px" />
  <img
    data-dominant-color="594e4a"
    data-has-transparency="false"
    decoding="async"
    width="1300"
    height="873"
    src="http://localhost:8888/wp-content/uploads/2024/08/image-1-jpeg.webp"
    alt=""
    class="not-transparent wp-image-155"
    srcset="
      http://localhost:8888/wp-content/uploads/2024/08/image-1-jpeg.webp     1300w,
      http://localhost:8888/wp-content/uploads/2024/08/image-1-300x201.webp   300w,
      http://localhost:8888/wp-content/uploads/2024/08/image-1-1024x688.webp 1024w,
      http://localhost:8888/wp-content/uploads/2024/08/image-1-768x516.webp   768w
    "
    sizes="(max-width: 650px) 100vw, 650px"
/></picture>

After:

<img
  data-dominant-color="594e4a"
  data-has-transparency="false"
  style="--dominant-color: #594e4a"
  decoding="async"
  width="1300"
  height="873"
  src="http://localhost:8888/wp-content/uploads/2024/08/image-1-jpeg.webp"
  alt=""
  class="not-transparent wp-image-155"
  srcset="
    http://localhost:8888/wp-content/uploads/2024/08/image-1-jpeg.webp     1300w,
    http://localhost:8888/wp-content/uploads/2024/08/image-1-300x201.webp   300w,
    http://localhost:8888/wp-content/uploads/2024/08/image-1-1024x688.webp 1024w,
    http://localhost:8888/wp-content/uploads/2024/08/image-1-768x516.webp   768w
  "
  sizes="(max-width: 650px) 100vw, 650px"
/>

Settings screen:

image

@mukeshpanchal27
Copy link
Member

#1450 (comment) the core issue is fixed now in https://core.trac.wordpress.org/changeset/58893

@mukeshpanchal27 mukeshpanchal27 merged commit 4f4a54f into WordPress:trunk Aug 14, 2024
@westonruter westonruter changed the title Don't wrap picture element if jpeg fallback not available Don't wrap PICTURE element if JPEG fallback is not available Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) [Type] Bug An existing feature is broken

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enabling picture element after having uploaded images while JPEG fallbacks were disabled results in invalid markup

4 participants