-
Notifications
You must be signed in to change notification settings - Fork 798
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
Customizer > Custom CSS: Add support for CSS variables #20129
base: trunk
Are you sure you want to change the base?
Conversation
Caution: This PR has changes that must be merged to WordPress.com |
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available. Once your PR is ready for review, check one last time that all required checks (other than "Required review") appearing at the bottom of this PR are passing or skipped. Jetpack plugin:
|
I added the unit tests for this from #19935. I tried to run them with |
@@ -1180,9 +1181,21 @@ function property_is_valid($property) { | |||
$property = strtolower($property); | |||
if (in_array(trim($property), $GLOBALS['csstidy']['multiple_properties'])) $property = trim($property); | |||
$all_properties = & $GLOBALS['csstidy']['all_properties']; | |||
return (isset($all_properties[$property]) && strpos($all_properties[$property], strtoupper($this->get_cfg('css_level'))) !== false ); | |||
return ( ( isset( $all_properties[ $property ] ) && strpos( $all_properties[ $property ], strtoupper( $this->get_cfg( 'css_level' ) ) ) !== false ) || ( $this->get_cfg( 'preserve_css_variables' ) && $this->property_is_css_variable( $property ) ) ); |
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.
This line is really hard to grok, could we extract it to a function?
I also added a changelog entry. |
Sorry @scruffian I didn't get chance to look at this as was busy with another priority all week, but should be able to take a look monday if you haven't sorted by then. |
You should be able to run them like so:
Here are the results I get from the branch at the moment:
You can also see them in the checks here on GitHub: |
* @version 1.5 | ||
*/ | ||
public function property_is_css_variable( $property ) { | ||
return preg_match( '/^(--[a-zA-Z0-9]+)(([-]{1,2}|[_]?)[a-zA-Z0-9]+)*$/', $property ); |
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.
FYI - I have modified the original regex: https://github.com/Automattic/jetpack/pull/19935/files#diff-09336221a25ca5f791ce459fbe1f11846ebd8ace76220d89eec08dca03591b70R1196 to allow for "nested" variables like, for example: --wp--custom--color--foreground
c797cef
to
2d19598
Compare
I still can't get the tests to run. I see this error:
|
I managed to get this working thanks to @brbrr! I added a commit to fix the unit tests. I think this is ready for another review. |
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.
The unit tests specific to this module are passing for me on this PR, but I don't fully understand how to replicate the issue. That is, CSS variables added via Customizer CSS apply on my development instance both with and without this PR, which makes me think I may be not testing this properly.
@danieldudzic Like Jeff, I was able to add/save CSS variables in the Customizer without issue on both my local dev and WoA test site, without applying the changes in this PR 🤔 I was testing with this CSS if it helps: :root {
--main-bg-color: purple;
}
body {
background-color: var(--main-bg-color);
} |
Danny is on leave until september. |
@scruffian @jffng @samiff would any of you mind if I take over this PR, give it a rebase, and add an additional line? I think this PR will unblock being able to introduce blocks that store CSS variables in inline styles in post content, which will come in very handy for future features like introducing gap block support (here's a WIP in Gutenberg that would be unblocked by this PR). The main change will be to add a line to the If there are no objections to me taking over this PR, I can then come up with some testing instructions to reproduce the issue and fix on wpcom 🤞 |
I may have spoken too soon, as this PR would only resolve the issue of savings CSS attributes in post content for CSSTidy, and not in core for users without the |
Happy for you to take it over :) |
It turns out there were some good arguments for not switching on arbitrary CSS variables in post content (discussion here), so for my use case, I'll look into server rendering CSS variables instead. I think this means I won't need to pursue the change in this PR after all, so I'll most likely put it back after only briefly picking it up 😄 Apologies for the false alarm! |
Hi folks! 👋🏻 What's preventing us from merging these additions? Supporting CSS variables in the CSS customizer is a commonly-requested feature (see Automattic/wp-calypso#62631) and would be very useful. I've read the security concerns from @jorgefilipecosta, but would that apply to the Custom CSS, which is a feature that only the user (or other authorized actors) have access to? Or is that because CSSTidy is currently used beyond the Custom CSS feature?* AFAIK, the only feature that officially uses CSSTidy in org, is the Custom CSS feature? |
The security concerns I raised in the past were not directly related to allow CSS vars on the custom CSS field, it was
Yes, I think my concerns do not apply to the custom CSS field. These users already have the ability to totally customize the site anyway. It may have been problematic if a user like a contributor using the editor and a group block can use arbitrary CSS variables because that user does not has customization capabilities, in the custom CSS case the user has customization capabilities so it is not the same case. |
Not customizer related per se, but just to also note that this Gutenberg PR – now part of release 13.2 – introduced CSS vars to control individual borders in the Block Editor. So rules such as Currently they're being stripped out: Is there a robust way to allow them for the editor only? |
Updating CSS variables appears to work now on self-hosted and WoA sites, but the version of CSS Tidy on WordPress.com sites still doesn't work, which came up in a ping earlier today p1658744878657319 -- would merging this PR also update the version that runs checks against WordPress.com CSS? Or would it make more sense to open a different GitHub issue for that? |
Given that CSS variables are working on self hosted and WoA sites, I'm not sure if this particular PR is still relevant. For reference, there are some CSS issues for simple WordPress.com sites catalogued here: Automattic/wp-calypso#61232 @fullofcaffeine given the work outlined in p9dueE-4t8-p2 do you think we can close this PR out? |
Sorry for the delay here. Yes, I think we can close this as D70703-code will take care of the underlying problem for simple sites once it's shipped. Thanks for working on this anyway! It served as a good reference on how to modify CSSTidy :) |
@samiff @fullofcaffeine Sorry to ping you on an older PR, but how was this fixed? Custom properties are still being stripped on WordCamp sites, but we do some custom processing using Jetpack's sanitization functions. Is it just a matter of removing those and falling back to the core CSS handling? |
It looks like |
@samiff , @fullofcaffeine : Are you seeing it work on self-hosted installs with
It works when you have jetpack/projects/plugins/jetpack/modules/custom-css/custom-css.php Lines 114 to 119 in 9f80daa
|
This update modifies the CSSTidy script following the example of #19935
Fixes #19669
Changes proposed in this Pull Request:
Adds support for CSS variables in Custom CSS
Jetpack product discussion
p1623934752160800-slack-calypso
Does this pull request change what data or activity we track or use?
No
Testing instructions: