w3c / csswg-drafts

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

[mediaqueries] Define `aspect-ratio` with `<number> [/ <number]?` instead of `<ratio>` #10906

Open cdoublev opened 1 month ago

cdoublev commented 1 month ago

<ratio> does not allow negative <number>s: <number [0,∞]> [ / <number [0,∞]> ]?.

(1) Media Queries defines range features that are false in the negative range*, with an example showing that aspect-ratio > -1 evaluates to true instead of unknown, which current versions of Chrome and FF (at least) do not do.

true seems largely reasonable. The problem is to evaluate something matching <general-enclosed>, ie. <any-value>, to true. Instead of defining this quirk, the syntax should reflect that negative values are valid in this context, imo.

(2) This would require defining <mf-value> with ... | <number> [/ <number]? instead of ... | <ratio>. An alternative could be to define the aspect-ratio property with <ratio [0,∞]>.

<ratio> is only used to declare *-aspect-ratio property/features in CSS, but negative ratios seem to be valid when used to measure other values, like sound compression, and more generally, in mathematics. Since I am not an expert in both fields, I might be wrong.

The spec example actually uses resolution = -300dpi, whereas <resolution> accepts negative values and I do not see how/where a negative resolution value could make sense.

However, restricting <resolution> to non-negative values was proposed in #1196, which was closed with this comment:

I don't think the definition of the unit should have anything to say about negative values. That should be handled in the definition of the properties (and descriptors) that accept resolution values.

My point is that this might be true for <ratio> but not for <resolution>.

(3) The result of a math function should be clamped to the closest range boundary (if any), therefore aspect-ratio > 1 / calc(-1) should presumably be evaluated to false (aspect-ratio is not greater than Infinity, the result of 1 / 0), which might be unexpected.


* In the definition of false in the negative range, about when querying whether the feature is greater or equal to a negative value, "if the relationship is true" is rather confusing to me: assuming feature is -2, f < -1 should evaluate to false, f > -3 to true, and f > -1 to false? I suspect that this concept expects related features to always have non-negative values, so for any n < 0, f > n is always true, no matter the value of f.

Loirooriol commented 1 month ago

Worth noting that we interpolate ratios in a way that only makes sense for non-negative ratios.

For example, the intermediate ratio between two ratios is their geometric mean, which is defined for non-negative numbers.

But I guess we could say that ratios can only be interpolated if they have the same sign (and aren't degenerate), then if both are negative we interpolate the absolute values and multiply the result by -1.

Alternatively, I guess <ratio> could be a generic pair of numbers and interpolate them independently, and then restrict aspect-ratio to non-negative ratios, and set the current interpolation behavior as the interpolation type of the property.

I'm not sure it's worth doing, though.