-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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: Support .length on numeric arrays and strings on the server #7751
Interactivity API: Support .length on numeric arrays and strings on the server #7751
Conversation
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. |
I'm up for this 🙂 Do you want me to add it to the WP 6.8 iteration? |
src/wp-includes/interactivity-api/class-wp-interactivity-api.php
Outdated
Show resolved
Hide resolved
Yeah, I think so! I was worried this would create a precedent where many things would need to be added, but it doesn't seem like there are actually many cases that are similar to this, really just |
86727eb
to
85581e4
Compare
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. |
This is ready for review @luisherranz @michalczaplinski @DAreRodz @cbravobernal. |
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.
Code looks good, nice unification 👍🏻
src/wp-includes/interactivity-api/class-wp-interactivity-api.php
Outdated
Show resolved
Hide resolved
Committed with https://core.trac.wordpress.org/changeset/59477. |
Add
.length
to directives processing on the server on strings and numeric arrays.Trac ticket: https://core.trac.wordpress.org/ticket/62582
Known issues
Differences in string encodings may result in different results for the
.length
of a string between what the server reports and what JavaScript would detect.For example:
While in JavaScript:
The proposed implementation for string length checking currently relies on
strlen
. This should be correct for any single-byte strings, but will often be incorrect with multibyte strings.JavaScript strings are UTF-16 encoded. This can be calculated in PHP, but doing so relies on knowing the input string encoding and relying on encoding conversions that may be costly, like
mb_strlen( mb_convert_encoding( $s, 'UTF-16', mb_internal_encoding() )
in the example above.This seems sufficient at the moment, if the precise length of a string must be known on the server then derived state or a static state property can be used with the appropriate conversion. What I suspect is the most common use case for string length checking (whether the string is empty or not) will always work correctly. Including information about this caveat in a dev note and documentation with workarounds will help.
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.