-
Notifications
You must be signed in to change notification settings - Fork 3.4k
KSES: Add support for rgb(a) color. #3097
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
Changes from 2 commits
5d2801f
692f37a
c9b2fb5
08e1e31
8d49e8f
af88ca5
b1dc05a
97fe09e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2395,6 +2395,14 @@ function safecss_filter_attr( $css, $deprecated = '' ) { | |
| 'list-style-image', | ||
| ); | ||
|
|
||
| /* | ||
| * CSS attributes that accept rgba color data types. | ||
| * | ||
| */ | ||
| $css_rgba_color_data_types = array( | ||
| 'background-color', | ||
| ); | ||
|
peterwilsoncc marked this conversation as resolved.
Outdated
|
||
|
|
||
| /* | ||
| * CSS attributes that accept gradient data types. | ||
| * | ||
|
|
@@ -2418,6 +2426,7 @@ function safecss_filter_attr( $css, $deprecated = '' ) { | |
| $css_test_string = $css_item; | ||
| $found = false; | ||
| $url_attr = false; | ||
| $rgba_attr = false; | ||
|
peterwilsoncc marked this conversation as resolved.
Outdated
|
||
| $gradient_attr = false; | ||
|
|
||
| if ( strpos( $css_item, ':' ) === false ) { | ||
|
|
@@ -2430,6 +2439,7 @@ function safecss_filter_attr( $css, $deprecated = '' ) { | |
| $found = true; | ||
| $url_attr = in_array( $css_selector, $css_url_data_types, true ); | ||
| $gradient_attr = in_array( $css_selector, $css_gradient_data_types, true ); | ||
| $rgba_attr = in_array( $css_selector, $css_rgba_color_data_types, true ); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -2466,6 +2476,13 @@ function safecss_filter_attr( $css, $deprecated = '' ) { | |
| } | ||
| } | ||
|
|
||
| if ( $found && $rgba_attr ) { | ||
| $css_value = trim( $parts[1] ); | ||
| if ( preg_match( '/^rgba\((\d{1,3}%?),\s*(\d{1,3}%?),\s*(\d{1,3}%?),\s*(\d*(?:\.\d+)?)\)$/', $css_value ) ) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to accept both This will require some changes to the |
||
| $css_test_string = str_replace( $css_value, '', $css_test_string ); | ||
| } | ||
| } | ||
|
|
||
| if ( $found ) { | ||
| // Allow CSS calc(). | ||
| $css_test_string = preg_replace( '/calc\(((?:\([^()]*\)?|[^()])*)\)/', '', $css_test_string ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1110,6 +1110,31 @@ public function data_test_safecss_filter_attr() { | |
| 'css' => 'height: expression( body.scrollTop + 50 + "px" )', | ||
| 'expected' => '', | ||
| ), | ||
| // RGBA background color are allowed. | ||
| array( | ||
| 'css' => 'background-color: rgba(0,0,0,0)', | ||
| 'expected' => 'background-color: rgba(0,0,0,0)', | ||
| ), | ||
| array( | ||
| 'css' => 'background-color: rgba(0, 0, 0, 0)', | ||
| 'expected' => 'background-color: rgba(0, 0, 0, 0)', | ||
| ), | ||
| array( | ||
| 'css' => 'background-color: rgba(100, 100, 100, 0)', | ||
| 'expected' => 'background-color: rgba(100, 100, 100, 0)', | ||
| ), | ||
| array( | ||
| 'css' => 'background-color: rgba(10%, 10%, 10%, 0)', | ||
| 'expected' => 'background-color: rgba(10%, 10%, 10%, 0)', | ||
| ), | ||
| array( | ||
| 'css' => 'background-color: rgba(0, 0, 0, 0.1)', | ||
| 'expected' => 'background-color: rgba(0, 0, 0, 0.1)', | ||
| ), | ||
| array( | ||
| 'css' => 'background-color: rgba(0, 0, 0, .1)', | ||
| 'expected' => 'background-color: rgba(0, 0, 0, .1)', | ||
| ), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's an test below under the comment "RGBA color values are not allowed." that will now fail. It would be good to test for some malformed values too.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am thinking about regular expression and test cases, am I correct in this perception? ✅ ✅ ✅ According to MDN, I believe the same rules can be applied to both rgb() and rgba().
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, you are. These are the sort of tests I was suggesting. You may also wish to include something containing either a Thanks so much. |
||
| // RGB color values are not allowed. | ||
| array( | ||
| 'css' => 'color: rgb( 100, 100, 100 )', | ||
|
|
||
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.
Also don't forget the shorthand
backgroundborderborder-(left|right|top|bottom)That's assuming support :)