Supports.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. MatchResult Supports::Condition::evaluate() const
  19. {
  20. switch (type) {
  21. case Type::Not:
  22. return negate(children.first().evaluate());
  23. case Type::And:
  24. return evaluate_and(children, [](auto& child) { return child.evaluate(); });
  25. case Type::Or:
  26. return evaluate_or(children, [](auto& child) { return child.evaluate(); });
  27. }
  28. VERIFY_NOT_REACHED();
  29. }
  30. MatchResult Supports::InParens::evaluate() const
  31. {
  32. return value.visit(
  33. [&](NonnullOwnPtr<Condition>& condition) {
  34. return condition->evaluate();
  35. },
  36. [&](Feature& feature) {
  37. return feature.evaluate();
  38. },
  39. [&](GeneralEnclosed& general_enclosed) {
  40. return general_enclosed.evaluate();
  41. });
  42. }
  43. MatchResult Supports::Feature::evaluate() const
  44. {
  45. auto style_property = Parser({}, declaration).parse_as_declaration();
  46. if (style_property.has_value())
  47. return MatchResult::True;
  48. return MatchResult::False;
  49. }
  50. }