mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-28 11:49:44 +00:00
LibWeb/CSS: Parse the CSS <syntax>
type into a tree
`<syntax>` is a limited subset of the "value definition syntax" used in CSS specs. It's used for `@property`'s `syntax` descriptor, and for the `type()` function in `attr()`.
This commit is contained in:
parent
5d1ba658c9
commit
ded2207762
Notes:
github-actions[bot]
2025-07-16 13:49:11 +00:00
Author: https://github.com/AtkinsSJ
Commit: ded2207762
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5400
Reviewed-by: https://github.com/tcl3 ✅
8 changed files with 684 additions and 0 deletions
142
Libraries/LibWeb/CSS/Parser/Syntax.cpp
Normal file
142
Libraries/LibWeb/CSS/Parser/Syntax.cpp
Normal file
|
@ -0,0 +1,142 @@
|
|||
/*
|
||||
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibWeb/CSS/Parser/Syntax.h>
|
||||
#include <LibWeb/CSS/Serialize.h>
|
||||
|
||||
namespace Web::CSS::Parser {
|
||||
|
||||
String SyntaxNode::dump() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
dump(builder, 0);
|
||||
return builder.to_string_without_validation();
|
||||
}
|
||||
|
||||
UniversalSyntaxNode::UniversalSyntaxNode()
|
||||
: SyntaxNode(NodeType::Universal)
|
||||
{
|
||||
}
|
||||
|
||||
UniversalSyntaxNode::~UniversalSyntaxNode() = default;
|
||||
|
||||
String UniversalSyntaxNode::to_string() const
|
||||
{
|
||||
return "*"_string;
|
||||
}
|
||||
|
||||
void UniversalSyntaxNode::dump(StringBuilder& builder, int indent) const
|
||||
{
|
||||
builder.appendff("{: >{}}Universal\n", "", indent);
|
||||
}
|
||||
|
||||
TypeSyntaxNode::TypeSyntaxNode(FlyString type_name)
|
||||
: SyntaxNode(NodeType::Type)
|
||||
, m_type_name(move(type_name))
|
||||
{
|
||||
}
|
||||
|
||||
TypeSyntaxNode::~TypeSyntaxNode() = default;
|
||||
|
||||
String TypeSyntaxNode::to_string() const
|
||||
{
|
||||
return MUST(String::formatted("<{}>", m_type_name));
|
||||
}
|
||||
|
||||
void TypeSyntaxNode::dump(StringBuilder& builder, int indent) const
|
||||
{
|
||||
builder.appendff("{: >{}}Type: {}\n", "", indent, m_type_name);
|
||||
}
|
||||
|
||||
IdentSyntaxNode::IdentSyntaxNode(FlyString ident)
|
||||
: SyntaxNode(NodeType::Ident)
|
||||
, m_ident(move(ident))
|
||||
{
|
||||
}
|
||||
|
||||
IdentSyntaxNode::~IdentSyntaxNode() = default;
|
||||
|
||||
String IdentSyntaxNode::to_string() const
|
||||
{
|
||||
return serialize_an_identifier(m_ident);
|
||||
}
|
||||
|
||||
void IdentSyntaxNode::dump(StringBuilder& builder, int indent) const
|
||||
{
|
||||
builder.appendff("{: >{}}Ident: {}\n", "", indent, m_ident);
|
||||
}
|
||||
|
||||
MultiplierSyntaxNode::MultiplierSyntaxNode(NonnullOwnPtr<SyntaxNode> child)
|
||||
: SyntaxNode(NodeType::Multiplier)
|
||||
, m_child(move(child))
|
||||
{
|
||||
}
|
||||
|
||||
MultiplierSyntaxNode::~MultiplierSyntaxNode() = default;
|
||||
|
||||
String MultiplierSyntaxNode::to_string() const
|
||||
{
|
||||
return MUST(String::formatted("{}+", m_child->to_string()));
|
||||
}
|
||||
|
||||
void MultiplierSyntaxNode::dump(StringBuilder& builder, int indent) const
|
||||
{
|
||||
builder.appendff("{: >{}}Multiplier:\n", "", indent);
|
||||
m_child->dump(builder, indent + 2);
|
||||
}
|
||||
|
||||
CommaSeparatedMultiplierSyntaxNode::CommaSeparatedMultiplierSyntaxNode(NonnullOwnPtr<SyntaxNode> child)
|
||||
: SyntaxNode(NodeType::CommaSeparatedMultiplier)
|
||||
, m_child(move(child))
|
||||
{
|
||||
}
|
||||
|
||||
CommaSeparatedMultiplierSyntaxNode::~CommaSeparatedMultiplierSyntaxNode() = default;
|
||||
|
||||
String CommaSeparatedMultiplierSyntaxNode::to_string() const
|
||||
{
|
||||
return MUST(String::formatted("{}#", m_child->to_string()));
|
||||
}
|
||||
|
||||
void CommaSeparatedMultiplierSyntaxNode::dump(StringBuilder& builder, int indent) const
|
||||
{
|
||||
builder.appendff("{: >{}}CommaSeparatedMultiplier:\n", "", indent);
|
||||
m_child->dump(builder, indent + 2);
|
||||
}
|
||||
|
||||
AlternativesSyntaxNode::AlternativesSyntaxNode(Vector<NonnullOwnPtr<SyntaxNode>> children)
|
||||
: SyntaxNode(NodeType::Alternatives)
|
||||
, m_children(move(children))
|
||||
{
|
||||
}
|
||||
|
||||
AlternativesSyntaxNode::~AlternativesSyntaxNode() = default;
|
||||
|
||||
String AlternativesSyntaxNode::to_string() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
|
||||
bool first = true;
|
||||
for (auto const& child : m_children) {
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
builder.append(" | "sv);
|
||||
}
|
||||
builder.append(child->to_string());
|
||||
}
|
||||
|
||||
return builder.to_string_without_validation();
|
||||
}
|
||||
|
||||
void AlternativesSyntaxNode::dump(StringBuilder& builder, int indent) const
|
||||
{
|
||||
builder.appendff("{: >{}}Alternatives:\n", "", indent);
|
||||
for (auto const& child : m_children)
|
||||
child->dump(builder, indent + 2);
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue