mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 09:27:35 +00:00
LibWeb: Make StyleComponentValueRule use a Variant
This shrinks its size from 96 bytes to 80. It's now limited by the size of Token.
This commit is contained in:
parent
ca1b855d99
commit
218a9af6b3
2 changed files with 27 additions and 64 deletions
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2020-2021, the SerenityOS developers.
|
* Copyright (c) 2020-2021, the SerenityOS developers.
|
||||||
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
|
* Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -20,46 +20,26 @@ class StyleComponentValueRule {
|
||||||
friend class Parser;
|
friend class Parser;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
enum class ComponentType {
|
|
||||||
Token,
|
|
||||||
Function,
|
|
||||||
Block
|
|
||||||
};
|
|
||||||
|
|
||||||
StyleComponentValueRule(Token);
|
StyleComponentValueRule(Token);
|
||||||
explicit StyleComponentValueRule(NonnullRefPtr<StyleFunctionRule>);
|
explicit StyleComponentValueRule(NonnullRefPtr<StyleFunctionRule>);
|
||||||
explicit StyleComponentValueRule(NonnullRefPtr<StyleBlockRule>);
|
explicit StyleComponentValueRule(NonnullRefPtr<StyleBlockRule>);
|
||||||
~StyleComponentValueRule();
|
~StyleComponentValueRule();
|
||||||
|
|
||||||
bool is_block() const { return m_type == ComponentType::Block; }
|
bool is_block() const { return m_value.has<NonnullRefPtr<StyleBlockRule>>(); }
|
||||||
StyleBlockRule const& block() const
|
StyleBlockRule const& block() const { return m_value.get<NonnullRefPtr<StyleBlockRule>>(); }
|
||||||
{
|
|
||||||
VERIFY(is_block());
|
|
||||||
return *m_block;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool is_function() const { return m_type == ComponentType::Function; }
|
bool is_function() const { return m_value.has<NonnullRefPtr<StyleFunctionRule>>(); }
|
||||||
StyleFunctionRule const& function() const
|
StyleFunctionRule const& function() const { return m_value.get<NonnullRefPtr<StyleFunctionRule>>(); }
|
||||||
{
|
|
||||||
VERIFY(is_function());
|
|
||||||
return *m_function;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool is_token() const { return m_type == ComponentType::Token; }
|
bool is_token() const { return m_value.has<Token>(); }
|
||||||
bool is(Token::Type type) const
|
bool is(Token::Type type) const { return is_token() && token().is(type); }
|
||||||
{
|
Token const& token() const { return m_value.get<Token>(); }
|
||||||
return m_type == ComponentType::Token && m_token.is(type);
|
operator Token() const { return m_value.get<Token>(); }
|
||||||
}
|
|
||||||
Token const& token() const { return m_token; }
|
|
||||||
operator Token() const { return m_token; }
|
|
||||||
|
|
||||||
String to_string() const;
|
String to_string() const;
|
||||||
String to_debug_string() const;
|
String to_debug_string() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ComponentType m_type;
|
Variant<Token, NonnullRefPtr<StyleFunctionRule>, NonnullRefPtr<StyleBlockRule>> m_value;
|
||||||
Token m_token;
|
|
||||||
RefPtr<StyleFunctionRule> m_function;
|
|
||||||
RefPtr<StyleBlockRule> m_block;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,18 +37,15 @@ StyleBlockRule::StyleBlockRule() = default;
|
||||||
StyleBlockRule::~StyleBlockRule() = default;
|
StyleBlockRule::~StyleBlockRule() = default;
|
||||||
|
|
||||||
StyleComponentValueRule::StyleComponentValueRule(Token token)
|
StyleComponentValueRule::StyleComponentValueRule(Token token)
|
||||||
: m_type(StyleComponentValueRule::ComponentType::Token)
|
: m_value(token)
|
||||||
, m_token(token)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
StyleComponentValueRule::StyleComponentValueRule(NonnullRefPtr<StyleFunctionRule> function)
|
StyleComponentValueRule::StyleComponentValueRule(NonnullRefPtr<StyleFunctionRule> function)
|
||||||
: m_type(StyleComponentValueRule::ComponentType::Function)
|
: m_value(function)
|
||||||
, m_function(function)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
StyleComponentValueRule::StyleComponentValueRule(NonnullRefPtr<StyleBlockRule> block)
|
StyleComponentValueRule::StyleComponentValueRule(NonnullRefPtr<StyleBlockRule> block)
|
||||||
: m_type(StyleComponentValueRule::ComponentType::Block)
|
: m_value(block)
|
||||||
, m_block(block)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
StyleComponentValueRule::~StyleComponentValueRule() = default;
|
StyleComponentValueRule::~StyleComponentValueRule() = default;
|
||||||
|
@ -129,38 +126,24 @@ String StyleBlockRule::to_string() const
|
||||||
|
|
||||||
String StyleComponentValueRule::to_string() const
|
String StyleComponentValueRule::to_string() const
|
||||||
{
|
{
|
||||||
switch (m_type) {
|
return m_value.visit(
|
||||||
case StyleComponentValueRule::ComponentType::Token:
|
[](Token const& token) { return token.to_string(); },
|
||||||
return m_token.to_string();
|
[](NonnullRefPtr<StyleBlockRule> const& block) { return block->to_string(); },
|
||||||
case StyleComponentValueRule::ComponentType::Function:
|
[](NonnullRefPtr<StyleFunctionRule> const& function) { return function->to_string(); });
|
||||||
return m_function->to_string();
|
|
||||||
case StyleComponentValueRule::ComponentType::Block:
|
|
||||||
return m_block->to_string();
|
|
||||||
default:
|
|
||||||
VERIFY_NOT_REACHED();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String StyleComponentValueRule::to_debug_string() const
|
String StyleComponentValueRule::to_debug_string() const
|
||||||
{
|
{
|
||||||
StringBuilder builder;
|
return m_value.visit(
|
||||||
|
[](Token const& token) {
|
||||||
switch (m_type) {
|
return String::formatted("Token: {}", token.to_debug_string());
|
||||||
case ComponentType::Token:
|
},
|
||||||
builder.append("Token: ");
|
[](NonnullRefPtr<StyleBlockRule> const& block) {
|
||||||
builder.append(m_token.to_debug_string());
|
return String::formatted("Function: {}", block->to_string());
|
||||||
break;
|
},
|
||||||
case ComponentType::Function:
|
[](NonnullRefPtr<StyleFunctionRule> const& function) {
|
||||||
builder.append("Function: ");
|
return String::formatted("Block: {}", function->to_string());
|
||||||
builder.append(m_function->to_string());
|
});
|
||||||
break;
|
|
||||||
case ComponentType::Block:
|
|
||||||
builder.append("Block: ");
|
|
||||||
builder.append(m_block->to_string());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return builder.to_string();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String StyleDeclarationRule::to_string() const
|
String StyleDeclarationRule::to_string() const
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue