1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 04:37:34 +00:00

LibWeb: Implement CSS sqrt()

This commit is contained in:
stelar7 2023-05-28 11:31:50 +02:00 committed by Sam Atkins
parent 9aed8ec7f0
commit 0d30fb5a6e
4 changed files with 92 additions and 0 deletions

View file

@ -1295,6 +1295,55 @@ ErrorOr<void> PowCalculationNode::dump(StringBuilder& builder, int indent) const
return {};
}
ErrorOr<NonnullOwnPtr<SqrtCalculationNode>> SqrtCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
{
return adopt_nonnull_own_or_enomem(new (nothrow) SqrtCalculationNode(move(value)));
}
SqrtCalculationNode::SqrtCalculationNode(NonnullOwnPtr<CalculationNode> value)
: CalculationNode(Type::Sqrt)
, m_value(move(value))
{
}
SqrtCalculationNode::~SqrtCalculationNode() = default;
ErrorOr<String> SqrtCalculationNode::to_string() const
{
StringBuilder builder;
builder.append("sqrt("sv);
builder.append(TRY(m_value->to_string()));
builder.append(")"sv);
return builder.to_string();
}
Optional<CalculatedStyleValue::ResolvedType> SqrtCalculationNode::resolved_type() const
{
return CalculatedStyleValue::ResolvedType::Number;
}
CalculatedStyleValue::CalculationResult SqrtCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
{
auto node_a = m_value->resolve(context, percentage_basis);
auto node_a_value = resolve_value(node_a.value(), context);
auto result = sqrt(node_a_value);
return { Number(Number::Type::Number, result) };
}
ErrorOr<void> SqrtCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
{
TRY(m_value->for_each_child_node(callback));
TRY(callback(m_value));
return {};
}
ErrorOr<void> SqrtCalculationNode::dump(StringBuilder& builder, int indent) const
{
TRY(builder.try_appendff("{: >{}}SQRT: {}\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);