Supports.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. String Supports::Declaration::to_string() const
  68. {
  69. return String::formatted("({})", declaration);
  70. }
  71. String Supports::Selector::to_string() const
  72. {
  73. return String::formatted("selector({})", selector);
  74. }
  75. String Supports::Feature::to_string() const
  76. {
  77. return value.visit([](auto& it) { return it.to_string(); });
  78. }
  79. String Supports::InParens::to_string() const
  80. {
  81. return value.visit(
  82. [](NonnullOwnPtr<Condition> const& condition) -> String { return String::formatted("({})", condition->to_string()); },
  83. [](auto& it) -> String { return it.to_string(); });
  84. }
  85. String Supports::Condition::to_string() const
  86. {
  87. switch (type) {
  88. case Type::Not:
  89. return String::formatted("not {}", children.first().to_string());
  90. case Type::And:
  91. return String::join(" and "sv, children);
  92. case Type::Or:
  93. return String::join(" or "sv, children);
  94. }
  95. VERIFY_NOT_REACHED();
  96. }
  97. String Supports::to_string() const
  98. {
  99. return m_condition->to_string();
  100. }
  101. }