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

Ensure macros can be used to replace any tag #32

Merged
merged 1 commit into from
Dec 16, 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

All notable changes to `WP Block Converter` will be documented in this file.

## 1.5.1

### Fixed

- Fixed bug where macros can be used to modify/replace any tag.

## 1.5.0

### Added
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ use Alley\WP\Block_Converter\Block_Converter;
use Alley\WP\Block_Converter\Block;

Block_Converter::macro( 'p', function ( \DOMNode $node ) {
if (special_condition()) {
if ( special_condition() ) {
return new Block( 'core/paragraph', [ 'attribute' => 123 ], 'This is a paragraph' );
}

Expand Down
51 changes: 21 additions & 30 deletions src/class-block-converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function convert(): string {
*
* @since 1.0.0
*
* @param string $html HTML converted into Gutenberg blocks.
* @param string $html HTML converted into Gutenberg blocks.
* @param DOMNodeList $content The original DOMNodeList.
*/
$html = trim( (string) apply_filters( 'wp_block_converter_document_html', $html, $content ) );
Expand All @@ -94,20 +94,37 @@ public function convert(): string {
* @param DOMNode $node The node to convert.
* @return Block|null
*/
protected function convert_node( DOMNode $node ): ?Block {
public function convert_node( DOMNode $node ): ?Block {
if ( '#text' === $node->nodeName ) {
return null;
}

if ( static::has_macro( $node->nodeName ) ) {
$block = static::macro_call( $node->nodeName, [ $node ] );
} else {
$block = match ( strtolower( $node->nodeName ) ) {
'ul' => $this->ul( $node ),
'ol' => $this->ol( $node ),
'img' => $this->img( $node ),
'blockquote' => $this->blockquote( $node ),
'h1', 'h2', 'h3', 'h4', 'h5', 'h6' => $this->h( $node ),
'p', 'a', 'abbr', 'b', 'code', 'em', 'i', 'strong', 'sub', 'sup', 'span', 'u' => $this->p( $node ),
'figure' => $this->figure( $node ),
'br', 'cite', 'source' => null,
'hr' => $this->separator(),
default => $this->html( $node ),
};
}

/**
* Hook to allow output customizations.
*
* @since 1.0.0
*
* @param Block|null $block The generated block object.
* @param DOMNode $node The node being converted.
* @param DOMNode $node The node being converted.
*/
$block = apply_filters( 'wp_block_converter_block', $this->{$node->nodeName}( $node ), $node );
$block = apply_filters( 'wp_block_converter_block', $block, $node );

if ( ! $block || ! $block instanceof Block ) {
return null;
Expand Down Expand Up @@ -180,32 +197,6 @@ protected function sideload_child_images( DOMNode $node ): void {
}
}

/**
* Magic function to call parsers for specific HTML tags.
*
* @param string $name The tag name.
* @param array $arguments The DOMNode.
* @return Block|null
*/
public function __call( $name, $arguments ): ?Block {
if ( static::has_macro( $name ) ) {
return static::macro_call( $name, $arguments );
}

return match ( $name ) {
'ul' => $this->ul( $arguments[0] ),
'ol' => $this->ol( $arguments[0] ),
'img' => $this->img( $arguments[0] ),
'blockquote' => $this->blockquote( $arguments[0] ),
'h1', 'h2', 'h3', 'h4', 'h5', 'h6' => $this->h( $arguments[0] ),
'p', 'a', 'abbr', 'b', 'code', 'em', 'i', 'strong', 'sub', 'sup', 'span', 'u' => $this->p( $arguments[0] ),
'figure' => $this->figure( $arguments[0] ),
'br', 'cite', 'source' => null,
'hr' => $this->separator(),
default => $this->html( $arguments[0] ),
};
}

/**
* Convert the children of a node to blocks.
*
Expand Down
61 changes: 61 additions & 0 deletions tests/feature/BlockConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Mantle\Testing\Concerns\Refresh_Database;
use PHPUnit\Framework\Attributes\DataProvider;

use function Mantle\Support\Helpers\collect;
use function Mantle\Testing\mock_http_response;

/**
Expand Down Expand Up @@ -159,6 +160,7 @@ public function test_convert_with_empty_paragraphs_of_arbitrary_length_to_block(

public function test_convert_with_filter_override_single_tag() {
$this->expectApplied( 'wp_block_converter_document_html' )->once();
$this->expectApplied( 'wp_block_converter_block' )->once()->andReturnInstanceOf( Block::class );

$html = '<p>Content to migrate</p><h1>Heading 01</h1>';

Expand Down Expand Up @@ -318,4 +320,63 @@ function (DOMNode $node) {
$block,
);
}

/**
* Test that all elements can be manually overridden with a macro.
*/
#[DataProvider( 'macroable_dataprovider' )]
public function test_macroable_override_built_in( string $tag ): void {
$is_single_tag = in_array( $tag, [ 'img', 'br', 'hr' ], true );

Block_Converter::macro(
$tag,
fn ( \DOMNode $node ) => new Block( 'core/paragraph', [], $is_single_tag ? $node->nodeName : $node->textContent ),
);

$block = ( new Block_Converter( $is_single_tag ? "<$tag />" : "<$tag>content here</$tag>" ) )->convert();

if ( $is_single_tag ) {
$this->assertEquals(
"<!-- wp:paragraph -->$tag<!-- /wp:paragraph -->",
$block,
);
} else {
$this->assertEquals(
"<!-- wp:paragraph -->content here<!-- /wp:paragraph -->",
$block,
);
}
}

public static function macroable_dataprovider(): array {
return collect( [
'ul',
'ol',
'img',
'blockquote',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'p',
'a',
'abbr',
'b',
'code',
'em',
'i',
'strong',
'sub',
'sup',
'span',
'u',
'figure',
'br',
'cite',
'source',
'hr',
] )->map_with_keys( fn ( $tag ) => [ $tag => [ $tag ] ] )->all();
}
}
Loading