mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:27:43 +00:00
LibWeb: Implement CSS exp()
This commit is contained in:
parent
6dde49404a
commit
5727e276ea
4 changed files with 94 additions and 0 deletions
|
@ -3940,6 +3940,29 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_log_function(Function const& func
|
||||||
return TRY(LogCalculationNode::create(node_a.release_nonnull(), node_b.release_nonnull()));
|
return TRY(LogCalculationNode::create(node_a.release_nonnull(), node_b.release_nonnull()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<OwnPtr<CalculationNode>> Parser::parse_exp_function(Function const& function)
|
||||||
|
{
|
||||||
|
auto node_a = TRY(parse_a_calculation(function.values()));
|
||||||
|
if (!node_a) {
|
||||||
|
dbgln_if(CSS_PARSER_DEBUG, "exp() parameter must be valid calculation"sv);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto maybe_parameter_type = node_a->resolved_type();
|
||||||
|
if (!maybe_parameter_type.has_value()) {
|
||||||
|
dbgln_if(CSS_PARSER_DEBUG, "Failed to resolve type for exp() parameter"sv);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto resolved_type = maybe_parameter_type.value();
|
||||||
|
if (resolved_type != CalculatedStyleValue::ResolvedType::Number) {
|
||||||
|
dbgln_if(CSS_PARSER_DEBUG, "exp() parameter must be number"sv);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return TRY(ExpCalculationNode::create(node_a.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()) {
|
||||||
|
@ -4017,6 +4040,9 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_a_calc_function_node(Function con
|
||||||
if (function.name().equals_ignoring_ascii_case("log"sv))
|
if (function.name().equals_ignoring_ascii_case("log"sv))
|
||||||
return TRY(parse_log_function(function));
|
return TRY(parse_log_function(function));
|
||||||
|
|
||||||
|
if (function.name().equals_ignoring_ascii_case("exp"sv))
|
||||||
|
return TRY(parse_exp_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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -306,6 +306,7 @@ private:
|
||||||
ErrorOr<OwnPtr<CalculationNode>> parse_sqrt_function(Function const&);
|
ErrorOr<OwnPtr<CalculationNode>> parse_sqrt_function(Function const&);
|
||||||
ErrorOr<OwnPtr<CalculationNode>> parse_hypot_function(Function const&);
|
ErrorOr<OwnPtr<CalculationNode>> parse_hypot_function(Function const&);
|
||||||
ErrorOr<OwnPtr<CalculationNode>> parse_log_function(Function const&);
|
ErrorOr<OwnPtr<CalculationNode>> parse_log_function(Function const&);
|
||||||
|
ErrorOr<OwnPtr<CalculationNode>> parse_exp_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>&);
|
||||||
|
|
|
@ -1478,6 +1478,55 @@ ErrorOr<void> LogCalculationNode::dump(StringBuilder& builder, int indent) const
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<NonnullOwnPtr<ExpCalculationNode>> ExpCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
|
||||||
|
{
|
||||||
|
return adopt_nonnull_own_or_enomem(new (nothrow) ExpCalculationNode(move(value)));
|
||||||
|
}
|
||||||
|
|
||||||
|
ExpCalculationNode::ExpCalculationNode(NonnullOwnPtr<CalculationNode> value)
|
||||||
|
: CalculationNode(Type::Exp)
|
||||||
|
, m_value(move(value))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
ExpCalculationNode::~ExpCalculationNode() = default;
|
||||||
|
|
||||||
|
ErrorOr<String> ExpCalculationNode::to_string() const
|
||||||
|
{
|
||||||
|
StringBuilder builder;
|
||||||
|
builder.append("exp("sv);
|
||||||
|
builder.append(TRY(m_value->to_string()));
|
||||||
|
builder.append(")"sv);
|
||||||
|
return builder.to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<CalculatedStyleValue::ResolvedType> ExpCalculationNode::resolved_type() const
|
||||||
|
{
|
||||||
|
return CalculatedStyleValue::ResolvedType::Number;
|
||||||
|
}
|
||||||
|
|
||||||
|
CalculatedStyleValue::CalculationResult ExpCalculationNode::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 = exp(node_a_value);
|
||||||
|
|
||||||
|
return { Number(Number::Type::Number, result) };
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorOr<void> ExpCalculationNode::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> ExpCalculationNode::dump(StringBuilder& builder, int indent) const
|
||||||
|
{
|
||||||
|
TRY(builder.try_appendff("{: >{}}EXP: {}\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);
|
||||||
|
|
|
@ -605,4 +605,22 @@ private:
|
||||||
NonnullOwnPtr<CalculationNode> m_y;
|
NonnullOwnPtr<CalculationNode> m_y;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class ExpCalculationNode final : public CalculationNode {
|
||||||
|
public:
|
||||||
|
static ErrorOr<NonnullOwnPtr<ExpCalculationNode>> create(NonnullOwnPtr<CalculationNode>);
|
||||||
|
~ExpCalculationNode();
|
||||||
|
|
||||||
|
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:
|
||||||
|
ExpCalculationNode(NonnullOwnPtr<CalculationNode>);
|
||||||
|
NonnullOwnPtr<CalculationNode> m_value;
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue