Skip to content

Ampersand's in the Navigation block are converted to u0026 #59516

Closed

Description

@kylekelly

Description

Adding ampersands '&' to the label field of a link (page & custom) in the Navigation block converts it to u0026, which displays on the frontend and backend once refreshed.

Step-by-step reproduction instructions

  1. Go to the latest 6.5-beta3 (or nightly) on your test site, or in playground: https://playground.wordpress.net/?php=8.0&wp=beta&storage=none
  2. Edit site, and add a link to the navigation block.
  3. Include an ampersand in the label of a link in the block, an example being 'News & Events'
  4. View the frontend of the site and see it converted to 'u0026'

Screenshots, screen recording, code snippet

Screenshot 2024-03-01 at 4 39 24 PM Screenshot 2024-03-01 at 4 38 17 PM

Environment info

  • WordPress 6.5 beta3 & Nightly
  • Tested and seen with both Gutenberg not installed, and installed and activated.
  • Tested in both latest Chrome & Firefox

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

Activity

  1. added
    [Type] Bug
    on Mar 1, 2024
  2. added
    [Block] Navigation
    on Mar 3, 2024
  3. annezazu commented on Mar 4, 2024

    @annezazu
    Contributor

    Adding to 6.5 but let's test with 6.4.3 to ensure this is related to the 6.5 release. If no one beats me to it, I'll do that tomorrow (@fabiankaegy in case you can!)

  4. fabiankaegy commented on Mar 4, 2024

    @fabiankaegy
    Member

    I just tested this and was able to verify that it is not happening in 6.4.3 but is happening on 6.5 Beta 3.

  5. fabiankaegy commented on Mar 4, 2024

    @fabiankaegy
    Member

    @getdave Tagging you here as one of the maintainers of the Navigation Block

  6. moved this from ❓ Triage to 📥 Todo in WordPress 6.5 Editor Taskson Mar 4, 2024
  7. getdave commented on Mar 4, 2024

    @getdave
    Contributor

    I can replicate. Likely to due to how label is escaped in the core/navigation-link block.

    I'm going to recommend that whatever fix we implement we add test coverage for in a follow up as similar issues have occurred a few times now.

  8. getdave commented on Mar 4, 2024

    @getdave
    Contributor

    Testing I found the following is stored in the database

    <!-- wp:navigation-link {"label":"News u0026amp; Events","type":"page","id":2,"url":"http://localhost:8888/?page_id=2","kind":"post-type"} /-->
    

    Notice the News u0026amp; Events.

    I tested with removing the escaping in

    const label = useNewLabel
    ? escapeHTML( newLabel )
    : originalLabel || escapeHTML( newUrlWithoutHttp );

    ...and it does change the conversion to entities.

  9. 7 remaining items

  10. t-hamano commented on Mar 4, 2024

    @t-hamano
    Contributor

    If I'm testing correctly, this problem first appeared on the backport for Beta2. Commit hash c84cddb on GitHub.

    commits

    WordPress/wordpress-develop@c84cddb

    This problem does not seem to occur if you check out the previous commit (18b21c3).

  11. t-hamano commented on Mar 4, 2024

    @t-hamano
    Contributor

    wp_update_post() is being executed here, is it because it is escaped in case of an array?

    https://developer.wordpress.org/reference/functions/wp_update_post/#parameters

    Post data. Arrays are expected to be escaped, objects are not.

  12. ockham commented on Mar 4, 2024

    @ockham
    Contributor

    wp_update_post() is being executed here, is it because it is escaped in case of an array?

    https://developer.wordpress.org/reference/functions/wp_update_post/#parameters

    Post data. Arrays are expected to be escaped, objects are not.

    Ohh, that looks like it could be the reason!

  13. ockham commented on Mar 4, 2024

    @ockham
    Contributor

    Oh, I'm not sure I read that correctly. Doesn't that mean that wp_update_post expects that the $postarr argument has already been escaped if it's an array, and escapes it itself if it's an object? If so, then I think that's what we want, in order to avoid escaping twice, no? 🤔

  14. getdave commented on Mar 4, 2024

    @getdave
    Contributor

    $postarr
    Post data. Arrays are expected to be escaped, objects are not. See wp_insert_post() for accepted arguments.

    https://developer.wordpress.org/reference/functions/wp_update_post/

  15. t-hamano commented on Mar 4, 2024

    @t-hamano
    Contributor

    Hmm, maybe wp_update_post itself is not the fundamental problem...

  16. ockham commented on Mar 4, 2024

    @ockham
    Contributor

    Yeah. My current theory is that this is happening somewhere along the way between parse_blocks and traverse_and_serialize_block.

    The fact that News & Events becomes News u0026amp; Evens indicates that there are some transforms that work okay, probably News & Events -> News &amp; Events -> News \\0026amp; Events ✅

    But somehow, the double backslash is then stripped: News \\0026amp; Events -> News 0026amp; Events ❌

    I haven't yet been able to find out where.

    (Edit: Backslash, not slash.)

  17. ockham commented on Mar 4, 2024

    @ockham
    Contributor

    Hmm, looks like we have test coverage for parsing/serialization of backslashes in attribute values though: https://github.com/WordPress/wordpress-develop/blob/0f45a3c9fc10d1abf68ec9e201e6113815cdbb34/tests/phpunit/tests/blocks/serialize.php#L44-L45

    (Edit: Backslashes, not slashes.)

  18. ockham commented on Mar 4, 2024

    @ockham
    Contributor

    I'll try creating a minimal unit test for this so we can debug more easily.

  19. ockham commented on Mar 4, 2024

    @ockham
    Contributor

    I'll try creating a minimal unit test for this so we can debug more easily.

    #59561

    I'll have to wrap up for today; I'll continue working on it tomorrow. In the meantime, feel free to continue investigating and push commits to that PR 😊

  20. t-hamano commented on Mar 5, 2024

    @t-hamano
    Contributor

    This problem appears to be not limited to the Navigation Link block. At least it also happens with Search and Home Link blocks. However, it doesn't seem to occur on the Button block.

    95cce3c9abab7bd69d5766f732b93ebe.mp4
  21. tjcafferkey commented on Mar 5, 2024

    @tjcafferkey
    Contributor

    This problem appears to be not limited to the Navigation Link block. At least it also happens with Search and Home Link blocks. However, it doesn't seem to occur on the Button block.

    Yeah, I suspect this is because button text is not stored in the blocks attribute values where as the other text/labels in the blocks you have mentioned are. It's the process of serializing/parsing these attributes which is where things go a bit wild.

  22. dmsnell commented on Mar 5, 2024

    @dmsnell
    Member

    @dmsnell I know you have specific expertise in these types of problems and may well also be aware of changes in Core that could have caused this. Can you help shed any light on this?

    Thanks for asking @getdave. I had no ideas other than I wish we were done rewriting Core with the HTML API. I'm hoping to eliminate these kinds of issues, which creep up to frequently. It took me a lot of time and frustration to figure out a way to write <![CDATA[test]]> in the Dev Notes for the HTML API, and the solution involves some carefully-placed zero-width spaces.

    The problem is almost certainly inside of wp_kses_normalize_entities() given the chain through u0026amp;, but there are many roads to and through there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

[Block] Navigation[Type] Bug

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions