mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-19 07:22:21 +00:00
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.
This commit is contained in:
parent
57dd85e4ac
commit
d5bee680b0
Notes:
github-actions[bot]
2025-07-09 14:06:20 +00:00
Author: https://github.com/AtkinsSJ
Commit: d5bee680b0
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5370
Reviewed-by: https://github.com/tcl3 ✅
4 changed files with 203 additions and 188 deletions
|
@ -1,15 +1,141 @@
|
|||
/*
|
||||
* Copyright (c) 2020-2021, the SerenityOS developers.
|
||||
* Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
|
||||
* Copyright (c) 2022-2025, Sam Atkins <sam@ladybird.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/GenericShorthands.h>
|
||||
#include <LibWeb/CSS/Parser/Token.h>
|
||||
#include <LibWeb/CSS/Serialize.h>
|
||||
|
||||
namespace Web::CSS::Parser {
|
||||
|
||||
Token Token::create(Type type, String original_source_text)
|
||||
{
|
||||
VERIFY(first_is_one_of(type,
|
||||
Type::Invalid,
|
||||
Type::EndOfFile,
|
||||
Type::BadString,
|
||||
Type::BadUrl,
|
||||
Type::CDO,
|
||||
Type::CDC,
|
||||
Type::Colon,
|
||||
Type::Semicolon,
|
||||
Type::Comma,
|
||||
Type::OpenSquare,
|
||||
Type::CloseSquare,
|
||||
Type::OpenParen,
|
||||
Type::CloseParen,
|
||||
Type::OpenCurly,
|
||||
Type::CloseCurly));
|
||||
|
||||
Token token;
|
||||
token.m_type = type;
|
||||
token.m_original_source_text = move(original_source_text);
|
||||
return token;
|
||||
}
|
||||
|
||||
Token Token::create_ident(FlyString ident, String original_source_text)
|
||||
{
|
||||
Token token;
|
||||
token.m_type = Type::Ident;
|
||||
token.m_value = move(ident);
|
||||
token.m_original_source_text = move(original_source_text);
|
||||
return token;
|
||||
}
|
||||
|
||||
Token Token::create_function(FlyString name, String original_source_text)
|
||||
{
|
||||
Token token;
|
||||
token.m_type = Type::Function;
|
||||
token.m_value = move(name);
|
||||
token.m_original_source_text = move(original_source_text);
|
||||
return token;
|
||||
}
|
||||
|
||||
Token Token::create_at_keyword(FlyString name, String original_source_text)
|
||||
{
|
||||
Token token;
|
||||
token.m_type = Type::AtKeyword;
|
||||
token.m_value = move(name);
|
||||
token.m_original_source_text = move(original_source_text);
|
||||
return token;
|
||||
}
|
||||
|
||||
Token Token::create_hash(FlyString value, HashType hash_type, String original_source_text)
|
||||
{
|
||||
Token token;
|
||||
token.m_type = Type::Hash;
|
||||
token.m_value = move(value);
|
||||
token.m_hash_type = hash_type;
|
||||
token.m_original_source_text = move(original_source_text);
|
||||
return token;
|
||||
}
|
||||
|
||||
Token Token::create_string(FlyString value, String original_source_text)
|
||||
{
|
||||
Token token;
|
||||
token.m_type = Type::String;
|
||||
token.m_value = move(value);
|
||||
token.m_original_source_text = move(original_source_text);
|
||||
return token;
|
||||
}
|
||||
|
||||
Token Token::create_url(FlyString url, String original_source_text)
|
||||
{
|
||||
Token token;
|
||||
token.m_type = Type::Url;
|
||||
token.m_value = move(url);
|
||||
token.m_original_source_text = move(original_source_text);
|
||||
return token;
|
||||
}
|
||||
|
||||
Token Token::create_delim(u32 delim, String original_source_text)
|
||||
{
|
||||
Token token;
|
||||
token.m_type = Type::Delim;
|
||||
token.m_value = String::from_code_point(delim);
|
||||
token.m_original_source_text = move(original_source_text);
|
||||
return token;
|
||||
}
|
||||
|
||||
Token Token::create_number(Number value, String original_source_text)
|
||||
{
|
||||
Token token;
|
||||
token.m_type = Type::Number;
|
||||
token.m_number_value = value;
|
||||
token.m_original_source_text = move(original_source_text);
|
||||
return token;
|
||||
}
|
||||
|
||||
Token Token::create_percentage(Number value, String original_source_text)
|
||||
{
|
||||
Token token;
|
||||
token.m_type = Type::Percentage;
|
||||
token.m_number_value = value;
|
||||
token.m_original_source_text = move(original_source_text);
|
||||
return token;
|
||||
}
|
||||
|
||||
Token Token::create_dimension(Number value, FlyString unit, String original_source_text)
|
||||
{
|
||||
Token token;
|
||||
token.m_type = Type::Dimension;
|
||||
token.m_number_value = value;
|
||||
token.m_value = move(unit);
|
||||
token.m_original_source_text = move(original_source_text);
|
||||
return token;
|
||||
}
|
||||
|
||||
Token Token::create_whitespace(String original_source_text)
|
||||
{
|
||||
Token token;
|
||||
token.m_type = Type::Whitespace;
|
||||
token.m_original_source_text = move(original_source_text);
|
||||
return token;
|
||||
}
|
||||
|
||||
String Token::to_string() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
|
@ -213,4 +339,10 @@ StringView Token::bracket_mirror_string() const
|
|||
return ""sv;
|
||||
}
|
||||
|
||||
void Token::set_position_range(Badge<Tokenizer>, Position start, Position end)
|
||||
{
|
||||
m_start_position = start;
|
||||
m_end_position = end;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue