Skip to content

Commit

Permalink
WIP: Set the numeric type of calculation nodes in their constructors
Browse files Browse the repository at this point in the history
The numeric type can't change after they're constructed, so we can avoid
recalculating it over and over, and also neatly avoid needing to know
what type percentages should be resolved to.

Currently this doesn't work because our CSSNumericType "matches_foo()"
methods are out of date with some fixes to the spec.
  • Loading branch information
AtkinsSJ committed Dec 12, 2024
1 parent 5da90b3 commit 39b3e95
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 367 deletions.
12 changes: 7 additions & 5 deletions Libraries/LibWeb/CSS/Parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1925,7 +1925,7 @@ RefPtr<CalculatedStyleValue> Parser::parse_calculated_value(ComponentValue const
if (!function_node)
return nullptr;

auto function_type = function_node->determine_type(m_context.current_property_id());
auto function_type = function_node->numeric_type();
if (!function_type.has_value())
return nullptr;

Expand All @@ -1937,7 +1937,7 @@ OwnPtr<CalculationNode> Parser::parse_a_calc_function_node(Function const& funct
if (function.name.equals_ignoring_ascii_case("calc"sv))
return parse_a_calculation(function.value);

if (auto maybe_function = parse_math_function(m_context.current_property_id(), function))
if (auto maybe_function = parse_math_function(function))
return maybe_function;

return nullptr;
Expand Down Expand Up @@ -8550,7 +8550,6 @@ class UnparsedCalculationNode final : public CalculationNode {
ComponentValue& component_value() { return m_component_value; }

virtual String to_string() const override { VERIFY_NOT_REACHED(); }
virtual Optional<CSSNumericType> determine_type(Web::CSS::PropertyID) const override { VERIFY_NOT_REACHED(); }
virtual bool contains_percentage() const override { VERIFY_NOT_REACHED(); }
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override { VERIFY_NOT_REACHED(); }
virtual void for_each_child_node(AK::Function<void(NonnullOwnPtr<CalculationNode>&)> const&) override { }
Expand All @@ -8563,7 +8562,7 @@ class UnparsedCalculationNode final : public CalculationNode {

private:
UnparsedCalculationNode(ComponentValue component_value)
: CalculationNode(Type::Unparsed)
: CalculationNode(Type::Unparsed, OptionalNone {})
, m_component_value(move(component_value))
{
}
Expand Down Expand Up @@ -8676,6 +8675,9 @@ LengthOrCalculated Parser::Parser::parse_as_sizes_attribute(DOM::Element const&
// https://www.w3.org/TR/css-values-4/#parse-a-calculation
OwnPtr<CalculationNode> Parser::parse_a_calculation(Vector<ComponentValue> const& original_values)
{
// FIXME: Figure this out in non-property contexts.
auto percentage_resolved_type = property_resolves_percentages_relative_to(m_context.current_property_id());

// 1. Discard any <whitespace-token>s from values.
// 2. An item in values is an “operator” if it’s a <delim-token> with the value "+", "-", "*", or "/". Otherwise, it’s a “value”.
struct Operator {
Expand Down Expand Up @@ -8718,7 +8720,7 @@ OwnPtr<CalculationNode> Parser::parse_a_calculation(Vector<ComponentValue> const
else if (dimension->is_length())
values.append({ NumericCalculationNode::create(dimension->length()) });
else if (dimension->is_percentage())
values.append({ NumericCalculationNode::create(dimension->percentage()) });
values.append({ NumericCalculationNode::create(dimension->percentage(), percentage_resolved_type) });
else if (dimension->is_resolution())
values.append({ NumericCalculationNode::create(dimension->resolution()) });
else if (dimension->is_time())
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibWeb/CSS/Parser/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ class Parser {
RefPtr<CalculatedStyleValue> parse_calculated_value(ComponentValue const&);
RefPtr<CustomIdentStyleValue> parse_custom_ident_value(TokenStream<ComponentValue>&, std::initializer_list<StringView> blacklist);
// NOTE: Implemented in generated code. (GenerateCSSMathFunctions.cpp)
OwnPtr<CalculationNode> parse_math_function(PropertyID, Function const&);
OwnPtr<CalculationNode> parse_math_function(Function const&);
OwnPtr<CalculationNode> parse_a_calc_function_node(Function const&);
RefPtr<CSSStyleValue> parse_keyword_value(TokenStream<ComponentValue>&);
RefPtr<CSSStyleValue> parse_hue_none_value(TokenStream<ComponentValue>&);
Expand Down
Loading

0 comments on commit 39b3e95

Please sign in to comment.