1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 07:57:46 +00:00

LibWeb: Replace BackgroundStyleValue with ShorthandStyleValue

The `to_string()` for this is modified a little from the original,
because we have to calculate what the layer-count is then, instead of
having it already calculated.
This commit is contained in:
Sam Atkins 2023-09-19 15:27:13 +01:00 committed by Sam Atkins
parent 1ae515c0b7
commit 34e0899ab0
11 changed files with 57 additions and 194 deletions

View file

@ -1,80 +0,0 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "BackgroundStyleValue.h"
#include <LibWeb/CSS/StyleValues/StyleValueList.h>
namespace Web::CSS {
BackgroundStyleValue::BackgroundStyleValue(
ValueComparingNonnullRefPtr<StyleValue const> color,
ValueComparingNonnullRefPtr<StyleValue const> image,
ValueComparingNonnullRefPtr<StyleValue const> position,
ValueComparingNonnullRefPtr<StyleValue const> size,
ValueComparingNonnullRefPtr<StyleValue const> repeat,
ValueComparingNonnullRefPtr<StyleValue const> attachment,
ValueComparingNonnullRefPtr<StyleValue const> origin,
ValueComparingNonnullRefPtr<StyleValue const> clip)
: StyleValueWithDefaultOperators(Type::Background)
, m_properties {
.color = move(color),
.image = move(image),
.position = move(position),
.size = move(size),
.repeat = move(repeat),
.attachment = move(attachment),
.origin = move(origin),
.clip = move(clip),
.layer_count = 0
}
{
auto layer_count = [](auto style_value) -> size_t {
if (style_value->is_value_list())
return style_value->as_value_list().size();
else
return 1;
};
m_properties.layer_count = max(layer_count(m_properties.image), layer_count(m_properties.position));
m_properties.layer_count = max(m_properties.layer_count, layer_count(m_properties.size));
m_properties.layer_count = max(m_properties.layer_count, layer_count(m_properties.repeat));
m_properties.layer_count = max(m_properties.layer_count, layer_count(m_properties.attachment));
m_properties.layer_count = max(m_properties.layer_count, layer_count(m_properties.origin));
m_properties.layer_count = max(m_properties.layer_count, layer_count(m_properties.clip));
VERIFY(!m_properties.color->is_value_list());
}
BackgroundStyleValue::~BackgroundStyleValue() = default;
String BackgroundStyleValue::to_string() const
{
if (m_properties.layer_count == 1) {
return MUST(String::formatted("{} {} {} {} {} {} {} {}", m_properties.color->to_string(), m_properties.image->to_string(), m_properties.position->to_string(), m_properties.size->to_string(), m_properties.repeat->to_string(), m_properties.attachment->to_string(), m_properties.origin->to_string(), m_properties.clip->to_string()));
}
auto get_layer_value_string = [](ValueComparingNonnullRefPtr<StyleValue const> const& style_value, size_t index) {
if (style_value->is_value_list())
return style_value->as_value_list().value_at(index, true)->to_string();
return style_value->to_string();
};
StringBuilder builder;
for (size_t i = 0; i < m_properties.layer_count; i++) {
if (i)
builder.append(", "sv);
if (i == m_properties.layer_count - 1)
builder.appendff("{} ", m_properties.color->to_string());
builder.appendff("{} {} {} {} {} {} {}", get_layer_value_string(m_properties.image, i), get_layer_value_string(m_properties.position, i), get_layer_value_string(m_properties.size, i), get_layer_value_string(m_properties.repeat, i), get_layer_value_string(m_properties.attachment, i), get_layer_value_string(m_properties.origin, i), get_layer_value_string(m_properties.clip, i));
}
return MUST(builder.to_string());
}
}

View file

@ -1,72 +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 BackgroundStyleValue final : public StyleValueWithDefaultOperators<BackgroundStyleValue> {
public:
static ValueComparingNonnullRefPtr<BackgroundStyleValue> create(
ValueComparingNonnullRefPtr<StyleValue const> color,
ValueComparingNonnullRefPtr<StyleValue const> image,
ValueComparingNonnullRefPtr<StyleValue const> position,
ValueComparingNonnullRefPtr<StyleValue const> size,
ValueComparingNonnullRefPtr<StyleValue const> repeat,
ValueComparingNonnullRefPtr<StyleValue const> attachment,
ValueComparingNonnullRefPtr<StyleValue const> origin,
ValueComparingNonnullRefPtr<StyleValue const> clip)
{
return adopt_ref(*new (nothrow) BackgroundStyleValue(move(color), move(image), move(position), move(size), move(repeat), move(attachment), move(origin), move(clip)));
}
virtual ~BackgroundStyleValue() override;
size_t layer_count() const { return m_properties.layer_count; }
auto attachment() const { return m_properties.attachment; }
auto clip() const { return m_properties.clip; }
auto color() const { return m_properties.color; }
auto image() const { return m_properties.image; }
auto origin() const { return m_properties.origin; }
auto position() const { return m_properties.position; }
auto repeat() const { return m_properties.repeat; }
auto size() const { return m_properties.size; }
virtual String to_string() const override;
bool properties_equal(BackgroundStyleValue const& other) const { return m_properties == other.m_properties; }
private:
BackgroundStyleValue(
ValueComparingNonnullRefPtr<StyleValue const> color,
ValueComparingNonnullRefPtr<StyleValue const> image,
ValueComparingNonnullRefPtr<StyleValue const> position,
ValueComparingNonnullRefPtr<StyleValue const> size,
ValueComparingNonnullRefPtr<StyleValue const> repeat,
ValueComparingNonnullRefPtr<StyleValue const> attachment,
ValueComparingNonnullRefPtr<StyleValue const> origin,
ValueComparingNonnullRefPtr<StyleValue const> clip);
struct Properties {
ValueComparingNonnullRefPtr<StyleValue const> color;
ValueComparingNonnullRefPtr<StyleValue const> image;
ValueComparingNonnullRefPtr<StyleValue const> position;
ValueComparingNonnullRefPtr<StyleValue const> size;
ValueComparingNonnullRefPtr<StyleValue const> repeat;
ValueComparingNonnullRefPtr<StyleValue const> attachment;
ValueComparingNonnullRefPtr<StyleValue const> origin;
ValueComparingNonnullRefPtr<StyleValue const> clip;
size_t layer_count;
bool operator==(Properties const&) const = default;
} m_properties;
};
}

View file

@ -35,6 +35,43 @@ String ShorthandStyleValue::to_string() const
{
// Special-cases first
switch (m_properties.shorthand_property) {
case PropertyID::Background: {
auto color = longhand(PropertyID::BackgroundColor);
auto image = longhand(PropertyID::BackgroundImage);
auto position = longhand(PropertyID::BackgroundPosition);
auto size = longhand(PropertyID::BackgroundSize);
auto repeat = longhand(PropertyID::BackgroundRepeat);
auto attachment = longhand(PropertyID::BackgroundAttachment);
auto origin = longhand(PropertyID::BackgroundOrigin);
auto clip = longhand(PropertyID::BackgroundClip);
auto get_layer_count = [](auto style_value) -> size_t {
return style_value->is_value_list() ? style_value->as_value_list().size() : 1;
};
auto layer_count = max(get_layer_count(image), max(get_layer_count(position), max(get_layer_count(size), max(get_layer_count(repeat), max(get_layer_count(attachment), max(get_layer_count(origin), get_layer_count(clip)))))));
if (layer_count == 1) {
return MUST(String::formatted("{} {} {} {} {} {} {} {}", color->to_string(), image->to_string(), position->to_string(), size->to_string(), repeat->to_string(), attachment->to_string(), origin->to_string(), clip->to_string()));
}
auto get_layer_value_string = [](ValueComparingRefPtr<StyleValue const> const& style_value, size_t index) {
if (style_value->is_value_list())
return style_value->as_value_list().value_at(index, true)->to_string();
return style_value->to_string();
};
StringBuilder builder;
for (size_t i = 0; i < layer_count; i++) {
if (i)
builder.append(", "sv);
if (i == layer_count - 1)
builder.appendff("{} ", color->to_string());
builder.appendff("{} {} {} {} {} {} {}", get_layer_value_string(image, i), get_layer_value_string(position, i), get_layer_value_string(size, i), get_layer_value_string(repeat, i), get_layer_value_string(attachment, i), get_layer_value_string(origin, i), get_layer_value_string(clip, i));
}
return MUST(builder.to_string());
}
case PropertyID::Flex:
return MUST(String::formatted("{} {} {}", longhand(PropertyID::FlexGrow)->to_string(), longhand(PropertyID::FlexShrink)->to_string(), longhand(PropertyID::FlexBasis)->to_string()));
case PropertyID::FlexFlow: