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

Don't try to parse strings that don't contain blocks #3903

Merged
merged 4 commits into from
Dec 11, 2017
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
13 changes: 13 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ function unregister_block_type( $name ) {
* @return array Array of parsed block objects.
*/
function gutenberg_parse_blocks( $content ) {
/*
* If there are no blocks in the content, return a single block, rather
* than wasting time trying to parse the string.
*/
if ( ! gutenberg_content_has_blocks( $content ) ) {
return array(
array(
'attrs' => array(),
'innerHTML' => $content,
),
);
}

$parser = new Gutenberg_PEG_Parser;
return $parser->parse( _gutenberg_utf8_split( $content ) );
}
Expand Down
3 changes: 2 additions & 1 deletion phpunit/class-parsing-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ function test_parser_output( $html_filename, $parsed_json_filename ) {
$html = self::strip_r( file_get_contents( $html_path ) );
$expected_parsed = json_decode( self::strip_r( file_get_contents( $parsed_json_path ) ), true );

$result = gutenberg_parse_blocks( $html );
$parser = new Gutenberg_PEG_Parser;
$result = $parser->parse( _gutenberg_utf8_split( $html ) );

$this->assertEquals(
$expected_parsed,
Expand Down