diff --git a/packages/block-library/src/details/index.php b/packages/block-library/src/details/index.php new file mode 100644 index 00000000000000..3b0ed59f321c5d --- /dev/null +++ b/packages/block-library/src/details/index.php @@ -0,0 +1,47 @@ +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 new file mode 100644 index 00000000000000..8124a371532f96 --- /dev/null +++ b/phpunit/blocks/render-block-details-test.php @@ -0,0 +1,72 @@ + +
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 ::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' + +
Expanded +
+
+ + 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' + +
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' ) ); + } +}