From dc643e9d190fae65952106cce5297a18a6730441 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Tue, 18 Feb 2025 12:26:59 -0500 Subject: [PATCH 1/4] Fixed issue when converting blockquotes with child text nodes --- CHANGELOG.md | 6 ++++++ src/class-block-converter.php | 6 ++++++ tests/feature/BlockConverterTest.php | 14 ++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f877a0b..3483a53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/class-block-converter.php b/src/class-block-converter.php index 6c4fdec..04ed476 100644 --- a/src/class-block-converter.php +++ b/src/class-block-converter.php @@ -208,6 +208,12 @@ public function convert_with_children( DOMNode $node ): string { // Recursively convert the children of the node. foreach ( $node->childNodes as $child ) { + if ( '#text' === $child->nodeName ) { + $children .= $child->nodeValue; + + continue; + } + $child_block = $this->{$child->nodeName}( $child ); if ( ! empty( $child_block ) ) { diff --git a/tests/feature/BlockConverterTest.php b/tests/feature/BlockConverterTest.php index 85f0a60..1a8d7c0 100644 --- a/tests/feature/BlockConverterTest.php +++ b/tests/feature/BlockConverterTest.php @@ -379,4 +379,18 @@ public static function macroable_dataprovider(): array { 'hr', ] )->map_with_keys( fn ( $tag ) => [ $tag => [ $tag ] ] )->all(); } + + public function test_convert_with_children(): void { + $html = <<

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

+
+ +HTML; + + $block = ( new Block_Converter( $html ) )->convert(); + + $this->assertNotEmpty( $block ); + } } From 4d8f41df06f2399978052730762af2c59d71151b Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Tue, 18 Feb 2025 12:30:39 -0500 Subject: [PATCH 2/4] Remove the __call and use converft node directly --- src/class-block-converter.php | 2 +- tests/feature/BlockConverterTest.php | 1 + .../BlockConverterTest__test_convert_with_children__1.txt | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 tests/feature/__snapshots__/BlockConverterTest__test_convert_with_children__1.txt diff --git a/src/class-block-converter.php b/src/class-block-converter.php index 04ed476..cdb122e 100644 --- a/src/class-block-converter.php +++ b/src/class-block-converter.php @@ -214,7 +214,7 @@ public function convert_with_children( DOMNode $node ): string { continue; } - $child_block = $this->{$child->nodeName}( $child ); + $child_block = $this->convert_node( $child ); if ( ! empty( $child_block ) ) { $children .= $this->minify_block( (string) $child_block ); diff --git a/tests/feature/BlockConverterTest.php b/tests/feature/BlockConverterTest.php index 1a8d7c0..7388ae3 100644 --- a/tests/feature/BlockConverterTest.php +++ b/tests/feature/BlockConverterTest.php @@ -392,5 +392,6 @@ public function test_convert_with_children(): void { $block = ( new Block_Converter( $html ) )->convert(); $this->assertNotEmpty( $block ); + $this->assertMatchesSnapshot( $block ); } } diff --git a/tests/feature/__snapshots__/BlockConverterTest__test_convert_with_children__1.txt b/tests/feature/__snapshots__/BlockConverterTest__test_convert_with_children__1.txt new file mode 100644 index 0000000..eb804d2 --- /dev/null +++ b/tests/feature/__snapshots__/BlockConverterTest__test_convert_with_children__1.txt @@ -0,0 +1 @@ +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 \ No newline at end of file From ecf19457266025a834bc55ba75f300c01d079de6 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Tue, 18 Feb 2025 12:44:32 -0500 Subject: [PATCH 3/4] Ensure cite does not get stripped --- src/class-block-converter.php | 7 ++++++- tests/feature/BlockConverterTest.php | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/class-block-converter.php b/src/class-block-converter.php index cdb122e..84384eb 100644 --- a/src/class-block-converter.php +++ b/src/class-block-converter.php @@ -110,7 +110,7 @@ public function convert_node( DOMNode $node ): ?Block { '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, + 'br', 'source', 'cite' => null, 'hr' => $this->separator(), default => $this->html( $node ), }; @@ -214,6 +214,11 @@ public function convert_with_children( DOMNode $node ): string { 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 ) ) { diff --git a/tests/feature/BlockConverterTest.php b/tests/feature/BlockConverterTest.php index 7388ae3..ee55dec 100644 --- a/tests/feature/BlockConverterTest.php +++ b/tests/feature/BlockConverterTest.php @@ -92,6 +92,10 @@ public static function converter_data_provider() { '

Lorem ipsum

', '

Lorem ipsum

', ], + 'blockquote with cite' => [ + '

Lorem ipsum

Source
', + '

Lorem ipsum

Source
', + ], 'non-oembed-embed' => [ '', '', From 88cb20dd5e3c55138b31e0354a3d98fa35f2faf5 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Tue, 18 Feb 2025 12:44:56 -0500 Subject: [PATCH 4/4] Revert this one-off change --- src/class-block-converter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/class-block-converter.php b/src/class-block-converter.php index 84384eb..343fc48 100644 --- a/src/class-block-converter.php +++ b/src/class-block-converter.php @@ -110,7 +110,7 @@ public function convert_node( DOMNode $node ): ?Block { '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', 'source', 'cite' => null, + 'br', 'cite', 'source' => null, 'hr' => $this->separator(), default => $this->html( $node ), };