Supports.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/NonnullOwnPtr.h>
  8. #include <AK/RefCounted.h>
  9. #include <AK/String.h>
  10. #include <AK/Variant.h>
  11. #include <AK/Vector.h>
  12. #include <LibWeb/CSS/Parser/StyleDeclarationRule.h>
  13. namespace Web::CSS {
  14. class Supports final : public RefCounted<Supports> {
  15. friend class Parser;
  16. private:
  17. enum class MatchResult {
  18. False,
  19. True,
  20. Unknown,
  21. };
  22. static MatchResult negate(MatchResult value)
  23. {
  24. switch (value) {
  25. case MatchResult::False:
  26. return MatchResult::True;
  27. case MatchResult::True:
  28. return MatchResult::False;
  29. case MatchResult::Unknown:
  30. return MatchResult::Unknown;
  31. }
  32. VERIFY_NOT_REACHED();
  33. }
  34. public:
  35. struct GeneralEnclosed {
  36. };
  37. struct Feature {
  38. // FIXME: Using this internal parser class is a bit of a hack.
  39. StyleDeclarationRule declaration;
  40. MatchResult evaluate() const;
  41. };
  42. struct Condition;
  43. struct InParens {
  44. Variant<NonnullOwnPtr<Condition>, Feature, GeneralEnclosed> value;
  45. MatchResult evaluate() const;
  46. };
  47. struct Condition {
  48. enum class Type {
  49. Not,
  50. And,
  51. Or,
  52. };
  53. Type type;
  54. Vector<InParens> children;
  55. MatchResult evaluate() const;
  56. };
  57. static NonnullRefPtr<Supports> create(NonnullOwnPtr<Condition>&& condition)
  58. {
  59. return adopt_ref(*new Supports(move(condition)));
  60. }
  61. bool matches() const { return m_matches; }
  62. private:
  63. Supports(NonnullOwnPtr<Condition>&&);
  64. NonnullOwnPtr<Condition> m_condition;
  65. bool m_matches { false };
  66. };
  67. }