LibWeb: Clamp calculated cubic-bezier() X coords using normal system

Previously we were doing this ad-hoc later in the process but we now
have the `calc` clamping system which can simplify things
This commit is contained in:
Callum Law 2025-10-10 00:59:59 +13:00 committed by Sam Atkins
parent ef4f01ea44
commit 06a57a280d
4 changed files with 11 additions and 15 deletions

View File

@ -312,9 +312,9 @@ EasingFunction EasingFunction::from_style_value(StyleValue const& style_value)
return LinearEasingFunction { resolved_control_points, linear.to_string(SerializationMode::ResolvedValue) };
},
[](EasingStyleValue::CubicBezier const& cubic_bezier) -> EasingFunction {
auto resolved_x1 = clamp(cubic_bezier.x1.resolved({}).value_or(0.0), 0.0, 1.0);
auto resolved_x1 = cubic_bezier.x1.resolved({}).value_or(0.0);
auto resolved_y1 = cubic_bezier.y1.resolved({}).value_or(0.0);
auto resolved_x2 = clamp(cubic_bezier.x2.resolved({}).value_or(0.0), 0.0, 1.0);
auto resolved_x2 = cubic_bezier.x2.resolved({}).value_or(0.0);
auto resolved_y2 = cubic_bezier.y2.resolved({}).value_or(0.0);
return CubicBezierEasingFunction { resolved_x1, resolved_y1, resolved_x2, resolved_y2, cubic_bezier.to_string(SerializationMode::Normal) };

View File

@ -566,6 +566,7 @@ private:
};
enum SpecialContext : u8 {
AngularColorStopList,
CubicBezierFunctionXCoordinate,
ShadowBlurRadius,
TranslateZArgument
};

View File

@ -2957,9 +2957,12 @@ RefPtr<StyleValue const> Parser::parse_easing_value(TokenStream<ComponentValue>&
return parse_number(argument_tokens);
};
m_value_context.append(SpecialContext::CubicBezierFunctionXCoordinate);
auto x1 = parse_argument(0);
auto y1 = parse_argument(1);
auto x2 = parse_argument(2);
m_value_context.take_last();
auto y1 = parse_argument(1);
auto y2 = parse_argument(3);
if (!x1.has_value() || !y1.has_value() || !x2.has_value() || !y2.has_value())
return nullptr;
@ -4136,6 +4139,9 @@ RefPtr<CalculatedStyleValue const> Parser::parse_calculated_value(ComponentValue
switch (special_context) {
case SpecialContext::AngularColorStopList:
return CalculationContext { .percentages_resolve_as = ValueType::Angle };
case SpecialContext::CubicBezierFunctionXCoordinate:
// Coordinates on the X axis must be between 0 and 1
return CalculationContext { .accepted_type_ranges = { { ValueType::Number, { 0, 1 } } } };
case SpecialContext::ShadowBlurRadius:
return CalculationContext { .accepted_type_ranges = { { ValueType::Length, { 0, NumericLimits<float>::max() } } } };
case SpecialContext::TranslateZArgument:

View File

@ -116,18 +116,7 @@ String EasingStyleValue::CubicBezier::to_string(SerializationMode mode) const
} else if (*this == CubicBezier::ease_in_out()) {
builder.append("ease-in-out"sv);
} else {
auto x1_value = x1;
auto y1_value = y1;
auto x2_value = x2;
auto y2_value = y2;
if (mode == SerializationMode::ResolvedValue) {
x1_value = clamp(x1_value.resolved({}).value_or(0.0), 0.0, 1.0);
x2_value = clamp(x2_value.resolved({}).value_or(0.0), 0.0, 1.0);
y1_value = y1_value.resolved({}).value_or(0.0);
y2_value = y2_value.resolved({}).value_or(0.0);
}
builder.appendff("cubic-bezier({}, {}, {}, {})",
x1_value.to_string(mode), y1_value.to_string(mode), x2_value.to_string(mode), y2_value.to_string(mode));
builder.appendff("cubic-bezier({}, {}, {}, {})", x1.to_string(mode), y1.to_string(mode), x2.to_string(mode), y2.to_string(mode));
}
return MUST(builder.to_string());
}