mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-22 09:12:13 -05:00
LibWeb/CSS: Use CSSNumericType for CalculationResult's numeric type
When we originally implemented calc(), the result of a calculation was guaranteed to be a single CSS type like a Length or Angle. However, CSS Values 4 now allows more complex type arithmetic, which is represented by the CSSNumericType class. Using that directly makes us more correct, and allows us to remove a large amount of now ad-hoc code. Unfortunately this is a large commit but the changes it makes are interconnected enough that doing one at a time causes test regressions. In no particular order: - Update our "determine the type of a calculation" code to match the newest spec, which sets percent hints in a couple more cases. (One of these we're skipping for now, I think it fails because of the FIXMEs in CSSNumericType::matches_foo().) - Make the generated math-function-parsing code aware of the difference between arguments being the same type, and being "consistent" types, for each function. Otherwise those extra percent hints would cause them to fail validation incorrectly. - Use the CSSNumericType as the type for the CalculationResult. - Calculate and assign each math function's type in its constructor, instead of calculating it repeatedly on-demand. The `CalculationNode::resolved_type()` method is now entirely unused and has been removed.
This commit is contained in:
parent
8d40550478
commit
eb11c35640
Notes:
github-actions[bot]
2024-12-21 17:15:19 +00:00
Author: https://github.com/AtkinsSJ Commit: https://github.com/LadybirdBrowser/ladybird/commit/eb11c356400 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2966
7 changed files with 446 additions and 1168 deletions
|
@ -149,7 +149,13 @@ This is a single JSON object, describing each [CSS math function](https://www.w3
|
|||
with the keys being the function name and the values being objects describing that function's properties.
|
||||
This generates `MathFunctions.h` and `MathFunctions.cpp`.
|
||||
|
||||
Each entry currently has a single property, `parameters`, which is an array of parameter definition objects.
|
||||
Each entry has two properties:
|
||||
|
||||
| Field | Description |
|
||||
|------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| `parameter-validation` | Optional string. Either "same" or "consistent", depending on whether the spec says the input calculations should be the same type or consistent types. Defaults to "same". Ignore this if there is only one parameter. |
|
||||
| `parameters` | An array of parameter definition objects, see below. |
|
||||
|
||||
Parameter definitions have the following properties:
|
||||
|
||||
| Field | Description |
|
||||
|
|
|
@ -36,6 +36,7 @@
|
|||
]
|
||||
},
|
||||
"atan2": {
|
||||
"parameter-validation": "consistent",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "y",
|
||||
|
@ -50,6 +51,7 @@
|
|||
]
|
||||
},
|
||||
"clamp": {
|
||||
"parameter-validation": "consistent",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "min",
|
||||
|
@ -87,6 +89,7 @@
|
|||
]
|
||||
},
|
||||
"hypot": {
|
||||
"parameter-validation": "consistent",
|
||||
"is-variadic": true,
|
||||
"parameters": [
|
||||
{
|
||||
|
@ -97,6 +100,7 @@
|
|||
]
|
||||
},
|
||||
"log": {
|
||||
"parameter-validation": "same",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "value",
|
||||
|
@ -112,6 +116,7 @@
|
|||
]
|
||||
},
|
||||
"max": {
|
||||
"parameter-validation": "consistent",
|
||||
"is-variadic": true,
|
||||
"parameters": [
|
||||
{
|
||||
|
@ -122,6 +127,7 @@
|
|||
]
|
||||
},
|
||||
"min": {
|
||||
"parameter-validation": "consistent",
|
||||
"is-variadic": true,
|
||||
"parameters": [
|
||||
{
|
||||
|
@ -132,6 +138,7 @@
|
|||
]
|
||||
},
|
||||
"mod": {
|
||||
"parameter-validation": "same",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "value",
|
||||
|
@ -146,6 +153,7 @@
|
|||
]
|
||||
},
|
||||
"pow": {
|
||||
"parameter-validation": "consistent",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "value",
|
||||
|
@ -160,6 +168,7 @@
|
|||
]
|
||||
},
|
||||
"rem": {
|
||||
"parameter-validation": "same",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "value",
|
||||
|
@ -174,6 +183,7 @@
|
|||
]
|
||||
},
|
||||
"round": {
|
||||
"parameter-validation": "consistent",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "strategy",
|
||||
|
|
|
@ -1924,7 +1924,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;
|
||||
|
||||
|
@ -1936,7 +1936,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;
|
||||
|
@ -9158,15 +9158,18 @@ OwnPtr<CalculationNode> Parser::convert_to_calculation_node(CalcParsing::Node co
|
|||
[](Number const& number) -> OwnPtr<CalculationNode> {
|
||||
return NumericCalculationNode::create(number);
|
||||
},
|
||||
[](Dimension const& dimension) -> OwnPtr<CalculationNode> {
|
||||
[this](Dimension const& dimension) -> OwnPtr<CalculationNode> {
|
||||
if (dimension.is_angle())
|
||||
return NumericCalculationNode::create(dimension.angle());
|
||||
if (dimension.is_frequency())
|
||||
return NumericCalculationNode::create(dimension.frequency());
|
||||
if (dimension.is_length())
|
||||
return NumericCalculationNode::create(dimension.length());
|
||||
if (dimension.is_percentage())
|
||||
return NumericCalculationNode::create(dimension.percentage());
|
||||
if (dimension.is_percentage()) {
|
||||
// FIXME: Figure this out in non-property contexts
|
||||
auto percentage_resolved_type = property_resolves_percentages_relative_to(m_context.current_property_id());
|
||||
return NumericCalculationNode::create(dimension.percentage(), percentage_resolved_type);
|
||||
}
|
||||
if (dimension.is_resolution())
|
||||
return NumericCalculationNode::create(dimension.resolution());
|
||||
if (dimension.is_time())
|
||||
|
|
|
@ -276,7 +276,7 @@ private:
|
|||
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>&);
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
||||
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
|
||||
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
|
||||
* Copyright (c) 2021-2024, Sam Atkins <sam@ladybird.org>
|
||||
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
|
@ -26,32 +26,19 @@ class CalculationNode;
|
|||
|
||||
class CalculatedStyleValue : public CSSStyleValue {
|
||||
public:
|
||||
enum class ResolvedType {
|
||||
Angle,
|
||||
Flex,
|
||||
Frequency,
|
||||
Integer,
|
||||
Length,
|
||||
Number,
|
||||
Percentage,
|
||||
Resolution,
|
||||
Time,
|
||||
};
|
||||
|
||||
enum class SumOperation {
|
||||
Add,
|
||||
Subtract,
|
||||
};
|
||||
|
||||
using PercentageBasis = Variant<Empty, Angle, Flex, Frequency, Length, Time>;
|
||||
|
||||
class CalculationResult {
|
||||
public:
|
||||
using Value = Variant<Number, Angle, Flex, Frequency, Length, Percentage, Resolution, Time>;
|
||||
CalculationResult(Value value)
|
||||
: m_value(move(value))
|
||||
static CalculationResult from_value(Value const&, Optional<Length::ResolutionContext const&>, Optional<CSSNumericType>);
|
||||
|
||||
CalculationResult(double value, Optional<CSSNumericType> type)
|
||||
: m_value(value)
|
||||
, m_type(move(type))
|
||||
{
|
||||
}
|
||||
|
||||
void add(CalculationResult const& other, Optional<Length::ResolutionContext const&>, PercentageBasis const& percentage_basis);
|
||||
void subtract(CalculationResult const& other, Optional<Length::ResolutionContext const&>, PercentageBasis const& percentage_basis);
|
||||
void multiply_by(CalculationResult const& other, Optional<Length::ResolutionContext const&>);
|
||||
|
@ -59,15 +46,14 @@ public:
|
|||
void negate();
|
||||
void invert();
|
||||
|
||||
Value const& value() const { return m_value; }
|
||||
|
||||
ResolvedType resolved_type() const;
|
||||
double value() const { return m_value; }
|
||||
Optional<CSSNumericType> const& type() const { return m_type; }
|
||||
|
||||
[[nodiscard]] bool operator==(CalculationResult const&) const = default;
|
||||
|
||||
private:
|
||||
void add_or_subtract_internal(SumOperation op, CalculationResult const& other, Optional<Length::ResolutionContext const&>, PercentageBasis const& percentage_basis);
|
||||
Value m_value;
|
||||
double m_value;
|
||||
Optional<CSSNumericType> m_type;
|
||||
};
|
||||
|
||||
static ValueComparingNonnullRefPtr<CalculatedStyleValue> create(NonnullOwnPtr<CalculationNode> calculation, CSSNumericType resolved_type)
|
||||
|
@ -256,37 +242,35 @@ public:
|
|||
}
|
||||
|
||||
virtual String to_string() const = 0;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const = 0;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const = 0;
|
||||
Optional<CSSNumericType> const& numeric_type() const { return m_numeric_type; }
|
||||
virtual bool contains_percentage() const = 0;
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const = 0;
|
||||
|
||||
|
||||
virtual void dump(StringBuilder&, int indent) const = 0;
|
||||
virtual bool equals(CalculationNode const&) const = 0;
|
||||
|
||||
protected:
|
||||
explicit CalculationNode(Type);
|
||||
CalculationNode(Type, Optional<CSSNumericType>);
|
||||
|
||||
private:
|
||||
Type m_type;
|
||||
Optional<CSSNumericType> m_numeric_type;
|
||||
};
|
||||
|
||||
class NumericCalculationNode final : public CalculationNode {
|
||||
public:
|
||||
static NonnullOwnPtr<NumericCalculationNode> create(NumericValue);
|
||||
static NonnullOwnPtr<NumericCalculationNode> create(NumericValue, Optional<ValueType> percentage_resolved_type = {});
|
||||
~NumericCalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override;
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
||||
virtual void dump(StringBuilder&, int indent) const override;
|
||||
virtual bool equals(CalculationNode const&) const override;
|
||||
|
||||
private:
|
||||
explicit NumericCalculationNode(NumericValue);
|
||||
NumericCalculationNode(NumericValue, CSSNumericType);
|
||||
NumericValue m_value;
|
||||
};
|
||||
|
||||
|
@ -296,16 +280,14 @@ public:
|
|||
~SumCalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override;
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
||||
virtual void dump(StringBuilder&, int indent) const override;
|
||||
virtual bool equals(CalculationNode const&) const override;
|
||||
|
||||
private:
|
||||
explicit SumCalculationNode(Vector<NonnullOwnPtr<CalculationNode>>);
|
||||
SumCalculationNode(Vector<NonnullOwnPtr<CalculationNode>>, Optional<CSSNumericType>);
|
||||
Vector<NonnullOwnPtr<CalculationNode>> m_values;
|
||||
};
|
||||
|
||||
|
@ -315,16 +297,14 @@ public:
|
|||
~ProductCalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override;
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
||||
virtual void dump(StringBuilder&, int indent) const override;
|
||||
virtual bool equals(CalculationNode const&) const override;
|
||||
|
||||
private:
|
||||
explicit ProductCalculationNode(Vector<NonnullOwnPtr<CalculationNode>>);
|
||||
ProductCalculationNode(Vector<NonnullOwnPtr<CalculationNode>>, Optional<CSSNumericType>);
|
||||
Vector<NonnullOwnPtr<CalculationNode>> m_values;
|
||||
};
|
||||
|
||||
|
@ -334,16 +314,14 @@ public:
|
|||
~NegateCalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override;
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
||||
virtual void dump(StringBuilder&, int indent) const override;
|
||||
virtual bool equals(CalculationNode const&) const override;
|
||||
|
||||
private:
|
||||
explicit NegateCalculationNode(NonnullOwnPtr<CalculationNode>);
|
||||
NegateCalculationNode(NonnullOwnPtr<CalculationNode>);
|
||||
NonnullOwnPtr<CalculationNode> m_value;
|
||||
};
|
||||
|
||||
|
@ -353,16 +331,14 @@ public:
|
|||
~InvertCalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override;
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
||||
virtual void dump(StringBuilder&, int indent) const override;
|
||||
virtual bool equals(CalculationNode const&) const override;
|
||||
|
||||
private:
|
||||
explicit InvertCalculationNode(NonnullOwnPtr<CalculationNode>);
|
||||
InvertCalculationNode(NonnullOwnPtr<CalculationNode>, Optional<CSSNumericType>);
|
||||
NonnullOwnPtr<CalculationNode> m_value;
|
||||
};
|
||||
|
||||
|
@ -372,8 +348,6 @@ public:
|
|||
~MinCalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override;
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
@ -381,7 +355,7 @@ public:
|
|||
virtual bool equals(CalculationNode const&) const override;
|
||||
|
||||
private:
|
||||
explicit MinCalculationNode(Vector<NonnullOwnPtr<CalculationNode>>);
|
||||
MinCalculationNode(Vector<NonnullOwnPtr<CalculationNode>>, Optional<CSSNumericType>);
|
||||
Vector<NonnullOwnPtr<CalculationNode>> m_values;
|
||||
};
|
||||
|
||||
|
@ -391,16 +365,14 @@ public:
|
|||
~MaxCalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override;
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
||||
virtual void dump(StringBuilder&, int indent) const override;
|
||||
virtual bool equals(CalculationNode const&) const override;
|
||||
|
||||
private:
|
||||
explicit MaxCalculationNode(Vector<NonnullOwnPtr<CalculationNode>>);
|
||||
MaxCalculationNode(Vector<NonnullOwnPtr<CalculationNode>>, Optional<CSSNumericType>);
|
||||
Vector<NonnullOwnPtr<CalculationNode>> m_values;
|
||||
};
|
||||
|
||||
|
@ -410,8 +382,6 @@ public:
|
|||
~ClampCalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override;
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
@ -419,7 +389,7 @@ public:
|
|||
virtual bool equals(CalculationNode const&) const override;
|
||||
|
||||
private:
|
||||
explicit ClampCalculationNode(NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>);
|
||||
ClampCalculationNode(NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>, Optional<CSSNumericType>);
|
||||
NonnullOwnPtr<CalculationNode> m_min_value;
|
||||
NonnullOwnPtr<CalculationNode> m_center_value;
|
||||
NonnullOwnPtr<CalculationNode> m_max_value;
|
||||
|
@ -431,8 +401,6 @@ public:
|
|||
~AbsCalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override;
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
@ -450,8 +418,6 @@ public:
|
|||
~SignCalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override;
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
@ -469,8 +435,6 @@ public:
|
|||
~ConstantCalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override { return false; }
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
@ -488,8 +452,6 @@ public:
|
|||
~SinCalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override;
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
@ -507,8 +469,6 @@ public:
|
|||
~CosCalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override;
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
@ -526,8 +486,6 @@ public:
|
|||
~TanCalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override;
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
@ -545,8 +503,6 @@ public:
|
|||
~AsinCalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override;
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
@ -564,8 +520,6 @@ public:
|
|||
~AcosCalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override;
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
@ -583,8 +537,6 @@ public:
|
|||
~AtanCalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override;
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
@ -602,8 +554,6 @@ public:
|
|||
~Atan2CalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override;
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
@ -622,8 +572,6 @@ public:
|
|||
~PowCalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override { return false; }
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
@ -631,7 +579,7 @@ public:
|
|||
virtual bool equals(CalculationNode const&) const override;
|
||||
|
||||
private:
|
||||
explicit PowCalculationNode(NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>);
|
||||
PowCalculationNode(NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>);
|
||||
NonnullOwnPtr<CalculationNode> m_x;
|
||||
NonnullOwnPtr<CalculationNode> m_y;
|
||||
};
|
||||
|
@ -642,8 +590,6 @@ public:
|
|||
~SqrtCalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override { return false; }
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
@ -661,8 +607,6 @@ public:
|
|||
~HypotCalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override;
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
@ -670,7 +614,7 @@ public:
|
|||
virtual bool equals(CalculationNode const&) const override;
|
||||
|
||||
private:
|
||||
explicit HypotCalculationNode(Vector<NonnullOwnPtr<CalculationNode>>);
|
||||
HypotCalculationNode(Vector<NonnullOwnPtr<CalculationNode>>, Optional<CSSNumericType>);
|
||||
Vector<NonnullOwnPtr<CalculationNode>> m_values;
|
||||
};
|
||||
|
||||
|
@ -680,8 +624,6 @@ public:
|
|||
~LogCalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override { return false; }
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
@ -700,8 +642,6 @@ public:
|
|||
~ExpCalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override { return false; }
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
@ -719,8 +659,6 @@ public:
|
|||
~RoundCalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override;
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
@ -728,7 +666,7 @@ public:
|
|||
virtual bool equals(CalculationNode const&) const override;
|
||||
|
||||
private:
|
||||
RoundCalculationNode(RoundingStrategy, NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>);
|
||||
RoundCalculationNode(RoundingStrategy, NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>, Optional<CSSNumericType>);
|
||||
RoundingStrategy m_strategy;
|
||||
NonnullOwnPtr<CalculationNode> m_x;
|
||||
NonnullOwnPtr<CalculationNode> m_y;
|
||||
|
@ -740,8 +678,6 @@ public:
|
|||
~ModCalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override;
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
@ -749,7 +685,7 @@ public:
|
|||
virtual bool equals(CalculationNode const&) const override;
|
||||
|
||||
private:
|
||||
ModCalculationNode(NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>);
|
||||
ModCalculationNode(NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>, Optional<CSSNumericType>);
|
||||
NonnullOwnPtr<CalculationNode> m_x;
|
||||
NonnullOwnPtr<CalculationNode> m_y;
|
||||
};
|
||||
|
@ -760,8 +696,6 @@ public:
|
|||
~RemCalculationNode();
|
||||
|
||||
virtual String to_string() const override;
|
||||
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||
virtual Optional<CSSNumericType> determine_type(PropertyID) const override;
|
||||
virtual bool contains_percentage() const override;
|
||||
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||
|
||||
|
@ -769,7 +703,7 @@ public:
|
|||
virtual bool equals(CalculationNode const&) const override;
|
||||
|
||||
private:
|
||||
RemCalculationNode(NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>);
|
||||
RemCalculationNode(NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>, Optional<CSSNumericType>);
|
||||
NonnullOwnPtr<CalculationNode> m_x;
|
||||
NonnullOwnPtr<CalculationNode> m_y;
|
||||
};
|
||||
|
|
|
@ -116,7 +116,7 @@ static Optional<RoundingStrategy> parse_rounding_strategy(Vector<ComponentValue>
|
|||
return keyword_to_rounding_strategy(maybe_keyword.value());
|
||||
}
|
||||
|
||||
OwnPtr<CalculationNode> Parser::parse_math_function(PropertyID property_id, Function const& function)
|
||||
OwnPtr<CalculationNode> Parser::parse_math_function(Function const& function)
|
||||
{
|
||||
TokenStream stream { function.value };
|
||||
auto arguments = parse_a_comma_separated_list_of_component_values(stream);
|
||||
|
@ -125,6 +125,8 @@ OwnPtr<CalculationNode> Parser::parse_math_function(PropertyID property_id, Func
|
|||
functions_data.for_each_member([&](auto& name, JsonValue const& value) -> void {
|
||||
auto& function_data = value.as_object();
|
||||
auto& parameters = function_data.get_array("parameters"sv).value();
|
||||
auto parameter_validation_rule = function_data.get_byte_string("parameter-validation"sv);
|
||||
bool requires_same_parameters = parameter_validation_rule.has_value() ? (parameter_validation_rule == "same"sv) : true;
|
||||
|
||||
auto function_generator = generator.fork();
|
||||
function_generator.set("name:lowercase", name);
|
||||
|
@ -133,7 +135,7 @@ OwnPtr<CalculationNode> Parser::parse_math_function(PropertyID property_id, Func
|
|||
if (function_data.get_bool("is-variadic"sv).value_or(false)) {
|
||||
// Variadic function
|
||||
function_generator.append(R"~~~(
|
||||
CSSNumericType determined_argument_type;
|
||||
Optional<CSSNumericType> determined_argument_type;
|
||||
Vector<NonnullOwnPtr<CalculationNode>> parsed_arguments;
|
||||
parsed_arguments.ensure_capacity(arguments.size());
|
||||
|
||||
|
@ -144,7 +146,7 @@ OwnPtr<CalculationNode> Parser::parse_math_function(PropertyID property_id, Func
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
auto maybe_argument_type = calculation_node->determine_type(m_context.current_property_id());
|
||||
auto maybe_argument_type = calculation_node->numeric_type();
|
||||
if (!maybe_argument_type.has_value()) {
|
||||
dbgln_if(CSS_PARSER_DEBUG, "@name:lowercase@() argument #{} couldn't determine its type", parsed_arguments.size());
|
||||
return nullptr;
|
||||
|
@ -163,13 +165,28 @@ OwnPtr<CalculationNode> Parser::parse_math_function(PropertyID property_id, Func
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
if (parsed_arguments.is_empty()) {
|
||||
if (!determined_argument_type.has_value()) {
|
||||
determined_argument_type = move(argument_type);
|
||||
} else {
|
||||
)~~~");
|
||||
if (requires_same_parameters) {
|
||||
function_generator.append(R"~~~(
|
||||
if (determined_argument_type != argument_type) {
|
||||
dbgln_if(CSS_PARSER_DEBUG, "@name:lowercase@() argument #{} type ({}) doesn't match type of previous arguments ({})", parsed_arguments.size(), argument_type.dump(), determined_argument_type.dump());
|
||||
dbgln_if(CSS_PARSER_DEBUG, "@name:lowercase@() argument #{} type ({}) doesn't match type of previous arguments ({})", parsed_arguments.size(), argument_type.dump(), determined_argument_type->dump());
|
||||
return nullptr;
|
||||
}
|
||||
)~~~");
|
||||
} else {
|
||||
function_generator.append(R"~~~(
|
||||
if (auto consistent_type = determined_argument_type->consistent_type(argument_type); consistent_type.has_value()) {
|
||||
determined_argument_type = consistent_type.release_value();
|
||||
} else {
|
||||
dbgln_if(CSS_PARSER_DEBUG, "@name:lowercase@() argument #{} type ({}) is not consistent with type of previous arguments ({})", parsed_arguments.size(), argument_type.dump(), determined_argument_type->dump());
|
||||
return nullptr;
|
||||
}
|
||||
)~~~");
|
||||
}
|
||||
function_generator.append(R"~~~(
|
||||
}
|
||||
|
||||
parsed_arguments.append(calculation_node.release_nonnull());
|
||||
|
@ -197,11 +214,10 @@ OwnPtr<CalculationNode> Parser::parse_math_function(PropertyID property_id, Func
|
|||
return nullptr;
|
||||
}
|
||||
size_t argument_index = 0;
|
||||
[[maybe_unused]] CSSNumericType previous_argument_type;
|
||||
Optional<CSSNumericType> determined_argument_type;
|
||||
)~~~");
|
||||
|
||||
size_t parameter_index = 0;
|
||||
StringView previous_parameter_type_string;
|
||||
parameters.for_each([&](JsonValue const& parameter_value) {
|
||||
auto& parameter = parameter_value.as_object();
|
||||
auto parameter_type_string = parameter.get_byte_string("type"sv).value();
|
||||
|
@ -279,7 +295,7 @@ OwnPtr<CalculationNode> Parser::parse_math_function(PropertyID property_id, Func
|
|||
auto parameter_type_variable = MUST(String::formatted("argument_type_{}", parameter_index));
|
||||
parameter_generator.set("type_check", generate_calculation_type_check(parameter_type_variable, parameter_type_string));
|
||||
parameter_generator.append(R"~~~(
|
||||
auto maybe_argument_type_@parameter_index@ = parameter_@parameter_index@->determine_type(property_id);
|
||||
auto maybe_argument_type_@parameter_index@ = parameter_@parameter_index@->numeric_type();
|
||||
if (!maybe_argument_type_@parameter_index@.has_value()) {
|
||||
dbgln_if(CSS_PARSER_DEBUG, "@name:lowercase@() argument '@parameter_name@' couldn't determine its type");
|
||||
return nullptr;
|
||||
|
@ -290,25 +306,35 @@ OwnPtr<CalculationNode> Parser::parse_math_function(PropertyID property_id, Func
|
|||
dbgln_if(CSS_PARSER_DEBUG, "@name:lowercase@() argument '@parameter_name@' type ({}) is not an accepted type", argument_type_@parameter_index@.dump());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!determined_argument_type.has_value()) {
|
||||
determined_argument_type = argument_type_@parameter_index@;
|
||||
} else {
|
||||
)~~~");
|
||||
|
||||
// NOTE: In all current cases, the parameters that take the same types must resolve to the same CSSNumericType.
|
||||
// This is a bit of a hack, but serves our needs for now.
|
||||
if (previous_parameter_type_string == parameter_type_string) {
|
||||
if (requires_same_parameters) {
|
||||
parameter_generator.append(R"~~~(
|
||||
if (argument_type_@parameter_index@ != previous_argument_type) {
|
||||
dbgln_if(CSS_PARSER_DEBUG, "@name:lowercase@() argument '@parameter_name@' type ({}) doesn't match type of previous arguments ({})", argument_type_@parameter_index@.dump(), previous_argument_type.dump());
|
||||
return nullptr;
|
||||
}
|
||||
if (determined_argument_type != argument_type_@parameter_index@) {
|
||||
dbgln_if(CSS_PARSER_DEBUG, "@name:lowercase@() argument '@parameter_name@' type ({}) doesn't match type of previous arguments ({})", argument_type_@parameter_index@.dump(), determined_argument_type->dump());
|
||||
return nullptr;
|
||||
}
|
||||
)~~~");
|
||||
} else {
|
||||
parameter_generator.append(R"~~~(
|
||||
if (auto consistent_type = determined_argument_type->consistent_type(argument_type_@parameter_index@); consistent_type.has_value()) {
|
||||
determined_argument_type = consistent_type.release_value();
|
||||
} else {
|
||||
dbgln_if(CSS_PARSER_DEBUG, "@name:lowercase@() argument '@parameter_name@' type ({}) is not consistent with type of previous arguments ({})", argument_type_@parameter_index@.dump(), determined_argument_type->dump());
|
||||
return nullptr;
|
||||
}
|
||||
)~~~");
|
||||
}
|
||||
parameter_generator.append(R"~~~(
|
||||
previous_argument_type = argument_type_@parameter_index@;
|
||||
}
|
||||
)~~~");
|
||||
}
|
||||
|
||||
parameter_index++;
|
||||
previous_parameter_type_string = parameter_type_string;
|
||||
});
|
||||
|
||||
// Generate the call to the constructor
|
||||
|
|
Loading…
Reference in a new issue