-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Add fetchpriority=low to IMG tags in collapsed Details blocks
#76269
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f993152
Add fetchpriority=low to IMG tags in collapsed Details blocks
westonruter d374939
Clarify comments
westonruter e4f9806
Add unit tests for Details block
westonruter 575c4a4
Use filter instead of render callback for Details block
westonruter e03eb44
Remove parens from covers tag
westonruter ecd8703
Use not-same assertion instead of null
westonruter 2df7344
Add test case to preserve fetchpriority=high in expanded Details block
westonruter b8e8442
End comment with period not comma
westonruter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| <?php | ||
| /** | ||
| * Server-side rendering of the `core/details` block. | ||
| * | ||
| * @package WordPress | ||
| */ | ||
|
|
||
| /** | ||
| * Sets fetchpriority="low" on all IMG tags within the collapsed Details block. | ||
| * | ||
| * Images in a collapsed Details block are hidden until the block is expanded, so they should | ||
| * not compete with any resources in the critical rendering path, such as the LCP element image. | ||
| * | ||
| * @since 7.0.0 | ||
| * | ||
| * @param string $block_content The block content. | ||
| * @param array $block The full block, including name and attributes. | ||
| * @return string Modified HTML with fetchpriority="low" on all IMG tags when the showContent attribute is false. | ||
| */ | ||
| function block_core_details_set_img_fetchpriority_low( $block_content, array $block ): string { | ||
| if ( ! is_string( $block_content ) ) { | ||
| return ''; | ||
| } | ||
|
|
||
| // If the Details block is open by default, short-circuit to let core add fetchpriority=high if appropriate. | ||
| if ( $block['attrs']['showContent'] ?? false ) { | ||
| return $block_content; | ||
| } | ||
|
|
||
| $tags = new WP_HTML_Tag_Processor( $block_content ); | ||
| while ( $tags->next_tag( 'IMG' ) ) { | ||
| $tags->set_attribute( 'fetchpriority', 'low' ); | ||
| } | ||
| return $tags->get_updated_html(); | ||
| } | ||
|
|
||
| add_filter( 'render_block_core/details', 'block_core_details_set_img_fetchpriority_low', 10, 2 ); | ||
|
|
||
| /** | ||
| * Registers the `core/details` block on server. | ||
| * | ||
| * @since 7.0.0 | ||
| */ | ||
| function register_block_core_details() { | ||
| register_block_type_from_metadata( __DIR__ . '/details' ); | ||
| } | ||
| add_action( 'init', 'register_block_core_details' ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| <?php | ||
| /** | ||
| * Details block rendering tests. | ||
| * | ||
| * @package WordPress | ||
| * @subpackage Blocks | ||
| */ | ||
|
|
||
| /** | ||
| * Tests for the Details block. | ||
| * | ||
| * @group blocks | ||
| */ | ||
| class Tests_Blocks_Render_Details extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * @covers ::block_core_details_set_img_fetchpriority_low | ||
| */ | ||
|
westonruter marked this conversation as resolved.
|
||
| public function test_should_add_fetchpriority_low_to_img_in_collapsed_details_block(): void { | ||
| $details_block = <<<'BLOCK_CONTENT' | ||
| <!-- wp:details --> | ||
| <details class="wp-block-details"><summary>Collapsed</summary><!-- wp:image {"linkDestination":"none"} --> | ||
| <figure class="wp-block-image size-large"><img src="https://example.com/image.jpg" alt="" /></figure> | ||
| <!-- /wp:image --></details> | ||
| <!-- /wp:details --> | ||
| BLOCK_CONTENT; | ||
|
|
||
| $rendered_block = do_blocks( $details_block ); | ||
|
|
||
| $processor = new WP_HTML_Tag_Processor( $rendered_block ); | ||
| $this->assertTrue( $processor->next_tag( 'IMG' ) ); | ||
| $this->assertSame( 'low', $processor->get_attribute( 'fetchpriority' ) ); | ||
| } | ||
|
westonruter marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * @covers ::block_core_details_set_img_fetchpriority_low | ||
| */ | ||
| public function test_should_not_add_fetchpriority_low_to_img_in_expanded_details_block(): void { | ||
| $details_block = <<<'BLOCK_CONTENT' | ||
| <!-- wp:details {"showContent":true} --> | ||
| <details class="wp-block-details" open><summary>Expanded</summary><!-- wp:image {"linkDestination":"none"} --> | ||
| <figure class="wp-block-image size-large"><img src="https://example.com/image.jpg" alt="" /></figure> | ||
| <!-- /wp:image --></details> | ||
| <!-- /wp:details --> | ||
| BLOCK_CONTENT; | ||
|
|
||
| $rendered_block = do_blocks( $details_block ); | ||
|
|
||
| $processor = new WP_HTML_Tag_Processor( $rendered_block ); | ||
| $this->assertTrue( $processor->next_tag( 'IMG' ) ); | ||
| $this->assertNotSame( 'low', $processor->get_attribute( 'fetchpriority' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * @covers ::block_core_details_set_img_fetchpriority_low | ||
| */ | ||
| public function test_should_preserve_fetchpriority_high_on_img_in_expanded_details_block(): void { | ||
| $details_block = <<<'BLOCK_CONTENT' | ||
| <!-- wp:details {"showContent":true} --> | ||
| <details class="wp-block-details" open><summary>Expanded</summary><!-- wp:image {"linkDestination":"none"} --> | ||
| <figure class="wp-block-image size-large"><img src="https://example.com/image.jpg" fetchpriority="high" alt="" /></figure> | ||
| <!-- /wp:image --></details> | ||
| <!-- /wp:details --> | ||
| BLOCK_CONTENT; | ||
|
|
||
| $rendered_block = do_blocks( $details_block ); | ||
|
|
||
| $processor = new WP_HTML_Tag_Processor( $rendered_block ); | ||
| $this->assertTrue( $processor->next_tag( 'IMG' ) ); | ||
| $this->assertSame( 'high', $processor->get_attribute( 'fetchpriority' ) ); | ||
| } | ||
|
westonruter marked this conversation as resolved.
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.