Skip to content

Commit

Permalink
Merge pull request #82 from Automattic/fix/raw-source-multiple-nodes
Browse files Browse the repository at this point in the history
Fix raw attribute sources with multiple nodes
  • Loading branch information
alecgeatches authored Oct 18, 2024
2 parents 079599c + a4939bb commit 7070759
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
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 );
}
}

0 comments on commit 7070759

Please sign in to comment.