Skip to content

Commit 5625b82

Browse files
dmsnellsirreal
andcommitted
Add tests for extract_full_block_and_advance() (WordPress#10769)
Trac ticket: Core-64537 Initially ported from sirreal#20 Developed in: WordPress#10769 Discussed in: https://core.trac.wordpress.org/ticket/64537#ticket Follow-up to [[60939]](https://core.trac.wordpress.org/changeset/60939). Props jonsurrell, dmsnell. Fixes Core-64537 Co-authored-by: Jon Surrell <jonsurrell@git.wordpress.org> Github-PR: 10769 Github-PR-URL: WordPress#10769 Trac-Ticket: 64537 Trac-Ticket-URL: https://core.trac.wordpress.org/ticket/64537 Branch-Name: blocks/add-block-processor-tests
1 parent 8a5274e commit 5625b82

2 files changed

Lines changed: 120 additions & 0 deletions

File tree

src/wp-includes/class-wp-block-processor.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,17 @@ public function extract_full_block_and_advance(): ?array {
12951295
$block['innerBlocks'][] = $inner_block;
12961296
$block['innerContent'][] = null;
12971297
}
1298+
1299+
/*
1300+
* Because the parser has advanced past the closing block token, any void
1301+
* tokens, such as HTML spans or void blocks, may need to be processed
1302+
* before advancing again at the start of the next loop iteration.
1303+
*/
1304+
if ( $this->is_html() ) {
1305+
$chunk = $this->get_html_content();
1306+
$block['innerHTML'] .= $chunk;
1307+
$block['innerContent'][] = $chunk;
1308+
}
12981309
}
12991310

13001311
return $block;

tests/phpunit/tests/block-processor/wpBlockProcessor.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,115 @@ public function test_scans_directly_to_requested_block_type( string $html, strin
12961296
);
12971297
}
12981298

1299+
/**
1300+
* Ensures that block extraction matches the behavior of the default block parser.
1301+
*
1302+
* @ticket {TICKET_NUMBER}
1303+
*
1304+
* @dataProvider data_various_block_posts
1305+
*
1306+
* @param string $test_document An HTML document to parse as blocks.
1307+
*/
1308+
public function test_extracts_equivalent_parses_as_parse_blocks( string $test_document ) {
1309+
$processor = new WP_Block_Processor( $test_document );
1310+
$blocks = array();
1311+
1312+
while ( $processor->next_block( '*' ) ) {
1313+
$blocks[] = $processor->extract_full_block_and_advance();
1314+
}
1315+
1316+
$this->assertSame(
1317+
parse_blocks( $test_document ),
1318+
$blocks,
1319+
'Failed to properly parse the block structure.'
1320+
);
1321+
}
1322+
1323+
/**
1324+
* Data provider.
1325+
*
1326+
* @return Generator
1327+
*/
1328+
public static function data_various_block_posts() {
1329+
yield 'Empty post' => array( '' );
1330+
1331+
yield 'Void block' => array( '<!-- wp:void /-->' );
1332+
1333+
yield 'Empty block' => array( '<!-- wp:empty --><!-- /wp:empty -->' );
1334+
1335+
yield 'Paragraph block' => array( '<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->' );
1336+
1337+
yield 'Paragraph block with attributes' => array(
1338+
'<!-- wp:paragraph {"dropCaps": true} --><p>Test</p><!-- /wp:paragraph -->',
1339+
);
1340+
1341+
yield 'Group with void inner' => array(
1342+
'<!-- wp:group --><!-- wp:void /--><!-- /wp:group -->',
1343+
);
1344+
1345+
/*
1346+
* @todo There is a hidden bug in here, which is possibly a problem in
1347+
* the default parser. There are HTML spans of newlines between
1348+
* these block delimiters, and without them, the parse doesn’t
1349+
* match `parse_blocks()`. However, `parse_blocks()` is inconsistent
1350+
* in its behavior. Whereas it produces an empty text chunk here,
1351+
* in the case of a void inner block it produces none. The test is
1352+
* being adjusted to step around this issue so that it can be resolved
1353+
* separately, and until it’s clear if there is an implementation issue
1354+
* with `parse_blocks()` itself.
1355+
*/
1356+
yield 'Empty columns' => array(
1357+
<<<HTML
1358+
<!-- wp:columns -->
1359+
<!-- wp:column -->
1360+
<!-- /wp:column -->
1361+
<!-- /wp:columns -->
1362+
HTML,
1363+
);
1364+
1365+
yield 'Contentful columns' => array(
1366+
<<<HTML
1367+
<!-- wp:columns -->
1368+
<ul>
1369+
<!-- wp:column -->
1370+
<li>A good point.</li>
1371+
<!-- wp:column /-->
1372+
</ul>
1373+
<!-- /wp:columns -->
1374+
HTML,
1375+
);
1376+
1377+
yield 'Group with mixed content' => array(
1378+
<<<HTML
1379+
<!-- wp:group -->
1380+
<div>
1381+
<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->
1382+
This is freeform.
1383+
<!-- wp:void /-->
1384+
End
1385+
<!-- wp:footer -->
1386+
<footer>That&rsquo;s it!</footer>
1387+
<!-- /wp:footer -->
1388+
<!-- wp:another-void /-->
1389+
</div>
1390+
<!-- /wp:group -->
1391+
HTML,
1392+
);
1393+
1394+
yield 'Nested blocks' => array(
1395+
<<<HTML
1396+
<!-- wp:a -->
1397+
<div>
1398+
<!-- wp:b -->
1399+
<span><!-- wp:c /--></span>
1400+
<!-- /wp:b -->
1401+
</div>
1402+
<!-- /wp:a -->
1403+
HTML,
1404+
1405+
);
1406+
}
1407+
12991408
/**
13001409
* Data provider.
13011410
*

0 commit comments

Comments
 (0)