MyIntervals / PHP-CSS-Parser

A Parser for CSS Files written in PHP. Allows extraction of CSS files into a data structure, manipulation of said structure and output as (optimized) CSS
http://www.sabberworm.com/blog/2010/6/10/php-css-parser
MIT License
1.75k stars 150 forks source link

Add format option to convert all colors to 6 character hex codes #728

Open hosef opened 3 days ago

hosef commented 3 days ago

In the HTML 5 spec for color input elements, it says that values should always be a 6character hex code. If it does not match that format, then the browser will use #000000 as the default value for your fields.

It would be really handy to have the option to convert all of the colors to that format so that they can be used in forms without needing to do further processing of them.

oliverklee commented 3 days ago

Hi @hosef, thanks for suggesting this!

Do you have a link to the part of the reference that says that color values need to be six-digit hex codes?

According to https://developer.mozilla.org/en-US/docs/Web/CSS/hex-color, there are multiple formats, ranging from 3- to 8-value formats.

Also, I'm not sure normalizing the color format should be a responsibility of the CSS parser. IMHO, a parser should return the CSS as it is and not transform ist. (That's one of the reasons we're going to drop the shorthand notation expansion in #511.)

@sabberworm @JakeQZ What do you think?

hosef commented 3 days ago

It is from the HTML5 spec, not the CSS spec. I am not sure why, but HTML color inputs require exactly that format to use them.

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/color#value

oliverklee commented 2 days ago

Thanks! I wasn't aware of that.

Still, I tend to think this is outside of the scope of parsing CSS, but should be done by the PHP that uses the parsed CSS. I'd like to hear the thoughts of the other project maintainers on this.

sabberworm commented 2 days ago

Also, I'm not sure normalizing the color format should be a responsibility of the CSS parser. IMHO, a parser should return the CSS as it is and not transform it. (That's one of the reasons we're going to drop the shorthand notation expansion in #511.)

I agree it’s outside the scope of parsing CSS. However, I don’t think it’s outside the scope of stringifying to CSS, which this library also does. As a matter of fact, I think OutputFormat already has an option bRGBHashNotation that does exactly this, if I’m not mistaken.

oliverklee commented 6 hours ago

It indeed has that option! https://github.com/MyIntervals/PHP-CSS-Parser/blob/main/src/OutputFormat.php#L27

@hosef Does this option solve your request?