Re: [csswg-drafts] [css-values] Type conversion functions (#6408)

To make progress on this, here's a concrete proposal:

Let's introduce a `convert()` function that converts one value into another value.
It's syntax should look like this:

```ebnf
convert() = convert( <declaration-value>, <conversion-type>, <conversion-unit>? )
```

where `<conversion-type>` must be a [syntax string](https://drafts.css-houdini.org/css-properties-values-api/#syntax-string) and `<conversion-unit>` a unit string.

Which syntax strings are allowed as `<conversion-type>` needs to be discussed. The current use cases would then look like this:

* #1026 needs `"<number>"`. E.g. `margin-left: 1.5rem * calc(convert(counter(), "<number>")))`
* #542 needs `"<string>". E.g. `background-image: src("http://someimageprovider.com/" + convert(3, "<string>"))`

Definition for converting to `<string>`:

- From `<integer>`: The integer will be converted including a possible negative sign, e.g. `5` ⇒ `"5"`, `-5` ⇒ `"-5"`.
- From `<number>`: The number will be converted including the decimal dot, e.g. `3.1415` ⇒ `"3.1415"`.
- From `<dimension>`, `<percentage>`, or `<flex>`: The dimension, percentage, or flexible value will be converted including the value plus a possible negative sign and the unit, e.g. `2em` ⇒ `"2em"`, `1.2s` ⇒ `"1.2s"`, `-90deg` ⇒ `"-90deg"`, `25%` ⇒ `"25%"`, `2fr` ⇒ `"2fr"`.
- From `<string>`: No change happens, e.g. `"foo"` ⇒ `"foo"`.

Definition for converting to `<number>`:
- From `<integer>`: The integer will be converted directly, e.g. `5` ⇒ `5`, `-5` ⇒ `-5`.
- From `<number>`: No change happens, e.g. `3.1415` ⇒ `3.1415`.
- From `<dimension>`, `<percentage>`, or `<flex>`: The dimension, percentage, or flexible value will be converted by taking its value and stripping away the unit, e.g. `2em` ⇒ `2`, `1.2s` ⇒ `1.2`, `-90deg` ⇒ `-90`, `25%` ⇒ `25`, `2fr` ⇒ `2`.
- From `<string>`: In case the string consists of a numeric value, it is converted, e.g. `"2"` ⇒ `2`, `"3.1415"` ⇒ `3.1415`, `"-5"` ⇒ `-5`. Otherwise, the conversion fails and results in an invalid value.

Sebastian

-- 
GitHub Notification of comment by SebastianZ
Please view or discuss this issue at https://github.com/w3c/csswg-drafts/issues/6408#issuecomment-2317424601 using your GitHub account


-- 
Sent via github-notify-ml as configured in https://github.com/w3c/github-notify-ml-config

Received on Thursday, 29 August 2024 11:50:04 UTC