From aa45b3dfe3893ee93760109b4ccb5dbc5a8035ff Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Tue, 19 Sep 2023 16:26:23 +0100 Subject: [PATCH] LibWeb: Replace FlexStyleValue with ShorthandStyleValue We still need the custom parsing and to_string() logic, but nothing else. :^) --- .../Libraries/LibWeb/CSS/StyleValues/BUILD.gn | 1 - Userland/Libraries/LibWeb/CMakeLists.txt | 1 - .../Libraries/LibWeb/CSS/Parser/Parser.cpp | 17 +++--- .../Libraries/LibWeb/CSS/StyleComputer.cpp | 9 ---- Userland/Libraries/LibWeb/CSS/StyleValue.cpp | 1 - Userland/Libraries/LibWeb/CSS/StyleValue.h | 1 - .../LibWeb/CSS/StyleValues/FlexStyleValue.cpp | 19 ------- .../LibWeb/CSS/StyleValues/FlexStyleValue.h | 53 ------------------- .../CSS/StyleValues/ShorthandStyleValue.cpp | 38 +++++++++---- .../CSS/StyleValues/ShorthandStyleValue.h | 9 ++-- Userland/Libraries/LibWeb/Forward.h | 1 - 11 files changed, 44 insertions(+), 106 deletions(-) delete mode 100644 Userland/Libraries/LibWeb/CSS/StyleValues/FlexStyleValue.cpp delete mode 100644 Userland/Libraries/LibWeb/CSS/StyleValues/FlexStyleValue.h diff --git a/Meta/gn/secondary/Userland/Libraries/LibWeb/CSS/StyleValues/BUILD.gn b/Meta/gn/secondary/Userland/Libraries/LibWeb/CSS/StyleValues/BUILD.gn index fd58c37872..1fe986b013 100644 --- a/Meta/gn/secondary/Userland/Libraries/LibWeb/CSS/StyleValues/BUILD.gn +++ b/Meta/gn/secondary/Userland/Libraries/LibWeb/CSS/StyleValues/BUILD.gn @@ -18,7 +18,6 @@ source_set("StyleValues") { "EdgeStyleValue.cpp", "FilterValueListStyleValue.cpp", "FlexFlowStyleValue.cpp", - "FlexStyleValue.cpp", "FontStyleValue.cpp", "GridAreaShorthandStyleValue.cpp", "GridAutoFlowStyleValue.cpp", diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 84a7bf2c8d..ed014236da 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -95,7 +95,6 @@ set(SOURCES CSS/StyleValues/EdgeStyleValue.cpp CSS/StyleValues/FilterValueListStyleValue.cpp CSS/StyleValues/FlexFlowStyleValue.cpp - CSS/StyleValues/FlexStyleValue.cpp CSS/StyleValues/FontStyleValue.cpp CSS/StyleValues/GridAreaShorthandStyleValue.cpp CSS/StyleValues/GridAutoFlowStyleValue.cpp diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index 2edcdd9bd0..4d83856659 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -49,7 +49,6 @@ #include #include #include -#include #include #include #include @@ -3849,6 +3848,12 @@ RefPtr Parser::parse_flex_value(Vector const& compon { auto tokens = TokenStream { component_values }; + auto make_flex_shorthand = [&](NonnullRefPtr flex_grow, NonnullRefPtr flex_shrink, NonnullRefPtr flex_basis) { + return ShorthandStyleValue::create(PropertyID::Flex, + { PropertyID::FlexGrow, PropertyID::FlexShrink, PropertyID::FlexBasis }, + { move(flex_grow), move(flex_shrink), move(flex_basis) }); + }; + if (component_values.size() == 1) { // One-value syntax: | | none auto properties = Array { PropertyID::FlexGrow, PropertyID::FlexBasis, PropertyID::Flex }; @@ -3863,16 +3868,16 @@ RefPtr Parser::parse_flex_value(Vector const& compon // https://github.com/w3c/csswg-drafts/issues/5742 auto flex_basis = PercentageStyleValue::create(Percentage(0)); auto one = NumberStyleValue::create(1); - return FlexStyleValue::create(*value, one, flex_basis); + return make_flex_shorthand(*value, one, flex_basis); } case PropertyID::FlexBasis: { auto one = NumberStyleValue::create(1); - return FlexStyleValue::create(one, one, *value); + return make_flex_shorthand(one, one, *value); } case PropertyID::Flex: { if (value->is_identifier() && value->to_identifier() == ValueID::None) { auto zero = NumberStyleValue::create(0); - return FlexStyleValue::create(zero, zero, IdentifierStyleValue::create(ValueID::Auto)); + return make_flex_shorthand(zero, zero, IdentifierStyleValue::create(ValueID::Auto)); } break; } @@ -3929,7 +3934,7 @@ RefPtr Parser::parse_flex_value(Vector const& compon flex_basis = PercentageStyleValue::create(Percentage(0)); } - return FlexStyleValue::create(flex_grow.release_nonnull(), flex_shrink.release_nonnull(), flex_basis.release_nonnull()); + return make_flex_shorthand(flex_grow.release_nonnull(), flex_shrink.release_nonnull(), flex_basis.release_nonnull()); } RefPtr Parser::parse_flex_flow_value(Vector const& component_values) @@ -6003,7 +6008,7 @@ Parser::ParseErrorOr> Parser::parse_css_value(Property longhand_values.unchecked_append(StyleValueList::create(move(it.value), StyleValueList::Separator::Space)); } - return { ShorthandStyleValue::create(move(longhand_properties), move(longhand_values)) }; + return { ShorthandStyleValue::create(property_id, move(longhand_properties), move(longhand_values)) }; } RefPtr Parser::parse_css_value_for_property(PropertyID property_id, TokenStream& tokens) diff --git a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp index 92d679204e..ba63c92989 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp @@ -40,7 +40,6 @@ #include #include #include -#include #include #include #include @@ -800,14 +799,6 @@ static void set_property_expanding_shorthands(StyleProperties& style, CSS::Prope } if (property_id == CSS::PropertyID::Flex) { - if (value.is_flex()) { - auto const& flex = value.as_flex(); - set_longhand_property(CSS::PropertyID::FlexGrow, flex.grow()); - set_longhand_property(CSS::PropertyID::FlexShrink, flex.shrink()); - set_longhand_property(CSS::PropertyID::FlexBasis, flex.basis()); - return; - } - set_longhand_property(CSS::PropertyID::FlexGrow, value); set_longhand_property(CSS::PropertyID::FlexShrink, value); set_longhand_property(CSS::PropertyID::FlexBasis, value); diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp index 6d7e31f3b5..4cd5532de7 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp @@ -28,7 +28,6 @@ #include #include #include -#include #include #include #include diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValue.h index 9cad015525..294032a953 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.h @@ -99,7 +99,6 @@ using StyleValueVector = Vector>; __ENUMERATE_STYLE_VALUE_TYPE(Easing, easing) \ __ENUMERATE_STYLE_VALUE_TYPE(Edge, edge) \ __ENUMERATE_STYLE_VALUE_TYPE(FilterValueList, filter_value_list) \ - __ENUMERATE_STYLE_VALUE_TYPE(Flex, flex) \ __ENUMERATE_STYLE_VALUE_TYPE(FlexFlow, flex_flow) \ __ENUMERATE_STYLE_VALUE_TYPE(Font, font) \ __ENUMERATE_STYLE_VALUE_TYPE(Frequency, frequency) \ diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/FlexStyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValues/FlexStyleValue.cpp deleted file mode 100644 index 8e9e5ab1a3..0000000000 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/FlexStyleValue.cpp +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling - * Copyright (c) 2021, Tobias Christiansen - * Copyright (c) 2021-2023, Sam Atkins - * Copyright (c) 2022-2023, MacDue - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include "FlexStyleValue.h" - -namespace Web::CSS { - -String FlexStyleValue::to_string() const -{ - return MUST(String::formatted("{} {} {}", m_properties.grow->to_string(), m_properties.shrink->to_string(), m_properties.basis->to_string())); -} - -} diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/FlexStyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValues/FlexStyleValue.h deleted file mode 100644 index 20350015a8..0000000000 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/FlexStyleValue.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling - * Copyright (c) 2021, Tobias Christiansen - * Copyright (c) 2021-2023, Sam Atkins - * Copyright (c) 2022-2023, MacDue - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include - -namespace Web::CSS { - -class FlexStyleValue final : public StyleValueWithDefaultOperators { -public: - static ValueComparingNonnullRefPtr create( - ValueComparingNonnullRefPtr grow, - ValueComparingNonnullRefPtr shrink, - ValueComparingNonnullRefPtr basis) - { - return adopt_ref(*new (nothrow) FlexStyleValue(move(grow), move(shrink), move(basis))); - } - virtual ~FlexStyleValue() override = default; - - ValueComparingNonnullRefPtr grow() const { return m_properties.grow; } - ValueComparingNonnullRefPtr shrink() const { return m_properties.shrink; } - ValueComparingNonnullRefPtr basis() const { return m_properties.basis; } - - virtual String to_string() const override; - - bool properties_equal(FlexStyleValue const& other) const { return m_properties == other.m_properties; } - -private: - FlexStyleValue( - ValueComparingNonnullRefPtr grow, - ValueComparingNonnullRefPtr shrink, - ValueComparingNonnullRefPtr basis) - : StyleValueWithDefaultOperators(Type::Flex) - , m_properties { .grow = move(grow), .shrink = move(shrink), .basis = move(basis) } - { - } - - struct Properties { - ValueComparingNonnullRefPtr grow; - ValueComparingNonnullRefPtr shrink; - ValueComparingNonnullRefPtr basis; - bool operator==(Properties const&) const = default; - } m_properties; -}; - -} diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/ShorthandStyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValues/ShorthandStyleValue.cpp index 9d2a4706d2..d715dac2e2 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/ShorthandStyleValue.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/ShorthandStyleValue.cpp @@ -5,13 +5,14 @@ */ #include "ShorthandStyleValue.h" +#include #include namespace Web::CSS { -ShorthandStyleValue::ShorthandStyleValue(Vector sub_properties, Vector> values) +ShorthandStyleValue::ShorthandStyleValue(PropertyID shorthand, Vector sub_properties, Vector> values) : StyleValueWithDefaultOperators(Type::Shorthand) - , m_properties { move(sub_properties), move(values) } + , m_properties { shorthand, move(sub_properties), move(values) } { if (m_properties.sub_properties.size() != m_properties.values.size()) { dbgln("ShorthandStyleValue: sub_properties and values must be the same size! {} != {}", m_properties.sub_properties.size(), m_properties.values.size()); @@ -21,18 +22,33 @@ ShorthandStyleValue::ShorthandStyleValue(Vector sub_properties, Vect ShorthandStyleValue::~ShorthandStyleValue() = default; +ValueComparingRefPtr ShorthandStyleValue::longhand(PropertyID longhand) const +{ + for (auto i = 0u; i < m_properties.sub_properties.size(); ++i) { + if (m_properties.sub_properties[i] == longhand) + return m_properties.values[i]; + } + return nullptr; +} + String ShorthandStyleValue::to_string() const { - StringBuilder builder; - auto first = true; - for (auto& value : m_properties.values) { - if (first) - first = false; - else - builder.append(' '); - builder.append(value->to_string()); + // Special-cases first + switch (m_properties.shorthand_property) { + case PropertyID::Flex: + return MUST(String::formatted("{} {} {}", longhand(PropertyID::FlexGrow)->to_string(), longhand(PropertyID::FlexShrink)->to_string(), longhand(PropertyID::FlexBasis)->to_string())); + default: + StringBuilder builder; + auto first = true; + for (auto& value : m_properties.values) { + if (first) + first = false; + else + builder.append(' '); + builder.append(value->to_string()); + } + return MUST(builder.to_string()); } - return MUST(builder.to_string()); } } diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/ShorthandStyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValues/ShorthandStyleValue.h index 37360cd7e9..85ee9fa223 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/ShorthandStyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/ShorthandStyleValue.h @@ -12,23 +12,26 @@ namespace Web::CSS { class ShorthandStyleValue final : public StyleValueWithDefaultOperators { public: - static ValueComparingNonnullRefPtr create(Vector sub_properties, Vector> values) + static ValueComparingNonnullRefPtr create(PropertyID shorthand, Vector sub_properties, Vector> values) { - return adopt_ref(*new ShorthandStyleValue(move(sub_properties), move(values))); + return adopt_ref(*new ShorthandStyleValue(shorthand, move(sub_properties), move(values))); } virtual ~ShorthandStyleValue() override; Vector const& sub_properties() const { return m_properties.sub_properties; } Vector> const& values() const { return m_properties.values; } + ValueComparingRefPtr longhand(PropertyID) const; + virtual String to_string() const override; bool properties_equal(ShorthandStyleValue const& other) const { return m_properties == other.m_properties; } private: - ShorthandStyleValue(Vector sub_properties, Vector> values); + ShorthandStyleValue(PropertyID shorthand, Vector sub_properties, Vector> values); struct Properties { + PropertyID shorthand_property; Vector sub_properties; Vector> values; bool operator==(Properties const&) const = default; diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index ec692ec080..4e3e098ae7 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -105,7 +105,6 @@ class ElementInlineCSSStyleDeclaration; class ExplicitGridTrack; class FilterValueListStyleValue; class FlexFlowStyleValue; -class FlexStyleValue; class FontFace; class FontStyleValue; class Frequency;