Skip to content

Commit

Permalink
Don't try to parse strings that don't contain blocks (#3903)
Browse files Browse the repository at this point in the history
* Short circuit gutenberg_parse_blocks() calls when there are no blocks to parse
* Fix linting errors.
* Add checks for <!--more--> and <!--noteaser-->
* Use gutenberg_content_has_blocks to check for existance of blocks
  • Loading branch information
pento authored and youknowriad committed Dec 11, 2017
1 parent 3f1302d commit a41173b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
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

0 comments on commit a41173b

Please sign in to comment.