-
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
Global Styles: Refactor wp_add_inline_style() to use HTML API. #7120
base: trunk
Are you sure you want to change the base?
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. |
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.
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 😄
Do you think the changes to the HTML API needs testing as well?
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.
Some quick notes but will come back to actually test the code.
Thanks @peterwilsoncc - updated! |
FWIW, the same goes for 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 |
@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 |
I agree, and this was the genesis of this PR. Why not tighten the checks up a little, even if out of courtesy?
Even when that weird markup comes from external sources, such as a theme's custom CSS? |
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 . |
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 |
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 |
Fair point. My thinking comes from the following assumptions:
Everything else I'd defer to you folks for advice. |
The HTML API update has been extracted into #7150 |
The HTML API update has been merged into |
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