-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Interactivity API: Server Directive Processing #5953
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
59 commits
Select commit
Hold shift + click to select a range
bb547e4
Code from GB
luisherranz 236eda2
Fix test pollution
luisherranz 7c95ee5
Fix tabs in multiline comments
luisherranz c084276
Improve state/config return msg and fix arg name
luisherranz 0fe3225
Remove unnecessary module registration
luisherranz e8daa1c
Fix typos
luisherranz c0f5804
Add missing @since statements
luisherranz eab8f28
Switch to null coalescing operator
luisherranz 19e454f
Replace array_push with $array[]
luisherranz 61d896f
Simplify conditional statement
luisherranz 84ea8fd
Replace null with empty array in state/config functions
luisherranz 0503eed
Add missing PHP 7 types
luisherranz 36e8adb
Remove default args passed to filter
luisherranz 2fef289
Add missing tests comments
luisherranz 4100309
Add @ticket to all the tests
luisherranz 8efcc12
Merge branch 'trunk' into wp-interactivity-api
luisherranz 69c6f78
Switch from static to global and move hooks/modules to settings
luisherranz 5462b18
Bail out if it finds an SVG or MathML tag
luisherranz 6dc2d42
Check for tags that don't visit their closer tag
luisherranz e9cb1fe
Use non-min script module versions
luisherranz 0534970
Add missing PHP 7 types
luisherranz 2bc2bea
Merge branch 'trunk' into wp-interactivity-api
luisherranz 553e8a4
Remove md5 in favor of array comparison
luisherranz ada6fe5
Use a hook to register the script modules so they can be overwritten …
luisherranz ee6e780
Omit curly brackets on string variable
luisherranz 1c73521
Make classes final
luisherranz 35ae36b
Fix typos
luisherranz a81d251
Add @uses to process_directives DocBlock
luisherranz 97d4c98
Don't return the result of array_pop
luisherranz 56a4d48
Don't return true on `has_and_visits_its_closer_tag` on null tag names
luisherranz 33fbc8d
Merge branch 'trunk' into wp-interactivity-api
luisherranz d19252b
Remove unnecessary file
luisherranz 49d5ebc
Remove more unnecessary files from wp-settings
luisherranz 264d0fb
Update WP_Interactivity_API_Directives_Processor
luisherranz 41ccbc1
Update WP_Interactivity_API
luisherranz c848fe2
Add tests for the new processors
luisherranz 6345e24
Support data-wp-interactive="myPlugin"
luisherranz 3b12a11
Add support for data_wp_context
luisherranz 4618663
Rename $start/$end to more descriptive names and add get_after_opener…
luisherranz ff61c7c
Fix some texts
luisherranz 71cda6e
Replace preg_replace with rtrim
luisherranz 4dbb4fe
Rename set_style_property to merge_style_property
luisherranz 330472c
Add json extension requirement to composer
luisherranz e0533e5
Fix some indentation issues
luisherranz e5b88fa
Replace array_push
luisherranz f010314
Use two block names in tests to make sure they render fine
luisherranz a2b4aae
Switch to render_block_{$this->name} filter
luisherranz c90d337
Add LanguageTool suggestion
luisherranz 8eec2f3
Add missing processors in @uses
luisherranz 8a1caa9
Try moving styles to a WP inline style
luisherranz 71d1918
Adapt interactivity detection to new schema
luisherranz 69afa13
Fix inline style
luisherranz 1bb9137
Change data-wp-router-region tests to check for the inline styles
luisherranz 30dc904
Checks for valid namespace characters in `data-wp-interactive`
luisherranz 9d21532
Replace underscores with hyphens
luisherranz a24af7d
Remove % in 0 values of CSS
luisherranz 3b189f7
Rename functions to use `get` and `print`
luisherranz ef1c9ae
Remove use of `@uses`
luisherranz 76dea4f
Don't use the primary color for the top loading bar
luisherranz 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
Code from GB
- Loading branch information
commit bb547e4bbc281165e67219cfbcd8691b645b4b13
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
src/wp-includes/interactivity-api/class-wp-interactivity-api-directives-processor.php
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 |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| <?php | ||
| /** | ||
| * Interactivity API: WP_Interactivity_API_Directives_Processor class. | ||
| * | ||
| * @package WordPress | ||
| * @subpackage Interactivity API | ||
| */ | ||
|
|
||
| /** | ||
| * Class used to iterate over the tags of an HTML string and help process the | ||
| * directive attributes. | ||
| * | ||
| * @access private | ||
| */ | ||
| class WP_Interactivity_API_Directives_Processor extends WP_HTML_Tag_Processor { | ||
luisherranz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /** | ||
| * Returns the content between two balanced tags. | ||
| * | ||
| * @access private | ||
swissspidy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * | ||
| * @return string|null The content between the current opening and its matching closing tag or null if it doesn't | ||
| * find the matching closing tag. | ||
| */ | ||
| public function get_content_between_balanced_tags() { | ||
| $bookmarks = $this->get_balanced_tag_bookmarks(); | ||
| if ( ! $bookmarks ) { | ||
| return null; | ||
| } | ||
| list( $start_name, $end_name ) = $bookmarks; | ||
|
|
||
| $start = $this->bookmarks[ $start_name ]->start + $this->bookmarks[ $start_name ]->length + 1; | ||
| $end = $this->bookmarks[ $end_name ]->start; | ||
luisherranz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| $this->seek( $start_name ); | ||
| $this->release_bookmark( $start_name ); | ||
| $this->release_bookmark( $end_name ); | ||
|
|
||
| return substr( $this->html, $start, $end - $start ); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the content between two balanced tags. | ||
| * | ||
| * @access private | ||
| * | ||
| * @param string $new_content The string to replace the content between the matching tags. | ||
| * @return bool Whether the content was successfully replaced. | ||
| */ | ||
| public function set_content_between_balanced_tags( string $new_content ): bool { | ||
| $this->get_updated_html(); | ||
|
|
||
| $bookmarks = $this->get_balanced_tag_bookmarks(); | ||
| if ( ! $bookmarks ) { | ||
| return false; | ||
| } | ||
| list( $start_name, $end_name ) = $bookmarks; | ||
|
|
||
| $start = $this->bookmarks[ $start_name ]->start + $this->bookmarks[ $start_name ]->length + 1; | ||
| $end = $this->bookmarks[ $end_name ]->start; | ||
|
|
||
| $this->seek( $start_name ); | ||
| $this->release_bookmark( $start_name ); | ||
| $this->release_bookmark( $end_name ); | ||
|
|
||
| $this->lexical_updates[] = new WP_HTML_Text_Replacement( $start, $end - $start, esc_html( $new_content ) ); | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a pair of bookmarks for the current opening tag and the matching | ||
| * closing tag. | ||
| * | ||
| * @return array|null A pair of bookmarks, or null if there's no matching closing tag. | ||
| */ | ||
| private function get_balanced_tag_bookmarks() { | ||
| static $i = 0; | ||
| $start_name = 'start_of_balanced_tag_' . ++$i; | ||
|
|
||
| $this->set_bookmark( $start_name ); | ||
| if ( ! $this->next_balanced_closer() ) { | ||
| $this->release_bookmark( $start_name ); | ||
| return null; | ||
| } | ||
|
|
||
| $end_name = 'end_of_balanced_tag_' . ++$i; | ||
| $this->set_bookmark( $end_name ); | ||
|
|
||
| return array( $start_name, $end_name ); | ||
| } | ||
|
|
||
| /** | ||
| * Finds the matching closing tag for an opening tag. | ||
| * | ||
| * When called while the processor is on an open tag, it traverses the HTML | ||
| * until it finds the matching closing tag, respecting any in-between content, | ||
| * including nested tags of the same name. Returns false when called on a | ||
| * closing or void tag, or if no matching closing tag was found. | ||
| * | ||
| * @return bool Whether a matching closing tag was found. | ||
| */ | ||
| private function next_balanced_closer(): bool { | ||
| $depth = 0; | ||
| $tag_name = $this->get_tag(); | ||
|
|
||
| if ( $this->is_void() ) { | ||
| return false; | ||
| } | ||
|
|
||
| while ( $this->next_tag( | ||
| array( | ||
| 'tag_name' => $tag_name, | ||
| 'tag_closers' => 'visit', | ||
| ) | ||
| ) ) { | ||
| if ( ! $this->is_tag_closer() ) { | ||
| ++$depth; | ||
| continue; | ||
| } | ||
|
|
||
| if ( 0 === $depth ) { | ||
| return true; | ||
| } | ||
luisherranz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| --$depth; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether the current tag is void. | ||
| * | ||
| * @access private | ||
| * | ||
| * @return bool Whether the current tag is void or not. | ||
| */ | ||
| public function is_void(): bool { | ||
| $tag_name = $this->get_tag(); | ||
| return WP_HTML_Processor::is_void( null !== $tag_name ? $tag_name : '' ); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.