MediaQuery.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/FlyString.h>
  8. #include <AK/NonnullOwnPtrVector.h>
  9. #include <AK/NonnullRefPtr.h>
  10. #include <AK/Optional.h>
  11. #include <AK/OwnPtr.h>
  12. #include <AK/RefCounted.h>
  13. #include <LibWeb/CSS/GeneralEnclosed.h>
  14. #include <LibWeb/CSS/StyleValue.h>
  15. namespace Web::CSS {
  16. // https://www.w3.org/TR/mediaqueries-4/#typedef-mf-value
  17. class MediaFeatureValue {
  18. public:
  19. explicit MediaFeatureValue(String ident)
  20. : m_value(move(ident))
  21. {
  22. }
  23. explicit MediaFeatureValue(Length length)
  24. : m_value(move(length))
  25. {
  26. }
  27. explicit MediaFeatureValue(double number)
  28. : m_value(number)
  29. {
  30. }
  31. String to_string() const;
  32. bool is_ident() const { return m_value.has<String>(); }
  33. bool is_length() const { return m_value.has<Length>(); }
  34. bool is_number() const { return m_value.has<double>(); }
  35. bool is_same_type(MediaFeatureValue const& other) const;
  36. String const& ident() const
  37. {
  38. VERIFY(is_ident());
  39. return m_value.get<String>();
  40. }
  41. Length const& length() const
  42. {
  43. VERIFY(is_length());
  44. return m_value.get<Length>();
  45. }
  46. double number() const
  47. {
  48. VERIFY(is_number());
  49. return m_value.get<double>();
  50. }
  51. bool operator==(MediaFeatureValue const& other) const { return equals(other); }
  52. bool operator!=(MediaFeatureValue const& other) const { return !(*this == other); }
  53. bool equals(MediaFeatureValue const& other) const;
  54. private:
  55. // TODO: Support <ratio> once we have that.
  56. Variant<String, Length, double> m_value;
  57. };
  58. // https://www.w3.org/TR/mediaqueries-4/#mq-features
  59. class MediaFeature {
  60. public:
  61. // Corresponds to `<mf-boolean>` grammar
  62. static MediaFeature boolean(String const& name)
  63. {
  64. return MediaFeature(Type::IsTrue, name);
  65. }
  66. // Corresponds to `<mf-plain>` grammar
  67. static MediaFeature plain(String const& name, MediaFeatureValue value)
  68. {
  69. if (name.starts_with("min-", CaseSensitivity::CaseInsensitive))
  70. return MediaFeature(Type::MinValue, name.substring_view(4), move(value));
  71. if (name.starts_with("max-", CaseSensitivity::CaseInsensitive))
  72. return MediaFeature(Type::MaxValue, name.substring_view(4), move(value));
  73. return MediaFeature(Type::ExactValue, move(name), move(value));
  74. }
  75. bool evaluate(DOM::Window const&) const;
  76. String to_string() const;
  77. private:
  78. // FIXME: Implement range syntax: https://www.w3.org/TR/mediaqueries-4/#mq-ranges
  79. enum class Type {
  80. IsTrue,
  81. ExactValue,
  82. MinValue,
  83. MaxValue,
  84. };
  85. MediaFeature(Type type, FlyString name, Optional<MediaFeatureValue> value = {})
  86. : m_type(type)
  87. , m_name(move(name))
  88. , m_value(move(value))
  89. {
  90. }
  91. Type m_type;
  92. FlyString m_name;
  93. Optional<MediaFeatureValue> m_value {};
  94. };
  95. class MediaQuery : public RefCounted<MediaQuery> {
  96. friend class Parser;
  97. public:
  98. ~MediaQuery() = default;
  99. // https://www.w3.org/TR/mediaqueries-4/#media-types
  100. enum class MediaType {
  101. All,
  102. Print,
  103. Screen,
  104. // Deprecated, must never match:
  105. TTY,
  106. TV,
  107. Projection,
  108. Handheld,
  109. Braille,
  110. Embossed,
  111. Aural,
  112. Speech,
  113. };
  114. // https://www.w3.org/TR/mediaqueries-4/#media-conditions
  115. struct MediaCondition {
  116. enum class Type {
  117. Single,
  118. And,
  119. Or,
  120. Not,
  121. GeneralEnclosed,
  122. };
  123. Type type;
  124. Optional<MediaFeature> feature;
  125. NonnullOwnPtrVector<MediaCondition> conditions;
  126. Optional<GeneralEnclosed> general_enclosed;
  127. MatchResult evaluate(DOM::Window const&) const;
  128. String to_string() const;
  129. };
  130. static NonnullRefPtr<MediaQuery> create_not_all();
  131. static NonnullRefPtr<MediaQuery> create() { return adopt_ref(*new MediaQuery); }
  132. bool matches() const { return m_matches; }
  133. bool evaluate(DOM::Window const&);
  134. String to_string() const;
  135. private:
  136. MediaQuery() = default;
  137. // https://www.w3.org/TR/mediaqueries-4/#mq-not
  138. bool m_negated { false };
  139. MediaType m_media_type { MediaType::All };
  140. OwnPtr<MediaCondition> m_media_condition { nullptr };
  141. // Cached value, updated by evaluate()
  142. bool m_matches { false };
  143. };
  144. String serialize_a_media_query_list(NonnullRefPtrVector<MediaQuery> const&);
  145. }
  146. namespace AK {
  147. template<>
  148. struct Formatter<Web::CSS::MediaFeature> : Formatter<StringView> {
  149. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::MediaFeature const& media_feature)
  150. {
  151. return Formatter<StringView>::format(builder, media_feature.to_string());
  152. }
  153. };
  154. template<>
  155. struct Formatter<Web::CSS::MediaQuery::MediaCondition> : Formatter<StringView> {
  156. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::MediaQuery::MediaCondition const& media_condition)
  157. {
  158. return Formatter<StringView>::format(builder, media_condition.to_string());
  159. }
  160. };
  161. template<>
  162. struct Formatter<Web::CSS::MediaQuery> : Formatter<StringView> {
  163. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::MediaQuery const& media_query)
  164. {
  165. return Formatter<StringView>::format(builder, media_query.to_string());
  166. }
  167. };
  168. }