-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Return inner HTML before and after inner blocks when parsing and fix … #8760
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
<div class="wp-block-column"> | ||
|
||
<p>Column One, Paragraph One</p> | ||
|
||
<p>Column One, Paragraph Two</p> | ||
|
||
|
||
</div> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
<div class="wp-block-columns has-3-columns"> | ||
|
||
<div class="wp-block-column"> | ||
|
||
<p>Column One, Paragraph One</p> | ||
|
||
<p>Column One, Paragraph Two</p> | ||
|
||
|
||
</div> | ||
|
||
<div class="wp-block-column"> | ||
|
||
<p>Column Two, Paragraph One</p> | ||
|
||
<p>Column Three, Paragraph One</p> | ||
|
||
|
||
</div> | ||
|
||
|
||
</div> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -266,14 +266,37 @@ private function peg_f3($blockName, $attrs) { | |
); | ||
} | ||
private function peg_f4($s, $children, $e) { | ||
list( $innerHTML, $innerBlocks ) = peg_array_partition( $children, 'is_string' ); | ||
|
||
return array( | ||
$innerHTMLBeforeInnerBlocks = array(); | ||
$innerHTMLAfterInnerBlocks = array(); | ||
$innerBlocks = array(); | ||
|
||
$inner_found = false; | ||
foreach ( $children as $child ) { | ||
if( is_string( $child ) ) { | ||
if ( $inner_found ) { | ||
$innerHTMLAfterInnerBlocks[] = $child; | ||
} else { | ||
$innerHTMLBeforeInnerBlocks[] = $child; | ||
} | ||
} else { | ||
$innerBlocks[] = $child; | ||
$inner_found = true; | ||
} | ||
} | ||
|
||
|
||
$block = array( | ||
'blockName' => $s['blockName'], | ||
'attrs' => $s['attrs'], | ||
'innerBlocks' => $innerBlocks, | ||
'innerHTML' => implode( '', $innerHTML ), | ||
'innerHTML' => implode( '', $innerHTMLBeforeInnerBlocks ) . implode( '', $innerHTMLAfterInnerBlocks ), | ||
); | ||
|
||
if( $inner_found ) { | ||
$block['innerHTMLBeforeInnerBlocks'] = implode( '', $innerHTMLBeforeInnerBlocks ); | ||
$block['innerHTMLAfterInnerBlocks'] = implode( '', $innerHTMLAfterInnerBlocks ); | ||
} | ||
return $block; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hm…it seems like we are trying to manually edit automatically-generated code here. @brucepearson did you get a chance to look at the spec grammar which is run through There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @dmsnell. Is the concept OK that I've applied here? I can then translate to the grammar file. |
||
} | ||
private function peg_f5($blockName, $attrs) { | ||
return array( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
/** | ||
* gutenberg_render_block tests | ||
* | ||
* @package Gutenberg | ||
*/ | ||
|
||
class Render_Test extends WP_UnitTestCase { | ||
protected static $fixtures_dir; | ||
|
||
/** | ||
* @test | ||
* @dataProvider inner_block_files | ||
*/ | ||
public function it_renders_inner_blocks( $blocks_file, $rendered_file ) { | ||
self::$fixtures_dir = dirname( dirname( __FILE__ ) ) . '/core-blocks/test/fixtures'; | ||
|
||
require_once dirname( dirname( __FILE__ ) ) . '/lib/blocks.php'; | ||
|
||
$blocks = json_decode( self::strip_r( file_get_contents( self::$fixtures_dir . $blocks_file ) ), true ); | ||
$expected = self::strip_r( file_get_contents( self::$fixtures_dir . $rendered_file ) ); | ||
|
||
$content = ''; | ||
foreach ( $blocks as $block ) { | ||
$content .= gutenberg_render_block( $block ); | ||
} | ||
|
||
$this->assertEquals( $expected, $content ); | ||
|
||
} | ||
|
||
function strip_r( $input ) { | ||
return str_replace( "\r", '', $input ); | ||
} | ||
|
||
public function inner_block_files() { | ||
return array( | ||
array( '/core__column.parsed.json', '/core__column.rendered.html' ), | ||
array( '/core__columns.parsed.json', '/core__columns.rendered.html' ), | ||
); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
although we shouldn't expect deeply nested blocks we may want to consider the possibility of their existence. suppose someone made a post with 1000 levels of nesting - that could theoretically cause WordPress to crash on account of overflowing the call stack.
I'd recommend considering one of two changes to the behavior here:
it should be noted that this work overlaps what's happening in #8083. if we get a fast internal PHP parser we might dramatically rewrite this function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dmsnell Is it worth continuing with this considering you might change the PHP parser. The intention of this change is to fix rendering of inner blocks that doesn't happen at all right now.