Skip to content

Commit

Permalink
Merge pull request #71 from Automattic/fix/asterisk-attribute-selector
Browse files Browse the repository at this point in the history
Fix asterisk attribute selector
  • Loading branch information
alecgeatches authored Aug 2, 2024
2 parents 184bca0 + 003e525 commit 68982d4
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/parser/content-parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ protected function source_block( $block, $registered_blocks, $filter_options ) {
$crawler = new Crawler( sprintf( '<!doctype html><html><body>%s</body></html>', $block['innerHTML'] ) );

// Enter the <body> tag for block parsing.
$crawler = $crawler->filter( 'body' );
$crawler = $crawler->filter( 'body' )->children();

$attribute_value = $this->source_attribute( $crawler, $block_attribute_definition );

Expand Down Expand Up @@ -313,11 +313,11 @@ protected function source_attribute( $crawler, $block_attribute_definition ) {

$attribute_value = $this->source_block_attribute( $crawler, $block_attribute_definition );
} elseif ( 'rich-text' === $attribute_source ) {
$attribute_value = $this->source_block_rich_text( $crawler, $block_attribute_definition );
} elseif ( 'html' === $attribute_source ) {
// Most 'html' sources were converted to 'rich-text' in WordPress 6.5.
// https://github.com/WordPress/gutenberg/pull/43204

$attribute_value = $this->source_block_rich_text( $crawler, $block_attribute_definition );
} elseif ( 'html' === $attribute_source ) {
$attribute_value = $this->source_block_html( $crawler, $block_attribute_definition );
} elseif ( 'text' === $attribute_source ) {
$attribute_value = $this->source_block_text( $crawler, $block_attribute_definition );
Expand Down Expand Up @@ -552,7 +552,7 @@ protected function source_block_raw( $crawler ) {
$attribute_value = null;

if ( $crawler->count() > 0 ) {
$attribute_value = trim( $crawler->html() );
$attribute_value = trim( $crawler->outerHtml() );
}

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

public function test_parse_attribute_source__with_asterisk_selector() {
$this->register_block_with_attributes( 'test/image', [
'anchor' => [
'type' => 'string',
'source' => 'attribute',
'selector' => '*',
'attribute' => 'id',
],
] );

$html = '
<!-- wp:test/image -->
<img src="/image.jpg" id="anchor123" />
<!-- /wp:test/image -->
';

$expected_blocks = [
[
'name' => 'test/image',
'attributes' => [
'anchor' => 'anchor123',
],
],
];

$content_parser = new ContentParser( $this->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 );
}
}
59 changes: 59 additions & 0 deletions tests/parser/test-content-parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,63 @@ public function test_parse_multiple_blocks() {
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
}

/* Default values and missing values */

public function test_parse_block_missing_attributes_and_defaults() {
$this->register_block_with_attributes( 'test/block-with-empty-attributes', [
'attributeOneWithDefaultValueAndSource' => [
'type' => 'string',
'source' => 'attribute',
'selector' => 'div',
'attribute' => 'data-attr-one',
'default' => 'Default Attribute 1 Value',
],
'attributeTwoWithDefaultValueAndNoSource' => [
'type' => 'string',
'source' => 'attribute',
'selector' => 'div',
'attribute' => 'data-attr-two',
'default' => 'Default Attribute 2 Value',
],
'attributeThreeWithNoDefaultValueAndSource' => [
'type' => 'string',
'source' => 'attribute',
'selector' => 'div',
'attribute' => 'data-attr-three',
],
'attributeFourWithNoDefaultValueAndNoSource' => [
'type' => 'string',
'source' => 'attribute',
'selector' => 'div',
'attribute' => 'data-attr-four',
],
] );

$html = '
<!-- wp:test/block-with-empty-attributes -->
<div
data-attr-one="Attribute 1 Value"
data-attr-three="Attribute 3 Value"
>Content</div>
<!-- /wp:test/block-with-empty-attributes -->
';

$expected_blocks = [
[
'name' => 'test/block-with-empty-attributes',
'attributes' => [
'attributeOneWithDefaultValueAndSource' => 'Attribute 1 Value',
'attributeTwoWithDefaultValueAndNoSource' => 'Default Attribute 2 Value',
'attributeThreeWithNoDefaultValueAndSource' => 'Attribute 3 Value',
// attributeFourWithNoDefaultValueAndNoSource has no default, not represented
],
],
];

$content_parser = new ContentParser( $this->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 68982d4

Please sign in to comment.