Supports.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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({}, declaration).parse_as_declaration();
  50. return style_property.has_value();
  51. }
  52. bool Supports::Selector::evaluate() const
  53. {
  54. auto style_property = 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. }