mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:47:44 +00:00
LibWeb: Implement CSS mod()
This commit is contained in:
parent
b2230c826b
commit
dc042662d1
4 changed files with 128 additions and 0 deletions
|
@ -4031,6 +4031,46 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_round_function(Function const& fu
|
||||||
return TRY(RoundCalculationNode::create(mode, node_a.release_nonnull(), node_b.release_nonnull()));
|
return TRY(RoundCalculationNode::create(mode, node_a.release_nonnull(), node_b.release_nonnull()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<OwnPtr<CalculationNode>> Parser::parse_mod_function(Function const& function)
|
||||||
|
{
|
||||||
|
TokenStream stream { function.values() };
|
||||||
|
auto parameters = parse_a_comma_separated_list_of_component_values(stream);
|
||||||
|
|
||||||
|
if (parameters.size() != 2) {
|
||||||
|
dbgln_if(CSS_PARSER_DEBUG, "mod() must have exactly two parameters"sv);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto node_a = TRY(parse_a_calculation(parameters[0]));
|
||||||
|
auto node_b = TRY(parse_a_calculation(parameters[1]));
|
||||||
|
|
||||||
|
if (!node_a || !node_b) {
|
||||||
|
dbgln_if(CSS_PARSER_DEBUG, "mod() 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 mod() 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 mod() 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, "mod() parameters must all be of the same type"sv);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return TRY(ModCalculationNode::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()) {
|
||||||
|
@ -4114,6 +4154,9 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_a_calc_function_node(Function con
|
||||||
if (function.name().equals_ignoring_ascii_case("round"sv))
|
if (function.name().equals_ignoring_ascii_case("round"sv))
|
||||||
return TRY(parse_round_function(function));
|
return TRY(parse_round_function(function));
|
||||||
|
|
||||||
|
if (function.name().equals_ignoring_ascii_case("mod"sv))
|
||||||
|
return TRY(parse_mod_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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -308,6 +308,7 @@ private:
|
||||||
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<OwnPtr<CalculationNode>> parse_exp_function(Function const&);
|
||||||
ErrorOr<OwnPtr<CalculationNode>> parse_round_function(Function const&);
|
ErrorOr<OwnPtr<CalculationNode>> parse_round_function(Function const&);
|
||||||
|
ErrorOr<OwnPtr<CalculationNode>> parse_mod_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>&);
|
||||||
|
|
|
@ -1631,6 +1631,71 @@ ErrorOr<void> RoundCalculationNode::dump(StringBuilder& builder, int indent) con
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<NonnullOwnPtr<ModCalculationNode>> ModCalculationNode::create(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
|
||||||
|
{
|
||||||
|
return adopt_nonnull_own_or_enomem(new (nothrow) ModCalculationNode(move(x), move(y)));
|
||||||
|
}
|
||||||
|
|
||||||
|
ModCalculationNode::ModCalculationNode(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
|
||||||
|
: CalculationNode(Type::Mod)
|
||||||
|
, m_x(move(x))
|
||||||
|
, m_y(move(y))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
ModCalculationNode::~ModCalculationNode() = default;
|
||||||
|
|
||||||
|
ErrorOr<String> ModCalculationNode::to_string() const
|
||||||
|
{
|
||||||
|
StringBuilder builder;
|
||||||
|
builder.append("mod("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> ModCalculationNode::resolved_type() const
|
||||||
|
{
|
||||||
|
// Note: We check during parsing that all values have the same type
|
||||||
|
return m_x->resolved_type();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ModCalculationNode::contains_percentage() const
|
||||||
|
{
|
||||||
|
return m_x->contains_percentage() || m_y->contains_percentage();
|
||||||
|
}
|
||||||
|
|
||||||
|
CalculatedStyleValue::CalculationResult ModCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
|
||||||
|
{
|
||||||
|
auto resolved_type = m_x->resolved_type().value();
|
||||||
|
auto node_a = m_x->resolve(context, percentage_basis);
|
||||||
|
auto node_b = m_y->resolve(context, percentage_basis);
|
||||||
|
|
||||||
|
auto node_a_value = resolve_value(node_a.value(), context);
|
||||||
|
auto node_b_value = resolve_value(node_b.value(), context);
|
||||||
|
|
||||||
|
auto quotient = floor(node_a_value / node_b_value);
|
||||||
|
auto value = node_a_value - (node_b_value * quotient);
|
||||||
|
return to_resolved_type(resolved_type, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorOr<void> ModCalculationNode::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> ModCalculationNode::dump(StringBuilder& builder, int indent) const
|
||||||
|
{
|
||||||
|
TRY(builder.try_appendff("{: >{}}MOD: {}\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);
|
||||||
|
|
|
@ -657,4 +657,23 @@ private:
|
||||||
NonnullOwnPtr<CalculationNode> m_y;
|
NonnullOwnPtr<CalculationNode> m_y;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class ModCalculationNode final : public CalculationNode {
|
||||||
|
public:
|
||||||
|
static ErrorOr<NonnullOwnPtr<ModCalculationNode>> create(NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>);
|
||||||
|
~ModCalculationNode();
|
||||||
|
|
||||||
|
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:
|
||||||
|
ModCalculationNode(NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>);
|
||||||
|
NonnullOwnPtr<CalculationNode> m_x;
|
||||||
|
NonnullOwnPtr<CalculationNode> m_y;
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue