Supports.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/Parser/Parser.h>
  7. #include <LibWeb/CSS/Supports.h>
  8. namespace Web::CSS {
  9. Supports::Supports(NonnullOwnPtr<Condition>&& condition)
  10. : m_condition(move(condition))
  11. {
  12. m_matches = m_condition->evaluate();
  13. }
  14. bool Supports::Condition::evaluate() const
  15. {
  16. switch (type) {
  17. case Type::Not:
  18. return !children.first().evaluate();
  19. case Type::And:
  20. for (auto& child : children) {
  21. if (!child.evaluate())
  22. return false;
  23. }
  24. return true;
  25. case Type::Or:
  26. for (auto& child : children) {
  27. if (child.evaluate())
  28. return true;
  29. }
  30. return false;
  31. }
  32. VERIFY_NOT_REACHED();
  33. }
  34. bool Supports::InParens::evaluate() const
  35. {
  36. return value.visit(
  37. [&](NonnullOwnPtr<Condition> const& condition) {
  38. return condition->evaluate();
  39. },
  40. [&](Feature const& feature) {
  41. return feature.evaluate();
  42. },
  43. [&](GeneralEnclosed const&) {
  44. return false;
  45. });
  46. }
  47. bool Supports::Declaration::evaluate() const
  48. {
  49. auto style_property = Parser::Parser({}, declaration).parse_as_supports_condition();
  50. return style_property.has_value();
  51. }
  52. bool Supports::Selector::evaluate() const
  53. {
  54. auto style_property = Parser::Parser({}, selector).parse_as_selector();
  55. return style_property.has_value();
  56. }
  57. bool Supports::Feature::evaluate() const
  58. {
  59. return value.visit(
  60. [&](Declaration const& declaration) {
  61. return declaration.evaluate();
  62. },
  63. [&](Selector const& selector) {
  64. return selector.evaluate();
  65. });
  66. }
  67. DeprecatedString Supports::Declaration::to_deprecated_string() const
  68. {
  69. return DeprecatedString::formatted("({})", declaration);
  70. }
  71. DeprecatedString Supports::Selector::to_deprecated_string() const
  72. {
  73. return DeprecatedString::formatted("selector({})", selector);
  74. }
  75. DeprecatedString Supports::Feature::to_deprecated_string() const
  76. {
  77. return value.visit([](auto& it) { return it.to_deprecated_string(); });
  78. }
  79. DeprecatedString Supports::InParens::to_deprecated_string() const
  80. {
  81. return value.visit(
  82. [](NonnullOwnPtr<Condition> const& condition) -> DeprecatedString { return DeprecatedString::formatted("({})", condition->to_deprecated_string()); },
  83. [](Supports::Feature const& it) -> DeprecatedString { return it.to_deprecated_string(); },
  84. [](GeneralEnclosed const& it) -> DeprecatedString { return it.to_string(); });
  85. }
  86. DeprecatedString Supports::Condition::to_deprecated_string() const
  87. {
  88. switch (type) {
  89. case Type::Not:
  90. return DeprecatedString::formatted("not {}", children.first().to_deprecated_string());
  91. case Type::And:
  92. return DeprecatedString::join(" and "sv, children);
  93. case Type::Or:
  94. return DeprecatedString::join(" or "sv, children);
  95. }
  96. VERIFY_NOT_REACHED();
  97. }
  98. DeprecatedString Supports::to_deprecated_string() const
  99. {
  100. return m_condition->to_deprecated_string();
  101. }
  102. }