Skip to content

Commit

Permalink
Framework: Strip comment demarcations in content filtering (#5042)
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth authored Feb 15, 2018
1 parent a7478db commit 14df09b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ function do_blocks( $content ) {
// Append remaining unmatched content.
$rendered_content .= $content;

// Strip remaining block comment demarcations.
$rendered_content = preg_replace( '/<!--\s+\/?wp:.*?-->\r?\n?/m', '', $rendered_content );

return $rendered_content;
}
add_filter( 'the_content', 'do_blocks', 9 ); // BEFORE do_shortcode().
25 changes: 25 additions & 0 deletions phpunit/class-do-blocks-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* `do_blocks` rendering test
*
* @package Gutenberg
*/

/**
* Test do_blocks
*/
class Do_Blocks_Test extends WP_UnitTestCase {
/**
* Test do_blocks removes comment demarcations.
*
* @covers ::do_blocks
*/
function test_do_blocks_removes_comments() {
$original_html = file_get_contents( dirname( __FILE__ ) . '/fixtures/do-blocks-original.html' );
$expected_html = file_get_contents( dirname( __FILE__ ) . '/fixtures/do-blocks-expected.html' );

$actual_html = do_blocks( $original_html );

$this->assertEquals( $expected_html, $actual_html );
}
}
12 changes: 12 additions & 0 deletions phpunit/fixtures/do-blocks-expected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<p>First Auto Paragraph</p>

<!--more-->

<p>First Gutenberg Paragraph</p>

<p>Second Auto Paragraph</p>


<p>Third Gutenberg Paragraph</p>

<p>Third Auto Paragraph</p>
17 changes: 17 additions & 0 deletions phpunit/fixtures/do-blocks-original.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<p>First Auto Paragraph</p>

<!--more-->

<!-- wp:core/paragraph -->
<p>First Gutenberg Paragraph</p>
<!-- /wp:core/paragraph -->

<p>Second Auto Paragraph</p>

<!-- wp:core/test-self-closing /-->

<!-- wp:core/paragraph -->
<p>Third Gutenberg Paragraph</p>
<!-- /wp:core/paragraph -->

<p>Third Auto Paragraph</p>

0 comments on commit 14df09b

Please sign in to comment.