From b54cd17c1ee7eb0a46af8feac66ca4180df8416d Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 27 Jan 2022 14:47:39 +0000 Subject: [PATCH] LibWeb: Allow percentage tokens again when parsing calc() I unintentionally broke this in my LengthPercentage PR, but it was not convenient to fix until now. --- Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp | 14 ++++++++++---- Userland/Libraries/LibWeb/CSS/StyleValue.cpp | 4 ++++ Userland/Libraries/LibWeb/CSS/StyleValue.h | 2 +- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index 0721ffacf2..2703e79558 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -4210,10 +4210,16 @@ Optional Parser::parse_calc_value(TokenStream(current_token.token().number_value()) }; if (current_token.is(Token::Type::Dimension) || current_token.is(Token::Type::Percentage)) { - auto maybe_length = parse_length(current_token); - if (maybe_length.has_value() && !maybe_length.value().is_undefined()) - return CalculatedStyleValue::CalcValue { maybe_length.value() }; - return {}; + auto maybe_dimension = parse_dimension(current_token); + if (!maybe_dimension.has_value()) + return {}; + auto& dimension = maybe_dimension.value(); + + if (dimension.is_length()) + return CalculatedStyleValue::CalcValue { dimension.length() }; + if (dimension.is_percentage()) + return CalculatedStyleValue::CalcValue { dimension.percentage() }; + VERIFY_NOT_REACHED(); } return {}; diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp index c067a015ad..cd91c4ef4a 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp @@ -574,6 +574,7 @@ Optional CalculatedStyleValue::CalcValue::re return value.visit( [](float) -> Optional { return { ResolvedType::Number }; }, [](Length const&) -> Optional { return { ResolvedType::Length }; }, + [](Percentage const&) -> Optional { return { ResolvedType::Percentage }; }, [](NonnullOwnPtr const& sum) { return sum->resolved_type(); }); } @@ -604,6 +605,9 @@ CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcValue::resolve [&](Length const& length) -> CalculatedStyleValue::CalculationResult { return CalculatedStyleValue::CalculationResult { length }; }, + [&](Percentage const& percentage) -> CalculatedStyleValue::CalculationResult { + return CalculatedStyleValue::CalculationResult { percentage }; + }, [&](NonnullOwnPtr const& sum) -> CalculatedStyleValue::CalculationResult { return sum->resolve(layout_node, percentage_basis); }); diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValue.h index 5c753ac4f2..be0c3bee44 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.h @@ -707,7 +707,7 @@ public: }; struct CalcValue { - Variant> value; + Variant> value; Optional resolved_type() const; CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const; };