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

Global Styles: Refactor wp_add_inline_style() to use HTML API. #7120

Draft
wants to merge 9 commits into
base: trunk
Choose a base branch
from

Conversation

dmsnell
Copy link
Member

@dmsnell dmsnell commented Aug 1, 2024

The existing wp_add_inline_style() code attempts to ensure that the provided code doesn't include STYLE tags, but this is difficult to do. In this change, the HTML API is employed to ensure that the HTML tags are properly detected.

With the ability to detect these more carefully, new options are available to the function when things go wrong. This makes the attempt to auto-detect when someone has provided a <style>-wrapped input by mistake, and will unwrap that properly. Any other defects are passed transparently into the styles.

Ticket: https://core.trac.wordpress.org/ticket/61828

Copy link

github-actions bot commented Aug 1, 2024

Test using WordPress Playground

The 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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Copy link
Member

@ramonjd ramonjd left a comment

Choose a reason for hiding this comment

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

Thanks a lot for working on this @dmsnell

I've tested this with theme.json, and also adding custom CSS in the site editor (global styles).

Everything works as expected.

✅ "valid" custom CSS works as expected, including nested rules and other selector manifestations
✅ Other inline styles, such as core-block-supports-inline-css have no regressions

I added some tests to ensure current functionality is preserved (stripping balanced style tags and injected content), and also testing the new behaviour - I hope you don't mind!

Also, when adding inline CSS that wp_add_inline_style() doesn't like, we'll know 😄

Screenshot 2024-08-06 at 3 05 03 PM

Do you think the changes to the HTML API needs testing as well?

Copy link
Contributor

@peterwilsoncc peterwilsoncc left a comment

Choose a reason for hiding this comment

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

Some quick notes but will come back to actually test the code.

tests/phpunit/tests/dependencies/styles.php Outdated Show resolved Hide resolved
tests/phpunit/tests/dependencies/styles.php Outdated Show resolved Hide resolved
tests/phpunit/tests/dependencies/styles.php Outdated Show resolved Hide resolved
@ramonjd
Copy link
Member

ramonjd commented Aug 6, 2024

Thanks @peterwilsoncc - updated!

@swissspidy
Copy link
Member

swissspidy commented Aug 6, 2024

FWIW, the same goes for wp_add_inline_script, where this was first introduced.

IMHO this is overly defensive programming and overkill for this use case.

In these functions we do a simple regex check out of courtesy to tell developers they are doing it wrong. If they added some weird markup that we don't detect, they deserve to get yelled at by the browser or whatever

@dmsnell
Copy link
Member Author

dmsnell commented Aug 6, 2024

@swissspidy I added the fallback conditions just because they were easy and because it wasn't clear what the right end goal should be. if you think we ought instead to truncate the provided content once it exists the STYLE context that would be fine too.

For example, if provided * {}</style><p>Done!</p> then we could simply send along * {} and not do any more work. The only real condition I considered was that many people might send <style>* {}</style> and it seemed reasonable to auto-correct this.

@ramonjd
Copy link
Member

ramonjd commented Aug 6, 2024

The only real condition I considered was that many people might send <script>runAThing();</script> and it seemed reasonable to auto-correct this.

I agree, and this was the genesis of this PR. Why not tighten the checks up a little, even if out of courtesy?

If they added some weird markup that we don't detect, they deserve to get yelled at by the browser or whatever

Even when that weird markup comes from external sources, such as a theme's custom CSS?

@swissspidy
Copy link
Member

The current code in core was not meant to magically fix a dev‘s broken inline css/js. We could actually even consider removing that logic with the doing_it_wrong .

@dmsnell
Copy link
Member Author

dmsnell commented Aug 6, 2024

I've updated this by adjusting the comments and test names. Happy to have someone revert if they don't like the slightly-more descriptive test names. I've also moved the interpolated $script var in the test failure output to the end after a : to more clearly separate it from the error message.

@dmsnell
Copy link
Member Author

dmsnell commented Aug 6, 2024

Why not tighten the checks up a little, even if out of courtesy?

you definitely won't hear me shouting this virtue 😉

IMO each case warrants its own review. here, we can reliably detect this condition and resolve it, so it seemed fine. whether we want to is more about what we allow and which habits we want to discourage.

leaving it in and taking it out are equally easy to do

@ramonjd
Copy link
Member

ramonjd commented Aug 6, 2024

IMO each case warrants its own review. here, we can reliably detect this condition and resolve it, so it seemed fine. whether we want to is more about what we allow and which habits we want to discourage.

Fair point.

My thinking comes from the following assumptions:

  • the current code attempts a code replace, so I'm assuming this should be retained in some fashion for backwards compatibility. (This PR does that).
  • It's possible to add scripts that do things in a theme.json's "css" field. A theme can be installed by any user, whether they've had a hand in producing the code or not. We should therefore tighten up the check to prevent it.

Everything else I'd defer to you folks for advice.

@dmsnell
Copy link
Member Author

dmsnell commented Aug 6, 2024

The HTML API update has been extracted into #7150

@dmsnell
Copy link
Member Author

dmsnell commented Aug 8, 2024

The HTML API update has been merged into trunk and trunk has been merged into this branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants