diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index fcc4c9198d4cc0..86023738370090 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -697,14 +697,10 @@ private static function get_responsive_container_markup( $attributes, $inner_blo if ( ! empty( $attributes['overlay'] ) ) { // Get blocks from the overlay template part. $overlay_blocks = static::get_overlay_blocks_from_template_part( $attributes['overlay'], $attributes ); - // Check if overlay contains a navigation-overlay-close block. - $has_custom_overlay_close_block = block_core_navigation_block_tree_has_block_type( - $overlay_blocks, - 'core/navigation-overlay-close', - array( 'core/navigation' ) // Skip navigation blocks, as they cannot contain an overlay close block - ); // Render template part blocks directly without navigation container wrapper. $overlay_blocks_html = static::get_template_part_blocks_html( $overlay_blocks ); + // Check if overlay contains a navigation-overlay-close block (detect in rendered HTML so it works with patterns). + $has_custom_overlay_close_block = block_core_navigation_overlay_html_has_close_block( $overlay_blocks_html ); // Add Interactivity API directives to the overlay close block if present. if ( $has_custom_overlay_close_block && $is_interactive ) { $tags = new WP_HTML_Tag_Processor( $overlay_blocks_html ); @@ -1094,6 +1090,28 @@ function block_core_navigation_get_inner_blocks_from_unstable_location( $attribu } } +/** + * Checks if the overlay HTML contains a navigation-overlay-close block. + * + * Uses WP_HTML_Tag_Processor to detect the close button in rendered output, + * so it works when the overlay uses patterns (pattern content is rendered at + * output time, not in the block tree). + * + * @since 7.0.0 + * + * @param string $html The rendered overlay HTML. + * @return bool True if a close button element is found. + */ +function block_core_navigation_overlay_html_has_close_block( $html ) { + $tags = new WP_HTML_Tag_Processor( $html ); + return $tags->next_tag( + array( + 'tag_name' => 'BUTTON', + 'class_name' => 'wp-block-navigation-overlay-close', + ) + ); +} + /** * Add Interactivity API directives to the navigation-overlay-close block * markup using the Tag Processor. diff --git a/phpunit/blocks/class-wp-navigation-block-renderer-test.php b/phpunit/blocks/class-wp-navigation-block-renderer-test.php index e6f7c1dd5144fb..fc81a57f659194 100644 --- a/phpunit/blocks/class-wp-navigation-block-renderer-test.php +++ b/phpunit/blocks/class-wp-navigation-block-renderer-test.php @@ -253,6 +253,98 @@ public function test_gutenberg_block_core_navigation_block_tree_has_block_type_r $this->assertFalse( $result ); } + /** + * Test that gutenberg_block_core_navigation_overlay_html_has_close_block returns true when HTML contains the close button element. + * + * @group navigation-renderer + * + * @covers ::gutenberg_block_core_navigation_overlay_html_has_close_block + */ + public function test_gutenberg_block_core_navigation_overlay_html_has_close_block_returns_true_when_close_button_present() { + $html = '
'; + $result = gutenberg_block_core_navigation_overlay_html_has_close_block( $html ); + $this->assertTrue( $result ); + } + + /** + * Test that gutenberg_block_core_navigation_overlay_html_has_close_block returns false when HTML does not contain the close button. + * + * @group navigation-renderer + * + * @covers ::gutenberg_block_core_navigation_overlay_html_has_close_block + */ + public function test_gutenberg_block_core_navigation_overlay_html_has_close_block_returns_false_when_absent() { + $html = 'No close button here
Use the wp-block-navigation-overlay-close button to close
'; + $result = gutenberg_block_core_navigation_overlay_html_has_close_block( $html ); + $this->assertFalse( $result ); + } + + /** + * Test that gutenberg_block_core_navigation_overlay_html_has_close_block finds nested close button. + * + * @group navigation-renderer + * + * @covers ::gutenberg_block_core_navigation_overlay_html_has_close_block + */ + public function test_block_core_navigation_overlay_html_has_close_block_finds_nested_close_button() { + $html = '