LibWeb/CSS: Merge TranslationStyleValue into TransformationStyleValue

As with ScaleStyleValue, the serialization is the only unique part of
this class, and we can just move it over.
This commit is contained in:
Sam Atkins 2025-01-15 16:48:44 +00:00 committed by Andreas Kling
commit 03a4ecce19
Notes: github-actions[bot] 2025-01-17 09:15:27 +00:00
9 changed files with 36 additions and 125 deletions

View file

@ -87,6 +87,32 @@ String TransformationStyleValue::to_string(SerializationMode mode) const
}
return builder.to_string_without_validation();
}
if (m_properties.property == PropertyID::Translate) {
auto resolve_to_string = [mode](CSSStyleValue const& value) -> Optional<String> {
if (value.is_length()) {
if (value.as_length().length().raw_value() == 0)
return {};
}
if (value.is_percentage()) {
if (value.as_percentage().percentage().value() == 0)
return {};
}
return value.to_string(mode);
};
auto x_value = resolve_to_string(m_properties.values[0]);
auto y_value = resolve_to_string(m_properties.values[1]);
// FIXME: 3D translation
StringBuilder builder;
builder.append(x_value.value_or("0px"_string));
if (y_value.has_value()) {
builder.append(" "sv);
builder.append(y_value.value());
}
return builder.to_string_without_validation();
}
StringBuilder builder;
builder.append(CSS::to_string(m_properties.transform_function));

View file

@ -1,44 +0,0 @@
/*
* Copyright (c) 2024, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/String.h>
#include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
#include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
#include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
#include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
#include <LibWeb/CSS/StyleValues/TranslationStyleValue.h>
namespace Web::CSS {
// https://www.w3.org/TR/2021/WD-css-transforms-2-20211109/#individual-transform-serialization
String TranslationStyleValue::to_string(SerializationMode) const
{
auto resolve_to_string = [](LengthPercentage const& value) -> Optional<String> {
if (value.is_length()) {
if (value.length().raw_value() == 0)
return {};
}
if (value.is_percentage()) {
if (value.percentage().value() == 0)
return {};
}
return value.to_string();
};
auto x_value = resolve_to_string(m_properties.x);
auto y_value = resolve_to_string(m_properties.y);
StringBuilder builder;
builder.append(x_value.value_or("0px"_string));
if (y_value.has_value()) {
builder.append(" "sv);
builder.append(y_value.value());
}
return builder.to_string_without_validation();
}
}

View file

@ -1,49 +0,0 @@
/*
* Copyright (c) 2024, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/CSS/CSSStyleValue.h>
#include <LibWeb/CSS/PercentageOr.h>
namespace Web::CSS {
class TranslationStyleValue : public StyleValueWithDefaultOperators<TranslationStyleValue> {
public:
static ValueComparingNonnullRefPtr<TranslationStyleValue> create(LengthPercentage x, LengthPercentage y)
{
return adopt_ref(*new (nothrow) TranslationStyleValue(move(x), move(y)));
}
virtual ~TranslationStyleValue() override = default;
LengthPercentage const& x() const { return m_properties.x; }
LengthPercentage const& y() const { return m_properties.y; }
virtual String to_string(SerializationMode) const override;
bool properties_equal(TranslationStyleValue const& other) const { return m_properties == other.m_properties; }
private:
explicit TranslationStyleValue(
LengthPercentage x,
LengthPercentage y)
: StyleValueWithDefaultOperators(Type::Translation)
, m_properties {
.x = move(x),
.y = move(y),
}
{
}
struct Properties {
LengthPercentage x;
LengthPercentage y;
bool operator==(Properties const&) const = default;
} m_properties;
};
}