Make WordPress Core

Opened 3 months ago

Last modified 3 months ago

#64208 new enhancement

Defining interactivity API context with single quotes fail

Reported by: fab1en's profile Fab1en Owned by:
Milestone: Future Release Priority: low
Severity: minor Version: 6.8.3
Component: Interactivity API Keywords:
Focuses: javascript, docs Cc:

Description

In the documentation, we can read an example using double quotes to define interactivity API context :

<li data-wp-context='{ "isMenuOpen": false }'>

However, the following HTML code is also valid (using single quotes instead of doubles inside the object):

<li data-wp-context="{ 'isMenuOpen': false }">

But it silently fails to properly define the context. I understand that single quotes are not valid in JSON, but I think that it should raise an error.

I think it should also be mentioned more explicitly in the documentation : to define a context with double quotes inside, one should surround it with single quotes, whereas all other HTML attributes definitions are surrounded by double quotes.

Change History (2)

#1 @jonsurrell
3 months ago

Ideally, folks rely on the wp_interactivity_data_wp_context() function for this. It outputs the HTML attribute ready to be printed to the page. There's no need to write or encode the data on your own.

It's mentioned in the linked documentation, but it would be good to update more examples to use it.

If no errors or warnings are printed when the directive is invalid JSON, or is not an object which I believe is a requirement, then it could certainly help to display some errors in debug environements.

#2 @luisherranz
3 months ago

  • Milestone changed from Awaiting Review to Future Release
  • Priority changed from normal to low

Thanks for reporting this, @Fab1en. PRs to solve the two shortcomings (improved documentation and warning in debug environments) are certainly welcome 🙂

Note: See TracTickets for help on using tickets.