1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 21:37:34 +00:00

LibWeb: Convert CSS Token value to new FlyString

This commit is contained in:
Sam Atkins 2023-02-13 13:57:40 +00:00 committed by Linus Groh
parent 09f7682feb
commit 7fc72d3838
4 changed files with 20 additions and 23 deletions

View file

@ -42,7 +42,7 @@ DeprecatedString Token::to_deprecated_string() const
case Type::BadUrl: case Type::BadUrl:
return "url()"; return "url()";
case Type::Delim: case Type::Delim:
return m_value; return DeprecatedString(m_value.bytes_as_string_view());
case Type::Number: case Type::Number:
return DeprecatedString::number(m_number_value.value()); return DeprecatedString::number(m_number_value.value());
case Type::Percentage: case Type::Percentage:

View file

@ -7,8 +7,8 @@
#pragma once #pragma once
#include <AK/DeprecatedFlyString.h>
#include <AK/DeprecatedString.h> #include <AK/DeprecatedString.h>
#include <AK/FlyString.h>
#include <AK/Utf8View.h> #include <AK/Utf8View.h>
#include <LibWeb/CSS/Number.h> #include <LibWeb/CSS/Number.h>
@ -63,37 +63,37 @@ public:
StringView ident() const StringView ident() const
{ {
VERIFY(m_type == Type::Ident); VERIFY(m_type == Type::Ident);
return m_value.view(); return m_value.bytes_as_string_view();
} }
StringView function() const StringView function() const
{ {
VERIFY(m_type == Type::Function); VERIFY(m_type == Type::Function);
return m_value.view(); return m_value.bytes_as_string_view();
} }
u32 delim() const u32 delim() const
{ {
VERIFY(m_type == Type::Delim); VERIFY(m_type == Type::Delim);
return *Utf8View(m_value.view()).begin(); return *Utf8View(m_value.bytes_as_string_view()).begin();
} }
StringView string() const StringView string() const
{ {
VERIFY(m_type == Type::String); VERIFY(m_type == Type::String);
return m_value.view(); return m_value.bytes_as_string_view();
} }
StringView url() const StringView url() const
{ {
VERIFY(m_type == Type::Url); VERIFY(m_type == Type::Url);
return m_value.view(); return m_value.bytes_as_string_view();
} }
StringView at_keyword() const StringView at_keyword() const
{ {
VERIFY(m_type == Type::AtKeyword); VERIFY(m_type == Type::AtKeyword);
return m_value.view(); return m_value.bytes_as_string_view();
} }
HashType hash_type() const HashType hash_type() const
@ -104,7 +104,7 @@ public:
StringView hash_value() const StringView hash_value() const
{ {
VERIFY(m_type == Type::Hash); VERIFY(m_type == Type::Hash);
return m_value.view(); return m_value.bytes_as_string_view();
} }
Number const& number() const Number const& number() const
@ -126,7 +126,7 @@ public:
StringView dimension_unit() const StringView dimension_unit() const
{ {
VERIFY(m_type == Type::Dimension); VERIFY(m_type == Type::Dimension);
return m_value.view(); return m_value.bytes_as_string_view();
} }
float dimension_value() const float dimension_value() const
{ {
@ -151,7 +151,7 @@ public:
Position const& start_position() const { return m_start_position; } Position const& start_position() const { return m_start_position; }
Position const& end_position() const { return m_end_position; } Position const& end_position() const { return m_end_position; }
static Token of_string(DeprecatedFlyString str) static Token of_string(FlyString str)
{ {
Token token; Token token;
token.m_type = Type::String; token.m_type = Type::String;
@ -178,7 +178,7 @@ public:
private: private:
Type m_type { Type::Invalid }; Type m_type { Type::Invalid };
DeprecatedFlyString m_value; FlyString m_value;
Number m_number_value; Number m_number_value;
HashType m_hash_type { HashType::Unrestricted }; HashType m_hash_type { HashType::Unrestricted };

View file

@ -1,6 +1,6 @@
/* /*
* Copyright (c) 2020-2022, the SerenityOS developers. * Copyright (c) 2020-2022, the SerenityOS developers.
* Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org> * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -356,7 +356,7 @@ Token Tokenizer::create_value_token(Token::Type type, DeprecatedString value)
{ {
Token token; Token token;
token.m_type = type; token.m_type = type;
token.m_value = move(value); token.m_value = FlyString::from_utf8(value.view()).release_value_but_fixme_should_propagate_errors();
return token; return token;
} }
@ -364,10 +364,7 @@ Token Tokenizer::create_value_token(Token::Type type, u32 value)
{ {
Token token = {}; Token token = {};
token.m_type = type; token.m_type = type;
// FIXME: Avoid temporary StringBuilder here token.m_value = FlyString(String::from_code_point(value));
StringBuilder builder;
builder.append_code_point(value);
token.m_value = builder.to_deprecated_string();
return token; return token;
} }
@ -640,7 +637,7 @@ Token Tokenizer::consume_a_url_token()
consume_as_much_whitespace_as_possible(); consume_as_much_whitespace_as_possible();
auto make_token = [&]() { auto make_token = [&]() {
token.m_value = builder.to_deprecated_string(); token.m_value = FlyString::from_utf8(builder.string_view()).release_value_but_fixme_should_propagate_errors();
return token; return token;
}; };
@ -784,7 +781,7 @@ Token Tokenizer::consume_a_numeric_token()
auto unit = consume_an_ident_sequence(); auto unit = consume_an_ident_sequence();
VERIFY(!unit.is_empty()); VERIFY(!unit.is_empty());
// NOTE: We intentionally store this in the `value`, to save space. // NOTE: We intentionally store this in the `value`, to save space.
token.m_value = move(unit); token.m_value = FlyString::from_utf8(unit.view()).release_value_but_fixme_should_propagate_errors();
// 3. Return the <dimension-token>. // 3. Return the <dimension-token>.
return token; return token;
@ -931,7 +928,7 @@ Token Tokenizer::consume_string_token(u32 ending_code_point)
StringBuilder builder; StringBuilder builder;
auto make_token = [&]() { auto make_token = [&]() {
token.m_value = builder.to_deprecated_string(); token.m_value = FlyString::from_utf8(builder.string_view()).release_value_but_fixme_should_propagate_errors();
return token; return token;
}; };
@ -1069,7 +1066,7 @@ Token Tokenizer::consume_a_token()
// 3. Consume an ident sequence, and set the <hash-token>s value to the returned string. // 3. Consume an ident sequence, and set the <hash-token>s value to the returned string.
auto name = consume_an_ident_sequence(); auto name = consume_an_ident_sequence();
token.m_value = move(name); token.m_value = FlyString::from_utf8(name.view()).release_value_but_fixme_should_propagate_errors();
// 4. Return the <hash-token>. // 4. Return the <hash-token>.
return token; return token;

View file

@ -703,7 +703,7 @@ bool StyleComputer::expand_unresolved_values(DOM::Element& element, StringView p
// 1. If the attr() function has a substitution value, replace the attr() function by the substitution value. // 1. If the attr() function has a substitution value, replace the attr() function by the substitution value.
if (!attr_value.is_null()) { if (!attr_value.is_null()) {
// FIXME: attr() should also accept an optional type argument, not just strings. // FIXME: attr() should also accept an optional type argument, not just strings.
dest.empend(Parser::Token::of_string(attr_value)); dest.empend(Parser::Token::of_string(FlyString::from_utf8(attr_value).release_value_but_fixme_should_propagate_errors()));
continue; continue;
} }