LibWeb/CSS: Make StringStyleValue hold a FlyString

We already have a FlyString of its value from parsing, and most users
also want a FlyString from it, so let's use that instead of converting
backwards and forwards.

The two users that did want a String are:
- Quotes, which make sense as FlyString instead, so I've converted that.
- Animation names, which should probably be FlyString too, but the code
  currently also allows for other kinds of StyleValue, and I don't want
  to dive into this right now to figure out if that's needed or not.
This commit is contained in:
Sam Atkins 2024-07-17 12:42:12 +01:00 committed by Tim Ledbetter
commit 9fb44cb057
Notes: sideshowbarker 2024-07-18 02:44:48 +09:00
5 changed files with 15 additions and 14 deletions

View file

@ -6,6 +6,7 @@
#pragma once #pragma once
#include <AK/FlyString.h>
#include <AK/Optional.h> #include <AK/Optional.h>
#include <LibGfx/FontCascadeList.h> #include <LibGfx/FontCascadeList.h>
#include <LibGfx/ScalingMode.h> #include <LibGfx/ScalingMode.h>
@ -49,7 +50,7 @@ struct QuotesData {
Auto, Auto,
Specified, Specified,
} type; } type;
Vector<Array<String, 2>> strings {}; Vector<Array<FlyString, 2>> strings {};
}; };
struct ResolvedBackdropFilter { struct ResolvedBackdropFilter {

View file

@ -2912,7 +2912,7 @@ RefPtr<StyleValue> Parser::parse_string_value(TokenStream<ComponentValue>& token
auto peek = tokens.peek_token(); auto peek = tokens.peek_token();
if (peek.is(Token::Type::String)) { if (peek.is(Token::Type::String)) {
(void)tokens.next_token(); (void)tokens.next_token();
return StringStyleValue::create(peek.token().string().to_string()); return StringStyleValue::create(peek.token().string());
} }
return nullptr; return nullptr;
@ -4761,7 +4761,7 @@ RefPtr<StyleValue> Parser::parse_font_family_value(TokenStream<ComponentValue>&
(void)tokens.next_token(); // String (void)tokens.next_token(); // String
if (!next_is_comma_or_eof()) if (!next_is_comma_or_eof())
return nullptr; return nullptr;
font_families.append(StringStyleValue::create(peek.token().string().to_string())); font_families.append(StringStyleValue::create(peek.token().string()));
(void)tokens.next_token(); // Comma (void)tokens.next_token(); // Comma
continue; continue;
} }
@ -7088,7 +7088,7 @@ Optional<Parser::PropertyAndValue> Parser::parse_css_value_for_properties(Readon
if (peek_token.is(Token::Type::String)) { if (peek_token.is(Token::Type::String)) {
if (auto property = any_property_accepts_type(property_ids, ValueType::String); property.has_value()) if (auto property = any_property_accepts_type(property_ids, ValueType::String); property.has_value())
return PropertyAndValue { *property, StringStyleValue::create(tokens.next_token().token().string().to_string()) }; return PropertyAndValue { *property, StringStyleValue::create(tokens.next_token().token().string()) };
} }
if (auto property = any_property_accepts_type(property_ids, ValueType::Url); property.has_value()) { if (auto property = any_property_accepts_type(property_ids, ValueType::Url); property.has_value()) {

View file

@ -1742,7 +1742,7 @@ void StyleComputer::compute_cascaded_values(StyleProperties& style, DOM::Element
if (animation_name.is_null()) if (animation_name.is_null())
return OptionalNone {}; return OptionalNone {};
if (animation_name->is_string()) if (animation_name->is_string())
return animation_name->as_string().string_value(); return animation_name->as_string().string_value().to_string();
return animation_name->to_string(); return animation_name->to_string();
}(); }();

View file

@ -656,13 +656,13 @@ StyleProperties::ContentDataAndQuoteNestingLevel StyleProperties::content(u32 in
auto get_quote_string = [&](bool open, auto depth) { auto get_quote_string = [&](bool open, auto depth) {
switch (quotes_data.type) { switch (quotes_data.type) {
case QuotesData::Type::None: case QuotesData::Type::None:
return String {}; return FlyString {};
case QuotesData::Type::Auto: case QuotesData::Type::Auto:
// FIXME: "A typographically appropriate used value for quotes is automatically chosen by the UA // FIXME: "A typographically appropriate used value for quotes is automatically chosen by the UA
// based on the content language of the element and/or its parent." // based on the content language of the element and/or its parent."
if (open) if (open)
return depth == 0 ? ""_string : ""_string; return depth == 0 ? ""_fly_string : ""_fly_string;
return depth == 0 ? ""_string : ""_string; return depth == 0 ? ""_fly_string : ""_fly_string;
case QuotesData::Type::Specified: case QuotesData::Type::Specified:
// If the depth is greater than the number of pairs, the last pair is repeated. // If the depth is greater than the number of pairs, the last pair is repeated.
auto& level = quotes_data.strings[min(depth, quotes_data.strings.size() - 1)]; auto& level = quotes_data.strings[min(depth, quotes_data.strings.size() - 1)];

View file

@ -1,37 +1,37 @@
/* /*
* Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org> * Copyright (c) 2022-2024, Sam Atkins <atkinssj@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#pragma once #pragma once
#include <AK/String.h> #include <AK/FlyString.h>
#include <LibWeb/CSS/StyleValue.h> #include <LibWeb/CSS/StyleValue.h>
namespace Web::CSS { namespace Web::CSS {
class StringStyleValue : public StyleValueWithDefaultOperators<StringStyleValue> { class StringStyleValue : public StyleValueWithDefaultOperators<StringStyleValue> {
public: public:
static ValueComparingNonnullRefPtr<StringStyleValue> create(String const& string) static ValueComparingNonnullRefPtr<StringStyleValue> create(FlyString const& string)
{ {
return adopt_ref(*new (nothrow) StringStyleValue(string)); return adopt_ref(*new (nothrow) StringStyleValue(string));
} }
virtual ~StringStyleValue() override = default; virtual ~StringStyleValue() override = default;
String string_value() const { return m_string; } FlyString string_value() const { return m_string; }
String to_string() const override { return serialize_a_string(m_string); } String to_string() const override { return serialize_a_string(m_string); }
bool properties_equal(StringStyleValue const& other) const { return m_string == other.m_string; } bool properties_equal(StringStyleValue const& other) const { return m_string == other.m_string; }
private: private:
explicit StringStyleValue(String const& string) explicit StringStyleValue(FlyString const& string)
: StyleValueWithDefaultOperators(Type::String) : StyleValueWithDefaultOperators(Type::String)
, m_string(string) , m_string(string)
{ {
} }
String m_string; FlyString m_string;
}; };
} }