mirror of
https://github.com/RGBCube/serenity
synced 2025-05-21 20:25:07 +00:00
LibWeb: Implement CSS log()
This commit is contained in:
parent
fa37bb8b76
commit
6dde49404a
4 changed files with 130 additions and 0 deletions
|
@ -3891,6 +3891,55 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_hypot_function(Function const& fu
|
||||||
return TRY(HypotCalculationNode::create(move(calculated_parameters)));
|
return TRY(HypotCalculationNode::create(move(calculated_parameters)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<OwnPtr<CalculationNode>> Parser::parse_log_function(Function const& function)
|
||||||
|
{
|
||||||
|
TokenStream stream { function.values() };
|
||||||
|
auto parameters = parse_a_comma_separated_list_of_component_values(stream);
|
||||||
|
|
||||||
|
OwnPtr<CalculationNode> node_b;
|
||||||
|
if (parameters.size() == 1) {
|
||||||
|
node_b = TRY(ConstantCalculationNode::create(CalculationNode::ConstantType::E));
|
||||||
|
} else if (parameters.size() == 2) {
|
||||||
|
node_b = TRY(parse_a_calculation(parameters[1]));
|
||||||
|
} else {
|
||||||
|
dbgln_if(CSS_PARSER_DEBUG, "log() must have exactly one or two parameters"sv);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto node_a = TRY(parse_a_calculation(parameters[0]));
|
||||||
|
|
||||||
|
if (!node_a || !node_b) {
|
||||||
|
dbgln_if(CSS_PARSER_DEBUG, "log() 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 log() 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 log() 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, "log() parameters must be of the same type"sv);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node_a_resolved_type != CalculatedStyleValue::ResolvedType::Number) {
|
||||||
|
dbgln_if(CSS_PARSER_DEBUG, "log() parameters must be numbers"sv);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return TRY(LogCalculationNode::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()) {
|
||||||
|
@ -3965,6 +4014,9 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_a_calc_function_node(Function con
|
||||||
if (function.name().equals_ignoring_ascii_case("hypot"sv))
|
if (function.name().equals_ignoring_ascii_case("hypot"sv))
|
||||||
return TRY(parse_hypot_function(function));
|
return TRY(parse_hypot_function(function));
|
||||||
|
|
||||||
|
if (function.name().equals_ignoring_ascii_case("log"sv))
|
||||||
|
return TRY(parse_log_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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -305,6 +305,7 @@ private:
|
||||||
ErrorOr<OwnPtr<CalculationNode>> parse_pow_function(Function const&);
|
ErrorOr<OwnPtr<CalculationNode>> parse_pow_function(Function const&);
|
||||||
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<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>&);
|
||||||
|
|
|
@ -1420,6 +1420,64 @@ ErrorOr<void> HypotCalculationNode::dump(StringBuilder& builder, int indent) con
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<NonnullOwnPtr<LogCalculationNode>> LogCalculationNode::create(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
|
||||||
|
{
|
||||||
|
return adopt_nonnull_own_or_enomem(new (nothrow) LogCalculationNode(move(x), move(y)));
|
||||||
|
}
|
||||||
|
|
||||||
|
LogCalculationNode::LogCalculationNode(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
|
||||||
|
: CalculationNode(Type::Log)
|
||||||
|
, m_x(move(x))
|
||||||
|
, m_y(move(y))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
LogCalculationNode::~LogCalculationNode() = default;
|
||||||
|
|
||||||
|
ErrorOr<String> LogCalculationNode::to_string() const
|
||||||
|
{
|
||||||
|
StringBuilder builder;
|
||||||
|
builder.append("log("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> LogCalculationNode::resolved_type() const
|
||||||
|
{
|
||||||
|
return CalculatedStyleValue::ResolvedType::Number;
|
||||||
|
}
|
||||||
|
|
||||||
|
CalculatedStyleValue::CalculationResult LogCalculationNode::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 = log2(node_a_value) / log2(node_b_value);
|
||||||
|
|
||||||
|
return { Number(Number::Type::Number, result) };
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorOr<void> LogCalculationNode::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> LogCalculationNode::dump(StringBuilder& builder, int indent) const
|
||||||
|
{
|
||||||
|
TRY(builder.try_appendff("{: >{}}LOG: {}\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);
|
||||||
|
|
|
@ -586,4 +586,23 @@ private:
|
||||||
Vector<NonnullOwnPtr<CalculationNode>> m_values;
|
Vector<NonnullOwnPtr<CalculationNode>> m_values;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class LogCalculationNode final : public CalculationNode {
|
||||||
|
public:
|
||||||
|
static ErrorOr<NonnullOwnPtr<LogCalculationNode>> create(NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>);
|
||||||
|
~LogCalculationNode();
|
||||||
|
|
||||||
|
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:
|
||||||
|
LogCalculationNode(NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>);
|
||||||
|
NonnullOwnPtr<CalculationNode> m_x;
|
||||||
|
NonnullOwnPtr<CalculationNode> m_y;
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue