-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Custom properties inside builtin functions like rgba()
throw error
#2986
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
Comments
Just escape. I put "Feature Request" label since it definitely becomes a problem when the custom properties are in TR. It's easy to fix by disabling any error detection for CSS functions args (though it's quite sad as it kills an important part of the language - i.e. we'll never find a error anymore until debug in a browser). |
rgba()
throw error
Custom properties have support across all browsers except IE11. They'll probably hit mainstream within the next year, so I think support should be implemented soon. Less, AFAIK, also doesn't have tests for the extremely permissive nature of custom property values. They can contain pretty much anything, even semi-colons, as long as they're not top-level tokens (not contained in bracket or curly-brace pairs). See: https://www.w3.org/TR/css-variables/#syntax |
Since this change requires an n-of-args checking anyway (to keep at least some error validation within the functions), it's also assumed that the newer rgba(var(--some), .42); // -> rgba(var(--some), .42)
rgba(#010101, .42); // -> rgba(1, 1, 1, .42) |
How should we handle custom properties in args in general? Technically, you can use |
I see a whole bunch of built-in CSS functions at https://github.com/less/less.js/blob/master/lib/less/functions/color.js#L34, and IMO most of that shouldn't be there. Less isn't a linter, and those aren't Less functions. If the parser tries to get super-clever at CSS functions, it's going to fail. So I think that's the source of the problem, that Less currently is monkey-patching regular CSS color functions rather than letting them pass-through. I'm not sure if that was added for browsers that didn't yet support |
Considering my comment at #3214, for this issue the quick fix would be just: "if (nargs < 3/4) fallback to css string (by returning nothing)" in the corresponding functions implementations. (And a more strict fix is in putting in checks for every arg in every such func to decide if it's evaluable and returning either a color object or a CSS-string fallback). |
And a similar check for
That is: should we do the following:
This is an interesting problem because the introduction of custom properties has made CSS functions un-resolvable at compile time. To some degree we may be at the end of an era of static pre-processing. But that's a whole different discussion. |
Nothing new really. All necessary facility to handle this with Less is there since its v1.x - see below. The only challenge is to code it in a non-bloating way.
No, the functions should just return
Also no, there's no need to check for Btw., notice that |
@seven-phases-max Oh okay, so you think the simple fix is for these CSS/Less functions to return undefined rather than throw an error (to render as-is)? That seems reasonable, then if function no. 2 (like |
Arguing with myself:
On the other hand, it would not be a problem (and even tempting) to detect
Yes, exactly - we don't even need any new code for this (though ideally they (functions like |
Well, the other thing not mentioned is that something like |
Yes, this is what I mean with
The only valid args for Less |
write like this~ :root {
--color-accent: 169,57,255;
}
button:hover {
background-color: ~"rgba(var(--color-accent), 0.2)";
color: ~"rgb(var(--color-accent))";
} |
@weivea On the latest version of Less 3.x, this is not necessary, just write |
Syntax of the content of CSS custom properties is very permissive but that causes problems when using such properties inside builtin functions like
rgba()
Example:
My project allows user to define accent color on which the whole app UI and various custom components is based. And I am using rgb format because of the need to fade in certain places and CSS does not provide anything like
fade(@color, 50%)
in less. That way i can dorgba( var(--color), 0.5 )
. This works in Chrome and is supported by the standart. However less throws the following errorerror evaluation function 'rgba': color functions take numbers as parameters
.Example code:
Note: This is a drop-in library and i don't want to force users to add another build step into their workflow only to build my less library with their colors. That's why i'm using the new and shiny custom properties (and also because of its scoping capability).
BTW: Is there some temporary workaround that'd skip such strict handling and build it anyway? It's not like I'm missing a bracket in a nested rules.
The text was updated successfully, but these errors were encountered: