1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 05:37:35 +00:00

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
11 changed files with 44 additions and 106 deletions

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;