Skip to content

Commit ddc91c9

Browse files
authored
Merge pull request #93 from Automattic/fix/parser-allow-empty-content
Parser: Allow empty content
2 parents ff4262f + 5a0811f commit ddc91c9

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/parser/content-parser.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,12 @@ public function parse( $post_content, $post_id = null, $filter_options = [] ) {
146146

147147
$this->warnings = [];
148148

149-
$has_blocks = has_blocks( $post_content );
149+
// An empty `post_content` fails `has_blocks` check, but is still valid.
150+
$content_is_empty = empty( trim( $post_content ) );
150151

151-
if ( ! $has_blocks ) {
152+
$has_content = ! empty( trim( $post_content ) );
153+
154+
if ( $has_content && ! has_blocks( $post_content ) ) {
152155
$error_message = join(' ', [
153156
sprintf( 'Error parsing post ID %d: This post does not appear to contain block content.', $post_id ),
154157
'The VIP Block Data API is designed to parse Gutenberg blocks and can not read classic editor content.',

tests/parser/test-content-parser.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace WPCOMVIP\BlockDataApi;
99

10+
use WP_Error;
11+
1012
/**
1113
* Content parser tests that are not source-specific.
1214
*/
@@ -215,4 +217,36 @@ public function test_parse_whitespace_block_removal() {
215217
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
216218
$this->assertEquals( $expected_blocks, $blocks['blocks'], sprintf( 'Blocks do not match: %s', wp_json_encode( $blocks ) ) );
217219
}
220+
221+
/* Classic editor content */
222+
223+
public function test_parse_classic_editor_content() {
224+
$html = 'Hello, world!';
225+
226+
$content_parser = new ContentParser( $this->get_block_registry() );
227+
$blocks = $content_parser->parse( $html );
228+
229+
$this->assertInstanceOf( WP_Error::class, $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
230+
231+
$errors = $blocks->get_error_messages();
232+
$this->assertEquals( 1, count( $errors ), 'Expected error messages but found none.' );
233+
$this->assertStringContainsString(
234+
'This post does not appear to contain block content.',
235+
$errors[0],
236+
sprintf( 'Unexpected error messages: %s', wp_json_encode( $errors ) )
237+
);
238+
}
239+
240+
/* Empty content */
241+
242+
public function test_parse_empty_content() {
243+
$html = '';
244+
$expected_blocks = [];
245+
246+
$content_parser = new ContentParser( $this->get_block_registry() );
247+
$blocks = $content_parser->parse( $html );
248+
249+
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
250+
$this->assertEquals( $expected_blocks, $blocks['blocks'], sprintf( 'Blocks do not match: %s', wp_json_encode( $blocks ) ) );
251+
}
218252
}

0 commit comments

Comments
 (0)