mirror of
https://github.com/RGBCube/serenity
synced 2025-07-22 22:27:39 +00:00
LibWeb: Implement CSS atan()
This commit is contained in:
parent
784e1cfb72
commit
1aa84dfddd
4 changed files with 100 additions and 0 deletions
|
@ -3714,6 +3714,30 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_acos_function(Function const& fun
|
||||||
return TRY(AcosCalculationNode::create(calculation_node.release_nonnull()));
|
return TRY(AcosCalculationNode::create(calculation_node.release_nonnull()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<OwnPtr<CalculationNode>> Parser::parse_atan_function(Function const& function)
|
||||||
|
{
|
||||||
|
auto calculation_node = TRY(parse_a_calculation(function.values()));
|
||||||
|
|
||||||
|
if (!calculation_node) {
|
||||||
|
dbgln_if(CSS_PARSER_DEBUG, "atan() parameter must be a valid calculation"sv);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto maybe_parameter_type = calculation_node->resolved_type();
|
||||||
|
if (!maybe_parameter_type.has_value()) {
|
||||||
|
dbgln_if(CSS_PARSER_DEBUG, "Failed to resolve type for atan() parameter"sv);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto resolved_type = maybe_parameter_type.value();
|
||||||
|
if (resolved_type != CalculatedStyleValue::ResolvedType::Number) {
|
||||||
|
dbgln_if(CSS_PARSER_DEBUG, "atan() parameter must be a number"sv);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return TRY(AtanCalculationNode::create(calculation_node.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()) {
|
||||||
|
@ -3773,6 +3797,9 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_a_calc_function_node(Function con
|
||||||
if (function.name().equals_ignoring_ascii_case("acos"sv))
|
if (function.name().equals_ignoring_ascii_case("acos"sv))
|
||||||
return TRY(parse_acos_function(function));
|
return TRY(parse_acos_function(function));
|
||||||
|
|
||||||
|
if (function.name().equals_ignoring_ascii_case("atan"sv))
|
||||||
|
return TRY(parse_atan_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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -300,6 +300,7 @@ private:
|
||||||
ErrorOr<OwnPtr<CalculationNode>> parse_tan_function(Function const&);
|
ErrorOr<OwnPtr<CalculationNode>> parse_tan_function(Function const&);
|
||||||
ErrorOr<OwnPtr<CalculationNode>> parse_asin_function(Function const&);
|
ErrorOr<OwnPtr<CalculationNode>> parse_asin_function(Function const&);
|
||||||
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<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>&);
|
||||||
|
|
|
@ -1120,6 +1120,60 @@ ErrorOr<void> AcosCalculationNode::dump(StringBuilder& builder, int indent) cons
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<NonnullOwnPtr<AtanCalculationNode>> AtanCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
|
||||||
|
{
|
||||||
|
return adopt_nonnull_own_or_enomem(new (nothrow) AtanCalculationNode(move(value)));
|
||||||
|
}
|
||||||
|
|
||||||
|
AtanCalculationNode::AtanCalculationNode(NonnullOwnPtr<CalculationNode> value)
|
||||||
|
: CalculationNode(Type::Atan)
|
||||||
|
, m_value(move(value))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
AtanCalculationNode::~AtanCalculationNode() = default;
|
||||||
|
|
||||||
|
ErrorOr<String> AtanCalculationNode::to_string() const
|
||||||
|
{
|
||||||
|
StringBuilder builder;
|
||||||
|
builder.append("atan("sv);
|
||||||
|
builder.append(TRY(m_value->to_string()));
|
||||||
|
builder.append(")"sv);
|
||||||
|
return builder.to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<CalculatedStyleValue::ResolvedType> AtanCalculationNode::resolved_type() const
|
||||||
|
{
|
||||||
|
return CalculatedStyleValue::ResolvedType::Angle;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AtanCalculationNode::contains_percentage() const
|
||||||
|
{
|
||||||
|
return m_value->contains_percentage();
|
||||||
|
}
|
||||||
|
|
||||||
|
CalculatedStyleValue::CalculationResult AtanCalculationNode::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 = atan(node_a_value);
|
||||||
|
|
||||||
|
return { Angle(result, Angle::Type::Rad) };
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorOr<void> AtanCalculationNode::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> AtanCalculationNode::dump(StringBuilder& builder, int indent) const
|
||||||
|
{
|
||||||
|
TRY(builder.try_appendff("{: >{}}ATAN: {}\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);
|
||||||
|
|
|
@ -486,4 +486,22 @@ private:
|
||||||
NonnullOwnPtr<CalculationNode> m_value;
|
NonnullOwnPtr<CalculationNode> m_value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class AtanCalculationNode final : public CalculationNode {
|
||||||
|
public:
|
||||||
|
static ErrorOr<NonnullOwnPtr<AtanCalculationNode>> create(NonnullOwnPtr<CalculationNode>);
|
||||||
|
~AtanCalculationNode();
|
||||||
|
|
||||||
|
virtual ErrorOr<String> to_string() const override;
|
||||||
|
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||||
|
virtual bool contains_percentage() const override;
|
||||||
|
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:
|
||||||
|
AtanCalculationNode(NonnullOwnPtr<CalculationNode>);
|
||||||
|
NonnullOwnPtr<CalculationNode> m_value;
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue