LibWeb/CSS: Merge ScaleStyleValue into TransformationStyleValue

The only ways this varies from the `scale()` function is with parsing
and serialization. Parsing stays separate, and serialization is done by
telling `TransformationStyleValue` which property it is, and overriding
its normal `to_string()` code for properties other than `transform`.
This commit is contained in:
Sam Atkins 2025-01-15 14:58:23 +00:00 committed by Andreas Kling
commit ac15e626dd
Notes: github-actions[bot] 2025-01-17 09:15:32 +00:00
12 changed files with 52 additions and 132 deletions

View file

@ -1,37 +0,0 @@
/*
* Copyright (c) 2024, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/String.h>
#include <LibWeb/CSS/StyleValues/ScaleStyleValue.h>
namespace Web::CSS {
// https://www.w3.org/TR/2021/WD-css-transforms-2-20211109/#individual-transform-serialization
String ScaleStyleValue::to_string(SerializationMode) const
{
auto resolve_to_string = [](NumberPercentage const& value) -> String {
if (value.is_number()) {
return MUST(String::formatted("{}", value.number().value()));
}
if (value.is_percentage()) {
return MUST(String::formatted("{}", value.percentage().value() / 100.0));
}
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);
if (x_value != y_value) {
builder.append(" "sv);
builder.append(y_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 ScaleStyleValue : public StyleValueWithDefaultOperators<ScaleStyleValue> {
public:
static ValueComparingNonnullRefPtr<ScaleStyleValue> create(NumberPercentage x, NumberPercentage y)
{
return adopt_ref(*new (nothrow) ScaleStyleValue(move(x), move(y)));
}
virtual ~ScaleStyleValue() override = default;
NumberPercentage const& x() const { return m_properties.x; }
NumberPercentage const& y() const { return m_properties.y; }
virtual String to_string(SerializationMode) const override;
bool properties_equal(ScaleStyleValue const& other) const { return m_properties == other.m_properties; }
private:
explicit ScaleStyleValue(
NumberPercentage x,
NumberPercentage y)
: StyleValueWithDefaultOperators(Type::Scale)
, m_properties {
.x = move(x),
.y = move(y),
}
{
}
struct Properties {
NumberPercentage x;
NumberPercentage y;
bool operator==(Properties const&) const = default;
} m_properties;
};
}

View file

@ -1,7 +1,7 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
@ -63,6 +63,31 @@ Transformation TransformationStyleValue::to_transformation() const
String TransformationStyleValue::to_string(SerializationMode mode) const
{
// https://drafts.csswg.org/css-transforms-2/#individual-transform-serialization
if (m_properties.property == PropertyID::Scale) {
auto resolve_to_string = [mode](CSSStyleValue const& value) -> String {
if (value.is_number()) {
return MUST(String::formatted("{}", value.as_number().number()));
}
if (value.is_percentage()) {
return MUST(String::formatted("{}", value.as_percentage().percentage().as_fraction()));
}
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 scaling
StringBuilder builder;
builder.append(x_value);
if (x_value != y_value) {
builder.append(" "sv);
builder.append(y_value);
}
return builder.to_string_without_validation();
}
StringBuilder builder;
builder.append(CSS::to_string(m_properties.transform_function));
builder.append('(');
@ -93,7 +118,9 @@ String TransformationStyleValue::to_string(SerializationMode mode) const
bool TransformationStyleValue::Properties::operator==(Properties const& other) const
{
return transform_function == other.transform_function && values.span() == other.values.span();
return property == other.property
&& transform_function == other.transform_function
&& values.span() == other.values.span();
}
}

View file

@ -16,9 +16,9 @@ namespace Web::CSS {
class TransformationStyleValue final : public StyleValueWithDefaultOperators<TransformationStyleValue> {
public:
static ValueComparingNonnullRefPtr<TransformationStyleValue> create(CSS::TransformFunction transform_function, StyleValueVector&& values)
static ValueComparingNonnullRefPtr<TransformationStyleValue> create(PropertyID property, TransformFunction transform_function, StyleValueVector&& values)
{
return adopt_ref(*new (nothrow) TransformationStyleValue(transform_function, move(values)));
return adopt_ref(*new (nothrow) TransformationStyleValue(property, transform_function, move(values)));
}
virtual ~TransformationStyleValue() override = default;
@ -32,14 +32,15 @@ public:
bool properties_equal(TransformationStyleValue const& other) const { return m_properties == other.m_properties; }
private:
TransformationStyleValue(CSS::TransformFunction transform_function, StyleValueVector&& values)
TransformationStyleValue(PropertyID property, TransformFunction transform_function, StyleValueVector&& values)
: StyleValueWithDefaultOperators(Type::Transformation)
, m_properties { .transform_function = transform_function, .values = move(values) }
, m_properties { .property = property, .transform_function = transform_function, .values = move(values) }
{
}
struct Properties {
CSS::TransformFunction transform_function;
PropertyID property;
TransformFunction transform_function;
StyleValueVector values;
bool operator==(Properties const& other) const;
} m_properties;