LibWeb: Add CSS ValueListStyleValue

As the new CSS parser tokenizes its input, we can no longer easily
rely on a StringStyleValue for multi-value properties. (eg, border)
ValueListStyleValue lets us wrap all of the ComponentValues that
the Parser produced for one declaration, as a single StyleValue, to
then be parsed into StyleValues by the StyleResolver.

Originally, I wanted it to be a list of StyleValues, but several
properties use syntax that makes use of non-StyleValue tokens, eg:
```css
/* Syntax using a / */
font: 12px/14px sans-serif;
/* Multiple values separated by commas */
background: url(catdog.png), url(another-image.jpg), blue;
```
Passing the ComponentValue tokens themselves means that all that
information is carried over. The alternative might be to create a
StyleValue subclass for each property and parse them fully inside
the Parser. (eg, `FontStyleValue`)

I decided against `ListStyleValue` as a name, to avoid confusion
with list styles. It's not ideal, but names are hard.
This commit is contained in:
Sam Atkins 2021-07-13 16:50:58 +01:00 committed by Andreas Kling
parent 8c2be4b3dc
commit 4a03279539
Notes: sideshowbarker 2024-07-18 08:31:40 +09:00
2 changed files with 42 additions and 0 deletions

View File

@ -1,11 +1,14 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Sam Atkins <atkinssj@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/ByteBuffer.h>
#include <AK/Vector.h>
#include <LibGfx/Palette.h>
#include <LibWeb/CSS/Parser/StyleComponentValueRule.h>
#include <LibWeb/CSS/StyleValue.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/InProcessWebView.h>
@ -169,4 +172,23 @@ void ImageStyleValue::resource_did_load()
m_document->browsing_context()->set_needs_display({});
}
ValueListStyleValue::ValueListStyleValue(Vector<StyleComponentValueRule>&& values)
: StyleValue(Type::ValueList)
, m_values(move(values))
{
}
String ValueListStyleValue::to_string() const
{
StringBuilder builder;
builder.appendff("List[{}](", m_values.size());
for (auto& value : m_values) {
builder.append(value.to_debug_string());
builder.append(",");
}
builder.append(")");
return builder.to_string();
}
}

View File

@ -1,12 +1,14 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Tobias Christiansen <tobi@tobyase.de>
* Copyright (c) 2021, Sam Atkins <atkinssj@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/NonnullRefPtrVector.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/String.h>
@ -16,6 +18,7 @@
#include <LibGfx/Bitmap.h>
#include <LibGfx/Color.h>
#include <LibWeb/CSS/Length.h>
#include <LibWeb/CSS/Parser/StyleComponentValueRule.h>
#include <LibWeb/CSS/PropertyID.h>
#include <LibWeb/CSS/ValueID.h>
#include <LibWeb/Forward.h>
@ -219,6 +222,7 @@ public:
Position,
CustomProperty,
Numeric,
ValueList,
};
Type type() const { return m_type; }
@ -233,6 +237,7 @@ public:
bool is_position() const { return type() == Type::Position; }
bool is_custom_property() const { return type() == Type::CustomProperty; }
bool is_numeric() const { return type() == Type::Numeric; }
bool is_value_list() const { return type() == Type::ValueList; }
virtual String to_string() const = 0;
virtual Length to_length() const { return Length::make_auto(); }
@ -460,6 +465,21 @@ private:
RefPtr<Gfx::Bitmap> m_bitmap;
};
class ValueListStyleValue final : public StyleValue {
public:
static NonnullRefPtr<ValueListStyleValue> create(Vector<StyleComponentValueRule>&& values) { return adopt_ref(*new ValueListStyleValue(move(values))); }
virtual ~ValueListStyleValue() override { }
virtual String to_string() const override;
Vector<StyleComponentValueRule> const& values() const { return m_values; }
private:
ValueListStyleValue(Vector<StyleComponentValueRule>&&);
Vector<StyleComponentValueRule> m_values;
};
inline CSS::ValueID StyleValue::to_identifier() const
{
if (is_identifier())