Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix raw attribute sources with multiple nodes #82

Merged
merged 4 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/parser/content-parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,14 @@ protected function source_block_raw( $crawler ) {
$attribute_value = null;

if ( $crawler->count() > 0 ) {
$attribute_value = trim( $crawler->outerHtml() );
// $crawler's outerHtml() will only return the HTML of the first node in this raw HTML.
// If the raw HTML contains multiple top-level nodes, we need to use the inner HTML of the wrapping
// 'body' tag. This will also preserve internal whitespace in the HTML.
$body_node = $crawler->closest( 'body' );

if ( $body_node && $body_node->count() > 0 ) {
$attribute_value = trim( $body_node->html() );
}
}

return $attribute_value;
Expand Down
57 changes: 57 additions & 0 deletions tests/parser/sources/test-source-raw.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,61 @@ public function test_parse_raw_source__nested() {
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
}

public function test_parse_raw_source_multiple_top_level_nodes() {
$this->register_block_with_attributes( 'test/html', [
'content' => [
'type' => 'string',
'source' => 'raw',
],
] );

$html = '
<!-- wp:test/html -->
<p>Node 1</p><p>Node 2</p>
<!-- /wp:test/html -->';

$expected_blocks = [
[
'name' => 'test/html',
'attributes' => [
'content' => '<p>Node 1</p><p>Node 2</p>',
],
],
];

$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
}

public function test_parse_raw_source_multiple_top_level_nodes_with_whitespace() {
$this->register_block_with_attributes( 'test/html', [
'content' => [
'type' => 'string',
'source' => 'raw',
],
] );

$html = '
<!-- wp:test/html -->
<span class="highlight">This</span> <span>should</span> <span>retain</span>&nbsp;<span>inner</span>
<span>whitespace</span>
<!-- /wp:test/html -->';

$expected_blocks = [
[
'name' => 'test/html',
'attributes' => [
'content' => "<span class=\"highlight\">This</span> <span>should</span> <span>retain</span>&nbsp;<span>inner</span>\n<span>whitespace</span>",
],
],
];

$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
}
}
Loading