Supports.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2021, 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. auto result = m_condition->evaluate();
  13. if (result == MatchResult::Unknown) {
  14. dbgln("!!! Evaluation of CSS Supports returned 'Unknown'!");
  15. }
  16. m_matches = result == MatchResult::True;
  17. }
  18. Supports::MatchResult Supports::Condition::evaluate() const
  19. {
  20. switch (type) {
  21. case Type::Not:
  22. return negate(children.first().evaluate());
  23. case Type::And: {
  24. size_t true_results = 0;
  25. for (auto& child : children) {
  26. auto child_match = child.evaluate();
  27. if (child_match == MatchResult::False)
  28. return MatchResult::False;
  29. if (child_match == MatchResult::True)
  30. true_results++;
  31. }
  32. if (true_results == children.size())
  33. return MatchResult::True;
  34. return MatchResult::Unknown;
  35. }
  36. case Type::Or: {
  37. size_t false_results = 0;
  38. for (auto& child : children) {
  39. auto child_match = child.evaluate();
  40. if (child_match == MatchResult::True)
  41. return MatchResult::True;
  42. if (child_match == MatchResult::False)
  43. false_results++;
  44. }
  45. if (false_results == children.size())
  46. return MatchResult::False;
  47. return MatchResult::Unknown;
  48. }
  49. }
  50. VERIFY_NOT_REACHED();
  51. }
  52. Supports::MatchResult Supports::InParens::evaluate() const
  53. {
  54. return value.visit(
  55. [&](NonnullOwnPtr<Condition>& condition) {
  56. return condition->evaluate();
  57. },
  58. [&](Feature& feature) {
  59. return feature.evaluate();
  60. },
  61. [&](GeneralEnclosed&) {
  62. return MatchResult::Unknown;
  63. });
  64. }
  65. Supports::MatchResult Supports::Feature::evaluate() const
  66. {
  67. auto style_property = Parser({}, "").convert_to_style_property(declaration);
  68. if (style_property.has_value())
  69. return MatchResult::True;
  70. return MatchResult::False;
  71. }
  72. }