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: Fix SSR context included in void tags shouldn't propagate to following elements #6267

Conversation

SantosGuillamot
Copy link

From my tests, when the same context is used in an inner void tag element and its parent, it is overwritten when it shouldn't. For example, in this use case:

<div data-wp-context='{"text": "outer"}'>
<img data-wp-context='{"text": "inner"}'>
<p data-wp-text="context.text"></p>
</div>

The last paragraph should receive the parent div context "outer" instead of the img "inner.

From what I've seen in the code, this specific case hasn't been considered while processing data-wp-context, and it just removes the context from the stack when it finds a closing tag: link.

In my tests, checking if it is a void tag in the directives processing and removing the context in that case, seems to solve the issue. Although I must say I am still unfamiliar with the SSR of the directives.

I've also added a test to cover this use case.

Trac ticket: https://core.trac.wordpress.org/ticket/60768


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Copy link

github-actions bot commented Mar 13, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props santosguillamot, luisherranz, cbravobernal, dmsnell, gziolo, swissspidy.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@swissspidy
Copy link
Member

cc @c4rl0sbr4v0 @luisherranz

Copy link
Member

@luisherranz luisherranz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🙂

Copy link
Contributor

@cbravobernal cbravobernal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cbravobernal
Copy link
Contributor

cc @dmsnell

foreach ( $directives_prefixes as $directive_prefix ) {
if ( 'data-wp-context' === $directive_prefix ) {
array_pop( $context_stack );
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that this is a surprise way to handle this based on how the directive handlers work. that is, they all handle their own cleanup, except now the specific context directive is coupled into the general directive dispatch.

cc: @ockham

what I think could work more generally is to do two things:

  • when matched on void or special elements (elements without a closing tag) we should run the handlers in reverse as is done above in line 307.
  • update the handlers so that instead of checking is_closing_tag() and using HTML semantics, they check something different, such as 'enter' or 'exit', representing whether the code is entering or existing a directive's scope

this implies of course that this function would then pass in that action to the directive handlers.

if ( ! $this->has_and_visits_its_closing_tag() ) {
  call_user_func_array(
	  $func,
	  array( $p, 'enter', &$context_stack, &$namespace_stack, &$tag_stack )
  );
  call_user_func_array(
	  $func,
	  array( $p, 'exit', &$context_stack, &$namespace_stack, &$tag_stack )
  );
} else {
  call_user_func_array(
	  $func,
	  array( $p, $p->is_tag_closer() ? 'exit' : 'enter', &$context_stack, &$namespace_stack, &$tag_stack )
  );
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it makes sense to use a more general mechanism and not something hardcoded for the context.

I implemented a different solution based on your feedback in this commit. For void tags or special elements, it runs the directives in both orders.

It seems to be working fine, but I'd love to know if that's what you had in mind.

By the way, I am not convinced about the variable name $is_exiting_tag. I can easily changed that if we pursue this approach.

Copy link
Member

@luisherranz luisherranz Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree this is a better solution, but I wouldn't mind to include this fix in 6.5 as it is, and work on this improvement for a future version because everything in this logic is still private and subject to change 🙂

Up to you!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

01ae241 - exercises enter vs exit mode.

Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@gziolo
Copy link
Member

gziolo commented Mar 14, 2024

If we find a way to cover also data-wp-interactivity directive with a test as noted in #6267 (comment), I will feel confident about landing this patch in WordPress 6.5.

@SantosGuillamot
Copy link
Author

I've added this test to cover data-wp-interactive as you mentioned. Prior to this pull request, it would bind the "void" value because the namespace is not removed. After this pull request, it returns null because that state doesn't exist. Let me know if that's what you had in mind.

Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent. Yes, that was exactly the test case I had in mind. I wasn't sure about the implications but now we have it documented 👍🏻

@swissspidy
Copy link
Member

Committed in https://core.trac.wordpress.org/changeset/57832

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants