mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-24 02:12:09 -05:00
LibWeb: Implement CSS sign()
This commit is contained in:
parent
79fc4c8a82
commit
e1e382152c
4 changed files with 93 additions and 0 deletions
|
@ -3582,6 +3582,18 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_abs_function(Function const& func
|
|||
return TRY(AbsCalculationNode::create(calculation_node.release_nonnull()));
|
||||
}
|
||||
|
||||
ErrorOr<OwnPtr<CalculationNode>> Parser::parse_sign_function(Function const& function)
|
||||
{
|
||||
auto calculation_node = TRY(parse_a_calculation(function.values()));
|
||||
|
||||
if (!calculation_node) {
|
||||
dbgln_if(CSS_PARSER_DEBUG, "sign() parameter must be a valid calculation"sv);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return TRY(SignCalculationNode::create(calculation_node.release_nonnull()));
|
||||
}
|
||||
|
||||
ErrorOr<RefPtr<StyleValue>> Parser::parse_dynamic_value(ComponentValue const& component_value)
|
||||
{
|
||||
if (component_value.is_function()) {
|
||||
|
@ -3623,6 +3635,9 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_a_calc_function_node(Function con
|
|||
if (function.name().equals_ignoring_ascii_case("abs"sv))
|
||||
return TRY(parse_abs_function(function));
|
||||
|
||||
if (function.name().equals_ignoring_ascii_case("sign"sv))
|
||||
return TRY(parse_sign_function(function));
|
||||
|
||||
dbgln_if(CSS_PARSER_DEBUG, "We didn't implement `{}` function yet", function.name());
|
||||
return nullptr;
|
||||
}
|
||||
|
|
|
@ -294,6 +294,7 @@ private:
|
|||
ErrorOr<OwnPtr<CalculationNode>> parse_max_function(Function const&);
|
||||
ErrorOr<OwnPtr<CalculationNode>> parse_clamp_function(Function const&);
|
||||
ErrorOr<OwnPtr<CalculationNode>> parse_abs_function(Function const&);
|
||||
ErrorOr<OwnPtr<CalculationNode>> parse_sign_function(Function const&);
|
||||
ErrorOr<RefPtr<StyleValue>> parse_dimension_value(ComponentValue const&);
|
||||
ErrorOr<RefPtr<StyleValue>> parse_integer_value(TokenStream<ComponentValue>&);
|
||||
ErrorOr<RefPtr<StyleValue>> parse_number_value(TokenStream<ComponentValue>&);
|
||||
|
|
|
@ -718,6 +718,65 @@ ErrorOr<void> AbsCalculationNode::dump(StringBuilder& builder, int indent) const
|
|||
return {};
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<SignCalculationNode>> SignCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
|
||||
{
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) SignCalculationNode(move(value)));
|
||||
}
|
||||
|
||||
SignCalculationNode::SignCalculationNode(NonnullOwnPtr<CalculationNode> value)
|
||||
: CalculationNode(Type::Sign)
|
||||
, m_value(move(value))
|
||||
{
|
||||
}
|
||||
|
||||
SignCalculationNode::~SignCalculationNode() = default;
|
||||
|
||||
ErrorOr<String> SignCalculationNode::to_string() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
builder.append("sign("sv);
|
||||
builder.append(TRY(m_value->to_string()));
|
||||
builder.append(")"sv);
|
||||
return builder.to_string();
|
||||
}
|
||||
|
||||
Optional<CalculatedStyleValue::ResolvedType> SignCalculationNode::resolved_type() const
|
||||
{
|
||||
return CalculatedStyleValue::ResolvedType::Integer;
|
||||
}
|
||||
|
||||
bool SignCalculationNode::contains_percentage() const
|
||||
{
|
||||
return m_value->contains_percentage();
|
||||
}
|
||||
|
||||
CalculatedStyleValue::CalculationResult SignCalculationNode::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);
|
||||
|
||||
if (node_a_value < 0)
|
||||
return { Number(Number::Type::Integer, -1) };
|
||||
|
||||
if (node_a_value > 0)
|
||||
return { Number(Number::Type::Integer, 1) };
|
||||
|
||||
return { Number(Number::Type::Integer, 0) };
|
||||
}
|
||||
|
||||
ErrorOr<void> SignCalculationNode::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> SignCalculationNode::dump(StringBuilder& builder, int indent) const
|
||||
{
|
||||
TRY(builder.try_appendff("{: >{}}SIGN: {}\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);
|
||||
|
|
|
@ -336,4 +336,22 @@ private:
|
|||
NonnullOwnPtr<CalculationNode> m_value;
|
||||
};
|
||||
|
||||
class SignCalculationNode final : public CalculationNode {
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<SignCalculationNode>> create(NonnullOwnPtr<CalculationNode>);
|
||||
~SignCalculationNode();
|
||||
|
||||
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:
|
||||
SignCalculationNode(NonnullOwnPtr<CalculationNode>);
|
||||
NonnullOwnPtr<CalculationNode> m_value;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue