-
Notifications
You must be signed in to change notification settings - Fork 3.2k
HTML API: Rely on HTML API for wp_html_split()
#6651
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
dmsnell
wants to merge
9
commits into
WordPress:trunk
Choose a base branch
from
dmsnell:html-api/replace-wp-html-split
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 all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8a0862c
HTML API: Rewrite wp_html_split relying on the HTML API.
dmsnell bc1c877
Fix off-by-1
sirreal 89d28fa
Fix interactivity
sirreal 8fa3657
Remove off-by-1 adjustment
sirreal 92f2c48
Fix tag token length error
sirreal 53b18ee
Properly split special atomic elements.
dmsnell 7670886
Rerarrange cod
dmsnell baefd6e
Add text nodes where missing, to preserve legacy behavior.
dmsnell db9c494
Remove off-by-1 adjustment
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
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -604,16 +604,116 @@ function wpautop( $text, $br = true ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| return $text; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Returns a Tag Processor exposing the raw matched tokens. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @since 6.6.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param string $html Passed into the Tag Processor. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @return WP_HTML_Tag_Processor|__anonymous@23567 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| function wp_get_internal_tag_processor( $html ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return new class( $html ) extends WP_HTML_Tag_Processor { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Returns the raw token from the input string at the | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * current location, if paused at a location. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @return false|string | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| public function get_raw_token() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| WP_HTML_Tag_Processor::STATE_READY === $this->parser_state || | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| WP_HTML_Tag_Processor::STATE_INCOMPLETE_INPUT === $this->parser_state || | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| WP_HTML_Tag_Processor::STATE_COMPLETE === $this->parser_state | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->set_bookmark( 'here' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $here = $this->bookmarks['here']; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| return substr( $this->html, $here->start, $here->length ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Separates HTML elements and comments from the text. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * This function tokenizes an HTML document into its | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * components and returns the array of tokens. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @since 4.2.4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @since 6.6.0 Relies on the HTML API for parsing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param string $input The text which has to be formatted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @return string[] Array of the formatted text. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param string $input_html Raw HTML potentially containing a mixture of tags, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * comments, text nodes, and other sytnax. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @return string[] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| function wp_html_split( $input ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return preg_split( get_html_split_regex(), $input, -1, PREG_SPLIT_DELIM_CAPTURE ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| function wp_html_split( $input_html ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| $chunks = array(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $processor = wp_get_internal_tag_processor( $input_html ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| while ( $processor->next_token() ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * There's a legacy behavior where text nodes are always stored in even | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * indices and "elements" are stored in odd indices. To preserve this, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * empty text nodes are inserted when there's none between other syntax | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * tokens. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( 0 === count( $chunks ) % 2 && '#text' !== $processor->get_token_name() ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $chunks[] = ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| $is_special_atomic_element = in_array( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $processor->get_tag(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| array( 'SCRIPT', 'STYLE', 'XMP', 'NOEMBED', 'NOFRAMES', 'TITLE', 'TEXTAREA' ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| true | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( ! $is_special_atomic_element ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $chunks[] = $processor->get_raw_token(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * For special atomic tags, it's necessary to redo some work to find | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * the opening and closing tag, because the Tag Processor consumes | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * them all in one go. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * By replacing the first character of the tag name, it's possible to | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * trick the Tag Processor into thinking it's non-special content, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * then get the starting and ending tags, then restore the tag name at | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * the end. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Because the end tag for these special atomic elements are matched | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * if they are unexpected, then the final closing tag will be found | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * after renaming the opening. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| $raw_html = $processor->get_raw_token(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $first_char = $raw_html[1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $raw_html[1] = 'X'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $special = wp_get_internal_tag_processor( $raw_html ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // The first tag is the modified tag. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $special->next_tag(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $opening_tag = $special->get_raw_token(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $opening_tag[1] = $first_char; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $chunks[] = $opening_tag; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| $special->set_bookmark( 'last' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| while ( $special->next_tag( array( 'tag_closers' => 'visit' ) ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $special->set_bookmark( 'last' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $special->seek( 'last' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $closing_tag = $special->get_raw_token(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| $chunks[] = substr( $raw_html, strlen( $opening_tag ), -strlen( $closing_tag ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| $chunks[] = $closing_tag; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| return $chunks; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
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
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
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.