diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt index cfc4f56ec2e..13ff88b1195 100644 --- a/Libraries/LibWeb/CMakeLists.txt +++ b/Libraries/LibWeb/CMakeLists.txt @@ -159,7 +159,6 @@ set(SOURCES CSS/StyleValues/RadialGradientStyleValue.cpp CSS/StyleValues/RectStyleValue.cpp CSS/StyleValues/RotationStyleValue.cpp - CSS/StyleValues/ScaleStyleValue.cpp CSS/StyleValues/ShadowStyleValue.cpp CSS/StyleValues/ShorthandStyleValue.cpp CSS/StyleValues/StyleValueList.cpp diff --git a/Libraries/LibWeb/CSS/CSSStyleValue.cpp b/Libraries/LibWeb/CSS/CSSStyleValue.cpp index 4a6ba5ae82e..5e3e651911b 100644 --- a/Libraries/LibWeb/CSS/CSSStyleValue.cpp +++ b/Libraries/LibWeb/CSS/CSSStyleValue.cpp @@ -49,7 +49,6 @@ #include #include #include -#include #include #include #include @@ -303,12 +302,6 @@ RotationStyleValue const& CSSStyleValue::as_rotation() const return static_cast(*this); } -ScaleStyleValue const& CSSStyleValue::as_scale() const -{ - VERIFY(is_scale()); - return static_cast(*this); -} - ScrollbarGutterStyleValue const& CSSStyleValue::as_scrollbar_gutter() const { VERIFY(is_scrollbar_gutter()); diff --git a/Libraries/LibWeb/CSS/CSSStyleValue.h b/Libraries/LibWeb/CSS/CSSStyleValue.h index 4973d4503dd..dcc3cd94b72 100644 --- a/Libraries/LibWeb/CSS/CSSStyleValue.h +++ b/Libraries/LibWeb/CSS/CSSStyleValue.h @@ -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(const_cast(*this).as_rotation()); } - bool is_scale() const { return type() == Type::Scale; } - ScaleStyleValue const& as_scale() const; - ScaleStyleValue& as_scale() { return const_cast(const_cast(*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(const_cast(*this).as_scrollbar_gutter()); } diff --git a/Libraries/LibWeb/CSS/ComputedProperties.cpp b/Libraries/LibWeb/CSS/ComputedProperties.cpp index 462fdd85b19..e6a9026d489 100644 --- a/Libraries/LibWeb/CSS/ComputedProperties.cpp +++ b/Libraries/LibWeb/CSS/ComputedProperties.cpp @@ -1,6 +1,6 @@ /* * Copyright (c) 2018-2024, Andreas Kling - * Copyright (c) 2021-2024, Sam Atkins + * Copyright (c) 2021-2025, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -31,7 +31,6 @@ #include #include #include -#include #include #include #include @@ -611,18 +610,12 @@ Optional ComputedProperties::translate() const return CSS::Transformation(CSS::TransformFunction::Translate, move(values)); } -Optional ComputedProperties::scale() const +Optional 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 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 length_percentage_for_style_value(CSSStyleValue const& value) diff --git a/Libraries/LibWeb/CSS/Interpolation.cpp b/Libraries/LibWeb/CSS/Interpolation.cpp index c6122bf294e..d30084ce081 100644 --- a/Libraries/LibWeb/CSS/Interpolation.cpp +++ b/Libraries/LibWeb/CSS/Interpolation.cpp @@ -420,7 +420,7 @@ RefPtr interpolate_transform(DOM::Element& element, CSSStyl values.ensure_capacity(16); for (int i = 0; i < 16; i++) values.append(NumberStyleValue::create(static_cast(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) diff --git a/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Libraries/LibWeb/CSS/Parser/Parser.cpp index 356a3e1445f..fe4d687328a 100644 --- a/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -1,7 +1,7 @@ /* * Copyright (c) 2018-2024, Andreas Kling * Copyright (c) 2020-2021, the SerenityOS developers. - * Copyright (c) 2021-2024, Sam Atkins + * Copyright (c) 2021-2025, Sam Atkins * Copyright (c) 2021, Tobias Christiansen * Copyright (c) 2022, MacDue * Copyright (c) 2024, Shannon Booth @@ -70,7 +70,6 @@ #include #include #include -#include #include #include #include @@ -7426,7 +7425,7 @@ RefPtr Parser::parse_transform_value(TokenStream& 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 Parser::parse_scale_value(TokenStream& 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 Parser::parse_fit_content(Vector const& component_values) diff --git a/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp b/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp index 836017e3aa0..6a42fb4fc23 100644 --- a/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp +++ b/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp @@ -407,7 +407,7 @@ RefPtr 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 function. @@ -430,7 +430,7 @@ RefPtr 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)); } } diff --git a/Libraries/LibWeb/CSS/StyleValues/ScaleStyleValue.cpp b/Libraries/LibWeb/CSS/StyleValues/ScaleStyleValue.cpp deleted file mode 100644 index 41b4c71b0ce..00000000000 --- a/Libraries/LibWeb/CSS/StyleValues/ScaleStyleValue.cpp +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2024, Andreas Kling - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include -#include - -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(); -} - -} diff --git a/Libraries/LibWeb/CSS/StyleValues/ScaleStyleValue.h b/Libraries/LibWeb/CSS/StyleValues/ScaleStyleValue.h deleted file mode 100644 index e68f86c75e0..00000000000 --- a/Libraries/LibWeb/CSS/StyleValues/ScaleStyleValue.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2024, Andreas Kling - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include -#include - -namespace Web::CSS { - -class ScaleStyleValue : public StyleValueWithDefaultOperators { -public: - static ValueComparingNonnullRefPtr 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; -}; - -} diff --git a/Libraries/LibWeb/CSS/StyleValues/TransformationStyleValue.cpp b/Libraries/LibWeb/CSS/StyleValues/TransformationStyleValue.cpp index 0d50a8423a1..4ba6f52cf0e 100644 --- a/Libraries/LibWeb/CSS/StyleValues/TransformationStyleValue.cpp +++ b/Libraries/LibWeb/CSS/StyleValues/TransformationStyleValue.cpp @@ -1,7 +1,7 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2018-2024, Andreas Kling * Copyright (c) 2021, Tobias Christiansen - * Copyright (c) 2021-2023, Sam Atkins + * Copyright (c) 2021-2025, Sam Atkins * Copyright (c) 2022-2023, MacDue * * 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(); } } diff --git a/Libraries/LibWeb/CSS/StyleValues/TransformationStyleValue.h b/Libraries/LibWeb/CSS/StyleValues/TransformationStyleValue.h index 0fd438fab19..0dfdb199da4 100644 --- a/Libraries/LibWeb/CSS/StyleValues/TransformationStyleValue.h +++ b/Libraries/LibWeb/CSS/StyleValues/TransformationStyleValue.h @@ -16,9 +16,9 @@ namespace Web::CSS { class TransformationStyleValue final : public StyleValueWithDefaultOperators { public: - static ValueComparingNonnullRefPtr create(CSS::TransformFunction transform_function, StyleValueVector&& values) + static ValueComparingNonnullRefPtr 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; diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h index 950a6d93f83..6e70e645bae 100644 --- a/Libraries/LibWeb/Forward.h +++ b/Libraries/LibWeb/Forward.h @@ -207,7 +207,6 @@ class Resolution; class ResolutionOrCalculated; class ResolutionStyleValue; class RotationStyleValue; -class ScaleStyleValue; class Screen; class ScreenOrientation; class ScrollbarGutterStyleValue;