ComponentValue.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2020-2021, the SerenityOS developers.
  3. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/CSS/Parser/Block.h>
  8. #include <LibWeb/CSS/Parser/ComponentValue.h>
  9. #include <LibWeb/CSS/Parser/Function.h>
  10. namespace Web::CSS::Parser {
  11. ComponentValue::ComponentValue(Token token)
  12. : m_value(token)
  13. {
  14. }
  15. ComponentValue::ComponentValue(NonnullRefPtr<Function> function)
  16. : m_value(function)
  17. {
  18. }
  19. ComponentValue::ComponentValue(NonnullRefPtr<Block> block)
  20. : m_value(block)
  21. {
  22. }
  23. ComponentValue::~ComponentValue() = default;
  24. bool ComponentValue::is_function(StringView name) const
  25. {
  26. return is_function() && function().name().equals_ignoring_ascii_case(name);
  27. }
  28. bool ComponentValue::is_ident(StringView ident) const
  29. {
  30. return is(Token::Type::Ident) && token().ident().equals_ignoring_ascii_case(ident);
  31. }
  32. String ComponentValue::to_string() const
  33. {
  34. return m_value.visit(
  35. [](Token const& token) { return token.to_string(); },
  36. [](NonnullRefPtr<Block> const& block) { return block->to_string(); },
  37. [](NonnullRefPtr<Function> const& function) { return function->to_string(); });
  38. }
  39. String ComponentValue::to_debug_string() const
  40. {
  41. return m_value.visit(
  42. [](Token const& token) {
  43. return MUST(String::formatted("Token: {}", token.to_debug_string()));
  44. },
  45. [](NonnullRefPtr<Block> const& block) {
  46. return MUST(String::formatted("Block: {}", block->to_string()));
  47. },
  48. [](NonnullRefPtr<Function> const& function) {
  49. return MUST(String::formatted("Function: {}", function->to_string()));
  50. });
  51. }
  52. }