ComponentValue.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Copyright (c) 2020-2021, the SerenityOS developers.
  3. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/NonnullRefPtr.h>
  9. #include <AK/RefPtr.h>
  10. #include <LibWeb/CSS/Parser/Token.h>
  11. namespace Web::CSS {
  12. class StyleBlockRule;
  13. class StyleFunctionRule;
  14. }
  15. namespace Web::CSS::Parser {
  16. // https://www.w3.org/TR/css-syntax-3/#component-value
  17. class ComponentValue {
  18. public:
  19. ComponentValue(Token);
  20. explicit ComponentValue(NonnullRefPtr<StyleFunctionRule>);
  21. explicit ComponentValue(NonnullRefPtr<StyleBlockRule>);
  22. ~ComponentValue();
  23. bool is_block() const { return m_value.has<NonnullRefPtr<StyleBlockRule>>(); }
  24. StyleBlockRule const& block() const { return m_value.get<NonnullRefPtr<StyleBlockRule>>(); }
  25. bool is_function() const { return m_value.has<NonnullRefPtr<StyleFunctionRule>>(); }
  26. StyleFunctionRule const& function() const { return m_value.get<NonnullRefPtr<StyleFunctionRule>>(); }
  27. bool is_token() const { return m_value.has<Token>(); }
  28. bool is(Token::Type type) const { return is_token() && token().is(type); }
  29. Token const& token() const { return m_value.get<Token>(); }
  30. operator Token() const { return m_value.get<Token>(); }
  31. String to_string() const;
  32. String to_debug_string() const;
  33. private:
  34. Variant<Token, NonnullRefPtr<StyleFunctionRule>, NonnullRefPtr<StyleBlockRule>> m_value;
  35. };
  36. }
  37. template<>
  38. struct AK::Formatter<Web::CSS::Parser::ComponentValue> : Formatter<StringView> {
  39. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Parser::ComponentValue const& component_value)
  40. {
  41. return Formatter<StringView>::format(builder, component_value.to_string());
  42. }
  43. };