Skip to content

Block Bindings: Allow more generic setting of block attributes (use WP_HTML_Text_Replacement) #6

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

Draft
wants to merge 2 commits into
base: try/simplify-block-bindings-replace-html
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/wp-includes/class-wp-block-bindings-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@
* @since 6.9.0
*/
class WP_Block_Bindings_Processor extends WP_HTML_Processor {
private $output = '';
private $end_of_flushed = 0;

public function build() {
return $this->output . substr( $this->get_updated_html(), $this->end_of_flushed );
return $this->get_updated_html();
}

/**
Expand All @@ -51,20 +49,25 @@ public function replace_rich_text( $rich_text ) {

$this->set_bookmark( '_wp_block_bindings_tag_opener' );
// The bookmark names are prefixed with `_` so the key below has an extra `_`.
$bm = $this->bookmarks['__wp_block_bindings_tag_opener'];
$this->output .= substr( $this->get_updated_html(), $this->end_of_flushed, $bm->start + $bm->length );
$this->output .= $rich_text;
$bm = $this->bookmarks['__wp_block_bindings_tag_opener'];
$start = $bm->start + $bm->length;
$this->release_bookmark( '_wp_block_bindings_tag_opener' );

// Find matching tag closer.
while ( $this->next_token() && $this->get_current_depth() >= $depth ) {
}

$this->set_bookmark( '_wp_block_bindings_tag_closer' );
$bm = $this->bookmarks['__wp_block_bindings_tag_closer'];
$this->end_of_flushed = $bm->start;
$bm = $this->bookmarks['__wp_block_bindings_tag_closer'];
$end = $bm->start;
$this->release_bookmark( '_wp_block_bindings_tag_closer' );

$this->lexical_updates[] = new WP_HTML_Text_Replacement(
$start,
$end - $start,
$rich_text
);

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ public function test_replace_rich_text_and_seek() {
$this->assertTrue( $processor->replace_rich_text( '<strong>New</strong> image caption' ) );

$processor->seek( 'image' );
$processor->add_class( 'extra-img-class' );

$this->assertEquals(
$figure_opener .
$img .
'<img src="breakfast.jpg" alt="" class="wp-image-1 extra-img-class"/>' .
'<figcaption class="wp-element-caption"><strong>New</strong> image caption</figcaption>' .
$figure_closer,
$processor->build()
Expand Down