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

Fix server processing of an empty data-wp-context directive #55482

Conversation

luisherranz
Copy link
Member

What?

This is a follow up on #55436.

The previous PR fixed the problem when the JSON provided to data-wp-context was not valid, and this PR fixes the problem when the data-wp-context is empty.

Why?

Because, as explained by @dmsnell in #55368, the user might forget the enter any value:

<div data-wp-context>...</div>

How?

I changed the logic a little bit to accommodate both checks (invalid JSON and empty value).

Testing Instructions

This is covered by a new unit test, but if you want to test it out:

  • Add an interactive block.
  • Add a div with a data-wp-context directive and valid JSON.
  • Add a nested div inside with an empty data-wp-context.
  • Check that it doesn't trigger a fatal error and that the initial context is still valid.

@luisherranz luisherranz added [Type] Bug An existing feature does not function as intended [Feature] Interactivity API API to add frontend interactivity to blocks. labels Oct 19, 2023
@luisherranz luisherranz self-assigned this Oct 19, 2023
@github-actions
Copy link

This pull request has changed or added PHP files. Please confirm whether these changes need to be synced to WordPress Core, and therefore featured in the next release of WordPress.

If so, it is recommended to create a new Trac ticket and submit a pull request to the WordPress Core Github repository soon after this pull request is merged.

If you're unsure, you can always ask for help in the #core-editor channel in WordPress Slack.

Thank you! ❤️

View changed files
❔ lib/experimental/interactivity-api/directives/wp-context.php
❔ phpunit/experimental/interactivity-api/directives/wp-context-test.php

Copy link
Member

@dmsnell dmsnell left a comment

Choose a reason for hiding this comment

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

It could be helpful to split the test into two since we're testing two separate situations. In fact, we already have a separate test for malformed JSON, so it could fit nicely to have one for "empty_directive" and one for "directive_without_value"

Either way this is an improvement so it would be good to get in, but it would be nice to split these here or in a follow-up PR.

@luisherranz
Copy link
Member Author

What do you mean by "empty_directive" and "directive_without_value"? What's the difference between those?

@dmsnell
Copy link
Member

dmsnell commented Oct 19, 2023

What do you mean by "empty_directive" and "directive_without_value"? What's the difference between those?

data-wp-context="" is an empty attribute and data-wp-context is a boolean attribute, one without a value. these are two distinct constructions in HTML and the HTML API exposes that difference.

in the DOM they both carry the value "" but in the HTML API they will be "" and true, respectively. this is something that I find unfortunate but don't know how to resolve, because empty( $processor->get_attribute( … ) ) can return false for the boolean attributes, since empty( true ) is false.

@luisherranz
Copy link
Member Author

Oh, ok. That makes sense, thanks Dennis 🙂

I've added a new test for that use case. I'll wait for your confirmation before merging the PR.

@dmsnell
Copy link
Member

dmsnell commented Oct 19, 2023

confirmation before merging the PR.

good to go!

@luisherranz luisherranz merged commit 4b6d9f5 into trunk Oct 19, 2023
49 checks passed
@luisherranz luisherranz deleted the fix/server-wp-context-processing-rewind-out-of-sync-second-try branch October 19, 2023 20:27
@github-actions github-actions bot added this to the Gutenberg 17.0 milestone Oct 19, 2023
noahtallen pushed a commit that referenced this pull request Oct 25, 2023
* Add failing test

* Fix the test

* Fix WPCS

* Add a test for an empty directive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Interactivity API API to add frontend interactivity to blocks. [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants