mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-24 18:32:28 -05:00
LibWeb: Implement CSS cos()
This commit is contained in:
parent
c73f476915
commit
46a5efe388
4 changed files with 100 additions and 0 deletions
|
@ -3618,6 +3618,30 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_sin_function(Function const& func
|
|||
return TRY(SinCalculationNode::create(calculation_node.release_nonnull()));
|
||||
}
|
||||
|
||||
ErrorOr<OwnPtr<CalculationNode>> Parser::parse_cos_function(Function const& function)
|
||||
{
|
||||
auto calculation_node = TRY(parse_a_calculation(function.values()));
|
||||
|
||||
if (!calculation_node) {
|
||||
dbgln_if(CSS_PARSER_DEBUG, "cos() 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 cos() parameter"sv);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto resolved_type = maybe_parameter_type.value();
|
||||
if (resolved_type != CalculatedStyleValue::ResolvedType::Number && resolved_type != CalculatedStyleValue::ResolvedType::Angle) {
|
||||
dbgln_if(CSS_PARSER_DEBUG, "cos() parameter must be a number or angle"sv);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return TRY(CosCalculationNode::create(calculation_node.release_nonnull()));
|
||||
}
|
||||
|
||||
ErrorOr<RefPtr<StyleValue>> Parser::parse_dynamic_value(ComponentValue const& component_value)
|
||||
{
|
||||
if (component_value.is_function()) {
|
||||
|
@ -3665,6 +3689,9 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_a_calc_function_node(Function con
|
|||
if (function.name().equals_ignoring_ascii_case("sin"sv))
|
||||
return TRY(parse_sin_function(function));
|
||||
|
||||
if (function.name().equals_ignoring_ascii_case("cos"sv))
|
||||
return TRY(parse_cos_function(function));
|
||||
|
||||
dbgln_if(CSS_PARSER_DEBUG, "We didn't implement `{}` function yet", function.name());
|
||||
return nullptr;
|
||||
}
|
||||
|
|
|
@ -296,6 +296,7 @@ private:
|
|||
ErrorOr<OwnPtr<CalculationNode>> parse_abs_function(Function const&);
|
||||
ErrorOr<OwnPtr<CalculationNode>> parse_sign_function(Function const&);
|
||||
ErrorOr<OwnPtr<CalculationNode>> parse_sin_function(Function const&);
|
||||
ErrorOr<OwnPtr<CalculationNode>> parse_cos_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>&);
|
||||
|
|
|
@ -904,6 +904,60 @@ ErrorOr<void> SinCalculationNode::dump(StringBuilder& builder, int indent) const
|
|||
return {};
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<CosCalculationNode>> CosCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
|
||||
{
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) CosCalculationNode(move(value)));
|
||||
}
|
||||
|
||||
CosCalculationNode::CosCalculationNode(NonnullOwnPtr<CalculationNode> value)
|
||||
: CalculationNode(Type::Cos)
|
||||
, m_value(move(value))
|
||||
{
|
||||
}
|
||||
|
||||
CosCalculationNode::~CosCalculationNode() = default;
|
||||
|
||||
ErrorOr<String> CosCalculationNode::to_string() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
builder.append("cos("sv);
|
||||
builder.append(TRY(m_value->to_string()));
|
||||
builder.append(")"sv);
|
||||
return builder.to_string();
|
||||
}
|
||||
|
||||
Optional<CalculatedStyleValue::ResolvedType> CosCalculationNode::resolved_type() const
|
||||
{
|
||||
return CalculatedStyleValue::ResolvedType::Number;
|
||||
}
|
||||
|
||||
bool CosCalculationNode::contains_percentage() const
|
||||
{
|
||||
return m_value->contains_percentage();
|
||||
}
|
||||
|
||||
CalculatedStyleValue::CalculationResult CosCalculationNode::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_radians(node_a.value());
|
||||
auto result = cos(node_a_value);
|
||||
|
||||
return { Number(Number::Type::Number, result) };
|
||||
}
|
||||
|
||||
ErrorOr<void> CosCalculationNode::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> CosCalculationNode::dump(StringBuilder& builder, int indent) const
|
||||
{
|
||||
TRY(builder.try_appendff("{: >{}}COS: {}\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);
|
||||
|
|
|
@ -414,4 +414,22 @@ private:
|
|||
NonnullOwnPtr<CalculationNode> m_value;
|
||||
};
|
||||
|
||||
class CosCalculationNode final : public CalculationNode {
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<CosCalculationNode>> create(NonnullOwnPtr<CalculationNode>);
|
||||
~CosCalculationNode();
|
||||
|
||||
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:
|
||||
CosCalculationNode(NonnullOwnPtr<CalculationNode>);
|
||||
NonnullOwnPtr<CalculationNode> m_value;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue