Framework: Optimize decodeEntities to skip unnecessary decoding #9725
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request seeks to replace the current
decodeEntities
implementation with a much simpler alternative, with the hopes of improving performance.decodeEntities
is a top culprit in profiling page transitions in Calypso, often the top time-consuming function in Chrome Timeline reports:(See earlier implementation results)
Implementation notes:
With these changes,decodeEntities
will no longer decode all entities, only those which we expect to have been encoded by the WordPress_wp_specialchars
method. In other words, it should now mimic the behavior ofwp_specialchars_decode
.There was previously a test that expected entities likeÑ
(Ñ) to have been decoded for plugin authors, but in my real-world testing, I find that these entities are never encoded to begin with and continue to display correctly. You can verify this yourself with an example plugin which specifies an author name including both "&" and "Ñ" characters. On the plugin detail screen, the author name shows correctly as "Andrew Duthie Ñáð & Friends". cc @enejb re: 6123-gh-calypso-pre-ossAnother minor optimization inspired by the
wp_specialchars_decode
implementation is to skip replacement attempts if there are no entities in the text argument, detected by presence of a&
character.Edit: After further review, it was determined that we rely on full entity decoding in many places (Reader, post titles with user-entered entities such as
•
). The changes here are limited to the above-noted inspired&
check optimization, and memoization of the title-generating selector which makes use ofdecodeEntities
on every screen.Testing instructions:
Verify that wherever content is rendered using REST API data where text would be encoded (e.g. post titles, assigned categories), that the text continues to display as decoded. This should include expected encoded values
&
<
>
'
"
as well as other special characters that are never encodedÑ
á
ð
.Open questions:
Is there anywhere in the codebase that we're expectingdecodeEntities
to decode more than just the entities that would have been encoded by WordPress core logic?cc @blowery