Supports.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2021-2023, 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/GeneralEnclosed.h>
  13. namespace Web::CSS {
  14. // https://www.w3.org/TR/css-conditional-4/#at-supports
  15. class Supports final : public RefCounted<Supports> {
  16. friend class Parser::Parser;
  17. public:
  18. struct Declaration {
  19. String declaration;
  20. [[nodiscard]] bool evaluate(JS::Realm&) const;
  21. String to_string() const;
  22. };
  23. struct Selector {
  24. String selector;
  25. [[nodiscard]] bool evaluate(JS::Realm&) const;
  26. String to_string() const;
  27. };
  28. struct Feature {
  29. Variant<Declaration, Selector> value;
  30. [[nodiscard]] bool evaluate(JS::Realm&) const;
  31. String to_string() const;
  32. };
  33. struct Condition;
  34. struct InParens {
  35. Variant<NonnullOwnPtr<Condition>, Feature, GeneralEnclosed> value;
  36. [[nodiscard]] bool evaluate(JS::Realm&) const;
  37. String to_string() const;
  38. };
  39. struct Condition {
  40. enum class Type {
  41. Not,
  42. And,
  43. Or,
  44. };
  45. Type type;
  46. Vector<InParens> children;
  47. [[nodiscard]] bool evaluate(JS::Realm&) const;
  48. String to_string() const;
  49. };
  50. static NonnullRefPtr<Supports> create(JS::Realm& realm, NonnullOwnPtr<Condition>&& condition)
  51. {
  52. return adopt_ref(*new Supports(realm, move(condition)));
  53. }
  54. bool matches() const { return m_matches; }
  55. String to_string() const;
  56. private:
  57. Supports(JS::Realm&, NonnullOwnPtr<Condition>&&);
  58. NonnullOwnPtr<Condition> m_condition;
  59. bool m_matches { false };
  60. };
  61. }
  62. template<>
  63. struct AK::Formatter<Web::CSS::Supports::InParens> : AK::Formatter<StringView> {
  64. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Supports::InParens const& in_parens)
  65. {
  66. return Formatter<StringView>::format(builder, in_parens.to_string());
  67. }
  68. };