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

CSSTidy: add support for css variable definition #19935

Closed
wants to merge 12 commits into from

Conversation

glendaviesnz
Copy link
Contributor

@glendaviesnz glendaviesnz commented May 26, 2021

Fixes issue with block validations when css variables used, eg with group block custom color. Although this is fixed in Gutenberg 10.7 for the group block, there are a number of other blocks that support the css vars for custom colours and it may be a few releases before they are are updated.

There has been previous discussion about the removal of css tidy, and given the age of this library that is probably worth looking at - and maybe a more modern and maintained library like https://github.com/wikimedia/css-sanitizer could be investigated, but as previously discussed this is a much bigger project so adding this css var support may still make sense in the short term.

This would also currently allow the use of css vars in the custom css in the customizer - would that be a bad/dangerous thing?

Changes proposed in this Pull Request:

  • Modify CSS Tidy library to allow css variable properties - while this is a modification to the underlying 3rd party library, this library hasn't been updated since 2007 and is no longer maintained.

Does this pull request change what data or activity we track or use?

no

Testing instructions:

  • Add related patch to sandbox
  • In editor code view add a <div style="--base-color: red;color:var(--base-color)">test css var preservation</div>
  • Save the page and reload the editor and check that --base-color property has been preserved in editor and front end.

@matticbot
Copy link
Contributor

Caution: This PR has changes that must be merged to WordPress.com
Hello glendaviesnz! These changes need to be synced to WordPress.com - If you 're an a11n, please commandeer and confirm D61884-code works as expected before merging this PR. Once this PR is merged, please commit the changes to WP.com. Thank you!
This revision will be updated with each commit to this PR

@github-actions github-actions bot added the [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ label May 26, 2021
@github-actions
Copy link
Contributor

github-actions bot commented May 26, 2021

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ⚠️ All commits were linted before commit.
  • ✅ Add a "[Status]" label (In Progress, Needs Team Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

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 🤖


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.
Then, add the "[Status] Needs Team review" label and ask someone from your team review the code.
Once you’ve done so, switch to the "[Status] Needs Review" label; someone from Jetpack Crew will then review this PR and merge it to be included in the next Jetpack release.


Jetpack plugin:

  • Next scheduled release: July 6, 2021.
  • Scheduled code freeze: June 28, 2021.

@github-actions github-actions bot added the [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! label May 26, 2021
@ramonjd
Copy link
Member

ramonjd commented May 26, 2021

With $this->settings['preserve_css_variables'] = true; I created a group block on 10.5.4 (current prod).

Source:

<!-- wp:group {"style":{"color":{"link":"var:preset|color|vivid-red"}}} -->
<div class="wp-block-group has-link-color" style="--wp--style--color--link:var(--wp--preset--color--vivid-red)"></div>
<!-- /wp:group -->

Before this patch, the result after save was:

<!-- wp:group {"className":"has-link-color"} -->
<div class="wp-block-group has-link-color"></div>
<!-- /wp:group -->

After this patch the HTML remained intact. 😄

Also checked Gutenberg: v10.7.0-rc.1 with the same result.

This would also currently allow the use of css vars in the custom css in the customizer - would that be a bad/dangerous thing?

Would it make sense to add some unit tests so we can prove that it will pass in various scenarios?

@glendaviesnz
Copy link
Contributor Author

Would it make sense to add some unit tests so we can prove that it will pass in various scenarios?

Good idea, will try and add some tests.

@ramonjd
Copy link
Member

ramonjd commented May 28, 2021

f171c17 updates the regex to accept all alphanumeric variable names separated by _ or -.

Things like --, --_ and --var_ are invalid.

Also updated the tests 👍

@glendaviesnz
Copy link
Contributor Author

Thanks @ramonjd, the changes tested well for me.

@glendaviesnz glendaviesnz added [Status] Needs Review To request a review from Crew. Label will be renamed soon. and removed [Status] Needs Team Review [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! labels May 30, 2021
@@ -0,0 +1,67 @@
<?php
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for adding some tests! ❤️

@@ -1,4 +1,5 @@
<?php
// phpcs:ignoreFile
Copy link
Member

Choose a reason for hiding this comment

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

Is there a specific reason while you set to ignore the whole file? It's full of errors at the moment, but maybe we can stick to our coding standards for new code we introduce?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeh, I wasn't sure how to handle it given it was original a 3rd party file, but given it is never going to be updated externally now you are probably right, so have removed the ignore.

@jeherve jeherve added [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! and removed [Status] Needs Review To request a review from Crew. Label will be renamed soon. labels May 31, 2021
@glendaviesnz
Copy link
Contributor Author

@jeherve Do you think this is something we should run past secops? I can't see how this would open up any sort of xss hole, etc. but is it worth getting a second opinion on it anyway?

@ramonjd
Copy link
Member

ramonjd commented Jun 1, 2021

Do you think this is something we should run past secops? I can't see how this would open up any sort of xss hole, etc. but is it worth getting a second opinion on it anyway?

I have something similar over here: #19993 Will be adding tests but it'd be good to get a 👍 just in case.

@jeherve
Copy link
Member

jeherve commented Jun 1, 2021

Do you think this is something we should run past secops? I can't see how this would open up any sort of xss hole, etc. but is it worth getting a second opinion on it anyway?

Will be adding tests but it'd be good to get a 👍 just in case.

That'd be my vote as well 👍

@glendaviesnz
Copy link
Contributor Author

@jeherve, @ramonjd, fyi I have copied secops into the accompanying diff for feedback

@glendaviesnz glendaviesnz added the [Status] Needs Review To request a review from Crew. Label will be renamed soon. label Jun 9, 2021
@glendaviesnz
Copy link
Contributor Author

The change that removed this requirement from the group block is now merged in Gutenberg. It turned out that the other blocks that use css vars use var(--main-color) inline, but don't declare the css properties inline (--main-color: red), only the group block was doing this, so closing this for now.

@glendaviesnz glendaviesnz deleted the add/css-vars-to-csstidy branch June 9, 2021 05:35
@github-actions github-actions bot removed [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! [Status] Needs Review To request a review from Crew. Label will be renamed soon. labels Jun 9, 2021
@kraftbj kraftbj removed the Csstidy label Aug 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Custom CSS [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ Touches WP.com Files Unit Tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants