LibWeb: Replace FlexStyleValue with ShorthandStyleValue

We still need the custom parsing and to_string() logic, but nothing
else. :^)
This commit is contained in:
Sam Atkins 2023-09-19 16:26:23 +01:00 committed by Sam Atkins
parent a72788b889
commit aa45b3dfe3
Notes: sideshowbarker 2024-07-17 02:37:08 +09:00
11 changed files with 44 additions and 106 deletions

View file

@ -18,7 +18,6 @@ source_set("StyleValues") {
"EdgeStyleValue.cpp",
"FilterValueListStyleValue.cpp",
"FlexFlowStyleValue.cpp",
"FlexStyleValue.cpp",
"FontStyleValue.cpp",
"GridAreaShorthandStyleValue.cpp",
"GridAutoFlowStyleValue.cpp",

View file

@ -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

View file

@ -49,7 +49,6 @@
#include <LibWeb/CSS/StyleValues/EdgeStyleValue.h>
#include <LibWeb/CSS/StyleValues/FilterValueListStyleValue.h>
#include <LibWeb/CSS/StyleValues/FlexFlowStyleValue.h>
#include <LibWeb/CSS/StyleValues/FlexStyleValue.h>
#include <LibWeb/CSS/StyleValues/FontStyleValue.h>
#include <LibWeb/CSS/StyleValues/FrequencyStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridAreaShorthandStyleValue.h>
@ -3849,6 +3848,12 @@ RefPtr<StyleValue> Parser::parse_flex_value(Vector<ComponentValue> const& compon
{
auto tokens = TokenStream { component_values };
auto make_flex_shorthand = [&](NonnullRefPtr<StyleValue> flex_grow, NonnullRefPtr<StyleValue> flex_shrink, NonnullRefPtr<StyleValue> 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: <flex-grow> | <flex-basis> | none
auto properties = Array { PropertyID::FlexGrow, PropertyID::FlexBasis, PropertyID::Flex };
@ -3863,16 +3868,16 @@ RefPtr<StyleValue> Parser::parse_flex_value(Vector<ComponentValue> 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<StyleValue> Parser::parse_flex_value(Vector<ComponentValue> 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<StyleValue> Parser::parse_flex_flow_value(Vector<ComponentValue> const& component_values)
@ -6003,7 +6008,7 @@ Parser::ParseErrorOr<NonnullRefPtr<StyleValue>> 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<StyleValue> Parser::parse_css_value_for_property(PropertyID property_id, TokenStream<ComponentValue>& tokens)

View file

@ -40,7 +40,6 @@
#include <LibWeb/CSS/StyleValues/EasingStyleValue.h>
#include <LibWeb/CSS/StyleValues/FilterValueListStyleValue.h>
#include <LibWeb/CSS/StyleValues/FlexFlowStyleValue.h>
#include <LibWeb/CSS/StyleValues/FlexStyleValue.h>
#include <LibWeb/CSS/StyleValues/FontStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridAreaShorthandStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridTrackPlacementShorthandStyleValue.h>
@ -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);

View file

@ -28,7 +28,6 @@
#include <LibWeb/CSS/StyleValues/EdgeStyleValue.h>
#include <LibWeb/CSS/StyleValues/FilterValueListStyleValue.h>
#include <LibWeb/CSS/StyleValues/FlexFlowStyleValue.h>
#include <LibWeb/CSS/StyleValues/FlexStyleValue.h>
#include <LibWeb/CSS/StyleValues/FontStyleValue.h>
#include <LibWeb/CSS/StyleValues/FrequencyStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridAreaShorthandStyleValue.h>

View file

@ -99,7 +99,6 @@ using StyleValueVector = Vector<ValueComparingNonnullRefPtr<StyleValue const>>;
__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) \

View file

@ -1,19 +0,0 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
*
* 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()));
}
}

View file

@ -1,53 +0,0 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/CSS/StyleValue.h>
namespace Web::CSS {
class FlexStyleValue final : public StyleValueWithDefaultOperators<FlexStyleValue> {
public:
static ValueComparingNonnullRefPtr<FlexStyleValue> create(
ValueComparingNonnullRefPtr<StyleValue> grow,
ValueComparingNonnullRefPtr<StyleValue> shrink,
ValueComparingNonnullRefPtr<StyleValue> basis)
{
return adopt_ref(*new (nothrow) FlexStyleValue(move(grow), move(shrink), move(basis)));
}
virtual ~FlexStyleValue() override = default;
ValueComparingNonnullRefPtr<StyleValue> grow() const { return m_properties.grow; }
ValueComparingNonnullRefPtr<StyleValue> shrink() const { return m_properties.shrink; }
ValueComparingNonnullRefPtr<StyleValue> 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<StyleValue> grow,
ValueComparingNonnullRefPtr<StyleValue> shrink,
ValueComparingNonnullRefPtr<StyleValue> basis)
: StyleValueWithDefaultOperators(Type::Flex)
, m_properties { .grow = move(grow), .shrink = move(shrink), .basis = move(basis) }
{
}
struct Properties {
ValueComparingNonnullRefPtr<StyleValue> grow;
ValueComparingNonnullRefPtr<StyleValue> shrink;
ValueComparingNonnullRefPtr<StyleValue> basis;
bool operator==(Properties const&) const = default;
} m_properties;
};
}

View file

@ -5,13 +5,14 @@
*/
#include "ShorthandStyleValue.h"
#include <LibWeb/CSS/PropertyID.h>
#include <LibWeb/CSS/StyleValues/StyleValueList.h>
namespace Web::CSS {
ShorthandStyleValue::ShorthandStyleValue(Vector<PropertyID> sub_properties, Vector<ValueComparingNonnullRefPtr<StyleValue const>> values)
ShorthandStyleValue::ShorthandStyleValue(PropertyID shorthand, Vector<PropertyID> sub_properties, Vector<ValueComparingNonnullRefPtr<StyleValue const>> 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<PropertyID> sub_properties, Vect
ShorthandStyleValue::~ShorthandStyleValue() = default;
ValueComparingRefPtr<StyleValue const> 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());
}
}

View file

@ -12,23 +12,26 @@ namespace Web::CSS {
class ShorthandStyleValue final : public StyleValueWithDefaultOperators<ShorthandStyleValue> {
public:
static ValueComparingNonnullRefPtr<ShorthandStyleValue> create(Vector<PropertyID> sub_properties, Vector<ValueComparingNonnullRefPtr<StyleValue const>> values)
static ValueComparingNonnullRefPtr<ShorthandStyleValue> create(PropertyID shorthand, Vector<PropertyID> sub_properties, Vector<ValueComparingNonnullRefPtr<StyleValue const>> 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<PropertyID> const& sub_properties() const { return m_properties.sub_properties; }
Vector<ValueComparingNonnullRefPtr<StyleValue const>> const& values() const { return m_properties.values; }
ValueComparingRefPtr<StyleValue const> 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<PropertyID> sub_properties, Vector<ValueComparingNonnullRefPtr<StyleValue const>> values);
ShorthandStyleValue(PropertyID shorthand, Vector<PropertyID> sub_properties, Vector<ValueComparingNonnullRefPtr<StyleValue const>> values);
struct Properties {
PropertyID shorthand_property;
Vector<PropertyID> sub_properties;
Vector<ValueComparingNonnullRefPtr<StyleValue const>> values;
bool operator==(Properties const&) const = default;

View file

@ -105,7 +105,6 @@ class ElementInlineCSSStyleDeclaration;
class ExplicitGridTrack;
class FilterValueListStyleValue;
class FlexFlowStyleValue;
class FlexStyleValue;
class FontFace;
class FontStyleValue;
class Frequency;