-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
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 Core Committers: Use this line as a base for the props when committing in SVN:
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
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.
LGTM! 🙂
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.
LGTM
cc @dmsnell |
foreach ( $directives_prefixes as $directive_prefix ) { | ||
if ( 'data-wp-context' === $directive_prefix ) { | ||
array_pop( $context_stack ); | ||
} |
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.
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 )
);
}
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.
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.
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.
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!
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.
01ae241 - exercises enter
vs exit
mode.
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
src/wp-includes/interactivity-api/class-wp-interactivity-api.php
Outdated
Show resolved
Hide resolved
src/wp-includes/interactivity-api/class-wp-interactivity-api.php
Outdated
Show resolved
Hide resolved
If we find a way to cover also |
I've added this test to cover |
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.
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 👍🏻
Committed in https://core.trac.wordpress.org/changeset/57832 |
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:
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.