ladybird/Libraries/LibWeb/CSS/Parser/Token.h
Sam Atkins d5bee680b0 LibWeb/CSS: Construct all CSS Tokens in a consistent way
Add `create_foo()` static methods for the missing Token::Types, and use
them in the Tokenizer. This means we slightly deviate from the spec now:
it says "create foo token... set its bar to 32", but we now just wait
and construct the Token fully-formed. But those cases are short so it
should still be clear what we're doing.

This makes it possible to construct all kinds of Token elsewhere, such
as for testing purposes.
2025-07-09 15:04:57 +01:00

184 lines
4.8 KiB
C++

/*
* Copyright (c) 2020-2021, the SerenityOS developers.
* Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/FlyString.h>
#include <LibWeb/CSS/Number.h>
#include <LibWeb/Forward.h>
namespace Web::CSS::Parser {
class Token {
public:
enum class Type : u8 {
Invalid,
EndOfFile,
Ident,
Function,
AtKeyword,
Hash,
String,
BadString,
Url,
BadUrl,
Delim,
Number,
Percentage,
Dimension,
Whitespace,
CDO,
CDC,
Colon,
Semicolon,
Comma,
OpenSquare,
CloseSquare,
OpenParen,
CloseParen,
OpenCurly,
CloseCurly
};
enum class HashType : u8 {
Id,
Unrestricted,
};
struct Position {
size_t line { 0 };
size_t column { 0 };
};
// Use this only to create types that don't have their own create_foo() methods below.
static Token create(Type, String original_source_text = {});
static Token create_ident(FlyString ident, String original_source_text = {});
static Token create_function(FlyString name, String original_source_text = {});
static Token create_at_keyword(FlyString name, String original_source_text = {});
static Token create_hash(FlyString value, HashType hash_type, String original_source_text = {});
static Token create_string(FlyString value, String original_source_text = {});
static Token create_url(FlyString url, String original_source_text = {});
static Token create_delim(u32 delim, String original_source_text = {});
static Token create_number(Number value, String original_source_text = {});
static Token create_percentage(Number value, String original_source_text = {});
static Token create_dimension(Number value, FlyString unit, String original_source_text = {});
static Token create_dimension(double value, FlyString unit, String original_source_text = {})
{
return create_dimension(Number { Number::Type::Number, value }, move(unit), move(original_source_text));
}
static Token create_whitespace(String original_source_text = {});
Type type() const { return m_type; }
bool is(Type type) const { return m_type == type; }
FlyString const& ident() const
{
VERIFY(m_type == Type::Ident);
return m_value;
}
FlyString const& function() const
{
VERIFY(m_type == Type::Function);
return m_value;
}
u32 delim() const
{
VERIFY(m_type == Type::Delim);
return *m_value.code_points().begin();
}
FlyString const& string() const
{
VERIFY(m_type == Type::String);
return m_value;
}
FlyString const& url() const
{
VERIFY(m_type == Type::Url);
return m_value;
}
FlyString const& at_keyword() const
{
VERIFY(m_type == Type::AtKeyword);
return m_value;
}
HashType hash_type() const
{
VERIFY(m_type == Type::Hash);
return m_hash_type;
}
FlyString const& hash_value() const
{
VERIFY(m_type == Type::Hash);
return m_value;
}
Number const& number() const
{
VERIFY(m_type == Type::Number || m_type == Type::Dimension || m_type == Type::Percentage);
return m_number_value;
}
double number_value() const
{
VERIFY(m_type == Type::Number);
return m_number_value.value();
}
i64 to_integer() const
{
VERIFY(m_type == Type::Number && m_number_value.is_integer());
return m_number_value.integer_value();
}
FlyString const& dimension_unit() const
{
VERIFY(m_type == Type::Dimension);
return m_value;
}
double dimension_value() const
{
VERIFY(m_type == Type::Dimension);
return m_number_value.value();
}
i64 dimension_value_int() const { return m_number_value.integer_value(); }
double percentage() const
{
VERIFY(m_type == Type::Percentage);
return m_number_value.value();
}
Type mirror_variant() const;
StringView bracket_string() const;
StringView bracket_mirror_string() const;
String to_string() const;
String to_debug_string() const;
String const& original_source_text() const { return m_original_source_text; }
Position const& start_position() const { return m_start_position; }
Position const& end_position() const { return m_end_position; }
void set_position_range(Badge<Tokenizer>, Position start, Position end);
private:
Type m_type { Type::Invalid };
FlyString m_value;
Number m_number_value;
HashType m_hash_type { HashType::Unrestricted };
String m_original_source_text;
Position m_start_position;
Position m_end_position;
};
}