-
Notifications
You must be signed in to change notification settings - Fork 677
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
[css-values] Type conversion functions #6408
Comments
See #1026. Rather than converting the internal counter value into a string, and then parsing it as a number, something like |
Thank you for pointing me at #1026, @Loirooriol! For my current use case, a Though I still believe general type conversion functions would have some value and would make the conversion explicit. Note that general conversion may also be used for functions returning a number like the discussed Sebastian |
The problem with a general conversion function is that often these conversions are impossible, so the function needs to either give up and return Since most use cases of conversion to number revolve around counters, I think we should add Note that the conversion to strings is an entirely different issue that is discussed in #542. |
@SebastianZ Seems to me we should close this out in favor of #1026 and #542, does that work for you or is there something left here that we should keep it open for? |
#1026 and #542 cover the current use cases of converting counter values to numbers and any kind of value to strings. Note that the main point of this issue is to discuss whether it would be better to provide a unified and explicit way for type conversion instead of having different mechanisms. And the idea of making all type conversions explicit actually rose from the discussion in #542, as I initially mentioned.
You're right. Though I don't see this as a problem but an advantage of an explicit function. And I include string conversion in this, as that is probably the most common use case for type conversions. Of course a general mechanism as I mentioned it in my initial post has the downside that it's much longer to write than implicit conversion. Also in relation to @LeaVerou's point, it might not be obvious to authors if and how specific conversions actually work. Sebastian |
I think there's one case where implicit conversion makes sense, and I just opened an issue on it: #6786
We generally don't add things without enough use cases, just for completeness' sake. If going from and to any type has enough use cases, sure. But so far it seems that use cases concentrate in a couple of conversions. |
A general function could also be defined to just cover the current use cases for now and be extended in the future. Sebastian |
In a comment on #10001, I suggested that it would be useful for authors to be able to parse an attribute value, With explicit type conversion functions, the current specification of |
Well, a space-separated list is is a more complex type than e.g. Sebastian |
To make progress on this, here's a concrete proposal: Let's introduce a convert() = convert( <declaration-value>, <conversion-type>, <conversion-unit>? ) where Which syntax strings are allowed as
Definition for converting to
Definition for converting to
Definition for converting to
Sebastian |
Is this addressing any usecase not covered by #1026 and #542? Because I would prefer to address these with a specific solution for them. Also it's not clear if converting string to number will accept scientific notation, leading/surrounding whitespace, strings like Also not a fan of just dropping units. Converting |
Yes, explicit type conversion functions may also cover type conversion in
Any string that can be interpreted as a valid
So how would you convert a string Sebastian |
If we had multiple functions like that, it could make sense to do it in a generic way. But if
Well, if it's parsing as
Yes, it should fail like in https://drafts.csswg.org/css-values-5/#valdef-attr-number |
Isn’t there also a proposal somewhere for getting query parameters or fragment identifiers from the stylesheet URL into property value space? That, |
@LeaVerou mentioned in a CSS Day talk from 2022 some other use cases for explicit type conversion. She used counters to display a percentage on a bar chart. Here's a screenshot of that talk (@LeaVerou Hope that's fine to post it here!): With explicit type conversion, you could shorten this example to <div style="--p: 49%">F</div> .bar-chart > div {
height: var(--p);
}
.bar-chart > div::before {
content: convert(var(--p), <string>);
} And it would look much less hacky.
Explicit type conversion is non-exclusive to implicit conversions. So, an
Fine for me.
Fair enough. Instead of using
I'm not sure there is one for extracting information from a URL. Maybe you are referring to linked parameters to pass parameters to a resource? If there was some way to extract information from a URL, explicit type conversion could be used to interpret those parameters, though. Sebastian |
@SebastianZ In general, the need to print out a numeric value as text comes up all the time. E.g. most recently in printing out page numbers that were set via a Paged.js-like script (and not via counters). But also for debugging — until there is a better way. @Crissov this is a completely orthogonal issue. Please search and open a new issue if it doesn't exist. |
I've updated my suggestion to include the use case for converting to
This is debatable. As an author, I'd expect Sebastian |
You can't directly convert lengths to numbers. If See Why does hypot() allow dimensions (values with units), but pow() and sqrt() only work on numbers? in the spec. |
In #542 there was already some discussion about whether type conversion for strings should be im- or explicit. And it seems the conclusion was to be explicit about that.
I'd like to go a step further and start the discussion whether other type conversion functions should be introduced, too.
One use case I had today was that I wanted to use a counter within a calculation, i.e. something like
calc(counter(x) * 3)
. Though that currently doesn't work becausecounter(x)
doesn't return a numeric value.This could be approached in two ways:
One conversion function that is able to convert any type into any other type. This would require several parameters for the value to convert, the type to convert to and an optional unit.
Examples:
One conversion function for each type.
Examples:
Sebastian
The text was updated successfully, but these errors were encountered: