From 06a57a280dc862f6439340a861550bad32e95e3f Mon Sep 17 00:00:00 2001 From: Callum Law Date: Fri, 10 Oct 2025 00:59:59 +1300 Subject: [PATCH] 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 --- Libraries/LibWeb/CSS/EasingFunction.cpp | 4 ++-- Libraries/LibWeb/CSS/Parser/Parser.h | 1 + Libraries/LibWeb/CSS/Parser/ValueParsing.cpp | 8 +++++++- .../LibWeb/CSS/StyleValues/EasingStyleValue.cpp | 13 +------------ 4 files changed, 11 insertions(+), 15 deletions(-) diff --git a/Libraries/LibWeb/CSS/EasingFunction.cpp b/Libraries/LibWeb/CSS/EasingFunction.cpp index 2e930f64f8b..b9b99402f3c 100644 --- a/Libraries/LibWeb/CSS/EasingFunction.cpp +++ b/Libraries/LibWeb/CSS/EasingFunction.cpp @@ -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) }; diff --git a/Libraries/LibWeb/CSS/Parser/Parser.h b/Libraries/LibWeb/CSS/Parser/Parser.h index 3e711781c9c..f05ee701740 100644 --- a/Libraries/LibWeb/CSS/Parser/Parser.h +++ b/Libraries/LibWeb/CSS/Parser/Parser.h @@ -566,6 +566,7 @@ private: }; enum SpecialContext : u8 { AngularColorStopList, + CubicBezierFunctionXCoordinate, ShadowBlurRadius, TranslateZArgument }; diff --git a/Libraries/LibWeb/CSS/Parser/ValueParsing.cpp b/Libraries/LibWeb/CSS/Parser/ValueParsing.cpp index af79bee6d2c..e98260424c2 100644 --- a/Libraries/LibWeb/CSS/Parser/ValueParsing.cpp +++ b/Libraries/LibWeb/CSS/Parser/ValueParsing.cpp @@ -2957,9 +2957,12 @@ RefPtr Parser::parse_easing_value(TokenStream& 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 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::max() } } } }; case SpecialContext::TranslateZArgument: diff --git a/Libraries/LibWeb/CSS/StyleValues/EasingStyleValue.cpp b/Libraries/LibWeb/CSS/StyleValues/EasingStyleValue.cpp index d89314e83c3..ec8853ca688 100644 --- a/Libraries/LibWeb/CSS/StyleValues/EasingStyleValue.cpp +++ b/Libraries/LibWeb/CSS/StyleValues/EasingStyleValue.cpp @@ -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()); }