1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-10 18:10:56 +09:00

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
parent bd5d1b092a
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

@ -49,7 +49,6 @@
#include <LibWeb/CSS/StyleValues/RectStyleValue.h>
#include <LibWeb/CSS/StyleValues/ResolutionStyleValue.h>
#include <LibWeb/CSS/StyleValues/RotationStyleValue.h>
#include <LibWeb/CSS/StyleValues/ScaleStyleValue.h>
#include <LibWeb/CSS/StyleValues/ScrollbarGutterStyleValue.h>
#include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
#include <LibWeb/CSS/StyleValues/ShorthandStyleValue.h>
@ -303,12 +302,6 @@ RotationStyleValue const& CSSStyleValue::as_rotation() const
return static_cast<RotationStyleValue const&>(*this);
}
ScaleStyleValue const& CSSStyleValue::as_scale() const
{
VERIFY(is_scale());
return static_cast<ScaleStyleValue const&>(*this);
}
ScrollbarGutterStyleValue const& CSSStyleValue::as_scrollbar_gutter() const
{
VERIFY(is_scrollbar_gutter());

View file

@ -128,7 +128,6 @@ public:
Rect,
Resolution,
Rotation,
Scale,
ScrollbarGutter,
Shadow,
Shorthand,
@ -303,10 +302,6 @@ public:
RotationStyleValue const& as_rotation() const;
RotationStyleValue& as_rotation() { return const_cast<RotationStyleValue&>(const_cast<CSSStyleValue const&>(*this).as_rotation()); }
bool is_scale() const { return type() == Type::Scale; }
ScaleStyleValue const& as_scale() const;
ScaleStyleValue& as_scale() { return const_cast<ScaleStyleValue&>(const_cast<CSSStyleValue const&>(*this).as_scale()); }
bool is_scrollbar_gutter() const { return type() == Type::ScrollbarGutter; }
ScrollbarGutterStyleValue const& as_scrollbar_gutter() const;
ScrollbarGutterStyleValue& as_scrollbar_gutter() { return const_cast<ScrollbarGutterStyleValue&>(const_cast<CSSStyleValue const&>(*this).as_scrollbar_gutter()); }

View file

@ -1,6 +1,6 @@
/*
* Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2021-2024, Sam Atkins <sam@ladybird.org>
* Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -31,7 +31,6 @@
#include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
#include <LibWeb/CSS/StyleValues/RectStyleValue.h>
#include <LibWeb/CSS/StyleValues/RotationStyleValue.h>
#include <LibWeb/CSS/StyleValues/ScaleStyleValue.h>
#include <LibWeb/CSS/StyleValues/ScrollbarGutterStyleValue.h>
#include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
#include <LibWeb/CSS/StyleValues/StringStyleValue.h>
@ -611,18 +610,12 @@ Optional<CSS::Transformation> ComputedProperties::translate() const
return CSS::Transformation(CSS::TransformFunction::Translate, move(values));
}
Optional<CSS::Transformation> ComputedProperties::scale() const
Optional<Transformation> ComputedProperties::scale() const
{
auto const& value = property(CSS::PropertyID::Scale);
if (!value.is_scale())
auto const& value = property(PropertyID::Scale);
if (!value.is_transformation())
return {};
auto const& scale = value.as_scale();
Vector<TransformValue> values;
values.append(scale.x());
values.append(scale.y());
return CSS::Transformation(CSS::TransformFunction::Scale, move(values));
return value.as_transformation().to_transformation();
}
static Optional<LengthPercentage> length_percentage_for_style_value(CSSStyleValue const& value)

View file

@ -420,7 +420,7 @@ RefPtr<CSSStyleValue const> interpolate_transform(DOM::Element& element, CSSStyl
values.ensure_capacity(16);
for (int i = 0; i < 16; i++)
values.append(NumberStyleValue::create(static_cast<double>(interpolated(i % 4, i / 4))));
return StyleValueList::create({ TransformationStyleValue::create(TransformFunction::Matrix3d, move(values)) }, StyleValueList::Separator::Comma);
return StyleValueList::create({ TransformationStyleValue::create(PropertyID::Transform, TransformFunction::Matrix3d, move(values)) }, StyleValueList::Separator::Comma);
}
Color interpolate_color(Color from, Color to, float delta)

View file

@ -1,7 +1,7 @@
/*
* Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2020-2021, the SerenityOS developers.
* Copyright (c) 2021-2024, Sam Atkins <sam@ladybird.org>
* Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
* Copyright (c) 2022, MacDue <macdue@dueutil.tech>
* Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
@ -70,7 +70,6 @@
#include <LibWeb/CSS/StyleValues/RectStyleValue.h>
#include <LibWeb/CSS/StyleValues/ResolutionStyleValue.h>
#include <LibWeb/CSS/StyleValues/RotationStyleValue.h>
#include <LibWeb/CSS/StyleValues/ScaleStyleValue.h>
#include <LibWeb/CSS/StyleValues/ScrollbarGutterStyleValue.h>
#include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
#include <LibWeb/CSS/StyleValues/ShorthandStyleValue.h>
@ -7426,7 +7425,7 @@ RefPtr<CSSStyleValue> Parser::parse_transform_value(TokenStream<ComponentValue>&
return nullptr;
}
transformations.append(TransformationStyleValue::create(function, move(values)));
transformations.append(TransformationStyleValue::create(PropertyID::Transform, function, move(values)));
}
transaction.commit();
return StyleValueList::create(move(transformations), StyleValueList::Separator::Space);
@ -7699,21 +7698,21 @@ RefPtr<CSSStyleValue> Parser::parse_scale_value(TokenStream<ComponentValue>& tok
auto transaction = tokens.begin_transaction();
auto maybe_x = parse_number_percentage(tokens);
if (!maybe_x.has_value())
auto maybe_x = parse_number_percentage_value(tokens);
if (!maybe_x)
return nullptr;
if (!tokens.has_next_token()) {
transaction.commit();
return ScaleStyleValue::create(maybe_x.value(), maybe_x.value());
return TransformationStyleValue::create(PropertyID::Scale, TransformFunction::Scale, { *maybe_x, *maybe_x });
}
auto maybe_y = parse_number_percentage(tokens);
if (!maybe_y.has_value())
auto maybe_y = parse_number_percentage_value(tokens);
if (!maybe_y)
return nullptr;
transaction.commit();
return ScaleStyleValue::create(maybe_x.release_value(), maybe_y.release_value());
return TransformationStyleValue::create(PropertyID::Scale, TransformFunction::Scale, { maybe_x.release_nonnull(), maybe_y.release_nonnull() });
}
Optional<CSS::GridFitContent> Parser::parse_fit_content(Vector<ComponentValue> const& component_values)

View file

@ -407,7 +407,7 @@ RefPtr<CSSStyleValue const> ResolvedCSSStyleDeclaration::style_value_for_propert
NumberStyleValue::create(transform.elements()[0][3]),
NumberStyleValue::create(transform.elements()[1][3]),
};
return TransformationStyleValue::create(TransformFunction::Matrix, move(parameters));
return TransformationStyleValue::create(PropertyID::Transform, TransformFunction::Matrix, move(parameters));
}
// -> Otherwise
// Serialize transform to a <matrix3d()> function.
@ -430,7 +430,7 @@ RefPtr<CSSStyleValue const> ResolvedCSSStyleDeclaration::style_value_for_propert
NumberStyleValue::create(transform.elements()[2][3]),
NumberStyleValue::create(transform.elements()[3][3]),
};
return TransformationStyleValue::create(TransformFunction::Matrix3d, move(parameters));
return TransformationStyleValue::create(PropertyID::Transform, TransformFunction::Matrix3d, move(parameters));
}
}

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;