2021-03-09 13:36:21 -03:00
|
|
|
/*
|
2021-04-28 17:46:44 -03:00
|
|
|
* Copyright (c) 2020-2021, the SerenityOS developers.
|
2023-02-14 16:04:12 -03:00
|
|
|
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2023, Andreas Kling <andreas@ladybird.org>
|
2021-03-09 13:36:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-03-09 13:36:21 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2021-07-01 10:13:48 -03:00
|
|
|
#include <AK/NonnullRefPtr.h>
|
|
|
|
|
#include <AK/RefPtr.h>
|
2021-03-09 16:06:52 -03:00
|
|
|
#include <LibWeb/CSS/Parser/Token.h>
|
2024-10-11 07:17:10 -03:00
|
|
|
#include <LibWeb/CSS/Parser/Types.h>
|
2021-03-09 13:36:21 -03:00
|
|
|
|
2022-04-12 08:13:10 -03:00
|
|
|
namespace Web::CSS::Parser {
|
2021-07-01 10:13:48 -03:00
|
|
|
|
2024-10-11 07:17:10 -03:00
|
|
|
// https://drafts.csswg.org/css-syntax/#component-value
|
2022-03-31 07:43:07 -03:00
|
|
|
class ComponentValue {
|
2024-10-31 17:43:21 -03:00
|
|
|
AK_MAKE_DEFAULT_COPYABLE(ComponentValue);
|
|
|
|
|
AK_MAKE_DEFAULT_MOVABLE(ComponentValue);
|
|
|
|
|
|
2021-03-09 13:36:21 -03:00
|
|
|
public:
|
2022-03-31 07:43:07 -03:00
|
|
|
ComponentValue(Token);
|
2024-10-11 07:17:10 -03:00
|
|
|
explicit ComponentValue(Function&&);
|
|
|
|
|
explicit ComponentValue(SimpleBlock&&);
|
2022-03-31 07:43:07 -03:00
|
|
|
~ComponentValue();
|
2021-03-09 13:36:21 -03:00
|
|
|
|
2024-10-11 07:17:10 -03:00
|
|
|
bool is_block() const { return m_value.has<SimpleBlock>(); }
|
|
|
|
|
SimpleBlock const& block() const { return m_value.get<SimpleBlock>(); }
|
2021-06-30 12:27:37 -03:00
|
|
|
|
2024-10-11 07:17:10 -03:00
|
|
|
bool is_function() const { return m_value.has<Function>(); }
|
2023-09-05 15:42:28 -03:00
|
|
|
bool is_function(StringView name) const;
|
2024-10-11 07:17:10 -03:00
|
|
|
Function const& function() const { return m_value.get<Function>(); }
|
2021-06-30 12:27:37 -03:00
|
|
|
|
2022-03-21 10:35:55 -03:00
|
|
|
bool is_token() const { return m_value.has<Token>(); }
|
|
|
|
|
bool is(Token::Type type) const { return is_token() && token().is(type); }
|
2023-06-06 10:28:42 -03:00
|
|
|
bool is_delim(u32 delim) const { return is(Token::Type::Delim) && token().delim() == delim; }
|
2023-09-05 15:42:28 -03:00
|
|
|
bool is_ident(StringView ident) const;
|
2022-03-21 10:35:55 -03:00
|
|
|
Token const& token() const { return m_value.get<Token>(); }
|
|
|
|
|
operator Token() const { return m_value.get<Token>(); }
|
2021-06-30 12:27:37 -03:00
|
|
|
|
2023-08-22 09:00:28 -03:00
|
|
|
String to_string() const;
|
|
|
|
|
String to_debug_string() const;
|
2024-10-14 12:16:42 -03:00
|
|
|
String original_source_text() const;
|
2021-03-09 13:36:21 -03:00
|
|
|
|
|
|
|
|
private:
|
2024-10-11 07:17:10 -03:00
|
|
|
Variant<Token, Function, SimpleBlock> m_value;
|
2021-03-09 13:36:21 -03:00
|
|
|
};
|
2025-05-13 08:06:33 -03:00
|
|
|
|
2021-03-09 13:36:21 -03:00
|
|
|
}
|
2022-04-12 08:30:48 -03:00
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
struct AK::Formatter<Web::CSS::Parser::ComponentValue> : Formatter<StringView> {
|
|
|
|
|
ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Parser::ComponentValue const& component_value)
|
|
|
|
|
{
|
2023-08-22 09:00:28 -03:00
|
|
|
return Formatter<StringView>::format(builder, component_value.to_string());
|
2022-04-12 08:30:48 -03:00
|
|
|
}
|
|
|
|
|
};
|