From c69749777c104d34747ee9639970225b9dcd9586 Mon Sep 17 00:00:00 2001 From: ingeniumed Date: Wed, 18 Nov 2026 11:03:14 +1100 Subject: [PATCH] Reuse one DOM per block when sourcing attributes --- src/parser/content-parser.php | 18 +++++-- tests/parser/test-dom-reuse.php | 89 +++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 tests/parser/test-dom-reuse.php diff --git a/src/parser/content-parser.php b/src/parser/content-parser.php index 44185ec..b96cf91 100644 --- a/src/parser/content-parser.php +++ b/src/parser/content-parser.php @@ -357,6 +357,8 @@ protected function apply_sourced_attributes( WP_Block $block ): array { $block_definition_attributes = $block_definition->attributes ?? []; $block_attributes = $block->attributes; + $crawler = null; + foreach ( $block_definition_attributes as $block_attribute_name => $block_attribute_definition ) { $attribute_source = $block_attribute_definition['source'] ?? null; $attribute_default_value = $block_attribute_definition['default'] ?? null; @@ -387,13 +389,19 @@ protected function apply_sourced_attributes( WP_Block $block ): array { } } - // Specify a manual doctype so that the parser will use the HTML5 parser. - $crawler = new Crawler( sprintf( '%s', $block->inner_html ) ); + if ( 'meta' === $attribute_source ) { + $attribute_value = $this->source_block_meta( $block_attribute_definition ) ?? $attribute_default_value; + } else { + if ( null === $crawler ) { + // Specify a manual doctype so that the parser will use the HTML5 parser. + $crawler = new Crawler( sprintf( '%s', $block->inner_html ) ); - // Enter the tag for block parsing. - $crawler = $crawler->filter( 'body' )->children(); + // Enter the tag for block parsing. + $crawler = $crawler->filter( 'body' )->children(); + } - $attribute_value = $this->source_attribute( $crawler, $block_attribute_definition ); + $attribute_value = $this->source_attribute( $crawler, $block_attribute_definition ); + } if ( null !== $attribute_value ) { $block_attributes[ $block_attribute_name ] = $attribute_value; diff --git a/tests/parser/test-dom-reuse.php b/tests/parser/test-dom-reuse.php new file mode 100644 index 0000000..55c48b7 --- /dev/null +++ b/tests/parser/test-dom-reuse.php @@ -0,0 +1,89 @@ + [ + 'type' => 'string', + 'source' => 'html', + 'selector' => 'h2', + ], + 'url' => [ + 'type' => 'string', + 'source' => 'attribute', + 'selector' => 'a', + 'attribute' => 'href', + ], + 'items' => [ + 'type' => 'array', + 'source' => 'query', + 'selector' => 'li', + 'query' => [ 'text' => [ 'source' => 'text' ] ], + ], + 'content' => [ + 'type' => 'string', + 'source' => 'rich-text', + 'selector' => 'p', + ], + 'raw' => [ + 'type' => 'string', + 'source' => 'raw', + ], + ]; + $this->register_block_with_attributes( 'test/forward', $attributes ); + $this->register_block_with_attributes( 'test/reverse', array_reverse( $attributes, true ) ); + $html = '

Heading one

Link

Text two

'; + $expected = [ + 'content' => 'Text two', + 'items' => [ [ 'text' => 'First' ], [ 'text' => 'Second' ] ], + 'raw' => $html, + 'title' => 'Heading one', + 'url' => '/example?a=1&b=2', + ]; + foreach ( [ 'forward', 'reverse' ] as $name ) { + $result = ( new ContentParser() )->parse( "{$html}" ); + $this->assertSame( $expected, $result['blocks'][0]['attributes'] ); + } + } + + /** @dataProvider metadata_provider */ + public function test_metadata_and_dom_sources_keep_values_and_defaults( $value, $present, $expected ) { + $this->register_block_with_attributes( 'test/mixed-meta', [ + 'meta' => [ + 'type' => 'string', + 'source' => 'meta', + 'meta' => 'dom_reuse_value', + 'default' => 'Default', + ], + 'title' => [ + 'type' => 'string', + 'source' => 'text', + 'selector' => 'h2', + ], + ] ); + $post_id = self::factory()->post->create(); + if ( $present ) { + update_post_meta( $post_id, 'dom_reuse_value', $value ); + } + $result = ( new ContentParser() )->parse( '

Heading

', $post_id ); + $this->assertSame( [ + 'meta' => $expected, + 'title' => 'Heading', + ], $result['blocks'][0]['attributes'] ); + } + + public function metadata_provider() { + return [ + 'empty' => [ '', true, '' ], + 'zero' => [ '0', true, '0' ], + 'missing' => [ null, false, 'Default' ], + ]; + } +}