PHP-CSS-Parser
PHP-CSS-Parser copied to clipboard
Add format option to convert all colors to 6 character hex codes
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.
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?
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
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.
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.
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?
@oliverklee I did see that. It is better than not using it, but unfortunately it still doesn't work because it tries to convert the color into a 3 digit color code when that is an option.
https://github.com/MyIntervals/PHP-CSS-Parser/blob/f0a587646a25a92ed6a23e1e1610807eeb8b0eb8/src/Value/Color.php#L167
Is the plan to move the code for manipulating the css into another project so this one will only parse the files?
@hosef No, at the moment we have no plans in that direction. The only similar thing that was decided was deprecating and removing the expansion of shorthand properties.
The only similar thing that was decided was deprecating and removing the expansion of shorthand properties.
Ridiculously, this would have been potentially useful to the sister project, Emogrifier.
Is there a space for a new library that deals with CSS shortand properties - specifically?
Is there a space for a new library that deals with CSS shortand properties - specifically?
I think it would make sense - either as a stand-alone library, or as part of Emogrifier, or starting out as part of Emogrifier and maybe later getting its own space. :-)