Skip to content
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

Interactivity API: Server Directive Processor for data-wp-each #58498

Merged
merged 27 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0f8008e
Add append_content_after_closing_tag_on_balanced_or_void_tags method
luisherranz Jan 29, 2024
2c70fb8
Make next_balanced_tag_closer_tag public and add tests
luisherranz Jan 30, 2024
093bb36
Add compat for array_is_list added in WP 6.5
luisherranz Jan 30, 2024
e378aad
Add missing covers in wp-text tests
luisherranz Jan 30, 2024
767b9aa
Minor fixes to the Interactivity API directive processor
luisherranz Jan 30, 2024
106eee6
Create internal method process_directives_args to call it from wp-each
luisherranz Jan 30, 2024
58abeb0
Add kebab-case to camelCase method
luisherranz Jan 30, 2024
4a62bb6
Add wp-each processor
luisherranz Jan 31, 2024
29f10fa
Make sure it doesn't process non-array values
luisherranz Jan 31, 2024
527a828
Merge branch 'trunk' into add/data-wp-each-server-directive-processor
luisherranz Jan 31, 2024
3b67d47
Trim before checking that starts and ends with tags
luisherranz Jan 31, 2024
72a2f19
Fix typo and some extra tabs
luisherranz Jan 31, 2024
5f3082c
Fix PHPCS
luisherranz Jan 31, 2024
a20240d
Add kebal to camel case conversion in the JS runtime
luisherranz Jan 31, 2024
fa33e6e
Add extra nested test
luisherranz Feb 2, 2024
ed4319e
Restrict appending to template tags
luisherranz Feb 3, 2024
b25a04c
Merge branch 'trunk' into add/data-wp-each-server-directive-processor
luisherranz Feb 3, 2024
f347057
Override WP Core script modules
luisherranz Feb 3, 2024
f434acc
Switch to `data-wp-remove` directive
luisherranz Feb 3, 2024
42d6404
Rename back to data-wp-each-child
luisherranz Feb 4, 2024
b1fd904
Merge branch 'trunk' into add/data-wp-each-server-directive-processor
luisherranz Feb 4, 2024
56f373e
Add missing continue in foreach loop
luisherranz Feb 4, 2024
62d19c4
Don't return a value
luisherranz Feb 4, 2024
f9a2731
Merge branch 'trunk' into add/data-wp-each-server-directive-processor
luisherranz Feb 4, 2024
8fda776
Transform interactivity index to TS
luisherranz Feb 4, 2024
bd858e6
Fix includes_url logic
luisherranz Feb 4, 2024
628509a
Also move vdom to TS
luisherranz Feb 4, 2024
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
38 changes: 38 additions & 0 deletions lib/compat/wordpress-6.5/compat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* WordPress 6.5 compatibility functions.
*
* @package WordPress
*/

if ( ! function_exists( 'array_is_list' ) ) {
/**
* Polyfill for `array_is_list()` function added in PHP 8.1.
*
* Determines if the given array is a list.
*
* An array is considered a list if its keys consist of consecutive numbers from 0 to count($array)-1.
*
* @see https://github.com/symfony/polyfill-php81/tree/main
*
* @since 6.5.0
*
* @param array<mixed> $arr The array being evaluated.
* @return bool True if array is a list, false otherwise.
*/
function array_is_list( $arr ) {
if ( ( array() === $arr ) || ( array_values( $arr ) === $arr ) ) {
return true;
}

$next_key = -1;

foreach ( $arr as $k => $v ) {
if ( ++$next_key !== $k ) {
return false;
}
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,24 @@
*/
class WP_Interactivity_API_Directives_Processor extends Gutenberg_HTML_Tag_Processor_6_5 {
/**
* Returns the content between two balanced tags.
* Returns the content between two balanced template tags.
*
* It positions the cursor in the closer tag of the balanced template tag,
* if it exists.
*
* @access private
*
* @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.
* @return string|null The content between the current opener template tag and its matching closer tag or null if it
* doesn't find the matching closing tag.
*/
public function get_content_between_balanced_tags() {
public function get_content_between_balanced_template_tags() {
if ( 'TEMPLATE' !== $this->get_tag() || $this->is_tag_closer() ) {
return null;
}

// Flushes any changes.
$this->get_updated_html();

$bookmarks = $this->get_balanced_tag_bookmarks();
if ( ! $bookmarks ) {
return null;
Expand All @@ -32,7 +42,6 @@ public function get_content_between_balanced_tags() {
$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 );

Expand All @@ -48,6 +57,7 @@ public function get_content_between_balanced_tags() {
* @return bool Whether the content was successfully replaced.
*/
public function set_content_between_balanced_tags( string $new_content ): bool {
// Flushes any changes.
$this->get_updated_html();

$bookmarks = $this->get_balanced_tag_bookmarks();
Expand All @@ -67,6 +77,37 @@ public function set_content_between_balanced_tags( string $new_content ): bool {
return true;
}

/**
* Appends content after the closing tag of a template tag.
*
* This method positions the processor in the last tag of the appended
* content, if it exists.
*
* @access private
*
* @param string $new_content The string to append after the closing template tag.
* @return bool Whether the content was successfully appended.
*/
public function append_content_after_template_tag_closer( string $new_content ): bool {
// Refuses to process if the content is empty or this is not a closer template tag.
if ( empty( $new_content ) || 'TEMPLATE' !== $this->get_tag() || ! $this->is_tag_closer() ) {
return false;
}

// Flushes any changes.
$this->get_updated_html();

$bookmark = 'append_content_after_template_tag_closer';
$this->set_bookmark( $bookmark );
$end = $this->bookmarks[ $bookmark ]->start + $this->bookmarks[ $bookmark ]->length + 1;
$this->release_bookmark( $bookmark );

// Appends the new content.
$this->lexical_updates[] = new Gutenberg_HTML_Text_Replacement_6_5( $end, 0, $new_content );

return true;
}

/**
* Returns a pair of bookmarks for the current opening tag and the matching
* closing tag.
Expand All @@ -78,7 +119,7 @@ private function get_balanced_tag_bookmarks() {
$start_name = 'start_of_balanced_tag_' . ++$i;

$this->set_bookmark( $start_name );
if ( ! $this->next_balanced_closer() ) {
if ( ! $this->next_balanced_tag_closer_tag() ) {
$this->release_bookmark( $start_name );
return null;
}
Expand All @@ -93,13 +134,15 @@ private function get_balanced_tag_bookmarks() {
* 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.
* 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.
*
* @access private
*
* @return bool Whether a matching closing tag was found.
*/
private function next_balanced_closer(): bool {
public function next_balanced_tag_closer_tag(): bool {
$depth = 0;
$tag_name = $this->get_tag();

Expand Down
Loading
Loading