Issue #2404 - Enable CSS aspect-ratio sizing

This commit is contained in:
Basilisk-Dev 2026-05-10 13:00:24 -04:00 • committed by EAZYBLACK
commit 7159f5fa08
3 changed files with 67 additions and 6 deletions

View file

@ -1170,6 +1170,8 @@ protected:
// Property specific parsing routines // Property specific parsing routines
bool ParseImageLayers(const nsCSSPropertyID aTable[]); bool ParseImageLayers(const nsCSSPropertyID aTable[]);
bool ParseAspectRatio(nsCSSValue& aValue);
bool ParseAspectRatioRatio(nsCSSValue& aValue);
struct ImageLayersShorthandParseState { struct ImageLayersShorthandParseState {
nsCSSValue& mColor; nsCSSValue& mColor;
@ -13270,6 +13272,59 @@ CSSParserImpl::ParsePropertyByFunction(nsCSSPropertyID aPropID)
#define BG_CLR (BG_CENTER | BG_LEFT | BG_RIGHT) #define BG_CLR (BG_CENTER | BG_LEFT | BG_RIGHT)
#define BG_LR (BG_LEFT | BG_RIGHT) #define BG_LR (BG_LEFT | BG_RIGHT)
bool
CSSParserImpl::ParseAspectRatioRatio(nsCSSValue& aValue)
{
nsCSSValue width;
if (!ParseNonNegativeNumber(width)) {
return false;
}
float w = width.GetFloatValue();
float h = 1.0f;
if (ExpectSymbol('/', true)) {
nsCSSValue height;
if (!ParseNonNegativeNumber(height)) {
return false;
}
h = height.GetFloatValue();
}
// Degenerate ratios behave as auto in layout.
aValue.SetFloatValue(w == 0.0f || h == 0.0f ? 0.0f : w / h,
eCSSUnit_Number);
return true;
}
bool
CSSParserImpl::ParseAspectRatio(nsCSSValue& aValue)
{
if (ParseSingleTokenVariant(aValue, VARIANT_INHERIT, nullptr)) {
return true;
}
nsCSSValue autoValue;
bool hasAuto = ParseSingleTokenVariant(autoValue, VARIANT_AUTO, nullptr);
nsCSSValue ratioValue;
bool hasRatio = ParseAspectRatioRatio(ratioValue);
if (!hasAuto && !hasRatio) {
return false;
}
if (hasRatio) {
if (!hasAuto) {
// The grammar is "auto || <ratio>", so auto may appear after the ratio.
ParseSingleTokenVariant(autoValue, VARIANT_AUTO, nullptr);
}
aValue = ratioValue;
return true;
}
aValue.SetAutoValue();
return true;
}
CSSParseResult CSSParseResult
CSSParserImpl::ParseBoxProperty(nsCSSValue& aValue, CSSParserImpl::ParseBoxProperty(nsCSSValue& aValue,
nsCSSPropertyID aPropID) nsCSSPropertyID aPropID)
@ -13308,6 +13363,8 @@ CSSParserImpl::ParseSingleValuePropertyByFunction(nsCSSValue& aValue,
switch (aPropID) { switch (aPropID) {
case eCSSProperty_clip_path: case eCSSProperty_clip_path:
return ParseClipPath(aValue); return ParseClipPath(aValue);
case eCSSProperty_aspect_ratio:
return ParseAspectRatio(aValue);
case eCSSProperty_contain: case eCSSProperty_contain:
return ParseContain(aValue); return ParseContain(aValue);
case eCSSProperty_font_family: case eCSSProperty_font_family:

View file

@ -7021,4 +7021,3 @@ nsComputedDOMStyle::DoGetOverflowBlockEnd()
{ {
return DoGetOverflowBlock(); return DoGetOverflowBlock();
} }

View file

@ -9310,11 +9310,16 @@ nsRuleNode::ComputePositionData(void* aStartStruct,
SETCOORD_UNSET_INITIAL, SETCOORD_UNSET_INITIAL,
aContext, mPresContext, conditions); aContext, mPresContext, conditions);
// aspect-ratio: float, initial // aspect-ratio: auto | <ratio>
SetFactor(*aRuleData->ValueForAspectRatio(), const nsCSSValue* aspectRatio = aRuleData->ValueForAspectRatio();
pos->mAspectRatio, conditions, if (aspectRatio->GetUnit() == eCSSUnit_Auto) {
parentPos->mAspectRatio, 0.0f, pos->mAspectRatio = 0.0f;
SETFCT_UNSET_INITIAL | SETFCT_POSITIVE | SETFCT_NONE); } else {
SetFactor(*aspectRatio,
pos->mAspectRatio, conditions,
parentPos->mAspectRatio, 0.0f,
SETFCT_UNSET_INITIAL | SETFCT_POSITIVE | SETFCT_NONE);
}
// box-sizing: enum, inherit, initial // box-sizing: enum, inherit, initial
SetValue(*aRuleData->ValueForBoxSizing(), SetValue(*aRuleData->ValueForBoxSizing(),