w3c / csswg-drafts

CSS Working Group Editor Drafts
https://drafts.csswg.org/
Other
4.44k stars 657 forks source link

[css-values-5] Separate `%` from `<dimension-unit>` #7381

Open cdoublev opened 2 years ago

cdoublev commented 2 years ago

CSS Values 5 defines <dimension-unit> as a literal "%" character (that is, a <delim-token> with a value of "%") or an ident whose value is any of the CSS units for <length>, <angle>, <time>, <frequency>, or <flex> values (such as px or ms).

According to Wikipedia (sorry for not providing a more "academic" reference): A percentage is a dimensionless number (pure number); it has no unit of measurement.

According to Syntax, <percentage-token> has no unit.

I would like to suggest separating % from <dimension-unit> and define <attr-type> as string | url | ident | color | number | percentage | length | angle | time | frequency | flex | '%' | <dimension-unit>.

The same confusion also exists in step 8.2 of simplify a calculation tree:

  1. For each set of root’s children that are numeric values with identical units, remove those children and replace them with a single numeric value containing the sum of the removed nodes, and with the same unit. (E.g. combine numbers, combine percentages, combine px values, etc.)

Imho, this can be reworded to For each set of root’s children that are <number>, <percentage>, <dimension> with an identical unit, [...].

tabatkins commented 2 years ago

The mathematical definition of percentage (just another way of writing a number) and the CSS definition (a distinct unit separate from a number) are already different; we can't and don't want to unify them.

The <dimension-unit> production appears to be correct and unambiguous. I suppose technically the definition in the next section is slightly incorrect:

If the result is a <number-token>, the substitution value is a dimension with the result’s value, and the given unit.

...and could have an "(or a percentage with the result's value)" appended to the end.

That said, I'd rather just clarify in Values that a percentage can also be considered to have a "unit" of %; that would also fix the calculation tree reference. For all intents and purposes it does; the separation of dimensions and percentages is pretty much a legacy thing that I carried from CSS2's organization. For example, there is no meaningful difference between 1% and 1vw; both are numeric values with a unit indicating that it's 1/100th of some reference value.

cdoublev commented 2 years ago

I'd rather just clarify in Values that a percentage can also be considered to have a "unit" of %

Alright, thanks.