Supports.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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-3/#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. void dump(StringBuilder&, int indent_levels = 0) const;
  23. };
  24. struct Selector {
  25. String selector;
  26. [[nodiscard]] bool evaluate(JS::Realm&) const;
  27. String to_string() const;
  28. void dump(StringBuilder&, int indent_levels = 0) const;
  29. };
  30. struct Feature {
  31. Variant<Declaration, Selector> value;
  32. [[nodiscard]] bool evaluate(JS::Realm&) const;
  33. String to_string() const;
  34. void dump(StringBuilder&, int indent_levels = 0) const;
  35. };
  36. struct Condition;
  37. struct InParens {
  38. Variant<NonnullOwnPtr<Condition>, Feature, GeneralEnclosed> value;
  39. [[nodiscard]] bool evaluate(JS::Realm&) const;
  40. String to_string() const;
  41. void dump(StringBuilder&, int indent_levels = 0) const;
  42. };
  43. struct Condition {
  44. enum class Type {
  45. Not,
  46. And,
  47. Or,
  48. };
  49. Type type;
  50. Vector<InParens> children;
  51. [[nodiscard]] bool evaluate(JS::Realm&) const;
  52. String to_string() const;
  53. void dump(StringBuilder&, int indent_levels = 0) const;
  54. };
  55. static NonnullRefPtr<Supports> create(JS::Realm& realm, NonnullOwnPtr<Condition>&& condition)
  56. {
  57. return adopt_ref(*new Supports(realm, move(condition)));
  58. }
  59. bool matches() const { return m_matches; }
  60. String to_string() const;
  61. void dump(StringBuilder&, int indent_levels = 0) const;
  62. private:
  63. Supports(JS::Realm&, NonnullOwnPtr<Condition>&&);
  64. NonnullOwnPtr<Condition> m_condition;
  65. bool m_matches { false };
  66. };
  67. }
  68. template<>
  69. struct AK::Formatter<Web::CSS::Supports::InParens> : AK::Formatter<StringView> {
  70. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Supports::InParens const& in_parens)
  71. {
  72. return Formatter<StringView>::format(builder, in_parens.to_string());
  73. }
  74. };