-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Block Bindings: Add private HTML API subclass with experimental set_inner_html #7742
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
sirreal
wants to merge
7
commits into
WordPress:trunk
Choose a base branch
from
sirreal:block-bindings/html-api-private-set-inner-html
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b49b317
Add html api processor for setting inner html
sirreal 28e93f2
Working block bindings set_inner_html implementation
sirreal 62cc6d3
Fix next_tag selector
sirreal 0fd7a10
Simplify set_inner_html function
sirreal c49dc0b
Add todo
sirreal 058d473
Documentation
sirreal c0a96fb
DO_NOT_MERGE: Make create fragment method public
sirreal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Add html api processor for setting inner html
- Loading branch information
commit b49b3174a27e677d35fc87b035add949745a9edc
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -607,3 +607,114 @@ public function render( $options = array() ) { | |
| return $block_content; | ||
| } | ||
| } | ||
|
|
||
| // phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound | ||
| class PrivateProcessor extends WP_HTML_Processor { | ||
|
|
||
| public function set_inner_html( ?string $html ) { | ||
|
||
| if ( $this->is_virtual() ) { | ||
| return false; | ||
| } | ||
|
|
||
| if ( $this->get_token_type() !== '#tag' ) { | ||
| return false; | ||
| } | ||
|
|
||
| if ( $this->is_tag_closer() ) { | ||
| return false; | ||
| } | ||
|
|
||
| if ( ! $this->expects_closer() ) { | ||
| return false; | ||
| } | ||
|
|
||
| // @todo check if this is necessary | ||
| /*if (*/ | ||
| /* 'html' !== $this->state->current_token->namespace &&*/ | ||
| /* $this->state->current_token->has_self_closing_flag*/ | ||
| /*) {*/ | ||
| /* return false;*/ | ||
| /*}*/ | ||
|
|
||
| if ( null === $html ) { | ||
| $html = ''; | ||
| } | ||
| if ( '' !== $html ) { | ||
| $fragment_parser = $this->spawn_fragment_parser( $html ); | ||
| if ( | ||
| null === $fragment_parser | ||
| ) { | ||
| return false; | ||
| } | ||
|
|
||
| try { | ||
| $html = $fragment_parser->serialize(); | ||
| } catch ( Exception $e ) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| // @todo apply modifications if there are any??? | ||
| if ( ! parent::set_bookmark( 'SET_INNER_HTML: opener' ) ) { | ||
| return false; | ||
| } | ||
|
|
||
| if ( ! $this->proceed_to_matching_closer() ) { | ||
| parent::seek( 'SET_INNER_HTML: opener' ); | ||
| return false; | ||
| } | ||
|
|
||
| if ( ! parent::set_bookmark( 'SET_INNER_HTML: closer' ) ) { | ||
| return false; | ||
| } | ||
|
|
||
| $inner_html_start = $this->bookmarks['SET_INNER_HTML: opener']->start + $this->bookmarks['SET_INNER_HTML: opener']->length; | ||
| $inner_html_length = $this->bookmarks['SET_INNER_HTML: closer']->start - $inner_html_start; | ||
|
|
||
| $this->lexical_updates['innerHTML'] = new WP_HTML_Text_Replacement( | ||
| $inner_html_start, | ||
| $inner_html_length, | ||
| $html | ||
| ); | ||
|
|
||
| parent::seek( 'SET_INNER_HTML: opener' ); | ||
| parent::release_bookmark( 'SET_INNER_HTML: opener' ); | ||
| parent::release_bookmark( 'SET_INNER_HTML: closer' ); | ||
|
|
||
| // @todo check for whether that html will make a mess! | ||
| // Will it break out of tags? | ||
| return true; | ||
| } | ||
|
|
||
| public function proceed_to_matching_closer(): bool { | ||
| $tag_name = $this->get_tag(); | ||
|
|
||
| if ( null === $tag_name ) { | ||
| return false; | ||
| } | ||
|
|
||
| if ( $this->is_tag_closer() ) { | ||
| return false; | ||
| } | ||
|
|
||
| if ( ! $this->expects_closer() ) { | ||
| return false; | ||
| } | ||
|
|
||
| $breadcrumbs = $this->get_breadcrumbs(); | ||
| array_pop( $breadcrumbs ); | ||
|
|
||
| // @todo Can't use these queries together | ||
| while ( $this->next_tag( | ||
| array( | ||
| 'tag_name' => $this->get_tag(), | ||
| 'tag_closers' => 'visit', | ||
| ) | ||
| ) ) { | ||
| if ( $this->get_breadcrumbs() === $breadcrumbs ) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#6838 uses a pattern for a private processor class:
wordpress-develop/src/wp-includes/class-wp-block.php
Lines 338 to 341 in 825f78e