From 43888b848c169a0672fa712a4485af034754effe Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 2 Nov 2022 20:26:11 +0100 Subject: [PATCH] LibWeb: Resolve unresolved CSS calc() values in StyleComputer When mixing calc() and var(), we're forced to delay resolving them until we're in StyleComputer. Previously we'd just skip over them. This patch handles calc() in the same pass as attr(). We reparse the calc() value after var() expansion, and then try to resolve it to a constant value if possible. If it's not possible, we leave the calc() where it is, and maybe layout can figure it out later. Note that I've only implemented resolution to integer and percentage in this commit. There are more things a calc() could resolve to, and we should implement those as well. --- .../Libraries/LibWeb/CSS/StyleComputer.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp index c3ee482b1e..e870817ec5 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp @@ -682,6 +682,27 @@ bool StyleComputer::expand_unresolved_values(DOM::Element& element, StringView p return false; } + if (value.function().name().equals_ignoring_case("calc"sv)) { + auto const& calc_function = value.function(); + if (auto calc_value = CSS::Parser::Parser::parse_calculated_value({}, Parser::ParsingContext { document() }, calc_function.values())) { + switch (calc_value->resolved_type()) { + case CalculatedStyleValue::ResolvedType::Integer: { + auto resolved_value = calc_value->resolve_integer(); + dest.empend(Parser::Token::create_number(resolved_value.value())); + continue; + } + case CalculatedStyleValue::ResolvedType::Percentage: { + auto resolved_value = calc_value->resolve_percentage(); + dest.empend(Parser::Token::create_percentage(resolved_value.value().value())); + continue; + } + default: + dbgln("FIXME: Unimplement calc() expansion in StyleComputer"); + break; + } + } + } + auto const& source_function = value.function(); Vector function_values; Parser::TokenStream source_function_contents { source_function.values() };