From f99315295329c4099989ce2b9ce382c91ba7e599 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 6 Mar 2026 16:17:52 -0800 Subject: [PATCH 1/8] Add fetchpriority=low to IMG tags in collapsed Details blocks --- packages/block-library/src/details/index.php | 49 ++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 packages/block-library/src/details/index.php diff --git a/packages/block-library/src/details/index.php b/packages/block-library/src/details/index.php new file mode 100644 index 00000000000000..edeaf294067e96 --- /dev/null +++ b/packages/block-library/src/details/index.php @@ -0,0 +1,49 @@ + 'render_block_core_details', + ) + ); +} + +add_action( 'init', 'register_block_core_details' ); + +/** + * Sets fetchpriority="low" on all IMG tags within the collapsed Details block. + * + * Images in the overlay are hidden until the menu is opened, so they should + * not compete with any actual LCP element image on the page. + * + * @since 7.0.0 + * + * @param array $attributes The block attributes. + * @param string $content The saved content. + * @return string Modified HTML with fetchpriority="low" on all IMG tags. + */ +function render_block_core_details( array $attributes, string $content ): string { + // If the Details block is open by default, short-circuit to let core add fetchpriority=high if appropriate, + if ( $attributes['showContent'] ?? false ) { + return $content; + } + + $tags = new WP_HTML_Tag_Processor( $content ); + while ( $tags->next_tag( 'IMG' ) ) { + $tags->set_attribute( 'fetchpriority', 'low' ); + } + return $tags->get_updated_html(); +} From d374939358c9906bdd633efbd97ca77241e17c99 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 6 Mar 2026 17:31:23 -0800 Subject: [PATCH 2/8] Clarify comments --- packages/block-library/src/details/index.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/details/index.php b/packages/block-library/src/details/index.php index edeaf294067e96..6be76aa487017d 100644 --- a/packages/block-library/src/details/index.php +++ b/packages/block-library/src/details/index.php @@ -26,14 +26,14 @@ function register_block_core_details() { /** * Sets fetchpriority="low" on all IMG tags within the collapsed Details block. * - * Images in the overlay are hidden until the menu is opened, so they should - * not compete with any actual LCP element image on the page. + * 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 array $attributes The block attributes. * @param string $content The saved content. - * @return string Modified HTML with fetchpriority="low" on all IMG tags. + * @return string Modified HTML with fetchpriority="low" on all IMG tags when the showContent attribute is false. */ function render_block_core_details( array $attributes, string $content ): string { // If the Details block is open by default, short-circuit to let core add fetchpriority=high if appropriate, From e4f980609a2bdc563908591cf1c72c9b3f6c1466 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sun, 8 Mar 2026 20:33:29 -0700 Subject: [PATCH 3/8] Add unit tests for Details block --- phpunit/blocks/render-block-details-test.php | 53 ++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 phpunit/blocks/render-block-details-test.php diff --git a/phpunit/blocks/render-block-details-test.php b/phpunit/blocks/render-block-details-test.php new file mode 100644 index 00000000000000..02e702d68bf9d2 --- /dev/null +++ b/phpunit/blocks/render-block-details-test.php @@ -0,0 +1,53 @@ + +
Collapsed +
+
+ + 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' ) ); + } + + /** + * @covers ::render_block_core_details() + */ + public function test_should_not_add_fetchpriority_low_to_img_in_expanded_details_block(): void { + $details_block = <<<'BLOCK_CONTENT' + +
Expanded +
+
+ + BLOCK_CONTENT; + + $rendered_block = do_blocks( $details_block ); + + $processor = new WP_HTML_Tag_Processor( $rendered_block ); + $this->assertTrue( $processor->next_tag( 'IMG' ) ); + $this->assertNull( $processor->get_attribute( 'fetchpriority' ) ); + } +} From 575c4a4324505b9b1a78145620579ec47f685407 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 9 Mar 2026 18:23:29 -0700 Subject: [PATCH 4/8] Use filter instead of render callback for Details block Co-authored-by: t-hamano --- packages/block-library/src/details/index.php | 46 ++++++++++---------- phpunit/blocks/render-block-details-test.php | 4 +- 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/packages/block-library/src/details/index.php b/packages/block-library/src/details/index.php index 6be76aa487017d..010bcedf3fe5bc 100644 --- a/packages/block-library/src/details/index.php +++ b/packages/block-library/src/details/index.php @@ -5,24 +5,6 @@ * @package WordPress */ -/** - * Register the details block. - * - * @since 7.0.0 - * - * @uses render_block_core_details() - */ -function register_block_core_details() { - register_block_type_from_metadata( - __DIR__ . '/details', - array( - 'render_callback' => 'render_block_core_details', - ) - ); -} - -add_action( 'init', 'register_block_core_details' ); - /** * Sets fetchpriority="low" on all IMG tags within the collapsed Details block. * @@ -31,19 +13,35 @@ function register_block_core_details() { * * @since 7.0.0 * - * @param array $attributes The block attributes. - * @param string $content The saved content. + * @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 render_block_core_details( array $attributes, string $content ): string { +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 ( $attributes['showContent'] ?? false ) { - return $content; + if ( $block['attrs']['showContent'] ?? false ) { + return $block_content; } - $tags = new WP_HTML_Tag_Processor( $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' ); diff --git a/phpunit/blocks/render-block-details-test.php b/phpunit/blocks/render-block-details-test.php index 02e702d68bf9d2..793c20595c22aa 100644 --- a/phpunit/blocks/render-block-details-test.php +++ b/phpunit/blocks/render-block-details-test.php @@ -14,7 +14,7 @@ class Tests_Blocks_Render_Details extends WP_UnitTestCase { /** - * @covers ::render_block_core_details() + * @covers ::block_core_details_set_img_fetchpriority_low() */ public function test_should_add_fetchpriority_low_to_img_in_collapsed_details_block(): void { $details_block = <<<'BLOCK_CONTENT' @@ -33,7 +33,7 @@ public function test_should_add_fetchpriority_low_to_img_in_collapsed_details_bl } /** - * @covers ::render_block_core_details() + * @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' From e03eb44f731b2e7fbe56c4101bfa279ea80a301b Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 9 Mar 2026 18:43:20 -0700 Subject: [PATCH 5/8] Remove parens from covers tag Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- phpunit/blocks/render-block-details-test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpunit/blocks/render-block-details-test.php b/phpunit/blocks/render-block-details-test.php index 793c20595c22aa..d746683d39027c 100644 --- a/phpunit/blocks/render-block-details-test.php +++ b/phpunit/blocks/render-block-details-test.php @@ -14,7 +14,7 @@ class Tests_Blocks_Render_Details extends WP_UnitTestCase { /** - * @covers ::block_core_details_set_img_fetchpriority_low() + * @covers ::block_core_details_set_img_fetchpriority_low */ public function test_should_add_fetchpriority_low_to_img_in_collapsed_details_block(): void { $details_block = <<<'BLOCK_CONTENT' @@ -33,7 +33,7 @@ public function test_should_add_fetchpriority_low_to_img_in_collapsed_details_bl } /** - * @covers ::block_core_details_set_img_fetchpriority_low() + * @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' From ecd87039f720279fa7a4a33f3539428b4f86e356 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 9 Mar 2026 18:45:09 -0700 Subject: [PATCH 6/8] Use not-same assertion instead of null Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- phpunit/blocks/render-block-details-test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit/blocks/render-block-details-test.php b/phpunit/blocks/render-block-details-test.php index d746683d39027c..9f72dd3279e9e8 100644 --- a/phpunit/blocks/render-block-details-test.php +++ b/phpunit/blocks/render-block-details-test.php @@ -48,6 +48,6 @@ public function test_should_not_add_fetchpriority_low_to_img_in_expanded_details $processor = new WP_HTML_Tag_Processor( $rendered_block ); $this->assertTrue( $processor->next_tag( 'IMG' ) ); - $this->assertNull( $processor->get_attribute( 'fetchpriority' ) ); + $this->assertNotSame( 'low', $processor->get_attribute( 'fetchpriority' ) ); } } From 2df734422ca6c65bc10b2df5638d01fc5a716dad Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 9 Mar 2026 18:48:18 -0700 Subject: [PATCH 7/8] Add test case to preserve fetchpriority=high in expanded Details block --- phpunit/blocks/render-block-details-test.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/phpunit/blocks/render-block-details-test.php b/phpunit/blocks/render-block-details-test.php index 9f72dd3279e9e8..8124a371532f96 100644 --- a/phpunit/blocks/render-block-details-test.php +++ b/phpunit/blocks/render-block-details-test.php @@ -50,4 +50,23 @@ public function test_should_not_add_fetchpriority_low_to_img_in_expanded_details $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' + +
Expanded +
+
+ + 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' ) ); + } } From b8e8442fa9ac2050ea7c27b52719f6de71e84639 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 9 Mar 2026 18:50:28 -0700 Subject: [PATCH 8/8] End comment with period not comma --- packages/block-library/src/details/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/details/index.php b/packages/block-library/src/details/index.php index 010bcedf3fe5bc..3b0ed59f321c5d 100644 --- a/packages/block-library/src/details/index.php +++ b/packages/block-library/src/details/index.php @@ -22,7 +22,7 @@ function block_core_details_set_img_fetchpriority_low( $block_content, array $bl return ''; } - // If the Details block is open by default, short-circuit to let core add fetchpriority=high if appropriate, + // 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; }