From 4b20d75cd5c7908aef1d602524d0af20e1a51be4 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Mon, 6 Nov 2023 14:05:00 +0100 Subject: [PATCH 1/3] Use tagName if exists --- packages/block-library/src/query/index.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/query/index.php b/packages/block-library/src/query/index.php index 201ceed737a12a..b6a5733632ff44 100644 --- a/packages/block-library/src/query/index.php +++ b/packages/block-library/src/query/index.php @@ -44,7 +44,11 @@ function render_block_core_query( $attributes, $content, $block ) { $block->block_type->supports['interactivity'] = true; // Add a div to announce messages using `aria-live`. - $last_div_position = strripos( $content, '' ); + $html_tag = 'div'; + if ( ! empty( $attributes['tagName'] ) ) { + $html_tag = esc_attr( $attributes['tagName'] ); + } + $last_tag_position = strripos( $content, '' ); $content = substr_replace( $content, '
', - $last_div_position, + $last_tag_position, 0 ); } From efb9df58bc5f8fbbdf045624f6185769cfb8fa70 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Mon, 6 Nov 2023 18:42:14 +0100 Subject: [PATCH 2/3] Added a test using string positions, we could refactor --- phpunit/blocks/render-query-test.php | 38 ++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/phpunit/blocks/render-query-test.php b/phpunit/blocks/render-query-test.php index 8a148e89be87bf..23094e2d3809cb 100644 --- a/phpunit/blocks/render-query-test.php +++ b/phpunit/blocks/render-query-test.php @@ -131,6 +131,44 @@ public function test_rendering_query_with_enhanced_pagination_auto_disabled_when $this->assertSame( 'true', $p->get_attribute( 'data-wp-navigation-disabled' ) ); } + + /** + * Tests that the `core/query` last tag is rendered with the tagName attribute + * if is defined, having a div as default. + */ + public function test_enhanced_query_markup_rendering_at_bottom_on_custom_html_element_tags() { + global $wp_query, $wp_the_query; + + $content = << + + +HTML; + + // Set main query to single post. + $wp_query = new WP_Query( + array( + 'posts_per_page' => 1, + ) + ); + + $wp_the_query = $wp_query; + + $output = do_blocks( $content ); + + $aside_closing_tag = ''; + $pos = strrpos( $output, $aside_closing_tag ); + $last_closing_tag_pos = strrpos( $output, '>', -( strlen( $output ) - $pos ) ); + $last_opening_tag_pos = strrpos( $output, '<', -( strlen( $output ) - $last_closing_tag_pos ) ); + $previous_tag = substr( $output, $last_opening_tag_pos, $last_closing_tag_pos - $last_opening_tag_pos + 1 ); + + $this->assertSame( '', $previous_tag ); + } + /** * Tests that the `core/query` block adds an extra attribute to disable the * enhanced pagination in the browser when a post content block is found inside. From ab2d4f5e3a0974c2ecdf1c9622dab100a89b6535 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Mon, 6 Nov 2023 22:16:45 +0100 Subject: [PATCH 3/3] Refactor to use Tag Processor --- phpunit/blocks/render-query-test.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/phpunit/blocks/render-query-test.php b/phpunit/blocks/render-query-test.php index 23094e2d3809cb..2d81bfdb513b33 100644 --- a/phpunit/blocks/render-query-test.php +++ b/phpunit/blocks/render-query-test.php @@ -145,8 +145,10 @@ public function test_enhanced_query_markup_rendering_at_bottom_on_custom_html_el + Helper to get last HTML Tag + HTML; // Set main query to single post. @@ -160,13 +162,16 @@ public function test_enhanced_query_markup_rendering_at_bottom_on_custom_html_el $output = do_blocks( $content ); - $aside_closing_tag = ''; - $pos = strrpos( $output, $aside_closing_tag ); - $last_closing_tag_pos = strrpos( $output, '>', -( strlen( $output ) - $pos ) ); - $last_opening_tag_pos = strrpos( $output, '<', -( strlen( $output ) - $last_closing_tag_pos ) ); - $previous_tag = substr( $output, $last_opening_tag_pos, $last_closing_tag_pos - $last_opening_tag_pos + 1 ); + $p = new WP_HTML_Tag_Processor( $output ); + + $p->next_tag( 'span' ); - $this->assertSame( '', $previous_tag ); + // Test that there is a div added just after the last tag inside the aside. + $this->assertSame( $p->next_tag(), true ); + // Test that that div is the accesibility one. + $this->assertSame( 'screen-reader-text', $p->get_attribute( 'class' ) ); + $this->assertSame( 'context.core.query.message', $p->get_attribute( 'data-wp-text' ) ); + $this->assertSame( 'polite', $p->get_attribute( 'aria-live' ) ); } /**