ComponentValue.h 1.9 KB

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