-
Notifications
You must be signed in to change notification settings - Fork 93
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
Use correct sizes for small images #1252
Use correct sizes for small images #1252
Conversation
Thanks @mukeshpanchal27. It looks like the intent of this PR to constrain the |
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
@joemcgill The PR is ready for review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After discussion with @mukeshpanchal27 and reviewing this PR in more detail, I've left the following comment on the original issue with suggestions for a different approach that we should take to resolve this issue.
@joemcgill @westonruter The PR is ready for review. As discussed in this comment, I have updated the PR to add custom attributes to the @joemcgill In this commit, I have removed the multiple parsing and added an early check to skip if the custom attribute is not present. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tested locally, and this seems to be working as expected for most of the use cases we're trying to solve with this PR. I've left some code quality feedback that would be good to address before merging.
// Disable lazy loading attribute. | ||
add_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a risky filter to disable while running these tests, because we'll be missing scenarios where the loading attribute changes the way the markup gets created. I assume you did this to avoid having auto sizes added while running these tests? If so, instead of removing the wp_img_tag_add_loading_attr
filter, I would suggest running these without the auto-sizes filter, which seems to be what is causing potential failures here.
// Disable lazy loading attribute. | |
add_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); | |
// Disable auto sizes in these tests. | |
remove_filter( 'wp_content_img_tag', 'auto_sizes_update_content_img_tag' ); |
However, since the hooks global won't get reset after this class runs, you'll also need to add this hook back after the class runs, or else it will affect any tests that run afterwards. Ex:
public static function tear_down_after_class(): void {
parent::tear_down_after_class();
add_filter( 'wp_content_img_tag', 'auto_sizes_update_content_img_tag' );
}
If there are specific tests where you need to disable lazy-loading, it would be better to modify hooks directly in that test, because then they will automatically get reset during the WP_UnitTestCase_Base::tear_down()
method that runs after each test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @joemcgill for suggestion. This will not working as the filter is re-added through the_content
filter so it show error in tests.
1) Tests_Improve_Sizes::test_image_block_with_full_alignment with data set "Return wideSize 1280px instead of medium size 300px" ('medium')
Failed asserting that '<figure class="wp-block-image size-medium"><img loading="lazy" decoding="async" width="300" height="200" src="http://localhost:8889/wp-content/uploads/2024/06/leaves-283-300x200.jpg" alt="" class="wp-image-5" srcset="http://localhost:8889/wp-content/uploads/2024/06/leaves-283-300x200.jpg 300w, http://localhost:8889/wp-content/uploads/2024/06/leaves-283-1024x683.jpg 1024w, http://localhost:8889/wp-content/uploads/2024/06/leaves-283-768x512.jpg 768w, http://localhost:8889/wp-content/uploads/2024/06/leaves-283.jpg 1080w" sizes="auto, 100vw" /></figure>' contains "sizes="100vw" ".
/var/www/html/wp-content/plugins/performance/plugins/auto-sizes/tests/test-improve-sizes.php:53
2) Tests_Improve_Sizes::test_image_block_with_full_alignment with data set "Return wideSize 1280px instead of large size 1024px" ('large')
Failed asserting that '<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="683" src="http://localhost:8889/wp-content/uploads/2024/06/leaves-283-1024x683.jpg" alt="" class="wp-image-5" srcset="http://localhost:8889/wp-content/uploads/2024/06/leaves-283-1024x683.jpg 1024w, http://localhost:8889/wp-content/uploads/2024/06/leaves-283-300x200.jpg 300w, http://localhost:8889/wp-content/uploads/2024/06/leaves-283-768x512.jpg 768w, http://localhost:8889/wp-content/uploads/2024/06/leaves-283.jpg 1080w" sizes="auto, 100vw" /></figure>' contains "sizes="100vw" ".
/var/www/html/wp-content/plugins/performance/plugins/auto-sizes/tests/test-improve-sizes.php:53
3) Tests_Improve_Sizes::test_image_block_with_full_alignment with data set "Return wideSize 1280px instead of full size 1080px" ('full')
Failed asserting that '<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1080" height="720" src="http://localhost:8889/wp-content/uploads/2024/06/leaves-283.jpg" alt="" class="wp-image-5" srcset="http://localhost:8889/wp-content/uploads/2024/06/leaves-283.jpg 1080w, http://localhost:8889/wp-content/uploads/2024/06/leaves-283-300x200.jpg 300w, http://localhost:8889/wp-content/uploads/2024/06/leaves-283-1024x683.jpg 1024w, http://localhost:8889/wp-content/uploads/2024/06/leaves-283-768x512.jpg 768w" sizes="auto, 100vw" /></figure>' contains "sizes="100vw" ".
/var/www/html/wp-content/plugins/performance/plugins/auto-sizes/tests/test-improve-sizes.php:53
@joemcgill @westonruter Thanks for the feedback. I have updated the PR as per your directions. It is now ready for review. |
… fix/small-image-sizes * 'trunk' of https://github.com/WordPress/performance: (113 commits) Further refine command explaining type casting Elaborate on comment explaining why type casting is added Add try/catch block around imagecolorsforindex() Fix grammar typos Fix static analysis issues in Test_WebP_Uploads_Image_Edit Eliminate checks for seemingly impossible false return values Update PHPStan to 1.11.5 Add missing '@' Apply suggestions from code review Update testcase Fix wp:gallery styling not applying Update banner image Extend check code for WordPress 6.6 Harmonize return descriptions Bump Optimization Detective 0.3.1 Log URL metrics group collection to console when WP_DEBUG Include non-intersecting elements in URL metrics Bump version to 0.1.1 Fix logic error for preg_match() return value Add failing test case for a non-background image style ...
…mage-sizes * feature/more-accurate-sizes-attribute:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is working as expected. I reverted the changes to the composer.lock
file that were added because I think they were unintentional and doesn't seem to be related to this change. If that was an incorrect assumption we can update composer dependencies in a follow-up task.
|
||
return $processor->get_updated_html(); | ||
} | ||
// Run filter prior to auto sizes "auto_sizes_update_content_img_tag" filter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💖
9e4ee2b
into
feature/more-accurate-sizes-attribute
Summary
Fixes: #1251
For initial work: