ladybird/Libraries/LibWeb/CSS/StyleValues/StyleValueList.cpp
Sam Atkins 5aba457009 LibWeb/CSS: Add tokenize() method to style values, to get a list of CVs
A couple of arbitrary substitution functions require us to get or
produce some style value, and then substitute its ComponentValues into
the original ComponentValue list. So this commit gives CSSStyleValue a
tokenize() method that does so.

Apart from a couple of unusual cases like the guaranteed-invalid value,
style values can all be converted into ComponentValues by serializing
them as a string, and then parsing that as a list of component values.
That feels unnecessarily inefficient in most cases though, so I've
implemented faster overrides for a lot of the basic style value
classes, but left that serialize-and-reparse method as the fallback.
2025-07-16 14:47:45 +01:00

68 lines
2 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
* Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "StyleValueList.h"
#include <LibWeb/CSS/Parser/ComponentValue.h>
namespace Web::CSS {
bool StyleValueList::Properties::operator==(Properties const& other) const
{
return separator == other.separator && values.span() == other.values.span();
}
String StyleValueList::to_string(SerializationMode mode) const
{
auto separator = ""sv;
switch (m_properties.separator) {
case Separator::Space:
separator = " "sv;
break;
case Separator::Comma:
separator = ", "sv;
break;
default:
VERIFY_NOT_REACHED();
}
StringBuilder builder;
for (size_t i = 0; i < m_properties.values.size(); ++i) {
builder.append(m_properties.values[i]->to_string(mode));
if (i != m_properties.values.size() - 1)
builder.append(separator);
}
return MUST(builder.to_string());
}
void StyleValueList::set_style_sheet(GC::Ptr<CSSStyleSheet> style_sheet)
{
Base::set_style_sheet(style_sheet);
for (auto& value : m_properties.values)
const_cast<CSSStyleValue&>(*value).set_style_sheet(style_sheet);
}
Vector<Parser::ComponentValue> StyleValueList::tokenize() const
{
Vector<Parser::ComponentValue> component_values;
bool first = true;
for (auto const& value : m_properties.values) {
if (first) {
first = false;
} else {
if (m_properties.separator == Separator::Comma)
component_values.empend(Parser::Token::create(Parser::Token::Type::Comma));
component_values.empend(Parser::Token::create_whitespace(" "_string));
}
component_values.extend(value->tokenize());
}
return component_values;
}
}