mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2026-04-19 02:10:26 +00:00
`EdgeStyleValues` which consist of an offset of a `calc()`s which resolves to 50% should be considered "centered" for `SerializationMode::ResolvedValue` for the purpose of omitting the position value from gradient serialization.
68 lines
2.3 KiB
C++
68 lines
2.3 KiB
C++
/*
|
|
* Copyright (c) 2023, MacDue <macdue@dueutil.tech>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include "EdgeStyleValue.h"
|
|
#include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
|
|
#include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
|
|
#include <LibWeb/CSS/ValueType.h>
|
|
|
|
namespace Web::CSS {
|
|
|
|
bool EdgeStyleValue::is_center(SerializationMode mode) const
|
|
{
|
|
if (m_properties.edge == PositionEdge::Center)
|
|
return true;
|
|
|
|
if (m_properties.offset && m_properties.offset->to_string(mode) == "50%"sv)
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
String EdgeStyleValue::to_string(SerializationMode mode) const
|
|
{
|
|
StringBuilder builder;
|
|
|
|
if (m_properties.edge.has_value())
|
|
builder.append(CSS::to_string(m_properties.edge.value()));
|
|
|
|
if (m_properties.edge.has_value() && m_properties.offset)
|
|
builder.append(' ');
|
|
|
|
if (m_properties.offset)
|
|
builder.append(m_properties.offset->to_string(mode));
|
|
|
|
return builder.to_string_without_validation();
|
|
}
|
|
|
|
ValueComparingNonnullRefPtr<StyleValue const> EdgeStyleValue::absolutized(ComputationContext const& computation_context) const
|
|
{
|
|
if (m_properties.edge == PositionEdge::Center)
|
|
return EdgeStyleValue::create({}, PercentageStyleValue::create(Percentage(50)));
|
|
|
|
CalculationContext calculation_context {
|
|
.percentages_resolve_as = ValueType::Length,
|
|
};
|
|
|
|
if (m_properties.edge == PositionEdge::Right || m_properties.edge == PositionEdge::Bottom) {
|
|
if (!m_properties.offset)
|
|
return EdgeStyleValue::create({}, PercentageStyleValue::create(Percentage(100)));
|
|
|
|
auto flipped_percentage = SumCalculationNode::create({ NumericCalculationNode::create(Percentage { 100 }, calculation_context),
|
|
NegateCalculationNode::create(CalculationNode::from_style_value(*m_properties.offset, calculation_context)) });
|
|
|
|
auto flipped_percentage_style_value = CalculatedStyleValue::create(flipped_percentage, NumericType(NumericType::BaseType::Length, 1), calculation_context);
|
|
|
|
return EdgeStyleValue::create({}, flipped_percentage_style_value->absolutized(computation_context));
|
|
}
|
|
|
|
if (!m_properties.offset)
|
|
return EdgeStyleValue::create({}, PercentageStyleValue::create(Percentage(0)));
|
|
|
|
return EdgeStyleValue::create({}, m_properties.offset->absolutized(computation_context));
|
|
}
|
|
|
|
}
|