mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-26 04:22:07 +00:00 
			
		
		
		
	 57a247530c
			
		
	
	
		57a247530c
		
	
	
	
	
		
			
			`foo.is(Token::Type::Delim) && foo.token().delim() == '!'` becomes
`foo.is_delim('!')`, which is a lot less verbose. I really should have
done this ages ago.
		
	
			
		
			
				
	
	
		
			52 lines
		
	
	
	
		
			1.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			52 lines
		
	
	
	
		
			1.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * Copyright (c) 2020-2021, the SerenityOS developers.
 | |
|  * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
 | |
|  * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
 | |
|  *
 | |
|  * SPDX-License-Identifier: BSD-2-Clause
 | |
|  */
 | |
| 
 | |
| #pragma once
 | |
| 
 | |
| #include <AK/NonnullRefPtr.h>
 | |
| #include <AK/RefPtr.h>
 | |
| #include <LibWeb/CSS/Parser/Token.h>
 | |
| #include <LibWeb/Forward.h>
 | |
| 
 | |
| namespace Web::CSS::Parser {
 | |
| 
 | |
| // https://www.w3.org/TR/css-syntax-3/#component-value
 | |
| class ComponentValue {
 | |
| public:
 | |
|     ComponentValue(Token);
 | |
|     explicit ComponentValue(NonnullRefPtr<Function>);
 | |
|     explicit ComponentValue(NonnullRefPtr<Block>);
 | |
|     ~ComponentValue();
 | |
| 
 | |
|     bool is_block() const { return m_value.has<NonnullRefPtr<Block>>(); }
 | |
|     Block& block() const { return m_value.get<NonnullRefPtr<Block>>(); }
 | |
| 
 | |
|     bool is_function() const { return m_value.has<NonnullRefPtr<Function>>(); }
 | |
|     Function& function() const { return m_value.get<NonnullRefPtr<Function>>(); }
 | |
| 
 | |
|     bool is_token() const { return m_value.has<Token>(); }
 | |
|     bool is(Token::Type type) const { return is_token() && token().is(type); }
 | |
|     bool is_delim(u32 delim) const { return is(Token::Type::Delim) && token().delim() == delim; }
 | |
|     Token const& token() const { return m_value.get<Token>(); }
 | |
|     operator Token() const { return m_value.get<Token>(); }
 | |
| 
 | |
|     ErrorOr<String> to_string() const;
 | |
|     ErrorOr<String> to_debug_string() const;
 | |
| 
 | |
| private:
 | |
|     Variant<Token, NonnullRefPtr<Function>, NonnullRefPtr<Block>> m_value;
 | |
| };
 | |
| }
 | |
| 
 | |
| template<>
 | |
| struct AK::Formatter<Web::CSS::Parser::ComponentValue> : Formatter<StringView> {
 | |
|     ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Parser::ComponentValue const& component_value)
 | |
|     {
 | |
|         return Formatter<StringView>::format(builder, TRY(component_value.to_string()));
 | |
|     }
 | |
| };
 |