mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 19:57:41 +00:00
LibWeb: Parse clamp()
css math function
This commit is contained in:
parent
eaee0ecd88
commit
2ef6aa5f3d
4 changed files with 145 additions and 0 deletions
|
@ -3512,6 +3512,45 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_max_function(Function const& func
|
||||||
return TRY(MaxCalculationNode::create(move(calculated_parameters)));
|
return TRY(MaxCalculationNode::create(move(calculated_parameters)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<OwnPtr<CalculationNode>> Parser::parse_clamp_function(Function const& function)
|
||||||
|
{
|
||||||
|
TokenStream stream { function.values() };
|
||||||
|
auto parameters = parse_a_comma_separated_list_of_component_values(stream);
|
||||||
|
|
||||||
|
if (parameters.size() != 3) {
|
||||||
|
dbgln_if(CSS_PARSER_DEBUG, "clamp() must have exactly three parameters"sv);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector<NonnullOwnPtr<CalculationNode>> calculated_parameters;
|
||||||
|
calculated_parameters.ensure_capacity(parameters.size());
|
||||||
|
|
||||||
|
CalculatedStyleValue::ResolvedType type;
|
||||||
|
bool first = true;
|
||||||
|
for (auto& parameter : parameters) {
|
||||||
|
auto calculation_node = TRY(parse_a_calculation(parameter));
|
||||||
|
|
||||||
|
if (!calculation_node) {
|
||||||
|
dbgln_if(CSS_PARSER_DEBUG, "clamp() parameters must be valid calculations"sv);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (first) {
|
||||||
|
type = calculation_node->resolved_type().value();
|
||||||
|
first = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (calculation_node->resolved_type().value() != type) {
|
||||||
|
dbgln_if(CSS_PARSER_DEBUG, "clamp() parameters must all be of same type"sv);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
calculated_parameters.append(calculation_node.release_nonnull());
|
||||||
|
}
|
||||||
|
|
||||||
|
return TRY(ClampCalculationNode::create(move(calculated_parameters[0]), move(calculated_parameters[1]), move(calculated_parameters[2])));
|
||||||
|
}
|
||||||
|
|
||||||
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()) {
|
||||||
|
@ -3544,6 +3583,9 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_a_calc_function_node(Function con
|
||||||
if (function.name().equals_ignoring_ascii_case("max"sv))
|
if (function.name().equals_ignoring_ascii_case("max"sv))
|
||||||
return TRY(parse_max_function(function));
|
return TRY(parse_max_function(function));
|
||||||
|
|
||||||
|
if (function.name().equals_ignoring_ascii_case("clamp"sv))
|
||||||
|
return TRY(parse_clamp_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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -292,6 +292,7 @@ private:
|
||||||
ErrorOr<OwnPtr<CalculationNode>> parse_a_calc_function_node(Function const&);
|
ErrorOr<OwnPtr<CalculationNode>> parse_a_calc_function_node(Function const&);
|
||||||
ErrorOr<OwnPtr<CalculationNode>> parse_min_function(Function const&);
|
ErrorOr<OwnPtr<CalculationNode>> parse_min_function(Function const&);
|
||||||
ErrorOr<OwnPtr<CalculationNode>> parse_max_function(Function const&);
|
ErrorOr<OwnPtr<CalculationNode>> parse_max_function(Function const&);
|
||||||
|
ErrorOr<OwnPtr<CalculationNode>> parse_clamp_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>&);
|
||||||
|
|
|
@ -557,6 +557,88 @@ ErrorOr<void> MaxCalculationNode::dump(StringBuilder& builder, int indent) const
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<NonnullOwnPtr<ClampCalculationNode>> ClampCalculationNode::create(NonnullOwnPtr<CalculationNode> min, NonnullOwnPtr<CalculationNode> center, NonnullOwnPtr<CalculationNode> max)
|
||||||
|
{
|
||||||
|
return adopt_nonnull_own_or_enomem(new (nothrow) ClampCalculationNode(move(min), move(center), move(max)));
|
||||||
|
}
|
||||||
|
|
||||||
|
ClampCalculationNode::ClampCalculationNode(NonnullOwnPtr<CalculationNode> min, NonnullOwnPtr<CalculationNode> center, NonnullOwnPtr<CalculationNode> max)
|
||||||
|
: CalculationNode(Type::Clamp)
|
||||||
|
, m_min_value(move(min))
|
||||||
|
, m_center_value(move(center))
|
||||||
|
, m_max_value(move(max))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
ClampCalculationNode::~ClampCalculationNode() = default;
|
||||||
|
|
||||||
|
ErrorOr<String> ClampCalculationNode::to_string() const
|
||||||
|
{
|
||||||
|
StringBuilder builder;
|
||||||
|
TRY(builder.try_append("clamp("sv));
|
||||||
|
TRY(builder.try_append(TRY(m_min_value->to_string())));
|
||||||
|
TRY(builder.try_append(", "sv));
|
||||||
|
TRY(builder.try_append(TRY(m_center_value->to_string())));
|
||||||
|
TRY(builder.try_append(", "sv));
|
||||||
|
TRY(builder.try_append(TRY(m_max_value->to_string())));
|
||||||
|
TRY(builder.try_append(")"sv));
|
||||||
|
return builder.to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<CalculatedStyleValue::ResolvedType> ClampCalculationNode::resolved_type() const
|
||||||
|
{
|
||||||
|
// NOTE: We check during parsing that all values have the same type.
|
||||||
|
return m_min_value->resolved_type();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ClampCalculationNode::contains_percentage() const
|
||||||
|
{
|
||||||
|
return m_min_value->contains_percentage() || m_center_value->contains_percentage() || m_max_value->contains_percentage();
|
||||||
|
}
|
||||||
|
|
||||||
|
CalculatedStyleValue::CalculationResult ClampCalculationNode::resolve(Layout::Node const* layout_node, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
|
||||||
|
{
|
||||||
|
auto min_node = m_min_value->resolve(layout_node, percentage_basis);
|
||||||
|
auto center_node = m_center_value->resolve(layout_node, percentage_basis);
|
||||||
|
auto max_node = m_max_value->resolve(layout_node, percentage_basis);
|
||||||
|
|
||||||
|
auto min_value = resolve_value(min_node.value(), layout_node);
|
||||||
|
auto center_value = resolve_value(center_node.value(), layout_node);
|
||||||
|
auto max_value = resolve_value(max_node.value(), layout_node);
|
||||||
|
|
||||||
|
// NOTE: The value should be returned as "max(MIN, min(VAL, MAX))"
|
||||||
|
auto chosen_value = max(min_value, min(center_value, max_value));
|
||||||
|
if (chosen_value == min_value)
|
||||||
|
return min_node;
|
||||||
|
if (chosen_value == center_value)
|
||||||
|
return center_node;
|
||||||
|
if (chosen_value == max_value)
|
||||||
|
return max_node;
|
||||||
|
|
||||||
|
VERIFY_NOT_REACHED();
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorOr<void> ClampCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
|
||||||
|
{
|
||||||
|
TRY(m_min_value->for_each_child_node(callback));
|
||||||
|
TRY(m_center_value->for_each_child_node(callback));
|
||||||
|
TRY(m_max_value->for_each_child_node(callback));
|
||||||
|
TRY(callback(m_min_value));
|
||||||
|
TRY(callback(m_center_value));
|
||||||
|
TRY(callback(m_max_value));
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorOr<void> ClampCalculationNode::dump(StringBuilder& builder, int indent) const
|
||||||
|
{
|
||||||
|
TRY(builder.try_appendff("{: >{}}CLAMP:\n", "", indent));
|
||||||
|
TRY(m_min_value->dump(builder, indent + 2));
|
||||||
|
TRY(m_center_value->dump(builder, indent + 2));
|
||||||
|
TRY(m_max_value->dump(builder, indent + 2));
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
|
void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
|
||||||
{
|
{
|
||||||
add_or_subtract_internal(SumOperation::Add, other, layout_node, percentage_basis);
|
add_or_subtract_internal(SumOperation::Add, other, layout_node, percentage_basis);
|
||||||
|
|
|
@ -292,4 +292,24 @@ private:
|
||||||
Vector<NonnullOwnPtr<CalculationNode>> m_values;
|
Vector<NonnullOwnPtr<CalculationNode>> m_values;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class ClampCalculationNode final : public CalculationNode {
|
||||||
|
public:
|
||||||
|
static ErrorOr<NonnullOwnPtr<ClampCalculationNode>> create(NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>);
|
||||||
|
~ClampCalculationNode();
|
||||||
|
|
||||||
|
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(Layout::Node 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:
|
||||||
|
explicit ClampCalculationNode(NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>);
|
||||||
|
NonnullOwnPtr<CalculationNode> m_min_value;
|
||||||
|
NonnullOwnPtr<CalculationNode> m_center_value;
|
||||||
|
NonnullOwnPtr<CalculationNode> m_max_value;
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue