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

Fixed issue when converting blockquotes with child text nodes #36

Merged
merged 4 commits into from
Feb 18, 2025
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.2

### Fixed

- Fixed issue when converting blockquotes with child text nodes.

## 1.5.1

### Fixed
Expand Down
13 changes: 12 additions & 1 deletion src/class-block-converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,18 @@ public function convert_with_children( DOMNode $node ): string {

// Recursively convert the children of the node.
foreach ( $node->childNodes as $child ) {
$child_block = $this->{$child->nodeName}( $child );
if ( '#text' === $child->nodeName ) {
$children .= $child->nodeValue;

continue;
}

// Ensure that the cite tag is not converted to a block.
if ( 'cite' === strtolower( $child->nodeName ) ) {
$children .= trim( static::get_node_html( $child ) );
}

$child_block = $this->convert_node( $child );

if ( ! empty( $child_block ) ) {
$children .= $this->minify_block( (string) $child_block );
Expand Down
19 changes: 19 additions & 0 deletions tests/feature/BlockConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public static function converter_data_provider() {
'<blockquote><p>Lorem ipsum</p></blockquote>',
'<!-- wp:quote --><blockquote class="wp-block-quote"><!-- wp:paragraph --><p>Lorem ipsum</p><!-- /wp:paragraph --></blockquote><!-- /wp:quote -->',
],
'blockquote with cite' => [
'<blockquote><p>Lorem ipsum</p><cite>Source</cite></blockquote>',
'<!-- wp:quote --><blockquote class="wp-block-quote"><!-- wp:paragraph --><p>Lorem ipsum</p><!-- /wp:paragraph --><cite>Source</cite></blockquote><!-- /wp:quote -->',
],
'non-oembed-embed' => [
'<embed type="video/webm" src="/media/mr-arnold.mp4" width="250" height="200" />',
'<!-- wp:html --><embed type="video/webm" src="/media/mr-arnold.mp4" width="250" height="200"></embed><!-- /wp:html -->',
Expand Down Expand Up @@ -379,4 +383,19 @@ public static function macroable_dataprovider(): array {
'hr',
] )->map_with_keys( fn ( $tag ) => [ $tag => [ $tag ] ] )->all();
}

public function test_convert_with_children(): void {
$html = <<<HTML
<blockquote><p><em>Sint sint nulla voluptate nulla adipisicing non proident excepteur duis fugiat fugiat qui minim reprehenderit. Irure adipisicing mollit ipsum eiusmod consequat reprehenderit elit anim irure deserunt in deserunt. In dolore ut quis ex quis laboris ex eu. Minim culpa cillum eu.</em></p>
<blockquote>
<blockquote><p><em>-proident excepteur duis fugiat fugiat qui minim reprehenderit </em></p></blockquote>
</blockquote>
</blockquote>
HTML;

$block = ( new Block_Converter( $html ) )->convert();

$this->assertNotEmpty( $block );
$this->assertMatchesSnapshot( $block );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:paragraph -->Sint sint nulla voluptate nulla adipisicing non proident excepteur duis fugiat fugiat qui minim reprehenderit. Irure adipisicing mollit ipsum eiusmod consequat reprehenderit elit anim irure deserunt in deserunt. In dolore ut quis ex quis laboris ex eu. Minim culpa cillum eu.-proident excepteur duis fugiat fugiat qui minim reprehenderit<!-- /wp:paragraph -->
Loading