/* * Copyright (c) 2018-2020, Andreas Kling * Copyright (c) 2021, Tobias Christiansen * Copyright (c) 2021-2023, Sam Atkins * Copyright (c) 2022-2023, MacDue * * SPDX-License-Identifier: BSD-2-Clause */ #include "CalculatedStyleValue.h" #include namespace Web::CSS { static bool is_number(CalculatedStyleValue::ResolvedType type) { return type == CalculatedStyleValue::ResolvedType::Number || type == CalculatedStyleValue::ResolvedType::Integer; } static bool is_dimension(CalculatedStyleValue::ResolvedType type) { return type != CalculatedStyleValue::ResolvedType::Number && type != CalculatedStyleValue::ResolvedType::Integer && type != CalculatedStyleValue::ResolvedType::Percentage; } static double resolve_value_radians(CalculatedStyleValue::CalculationResult::Value value) { return value.visit( [](Number const& number) { return number.value(); }, [](Angle const& angle) { return angle.to_radians(); }, [](auto const&) { VERIFY_NOT_REACHED(); return 0.0; }); }; static double resolve_value(CalculatedStyleValue::CalculationResult::Value value, Optional context) { return value.visit( [](Number const& number) { return number.value(); }, [](Angle const& angle) { return angle.to_degrees(); }, [](Frequency const& frequency) { return frequency.to_hertz(); }, [&context](Length const& length) { return length.to_px(*context).to_double(); }, [](Percentage const& percentage) { return percentage.value(); }, [](Time const& time) { return time.to_seconds(); }); }; static CalculatedStyleValue::CalculationResult to_resolved_type(CalculatedStyleValue::ResolvedType type, double value) { switch (type) { case CalculatedStyleValue::ResolvedType::Integer: return { Number(Number::Type::Integer, value) }; case CalculatedStyleValue::ResolvedType::Number: return { Number(Number::Type::Number, value) }; case CalculatedStyleValue::ResolvedType::Angle: return { Angle::make_degrees(value) }; case CalculatedStyleValue::ResolvedType::Frequency: return { Frequency::make_hertz(value) }; case CalculatedStyleValue::ResolvedType::Length: return { Length::make_px(value) }; case CalculatedStyleValue::ResolvedType::Percentage: return { Percentage(value) }; case CalculatedStyleValue::ResolvedType::Time: return { Time::make_seconds(value) }; } VERIFY_NOT_REACHED(); }; CalculationNode::CalculationNode(Type type) : m_type(type) { } CalculationNode::~CalculationNode() = default; ErrorOr> NumericCalculationNode::create(NumericValue value) { return adopt_nonnull_own_or_enomem(new (nothrow) NumericCalculationNode(move(value))); } NumericCalculationNode::NumericCalculationNode(NumericValue value) : CalculationNode(Type::Numeric) , m_value(move(value)) { } NumericCalculationNode::~NumericCalculationNode() = default; ErrorOr NumericCalculationNode::to_string() const { return m_value.visit([](auto& value) { return value.to_string(); }); } Optional NumericCalculationNode::resolved_type() const { return m_value.visit( [](Number const&) { return CalculatedStyleValue::ResolvedType::Number; }, [](Angle const&) { return CalculatedStyleValue::ResolvedType::Angle; }, [](Frequency const&) { return CalculatedStyleValue::ResolvedType::Frequency; }, [](Length const&) { return CalculatedStyleValue::ResolvedType::Length; }, [](Percentage const&) { return CalculatedStyleValue::ResolvedType::Percentage; }, [](Time const&) { return CalculatedStyleValue::ResolvedType::Time; }); } bool NumericCalculationNode::contains_percentage() const { return m_value.has(); } CalculatedStyleValue::CalculationResult NumericCalculationNode::resolve(Optional, CalculatedStyleValue::PercentageBasis const&) const { return m_value; } ErrorOr NumericCalculationNode::dump(StringBuilder& builder, int indent) const { return builder.try_appendff("{: >{}}NUMERIC({})\n", "", indent, TRY(m_value.visit([](auto& it) { return it.to_string(); }))); } ErrorOr> SumCalculationNode::create(Vector> values) { return adopt_nonnull_own_or_enomem(new (nothrow) SumCalculationNode(move(values))); } SumCalculationNode::SumCalculationNode(Vector> values) : CalculationNode(Type::Sum) , m_values(move(values)) { VERIFY(!m_values.is_empty()); } SumCalculationNode::~SumCalculationNode() = default; ErrorOr SumCalculationNode::to_string() const { bool first = true; StringBuilder builder; for (auto& value : m_values) { if (!first) TRY(builder.try_append(" + "sv)); TRY(builder.try_append(TRY(value->to_string()))); first = false; } return builder.to_string(); } Optional SumCalculationNode::resolved_type() const { // FIXME: Implement https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation // For now, this is just ad-hoc, based on the old implementation. Optional type; for (auto const& value : m_values) { auto maybe_value_type = value->resolved_type(); if (!maybe_value_type.has_value()) return {}; auto value_type = maybe_value_type.value(); if (!type.has_value()) { type = value_type; continue; } // At + or -, check that both sides have the same type, or that one side is a and the other is an . // If both sides are the same type, resolve to that type. if (value_type == type) continue; // If one side is a and the other is an , resolve to . if (is_number(*type) && is_number(value_type)) { type = CalculatedStyleValue::ResolvedType::Number; continue; } // FIXME: calc() handles by allowing them to pretend to be whatever type is allowed at this location. // Since we can't easily check what that type is, we just allow to combine with any other type. if (type == CalculatedStyleValue::ResolvedType::Percentage && is_dimension(value_type)) { type = value_type; continue; } if (is_dimension(*type) && value_type == CalculatedStyleValue::ResolvedType::Percentage) continue; return {}; } return type; } bool SumCalculationNode::contains_percentage() const { for (auto const& value : m_values) { if (value->contains_percentage()) return true; } return false; } CalculatedStyleValue::CalculationResult SumCalculationNode::resolve(Optional context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const { Optional total; for (auto& additional_product : m_values) { auto additional_value = additional_product->resolve(context, percentage_basis); if (!total.has_value()) { total = additional_value; continue; } total->add(additional_value, context, percentage_basis); } return total.value(); } ErrorOr SumCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> const& callback) { for (auto& item : m_values) { TRY(item->for_each_child_node(callback)); TRY(callback(item)); } return {}; } ErrorOr SumCalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}SUM:\n", "", indent)); for (auto const& item : m_values) TRY(item->dump(builder, indent + 2)); return {}; } ErrorOr> ProductCalculationNode::create(Vector> values) { return adopt_nonnull_own_or_enomem(new (nothrow) ProductCalculationNode(move(values))); } ProductCalculationNode::ProductCalculationNode(Vector> values) : CalculationNode(Type::Product) , m_values(move(values)) { VERIFY(!m_values.is_empty()); } ProductCalculationNode::~ProductCalculationNode() = default; ErrorOr ProductCalculationNode::to_string() const { bool first = true; StringBuilder builder; for (auto& value : m_values) { if (!first) TRY(builder.try_append(" * "sv)); TRY(builder.try_append(TRY(value->to_string()))); first = false; } return builder.to_string(); } Optional ProductCalculationNode::resolved_type() const { // FIXME: Implement https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation // For now, this is just ad-hoc, based on the old implementation. Optional type; for (auto const& value : m_values) { auto maybe_value_type = value->resolved_type(); if (!maybe_value_type.has_value()) return {}; auto value_type = maybe_value_type.value(); if (!type.has_value()) { type = value_type; continue; } // At *, check that at least one side is . if (!(is_number(*type) || is_number(value_type))) return {}; // If both sides are , resolve to . if (type == CalculatedStyleValue::ResolvedType::Integer && value_type == CalculatedStyleValue::ResolvedType::Integer) { type = CalculatedStyleValue::ResolvedType::Integer; } else { // Otherwise, resolve to the type of the other side. if (is_number(*type)) type = value_type; } } return type; } bool ProductCalculationNode::contains_percentage() const { for (auto const& value : m_values) { if (value->contains_percentage()) return true; } return false; } CalculatedStyleValue::CalculationResult ProductCalculationNode::resolve(Optional context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const { Optional total; for (auto& additional_product : m_values) { auto additional_value = additional_product->resolve(context, percentage_basis); if (!total.has_value()) { total = additional_value; continue; } total->multiply_by(additional_value, context); } return total.value(); } ErrorOr ProductCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> const& callback) { for (auto& item : m_values) { TRY(item->for_each_child_node(callback)); TRY(callback(item)); } return {}; } ErrorOr ProductCalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}PRODUCT:\n", "", indent)); for (auto const& item : m_values) TRY(item->dump(builder, indent + 2)); return {}; } ErrorOr> NegateCalculationNode::create(NonnullOwnPtr value) { return adopt_nonnull_own_or_enomem(new (nothrow) NegateCalculationNode(move(value))); } NegateCalculationNode::NegateCalculationNode(NonnullOwnPtr value) : CalculationNode(Type::Negate) , m_value(move(value)) { } NegateCalculationNode::~NegateCalculationNode() = default; ErrorOr NegateCalculationNode::to_string() const { return String::formatted("(0 - {})", TRY(m_value->to_string())); } Optional NegateCalculationNode::resolved_type() const { return m_value->resolved_type(); } bool NegateCalculationNode::contains_percentage() const { return m_value->contains_percentage(); } CalculatedStyleValue::CalculationResult NegateCalculationNode::resolve(Optional context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const { auto child_value = m_value->resolve(context, percentage_basis); child_value.negate(); return child_value; } ErrorOr NegateCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> const& callback) { TRY(m_value->for_each_child_node(callback)); TRY(callback(m_value)); return {}; } ErrorOr NegateCalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}NEGATE:\n", "", indent)); TRY(m_value->dump(builder, indent + 2)); return {}; } ErrorOr> InvertCalculationNode::create(NonnullOwnPtr value) { return adopt_nonnull_own_or_enomem(new (nothrow) InvertCalculationNode(move(value))); } InvertCalculationNode::InvertCalculationNode(NonnullOwnPtr value) : CalculationNode(Type::Invert) , m_value(move(value)) { } InvertCalculationNode::~InvertCalculationNode() = default; ErrorOr InvertCalculationNode::to_string() const { return String::formatted("(1 / {})", TRY(m_value->to_string())); } Optional InvertCalculationNode::resolved_type() const { auto type = m_value->resolved_type(); if (type == CalculatedStyleValue::ResolvedType::Integer) return CalculatedStyleValue::ResolvedType::Number; return type; } bool InvertCalculationNode::contains_percentage() const { return m_value->contains_percentage(); } CalculatedStyleValue::CalculationResult InvertCalculationNode::resolve(Optional context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const { auto child_value = m_value->resolve(context, percentage_basis); child_value.invert(); return child_value; } ErrorOr InvertCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> const& callback) { TRY(m_value->for_each_child_node(callback)); TRY(callback(m_value)); return {}; } ErrorOr InvertCalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}INVERT:\n", "", indent)); TRY(m_value->dump(builder, indent + 2)); return {}; } ErrorOr> MinCalculationNode::create(Vector> values) { return adopt_nonnull_own_or_enomem(new (nothrow) MinCalculationNode(move(values))); } MinCalculationNode::MinCalculationNode(Vector> values) : CalculationNode(Type::Min) , m_values(move(values)) { } MinCalculationNode::~MinCalculationNode() = default; ErrorOr MinCalculationNode::to_string() const { StringBuilder builder; TRY(builder.try_append("min("sv)); for (size_t i = 0; i < m_values.size(); ++i) { if (i != 0) TRY(builder.try_append(", "sv)); TRY(builder.try_append(TRY(m_values[i]->to_string()))); } TRY(builder.try_append(")"sv)); return builder.to_string(); } Optional MinCalculationNode::resolved_type() const { // NOTE: We check during parsing that all values have the same type. return m_values[0]->resolved_type(); } bool MinCalculationNode::contains_percentage() const { for (auto const& value : m_values) { if (value->contains_percentage()) return true; } return false; } CalculatedStyleValue::CalculationResult MinCalculationNode::resolve(Optional context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const { CalculatedStyleValue::CalculationResult smallest_node = m_values.first()->resolve(context, percentage_basis); auto smallest_value = resolve_value(smallest_node.value(), context); for (size_t i = 1; i < m_values.size(); i++) { auto child_resolved = m_values[i]->resolve(context, percentage_basis); auto child_value = resolve_value(child_resolved.value(), context); if (child_value < smallest_value) { smallest_value = child_value; smallest_node = child_resolved; } } return smallest_node; } ErrorOr MinCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> const& callback) { for (auto& value : m_values) { TRY(value->for_each_child_node(callback)); TRY(callback(value)); } return {}; } ErrorOr MinCalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}MIN:\n", "", indent)); for (auto const& value : m_values) TRY(value->dump(builder, indent + 2)); return {}; } ErrorOr> MaxCalculationNode::create(Vector> values) { return adopt_nonnull_own_or_enomem(new (nothrow) MaxCalculationNode(move(values))); } MaxCalculationNode::MaxCalculationNode(Vector> values) : CalculationNode(Type::Max) , m_values(move(values)) { } MaxCalculationNode::~MaxCalculationNode() = default; ErrorOr MaxCalculationNode::to_string() const { StringBuilder builder; TRY(builder.try_append("max("sv)); for (size_t i = 0; i < m_values.size(); ++i) { if (i != 0) TRY(builder.try_append(", "sv)); TRY(builder.try_append(TRY(m_values[i]->to_string()))); } TRY(builder.try_append(")"sv)); return builder.to_string(); } Optional MaxCalculationNode::resolved_type() const { // NOTE: We check during parsing that all values have the same type. return m_values[0]->resolved_type(); } bool MaxCalculationNode::contains_percentage() const { for (auto const& value : m_values) { if (value->contains_percentage()) return true; } return false; } CalculatedStyleValue::CalculationResult MaxCalculationNode::resolve(Optional context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const { CalculatedStyleValue::CalculationResult largest_node = m_values.first()->resolve(context, percentage_basis); auto largest_value = resolve_value(largest_node.value(), context); for (size_t i = 1; i < m_values.size(); i++) { auto child_resolved = m_values[i]->resolve(context, percentage_basis); auto child_value = resolve_value(child_resolved.value(), context); if (child_value > largest_value) { largest_value = child_value; largest_node = child_resolved; } } return largest_node; } ErrorOr MaxCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> const& callback) { for (auto& value : m_values) { TRY(value->for_each_child_node(callback)); TRY(callback(value)); } return {}; } ErrorOr MaxCalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}MAX:\n", "", indent)); for (auto const& value : m_values) TRY(value->dump(builder, indent + 2)); return {}; } ErrorOr> ClampCalculationNode::create(NonnullOwnPtr min, NonnullOwnPtr center, NonnullOwnPtr max) { return adopt_nonnull_own_or_enomem(new (nothrow) ClampCalculationNode(move(min), move(center), move(max))); } ClampCalculationNode::ClampCalculationNode(NonnullOwnPtr min, NonnullOwnPtr center, NonnullOwnPtr max) : CalculationNode(Type::Clamp) , m_min_value(move(min)) , m_center_value(move(center)) , m_max_value(move(max)) { } ClampCalculationNode::~ClampCalculationNode() = default; ErrorOr 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 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(Optional context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const { auto min_node = m_min_value->resolve(context, percentage_basis); auto center_node = m_center_value->resolve(context, percentage_basis); auto max_node = m_max_value->resolve(context, percentage_basis); auto min_value = resolve_value(min_node.value(), context); auto center_value = resolve_value(center_node.value(), context); auto max_value = resolve_value(max_node.value(), context); // 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 ClampCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> 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 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 {}; } ErrorOr> AbsCalculationNode::create(NonnullOwnPtr value) { return adopt_nonnull_own_or_enomem(new (nothrow) AbsCalculationNode(move(value))); } AbsCalculationNode::AbsCalculationNode(NonnullOwnPtr value) : CalculationNode(Type::Abs) , m_value(move(value)) { } AbsCalculationNode::~AbsCalculationNode() = default; ErrorOr AbsCalculationNode::to_string() const { StringBuilder builder; builder.append("abs("sv); builder.append(TRY(m_value->to_string())); builder.append(")"sv); return builder.to_string(); } Optional AbsCalculationNode::resolved_type() const { return m_value->resolved_type(); } bool AbsCalculationNode::contains_percentage() const { return m_value->contains_percentage(); } CalculatedStyleValue::CalculationResult AbsCalculationNode::resolve(Optional context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const { auto resolved_type = m_value->resolved_type().value(); 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 to_resolved_type(resolved_type, -node_a_value); return node_a; } ErrorOr AbsCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> const& callback) { TRY(m_value->for_each_child_node(callback)); TRY(callback(m_value)); return {}; } ErrorOr AbsCalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}ABS: {}\n", "", indent, TRY(to_string()))); return {}; } ErrorOr> SignCalculationNode::create(NonnullOwnPtr value) { return adopt_nonnull_own_or_enomem(new (nothrow) SignCalculationNode(move(value))); } SignCalculationNode::SignCalculationNode(NonnullOwnPtr value) : CalculationNode(Type::Sign) , m_value(move(value)) { } SignCalculationNode::~SignCalculationNode() = default; ErrorOr 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 SignCalculationNode::resolved_type() const { return CalculatedStyleValue::ResolvedType::Integer; } bool SignCalculationNode::contains_percentage() const { return m_value->contains_percentage(); } CalculatedStyleValue::CalculationResult SignCalculationNode::resolve(Optional 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 SignCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> const& callback) { TRY(m_value->for_each_child_node(callback)); TRY(callback(m_value)); return {}; } ErrorOr SignCalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}SIGN: {}\n", "", indent, TRY(to_string()))); return {}; } ErrorOr> ConstantCalculationNode::create(ConstantType constant) { return adopt_nonnull_own_or_enomem(new (nothrow) ConstantCalculationNode(constant)); } ConstantCalculationNode::ConstantCalculationNode(ConstantType constant) : CalculationNode(Type::Constant) , m_constant(constant) { } ConstantCalculationNode::~ConstantCalculationNode() = default; ErrorOr ConstantCalculationNode::to_string() const { switch (m_constant) { case CalculationNode::ConstantType::E: return "e"_short_string; case CalculationNode::ConstantType::PI: return "pi"_short_string; case CalculationNode::ConstantType::Infinity: return "infinity"_string; case CalculationNode::ConstantType::MinusInfinity: return "-infinity"_string; case CalculationNode::ConstantType::NaN: return "NaN"_string; } VERIFY_NOT_REACHED(); } Optional ConstantCalculationNode::resolved_type() const { return CalculatedStyleValue::ResolvedType::Number; } CalculatedStyleValue::CalculationResult ConstantCalculationNode::resolve([[maybe_unused]] Optional context, [[maybe_unused]] CalculatedStyleValue::PercentageBasis const& percentage_basis) const { switch (m_constant) { case CalculationNode::ConstantType::E: return { Number(Number::Type::Number, M_E) }; case CalculationNode::ConstantType::PI: return { Number(Number::Type::Number, M_PI) }; // FIXME: We need to keep track of Infinity and NaN across all nodes, since they require special handling. case CalculationNode::ConstantType::Infinity: return { Number(Number::Type::Number, NumericLimits::max()) }; case CalculationNode::ConstantType::MinusInfinity: return { Number(Number::Type::Number, NumericLimits::lowest()) }; case CalculationNode::ConstantType::NaN: return { Number(Number::Type::Number, NAN) }; } VERIFY_NOT_REACHED(); } ErrorOr ConstantCalculationNode::for_each_child_node([[maybe_unused]] Function(NonnullOwnPtr&)> const& callback) { return {}; } ErrorOr ConstantCalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}CONSTANT: {}\n", "", indent, TRY(to_string()))); return {}; } ErrorOr> SinCalculationNode::create(NonnullOwnPtr value) { return adopt_nonnull_own_or_enomem(new (nothrow) SinCalculationNode(move(value))); } SinCalculationNode::SinCalculationNode(NonnullOwnPtr value) : CalculationNode(Type::Sin) , m_value(move(value)) { } SinCalculationNode::~SinCalculationNode() = default; ErrorOr SinCalculationNode::to_string() const { StringBuilder builder; builder.append("sin("sv); builder.append(TRY(m_value->to_string())); builder.append(")"sv); return builder.to_string(); } Optional SinCalculationNode::resolved_type() const { return CalculatedStyleValue::ResolvedType::Number; } bool SinCalculationNode::contains_percentage() const { return m_value->contains_percentage(); } CalculatedStyleValue::CalculationResult SinCalculationNode::resolve(Optional 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 = sin(node_a_value); return { Number(Number::Type::Number, result) }; } ErrorOr SinCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> const& callback) { TRY(m_value->for_each_child_node(callback)); TRY(callback(m_value)); return {}; } ErrorOr SinCalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}SIN: {}\n", "", indent, TRY(to_string()))); return {}; } ErrorOr> CosCalculationNode::create(NonnullOwnPtr value) { return adopt_nonnull_own_or_enomem(new (nothrow) CosCalculationNode(move(value))); } CosCalculationNode::CosCalculationNode(NonnullOwnPtr value) : CalculationNode(Type::Cos) , m_value(move(value)) { } CosCalculationNode::~CosCalculationNode() = default; ErrorOr 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 CosCalculationNode::resolved_type() const { return CalculatedStyleValue::ResolvedType::Number; } bool CosCalculationNode::contains_percentage() const { return m_value->contains_percentage(); } CalculatedStyleValue::CalculationResult CosCalculationNode::resolve(Optional 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 CosCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> const& callback) { TRY(m_value->for_each_child_node(callback)); TRY(callback(m_value)); return {}; } ErrorOr CosCalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}COS: {}\n", "", indent, TRY(to_string()))); return {}; } ErrorOr> TanCalculationNode::create(NonnullOwnPtr value) { return adopt_nonnull_own_or_enomem(new (nothrow) TanCalculationNode(move(value))); } TanCalculationNode::TanCalculationNode(NonnullOwnPtr value) : CalculationNode(Type::Tan) , m_value(move(value)) { } TanCalculationNode::~TanCalculationNode() = default; ErrorOr TanCalculationNode::to_string() const { StringBuilder builder; builder.append("tan("sv); builder.append(TRY(m_value->to_string())); builder.append(")"sv); return builder.to_string(); } Optional TanCalculationNode::resolved_type() const { return CalculatedStyleValue::ResolvedType::Number; } bool TanCalculationNode::contains_percentage() const { return m_value->contains_percentage(); } CalculatedStyleValue::CalculationResult TanCalculationNode::resolve(Optional 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 = tan(node_a_value); return { Number(Number::Type::Number, result) }; } ErrorOr TanCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> const& callback) { TRY(m_value->for_each_child_node(callback)); TRY(callback(m_value)); return {}; } ErrorOr TanCalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}TAN: {}\n", "", indent, TRY(to_string()))); return {}; } ErrorOr> AsinCalculationNode::create(NonnullOwnPtr value) { return adopt_nonnull_own_or_enomem(new (nothrow) AsinCalculationNode(move(value))); } AsinCalculationNode::AsinCalculationNode(NonnullOwnPtr value) : CalculationNode(Type::Asin) , m_value(move(value)) { } AsinCalculationNode::~AsinCalculationNode() = default; ErrorOr AsinCalculationNode::to_string() const { StringBuilder builder; builder.append("asin("sv); builder.append(TRY(m_value->to_string())); builder.append(")"sv); return builder.to_string(); } Optional AsinCalculationNode::resolved_type() const { return CalculatedStyleValue::ResolvedType::Angle; } bool AsinCalculationNode::contains_percentage() const { return m_value->contains_percentage(); } CalculatedStyleValue::CalculationResult AsinCalculationNode::resolve(Optional 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); auto result = asin(node_a_value); return { Angle(result, Angle::Type::Rad) }; } ErrorOr AsinCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> const& callback) { TRY(m_value->for_each_child_node(callback)); TRY(callback(m_value)); return {}; } ErrorOr AsinCalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}ASIN: {}\n", "", indent, TRY(to_string()))); return {}; } ErrorOr> AcosCalculationNode::create(NonnullOwnPtr value) { return adopt_nonnull_own_or_enomem(new (nothrow) AcosCalculationNode(move(value))); } AcosCalculationNode::AcosCalculationNode(NonnullOwnPtr value) : CalculationNode(Type::Acos) , m_value(move(value)) { } AcosCalculationNode::~AcosCalculationNode() = default; ErrorOr AcosCalculationNode::to_string() const { StringBuilder builder; builder.append("acos("sv); builder.append(TRY(m_value->to_string())); builder.append(")"sv); return builder.to_string(); } Optional AcosCalculationNode::resolved_type() const { return CalculatedStyleValue::ResolvedType::Angle; } bool AcosCalculationNode::contains_percentage() const { return m_value->contains_percentage(); } CalculatedStyleValue::CalculationResult AcosCalculationNode::resolve(Optional 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); auto result = acos(node_a_value); return { Angle(result, Angle::Type::Rad) }; } ErrorOr AcosCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> const& callback) { TRY(m_value->for_each_child_node(callback)); TRY(callback(m_value)); return {}; } ErrorOr AcosCalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}ACOS: {}\n", "", indent, TRY(to_string()))); return {}; } ErrorOr> AtanCalculationNode::create(NonnullOwnPtr value) { return adopt_nonnull_own_or_enomem(new (nothrow) AtanCalculationNode(move(value))); } AtanCalculationNode::AtanCalculationNode(NonnullOwnPtr value) : CalculationNode(Type::Atan) , m_value(move(value)) { } AtanCalculationNode::~AtanCalculationNode() = default; ErrorOr AtanCalculationNode::to_string() const { StringBuilder builder; builder.append("atan("sv); builder.append(TRY(m_value->to_string())); builder.append(")"sv); return builder.to_string(); } Optional AtanCalculationNode::resolved_type() const { return CalculatedStyleValue::ResolvedType::Angle; } bool AtanCalculationNode::contains_percentage() const { return m_value->contains_percentage(); } CalculatedStyleValue::CalculationResult AtanCalculationNode::resolve(Optional 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); auto result = atan(node_a_value); return { Angle(result, Angle::Type::Rad) }; } ErrorOr AtanCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> const& callback) { TRY(m_value->for_each_child_node(callback)); TRY(callback(m_value)); return {}; } ErrorOr AtanCalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}ATAN: {}\n", "", indent, TRY(to_string()))); return {}; } ErrorOr> Atan2CalculationNode::create(NonnullOwnPtr y, NonnullOwnPtr x) { return adopt_nonnull_own_or_enomem(new (nothrow) Atan2CalculationNode(move(y), move(x))); } Atan2CalculationNode::Atan2CalculationNode(NonnullOwnPtr y, NonnullOwnPtr x) : CalculationNode(Type::Atan2) , m_y(move(y)) , m_x(move(x)) { } Atan2CalculationNode::~Atan2CalculationNode() = default; ErrorOr Atan2CalculationNode::to_string() const { StringBuilder builder; builder.append("atan2("sv); builder.append(TRY(m_y->to_string())); builder.append(", "sv); builder.append(TRY(m_x->to_string())); builder.append(")"sv); return builder.to_string(); } Optional Atan2CalculationNode::resolved_type() const { return CalculatedStyleValue::ResolvedType::Angle; } bool Atan2CalculationNode::contains_percentage() const { return m_y->contains_percentage() || m_x->contains_percentage(); } CalculatedStyleValue::CalculationResult Atan2CalculationNode::resolve(Optional context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const { auto node_a = m_y->resolve(context, percentage_basis); auto node_a_value = resolve_value(node_a.value(), context); auto node_b = m_x->resolve(context, percentage_basis); auto node_b_value = resolve_value(node_b.value(), context); auto result = atan2(node_a_value, node_b_value); return { Angle(result, Angle::Type::Rad) }; } ErrorOr Atan2CalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> const& callback) { TRY(m_y->for_each_child_node(callback)); TRY(m_x->for_each_child_node(callback)); TRY(callback(m_y)); TRY(callback(m_x)); return {}; } ErrorOr Atan2CalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}ATAN2: {}\n", "", indent, TRY(to_string()))); return {}; } ErrorOr> PowCalculationNode::create(NonnullOwnPtr x, NonnullOwnPtr y) { return adopt_nonnull_own_or_enomem(new (nothrow) PowCalculationNode(move(x), move(y))); } PowCalculationNode::PowCalculationNode(NonnullOwnPtr x, NonnullOwnPtr y) : CalculationNode(Type::Pow) , m_x(move(x)) , m_y(move(y)) { } PowCalculationNode::~PowCalculationNode() = default; ErrorOr PowCalculationNode::to_string() const { StringBuilder builder; builder.append("pow("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 PowCalculationNode::resolved_type() const { return CalculatedStyleValue::ResolvedType::Number; } CalculatedStyleValue::CalculationResult PowCalculationNode::resolve(Optional 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 = pow(node_a_value, node_b_value); return { Number(Number::Type::Number, result) }; } ErrorOr PowCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> 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 PowCalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}POW: {}\n", "", indent, TRY(to_string()))); return {}; } ErrorOr> SqrtCalculationNode::create(NonnullOwnPtr value) { return adopt_nonnull_own_or_enomem(new (nothrow) SqrtCalculationNode(move(value))); } SqrtCalculationNode::SqrtCalculationNode(NonnullOwnPtr value) : CalculationNode(Type::Sqrt) , m_value(move(value)) { } SqrtCalculationNode::~SqrtCalculationNode() = default; ErrorOr SqrtCalculationNode::to_string() const { StringBuilder builder; builder.append("sqrt("sv); builder.append(TRY(m_value->to_string())); builder.append(")"sv); return builder.to_string(); } Optional SqrtCalculationNode::resolved_type() const { return CalculatedStyleValue::ResolvedType::Number; } CalculatedStyleValue::CalculationResult SqrtCalculationNode::resolve(Optional 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); auto result = sqrt(node_a_value); return { Number(Number::Type::Number, result) }; } ErrorOr SqrtCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> const& callback) { TRY(m_value->for_each_child_node(callback)); TRY(callback(m_value)); return {}; } ErrorOr SqrtCalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}SQRT: {}\n", "", indent, TRY(to_string()))); return {}; } ErrorOr> HypotCalculationNode::create(Vector> values) { return adopt_nonnull_own_or_enomem(new (nothrow) HypotCalculationNode(move(values))); } HypotCalculationNode::HypotCalculationNode(Vector> values) : CalculationNode(Type::Hypot) , m_values(move(values)) { } HypotCalculationNode::~HypotCalculationNode() = default; ErrorOr HypotCalculationNode::to_string() const { StringBuilder builder; TRY(builder.try_append("hypot("sv)); for (size_t i = 0; i < m_values.size(); ++i) { if (i != 0) TRY(builder.try_append(", "sv)); TRY(builder.try_append(TRY(m_values[i]->to_string()))); } TRY(builder.try_append(")"sv)); return builder.to_string(); } Optional HypotCalculationNode::resolved_type() const { // NOTE: We check during parsing that all values have the same type. return m_values[0]->resolved_type(); } bool HypotCalculationNode::contains_percentage() const { for (auto const& value : m_values) { if (value->contains_percentage()) return true; } return false; } CalculatedStyleValue::CalculationResult HypotCalculationNode::resolve(Optional context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const { double square_sum = 0.0; for (auto const& value : m_values) { auto child_resolved = value->resolve(context, percentage_basis); auto child_value = resolve_value(child_resolved.value(), context); square_sum += child_value * child_value; } auto result = sqrt(square_sum); return to_resolved_type(resolved_type().value(), result); } ErrorOr HypotCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> const& callback) { for (auto& value : m_values) { TRY(value->for_each_child_node(callback)); TRY(callback(value)); } return {}; } ErrorOr HypotCalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}HYPOT:\n", "", indent)); for (auto const& value : m_values) TRY(value->dump(builder, indent + 2)); return {}; } ErrorOr> LogCalculationNode::create(NonnullOwnPtr x, NonnullOwnPtr y) { return adopt_nonnull_own_or_enomem(new (nothrow) LogCalculationNode(move(x), move(y))); } LogCalculationNode::LogCalculationNode(NonnullOwnPtr x, NonnullOwnPtr y) : CalculationNode(Type::Log) , m_x(move(x)) , m_y(move(y)) { } LogCalculationNode::~LogCalculationNode() = default; ErrorOr 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 LogCalculationNode::resolved_type() const { return CalculatedStyleValue::ResolvedType::Number; } CalculatedStyleValue::CalculationResult LogCalculationNode::resolve(Optional 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 LogCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> 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 LogCalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}LOG: {}\n", "", indent, TRY(to_string()))); return {}; } ErrorOr> ExpCalculationNode::create(NonnullOwnPtr value) { return adopt_nonnull_own_or_enomem(new (nothrow) ExpCalculationNode(move(value))); } ExpCalculationNode::ExpCalculationNode(NonnullOwnPtr value) : CalculationNode(Type::Exp) , m_value(move(value)) { } ExpCalculationNode::~ExpCalculationNode() = default; ErrorOr ExpCalculationNode::to_string() const { StringBuilder builder; builder.append("exp("sv); builder.append(TRY(m_value->to_string())); builder.append(")"sv); return builder.to_string(); } Optional ExpCalculationNode::resolved_type() const { return CalculatedStyleValue::ResolvedType::Number; } CalculatedStyleValue::CalculationResult ExpCalculationNode::resolve(Optional 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); auto result = exp(node_a_value); return { Number(Number::Type::Number, result) }; } ErrorOr ExpCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> const& callback) { TRY(m_value->for_each_child_node(callback)); TRY(callback(m_value)); return {}; } ErrorOr ExpCalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}EXP: {}\n", "", indent, TRY(to_string()))); return {}; } ErrorOr> RoundCalculationNode::create(RoundingMode mode, NonnullOwnPtr x, NonnullOwnPtr y) { return adopt_nonnull_own_or_enomem(new (nothrow) RoundCalculationNode(mode, move(x), move(y))); } RoundCalculationNode::RoundCalculationNode(RoundingMode mode, NonnullOwnPtr x, NonnullOwnPtr y) : CalculationNode(Type::Round) , m_mode(mode) , m_x(move(x)) , m_y(move(y)) { } RoundCalculationNode::~RoundCalculationNode() = default; ErrorOr RoundCalculationNode::to_string() const { StringBuilder builder; builder.append("round("sv); switch (m_mode) { case RoundingMode::Nearest: builder.append("nearest"sv); break; case RoundingMode::Up: builder.append("up"sv); break; case RoundingMode::Down: builder.append("down"sv); break; case RoundingMode::TowardZero: builder.append("toward-zero"sv); break; } builder.append(", "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 RoundCalculationNode::resolved_type() const { // Note: We check during parsing that all values have the same type return m_x->resolved_type(); } bool RoundCalculationNode::contains_percentage() const { return m_x->contains_percentage() || m_y->contains_percentage(); } CalculatedStyleValue::CalculationResult RoundCalculationNode::resolve(Optional 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 upper_b = ceil(node_a_value / node_b_value) * node_b_value; auto lower_b = floor(node_a_value / node_b_value) * node_b_value; if (m_mode == RoundingMode::Nearest) { auto upper_diff = fabs(upper_b - node_a_value); auto lower_diff = fabs(node_a_value - lower_b); auto rounded_value = upper_diff < lower_diff ? upper_b : lower_b; return to_resolved_type(resolved_type, rounded_value); } if (m_mode == RoundingMode::Up) { return to_resolved_type(resolved_type, upper_b); } if (m_mode == RoundingMode::Down) { return to_resolved_type(resolved_type, lower_b); } if (m_mode == RoundingMode::TowardZero) { auto upper_diff = fabs(upper_b); auto lower_diff = fabs(lower_b); auto rounded_value = upper_diff < lower_diff ? upper_b : lower_b; return to_resolved_type(resolved_type, rounded_value); } VERIFY_NOT_REACHED(); } ErrorOr RoundCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> 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 RoundCalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}ROUND: {}\n", "", indent, TRY(to_string()))); return {}; } void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Optional context, PercentageBasis const& percentage_basis) { add_or_subtract_internal(SumOperation::Add, other, context, percentage_basis); } void CalculatedStyleValue::CalculationResult::subtract(CalculationResult const& other, Optional context, PercentageBasis const& percentage_basis) { add_or_subtract_internal(SumOperation::Subtract, other, context, percentage_basis); } void CalculatedStyleValue::CalculationResult::add_or_subtract_internal(SumOperation op, CalculationResult const& other, Optional context, PercentageBasis const& percentage_basis) { // We know from validation when resolving the type, that "both sides have the same type, or that one side is a and the other is an ". // Though, having the same type may mean that one side is a and the other a . // Note: This is almost identical to ::add() m_value.visit( [&](Number const& number) { auto other_number = other.m_value.get(); if (op == SumOperation::Add) { m_value = number + other_number; } else { m_value = number - other_number; } }, [&](Angle const& angle) { auto this_degrees = angle.to_degrees(); if (other.m_value.has()) { auto other_degrees = other.m_value.get().to_degrees(); if (op == SumOperation::Add) m_value = Angle::make_degrees(this_degrees + other_degrees); else m_value = Angle::make_degrees(this_degrees - other_degrees); } else { VERIFY(percentage_basis.has()); auto other_degrees = percentage_basis.get().percentage_of(other.m_value.get()).to_degrees(); if (op == SumOperation::Add) m_value = Angle::make_degrees(this_degrees + other_degrees); else m_value = Angle::make_degrees(this_degrees - other_degrees); } }, [&](Frequency const& frequency) { auto this_hertz = frequency.to_hertz(); if (other.m_value.has()) { auto other_hertz = other.m_value.get().to_hertz(); if (op == SumOperation::Add) m_value = Frequency::make_hertz(this_hertz + other_hertz); else m_value = Frequency::make_hertz(this_hertz - other_hertz); } else { VERIFY(percentage_basis.has()); auto other_hertz = percentage_basis.get().percentage_of(other.m_value.get()).to_hertz(); if (op == SumOperation::Add) m_value = Frequency::make_hertz(this_hertz + other_hertz); else m_value = Frequency::make_hertz(this_hertz - other_hertz); } }, [&](Length const& length) { auto this_px = length.to_px(*context); if (other.m_value.has()) { auto other_px = other.m_value.get().to_px(*context); if (op == SumOperation::Add) m_value = Length::make_px(this_px + other_px); else m_value = Length::make_px(this_px - other_px); } else { VERIFY(percentage_basis.has()); auto other_px = percentage_basis.get().percentage_of(other.m_value.get()).to_px(*context); if (op == SumOperation::Add) m_value = Length::make_px(this_px + other_px); else m_value = Length::make_px(this_px - other_px); } }, [&](Time const& time) { auto this_seconds = time.to_seconds(); if (other.m_value.has