Skip to content

Commit fc4beff

Browse files
committed
Blocks: Fix duplicated content when implicitly closing a final open block
When a fix was recently applied to the block parser for implicitly closing open blocks that remain open at the end of a document, a mistake was made when collecting HTML spans preceding the still-open blocks. This led to duplicated content in documents missing expected closing delimiters. This patch adjusts the implict closing algorithm to properly detect those HTML spans. Developed in: #15197 Discussed in: https://core.trac.wordpress.org/ticket/66138 Follow-up to r64996. Props chihsuan, dmsnell. See #66138. git-svn-id: https://develop.svn.wordpress.org/trunk@65090 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 92eb256 commit fc4beff

2 files changed

Lines changed: 116 additions & 19 deletions

File tree

‎src/wp-includes/class-wp-block-parser.php‎

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -395,40 +395,55 @@ public function add_block_from_stack( $end_offset = null ) {
395395
* @since 7.2.0
396396
*/
397397
public function implicitly_close_and_add_all_open_blocks(): void {
398-
$implicitly_closed = null;
399-
$last_freeform = null;
400-
398+
$last_closed_block = null;
399+
$last_opener_at = strlen( $this->document );
400+
$last_leading_html = null;
401+
402+
/**
403+
* There is a stack of open blocks, starting with a top-level block,
404+
* and where each successive item in the stack is a child of that block.
405+
* There are also spans of HTML content between the block delimiters.
406+
*
407+
* As the stack is collapsed, any HTML segments within that block must
408+
* be collected into it, before closing and appending to its parent’s
409+
* inner blocks array.
410+
*/
401411
while ( null !== ( $stack_top = array_pop( $this->stack ) ) ) {
402-
if ( isset( $last_freeform ) ) {
403-
$html = substr( $this->document, $stack_top->prev_offset, $last_freeform[1] - $stack_top->prev_offset );
404-
} else {
405-
$html = substr( $this->document, $stack_top->prev_offset );
406-
}
412+
/*
413+
* Since there is no open inner block on the top-most block on the stack,
414+
* the first iteration’s remaining HTML span will be from the last `prev_offset`
415+
* until the end of the document. For each containing parent, it will run from
416+
* the `prev_offset` until the opening of the block that was just closed.
417+
*/
418+
$html_length = $last_opener_at - $stack_top->prev_offset;
419+
if ( 0 !== $html_length ) {
420+
$html = substr( $this->document, $stack_top->prev_offset, $html_length );
407421

408-
if ( '' !== $html ) {
409422
$stack_top->block->innerHTML .= $html;
410423
$stack_top->block->innerContent[] = $html;
411424
}
412425

413-
// Trap potential leading freeform content for the final output.
414-
$last_freeform = array( $stack_top->leading_html_start ?? 0, $stack_top->token_start );
415-
416-
if ( isset( $implicitly_closed ) ) {
426+
if ( isset( $last_closed_block ) ) {
417427
$stack_top->block->innerContent[] = null;
418-
$stack_top->block->innerBlocks[] = (array) $implicitly_closed;
428+
$stack_top->block->innerBlocks[] = (array) $last_closed_block;
419429
}
420430

421-
$implicitly_closed = $stack_top->block;
431+
$last_closed_block = $stack_top->block;
432+
$last_opener_at = $stack_top->token_start;
433+
$last_leading_html = $stack_top->leading_html_start;
422434
}
423435

424-
if ( isset( $last_freeform ) && $last_freeform[1] > $last_freeform[0] ) {
425-
$html = substr( $this->document, $last_freeform[0], $last_freeform[1] - $last_freeform[0] );
436+
// Collect any HTML preceding the outermost open block.
437+
if ( isset( $last_leading_html ) && $last_opener_at > $last_leading_html ) {
438+
$html = substr( $this->document, $last_leading_html, $last_opener_at - $last_leading_html );
426439
$this->output[] = (array) $this->freeform( $html );
427440
}
428441

429-
if ( isset( $implicitly_closed ) ) {
430-
$this->output[] = (array) $implicitly_closed;
442+
if ( isset( $last_closed_block ) ) {
443+
$this->output[] = (array) $last_closed_block;
431444
}
445+
446+
$this->offset = strlen( $this->document );
432447
}
433448
}
434449

‎tests/phpunit/tests/blocks/wpBlockParser.php‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,88 @@ public function test_parses_basic_empty_block() {
4242
);
4343
}
4444

45+
/**
46+
* Ensures that implicitly closing a final open block does not repeat content parsed before it.
47+
*
48+
* @ticket 66138
49+
*
50+
* @dataProvider data_documents_with_implicit_closings
51+
*
52+
* @param string[] $tokens List of block and HTML tokens comprising a document, where the first
53+
* is a partial document ending in open blocks, while the rest are the
54+
* explicit closers corresponding to those open blocks.
55+
*/
56+
public function test_implicitly_closed_blocks_is_equivalent_to_explicitly_closed( $tokens ) {
57+
$parser = new WP_Block_Parser();
58+
$full_document = $parser->parse( implode( '', $tokens ) );
59+
$document = array_shift( $tokens );
60+
61+
foreach ( $tokens as $token ) {
62+
$document .= $token;
63+
64+
$this->assertSame(
65+
$full_document,
66+
$parser->parse( $document ),
67+
'Should have implicitly closed open blocks as if the closers had been there.'
68+
);
69+
}
70+
}
71+
72+
/**
73+
* Data provider.
74+
*
75+
* @return array<string, list<non-empty-string>>
76+
*/
77+
public static function data_documents_with_implicit_closings() {
78+
return array(
79+
'After a closed block' => array(
80+
array(
81+
'<!-- wp:paragraph -->A<!-- /wp:paragraph --><!-- wp:latest-posts -->',
82+
'<!-- /wp:latest-posts -->',
83+
),
84+
),
85+
'After leading HTML and a closed block' => array(
86+
array(
87+
'Hi<!-- wp:paragraph -->A<!-- /wp:paragraph --><!-- wp:latest-posts -->',
88+
'<!-- /wp:latest-posts -->',
89+
),
90+
),
91+
'After a closed block and HTML' => array(
92+
array(
93+
'<!-- wp:paragraph -->A<!-- /wp:paragraph -->Bye<!-- wp:latest-posts -->',
94+
'<!-- /wp:latest-posts -->',
95+
),
96+
),
97+
'Deeply-nested open blocks' => array(
98+
array(
99+
'0<!-- wp:a -->1<!-- wp:b -->2<!-- wp:c -->3<!-- wp:d -->4',
100+
'<!-- /wp:d -->',
101+
'<!-- /wp:c -->',
102+
'<!-- /wp:b -->',
103+
'<!-- /wp:a -->',
104+
),
105+
),
106+
'After a closed void block' => array(
107+
array(
108+
'<!-- wp:separator /--><!-- wp:group -->A',
109+
'<!-- /wp:group -->',
110+
),
111+
),
112+
'With the wrong closer' => array(
113+
array(
114+
'<!-- wp:opener -->A',
115+
'<!-- /wp:closer -->',
116+
),
117+
),
118+
'With an invalid closer' => array(
119+
array(
120+
'<!-- wp:opener -->A',
121+
'<!-- /wp:opener /-->',
122+
),
123+
),
124+
);
125+
}
126+
45127
/**
46128
* @dataProvider data_parsing_test_filenames
47129
* @ticket 45109

0 commit comments

Comments
 (0)