mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:07:34 +00:00
LibWeb: Implement CSS pow()
This commit is contained in:
parent
8ef25989b6
commit
9aed8ec7f0
4 changed files with 136 additions and 2 deletions
|
@ -3758,13 +3758,13 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_atan2_function(Function const& fu
|
||||||
|
|
||||||
auto node_a_maybe_parameter_type = node_a->resolved_type();
|
auto node_a_maybe_parameter_type = node_a->resolved_type();
|
||||||
if (!node_a_maybe_parameter_type.has_value()) {
|
if (!node_a_maybe_parameter_type.has_value()) {
|
||||||
dbgln_if(CSS_PARSER_DEBUG, "Failed to resolve type for atan() parameter 1"sv);
|
dbgln_if(CSS_PARSER_DEBUG, "Failed to resolve type for atan2() parameter 1"sv);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto node_b_maybe_parameter_type = node_b->resolved_type();
|
auto node_b_maybe_parameter_type = node_b->resolved_type();
|
||||||
if (!node_b_maybe_parameter_type.has_value()) {
|
if (!node_b_maybe_parameter_type.has_value()) {
|
||||||
dbgln_if(CSS_PARSER_DEBUG, "Failed to resolve type for atan() parameter 2"sv);
|
dbgln_if(CSS_PARSER_DEBUG, "Failed to resolve type for atan2() parameter 2"sv);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3778,6 +3778,51 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_atan2_function(Function const& fu
|
||||||
return TRY(Atan2CalculationNode::create(node_a.release_nonnull(), node_b.release_nonnull()));
|
return TRY(Atan2CalculationNode::create(node_a.release_nonnull(), node_b.release_nonnull()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<OwnPtr<CalculationNode>> Parser::parse_pow_function(Function const& function)
|
||||||
|
{
|
||||||
|
TokenStream stream { function.values() };
|
||||||
|
auto parameters = parse_a_comma_separated_list_of_component_values(stream);
|
||||||
|
|
||||||
|
if (parameters.size() != 2) {
|
||||||
|
dbgln_if(CSS_PARSER_DEBUG, "pow() must have exactly two parameters"sv);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto node_a = TRY(parse_a_calculation(parameters[0]));
|
||||||
|
auto node_b = TRY(parse_a_calculation(parameters[1]));
|
||||||
|
|
||||||
|
if (!node_a || !node_b) {
|
||||||
|
dbgln_if(CSS_PARSER_DEBUG, "pow() parameters must be valid calculations"sv);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto node_a_maybe_parameter_type = node_a->resolved_type();
|
||||||
|
if (!node_a_maybe_parameter_type.has_value()) {
|
||||||
|
dbgln_if(CSS_PARSER_DEBUG, "Failed to resolve type for pow() parameter 1"sv);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto node_b_maybe_parameter_type = node_b->resolved_type();
|
||||||
|
if (!node_b_maybe_parameter_type.has_value()) {
|
||||||
|
dbgln_if(CSS_PARSER_DEBUG, "Failed to resolve type for pow() parameter 2"sv);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto node_a_resolved_type = node_a_maybe_parameter_type.value();
|
||||||
|
auto node_b_resolved_type = node_b_maybe_parameter_type.value();
|
||||||
|
if (node_a_resolved_type != node_b_resolved_type) {
|
||||||
|
dbgln_if(CSS_PARSER_DEBUG, "pow() parameters must be of the same type"sv);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node_a_resolved_type != CalculatedStyleValue::ResolvedType::Number) {
|
||||||
|
dbgln_if(CSS_PARSER_DEBUG, "pow() parameters must be numbers"sv);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return TRY(PowCalculationNode::create(node_a.release_nonnull(), node_b.release_nonnull()));
|
||||||
|
}
|
||||||
|
|
||||||
ErrorOr<RefPtr<StyleValue>> Parser::parse_dynamic_value(ComponentValue const& component_value)
|
ErrorOr<RefPtr<StyleValue>> Parser::parse_dynamic_value(ComponentValue const& component_value)
|
||||||
{
|
{
|
||||||
if (component_value.is_function()) {
|
if (component_value.is_function()) {
|
||||||
|
@ -3843,6 +3888,9 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_a_calc_function_node(Function con
|
||||||
if (function.name().equals_ignoring_ascii_case("atan2"sv))
|
if (function.name().equals_ignoring_ascii_case("atan2"sv))
|
||||||
return TRY(parse_atan2_function(function));
|
return TRY(parse_atan2_function(function));
|
||||||
|
|
||||||
|
if (function.name().equals_ignoring_ascii_case("pow"sv))
|
||||||
|
return TRY(parse_pow_function(function));
|
||||||
|
|
||||||
dbgln_if(CSS_PARSER_DEBUG, "We didn't implement `{}` function yet", function.name());
|
dbgln_if(CSS_PARSER_DEBUG, "We didn't implement `{}` function yet", function.name());
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
|
@ -302,6 +302,7 @@ private:
|
||||||
ErrorOr<OwnPtr<CalculationNode>> parse_acos_function(Function const&);
|
ErrorOr<OwnPtr<CalculationNode>> parse_acos_function(Function const&);
|
||||||
ErrorOr<OwnPtr<CalculationNode>> parse_atan_function(Function const&);
|
ErrorOr<OwnPtr<CalculationNode>> parse_atan_function(Function const&);
|
||||||
ErrorOr<OwnPtr<CalculationNode>> parse_atan2_function(Function const&);
|
ErrorOr<OwnPtr<CalculationNode>> parse_atan2_function(Function const&);
|
||||||
|
ErrorOr<OwnPtr<CalculationNode>> parse_pow_function(Function const&);
|
||||||
ErrorOr<RefPtr<StyleValue>> parse_dimension_value(ComponentValue const&);
|
ErrorOr<RefPtr<StyleValue>> parse_dimension_value(ComponentValue const&);
|
||||||
ErrorOr<RefPtr<StyleValue>> parse_integer_value(TokenStream<ComponentValue>&);
|
ErrorOr<RefPtr<StyleValue>> parse_integer_value(TokenStream<ComponentValue>&);
|
||||||
ErrorOr<RefPtr<StyleValue>> parse_number_value(TokenStream<ComponentValue>&);
|
ErrorOr<RefPtr<StyleValue>> parse_number_value(TokenStream<ComponentValue>&);
|
||||||
|
|
|
@ -1237,6 +1237,64 @@ ErrorOr<void> Atan2CalculationNode::dump(StringBuilder& builder, int indent) con
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<NonnullOwnPtr<PowCalculationNode>> PowCalculationNode::create(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
|
||||||
|
{
|
||||||
|
return adopt_nonnull_own_or_enomem(new (nothrow) PowCalculationNode(move(x), move(y)));
|
||||||
|
}
|
||||||
|
|
||||||
|
PowCalculationNode::PowCalculationNode(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
|
||||||
|
: CalculationNode(Type::Pow)
|
||||||
|
, m_x(move(x))
|
||||||
|
, m_y(move(y))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
PowCalculationNode::~PowCalculationNode() = default;
|
||||||
|
|
||||||
|
ErrorOr<String> PowCalculationNode::to_string() const
|
||||||
|
{
|
||||||
|
StringBuilder builder;
|
||||||
|
builder.append("pow("sv);
|
||||||
|
builder.append(TRY(m_x->to_string()));
|
||||||
|
builder.append(", "sv);
|
||||||
|
builder.append(TRY(m_y->to_string()));
|
||||||
|
builder.append(")"sv);
|
||||||
|
return builder.to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<CalculatedStyleValue::ResolvedType> PowCalculationNode::resolved_type() const
|
||||||
|
{
|
||||||
|
return CalculatedStyleValue::ResolvedType::Number;
|
||||||
|
}
|
||||||
|
|
||||||
|
CalculatedStyleValue::CalculationResult PowCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
|
||||||
|
{
|
||||||
|
auto node_a = m_x->resolve(context, percentage_basis);
|
||||||
|
auto node_a_value = resolve_value(node_a.value(), context);
|
||||||
|
|
||||||
|
auto node_b = m_y->resolve(context, percentage_basis);
|
||||||
|
auto node_b_value = resolve_value(node_b.value(), context);
|
||||||
|
|
||||||
|
auto result = pow(node_a_value, node_b_value);
|
||||||
|
|
||||||
|
return { Number(Number::Type::Number, result) };
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorOr<void> PowCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
|
||||||
|
{
|
||||||
|
TRY(m_x->for_each_child_node(callback));
|
||||||
|
TRY(m_y->for_each_child_node(callback));
|
||||||
|
TRY(callback(m_x));
|
||||||
|
TRY(callback(m_y));
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorOr<void> PowCalculationNode::dump(StringBuilder& builder, int indent) const
|
||||||
|
{
|
||||||
|
TRY(builder.try_appendff("{: >{}}POW: {}\n", "", indent, TRY(to_string())));
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Optional<Length::ResolutionContext const&> context, PercentageBasis const& percentage_basis)
|
void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Optional<Length::ResolutionContext const&> context, PercentageBasis const& percentage_basis)
|
||||||
{
|
{
|
||||||
add_or_subtract_internal(SumOperation::Add, other, context, percentage_basis);
|
add_or_subtract_internal(SumOperation::Add, other, context, percentage_basis);
|
||||||
|
|
|
@ -162,6 +162,14 @@ public:
|
||||||
Atan,
|
Atan,
|
||||||
Atan2,
|
Atan2,
|
||||||
|
|
||||||
|
// Exponential functions, a sub-type of operator node
|
||||||
|
// https://drafts.csswg.org/css-values-4/#exponent-funcs
|
||||||
|
Pow,
|
||||||
|
Sqrt,
|
||||||
|
Hypot,
|
||||||
|
Log,
|
||||||
|
Exp,
|
||||||
|
|
||||||
// This only exists during parsing.
|
// This only exists during parsing.
|
||||||
Unparsed,
|
Unparsed,
|
||||||
};
|
};
|
||||||
|
@ -523,4 +531,23 @@ private:
|
||||||
NonnullOwnPtr<CalculationNode> m_x;
|
NonnullOwnPtr<CalculationNode> m_x;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class PowCalculationNode final : public CalculationNode {
|
||||||
|
public:
|
||||||
|
static ErrorOr<NonnullOwnPtr<PowCalculationNode>> create(NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>);
|
||||||
|
~PowCalculationNode();
|
||||||
|
|
||||||
|
virtual ErrorOr<String> to_string() const override;
|
||||||
|
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||||
|
virtual bool contains_percentage() const override { return false; };
|
||||||
|
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||||
|
virtual ErrorOr<void> for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const&) override;
|
||||||
|
|
||||||
|
virtual ErrorOr<void> dump(StringBuilder&, int indent) const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
explicit PowCalculationNode(NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>);
|
||||||
|
NonnullOwnPtr<CalculationNode> m_x;
|
||||||
|
NonnullOwnPtr<CalculationNode> m_y;
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue