mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 23:27:43 +00:00
LibWeb: Implement CSS pow()
This commit is contained in:
parent
8ef25989b6
commit
9aed8ec7f0
4 changed files with 136 additions and 2 deletions
|
@ -1237,6 +1237,64 @@ ErrorOr<void> Atan2CalculationNode::dump(StringBuilder& builder, int indent) con
|
|||
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)
|
||||
{
|
||||
add_or_subtract_internal(SumOperation::Add, other, context, percentage_basis);
|
||||
|
|
|
@ -162,6 +162,14 @@ public:
|
|||
Atan,
|
||||
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.
|
||||
Unparsed,
|
||||
};
|
||||
|
@ -523,4 +531,23 @@ private:
|
|||
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