Skip to content

Commit 8ad758e

Browse files
authored
[BlockMarkupProcessor] Preserve self-closing block style when rewriting URLs (#191)
Before this PR, `BlockMarkupProcessor` (and `BlockMarkupUrlProcessor`) would lose the self-closing solidus when rewriting block attributes: ```php $p = new BlockMarkupUrlProcessor( '<!-- wp:navigation-link {"url": "https://w.org"} /-->' ); $p->next_url(); $p->set_url('https://new-site.org', WPURL::parse('https://new-site.org')); echo $p->__toString(); // <!-- wp:navigation-link {"url": "https://new-site.org"} --> ``` With this PR, the self-closing solidus is preserved: ```php $p = new BlockMarkupUrlProcessor( '<!-- wp:navigation-link {"url": "https://w.org"} /-->' ); $p->next_url(); $p->set_url('https://new-site.org', WPURL::parse('https://new-site.org')); echo $p->__toString(); // <!-- wp:navigation-link {"url": "https://new-site.org"} /--> ``` ## Testing instructions CI – I've added two more test cases to prevent regressions.
1 parent 85d938e commit 8ad758e

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

components/DataLiberation/BlockMarkup/class-blockmarkupprocessor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,8 @@ private function block_attribute_updates_to_modifiable_text_updates() {
496496
' ' .
497497
$this->block_name .
498498
' ' .
499-
$encoded_attributes
499+
$encoded_attributes .
500+
( $this->is_self_closing_block() ? '/' : '' )
500501
);
501502

502503
return true;

components/DataLiberation/Tests/BlockMarkupProcessorTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,24 @@ public function test_set_block_attribute_value_updates_a_list_value() {
358358
);
359359
}
360360

361+
public function test_set_block_attribute_value_updates_a_list_value_with_a_self_closing_block() {
362+
$p = new BlockMarkupProcessor(
363+
'<!-- wp:image {"sources": ["small.png", "large.png"] } /-->'
364+
);
365+
$this->assertTrue( $p->next_token(), 'Failed to find the block opener' );
366+
$this->assertTrue( $p->next_block_attribute(), 'Failed to find the first block attribute' );
367+
$this->assertTrue( $p->next_block_attribute(), 'Failed to find the second block attribute' );
368+
$this->assertTrue( $p->next_block_attribute(), 'Failed to find the third block attribute' );
369+
370+
$p->set_block_attribute_value( 'medium.png' );
371+
$this->assertEquals( 'medium.png', $p->get_block_attribute_value(), 'Failed to find the block attribute value' );
372+
$this->assertEquals(
373+
'<!-- wp:image {"sources":["small.png","medium.png"]} /-->',
374+
$p->get_updated_html(),
375+
'Failed to update the block attribute value'
376+
);
377+
}
378+
361379
public function test_set_block_attribute_can_be_called_multiple_times() {
362380
$p = new BlockMarkupProcessor(
363381
'<!-- wp:image {"sources": { "lowres": "small.png", "hires": "large.png" } } -->'

components/DataLiberation/Tests/BlockMarkupUrlProcessorTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ public static function provider_test_set_url_examples() {
193193
'https://w.org',
194194
'<!-- wp:image {"src":"https:\/\/w.org"} -->',
195195
),
196+
'In the "url" block attribute of a navigation-link block' => array(
197+
'<!-- wp:navigation-link {"url": "https://w.org"} /-->',
198+
'https://w.org',
199+
'<!-- wp:navigation-link {"url":"https:\/\/w.org"} /-->',
200+
),
196201
'In a text node' => array(
197202
'Have you seen https://wordpress.org yet?',
198203
'https://w.org',

0 commit comments

Comments
 (0)