Selaa lähdekoodia

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.
Andreas Kling 2 vuotta sitten
vanhempi
commit
43888b848c
1 muutettua tiedostoa jossa 21 lisäystä ja 0 poistoa
  1. 21 0
      Userland/Libraries/LibWeb/CSS/StyleComputer.cpp

+ 21 - 0
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<Parser::ComponentValue> function_values;
             Parser::TokenStream source_function_contents { source_function.values() };