MediaQuery.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. private:
  52. // TODO: Support <ratio> once we have that.
  53. Variant<String, Length, double> m_value;
  54. };
  55. // https://www.w3.org/TR/mediaqueries-4/#mq-features
  56. class MediaFeature {
  57. public:
  58. enum class Comparison {
  59. Equal,
  60. LessThan,
  61. LessThanOrEqual,
  62. GreaterThan,
  63. GreaterThanOrEqual,
  64. };
  65. // Corresponds to `<mf-boolean>` grammar
  66. static MediaFeature boolean(String const& name)
  67. {
  68. return MediaFeature(Type::IsTrue, name);
  69. }
  70. // Corresponds to `<mf-plain>` grammar
  71. static MediaFeature plain(String const& name, MediaFeatureValue value)
  72. {
  73. if (name.starts_with("min-", CaseSensitivity::CaseInsensitive))
  74. return MediaFeature(Type::MinValue, name.substring_view(4), move(value));
  75. if (name.starts_with("max-", CaseSensitivity::CaseInsensitive))
  76. return MediaFeature(Type::MaxValue, name.substring_view(4), move(value));
  77. return MediaFeature(Type::ExactValue, move(name), move(value));
  78. }
  79. // Corresponds to `<mf-range>` grammar, with a single comparison
  80. static MediaFeature half_range(MediaFeatureValue value, Comparison comparison, String const& name)
  81. {
  82. MediaFeature feature { Type::Range, name };
  83. feature.m_range = Range {
  84. .left_value = value,
  85. .left_comparison = comparison,
  86. };
  87. return feature;
  88. }
  89. // Corresponds to `<mf-range>` grammar, with two comparisons
  90. static MediaFeature range(MediaFeatureValue left_value, Comparison left_comparison, String const& name, Comparison right_comparison, MediaFeatureValue right_value)
  91. {
  92. MediaFeature feature { Type::Range, name };
  93. feature.m_range = Range {
  94. .left_value = left_value,
  95. .left_comparison = left_comparison,
  96. .right_comparison = right_comparison,
  97. .right_value = right_value,
  98. };
  99. return feature;
  100. }
  101. bool evaluate(DOM::Window const&) const;
  102. String to_string() const;
  103. private:
  104. enum class Type {
  105. IsTrue,
  106. ExactValue,
  107. MinValue,
  108. MaxValue,
  109. Range,
  110. };
  111. MediaFeature(Type type, FlyString name, Optional<MediaFeatureValue> value = {})
  112. : m_type(type)
  113. , m_name(move(name))
  114. , m_value(move(value))
  115. {
  116. }
  117. static bool compare(DOM::Window const& window, MediaFeatureValue left, Comparison comparison, MediaFeatureValue right);
  118. struct Range {
  119. MediaFeatureValue left_value;
  120. Comparison left_comparison;
  121. Optional<Comparison> right_comparison {};
  122. Optional<MediaFeatureValue> right_value {};
  123. };
  124. Type m_type;
  125. FlyString m_name;
  126. Optional<MediaFeatureValue> m_value {};
  127. Optional<Range> m_range {};
  128. };
  129. // https://www.w3.org/TR/mediaqueries-4/#media-conditions
  130. struct MediaCondition {
  131. enum class Type {
  132. Single,
  133. And,
  134. Or,
  135. Not,
  136. GeneralEnclosed,
  137. };
  138. // Only used in parsing
  139. enum class AllowOr {
  140. No = 0,
  141. Yes = 1,
  142. };
  143. static NonnullOwnPtr<MediaCondition> from_general_enclosed(GeneralEnclosed&&);
  144. static NonnullOwnPtr<MediaCondition> from_feature(MediaFeature&&);
  145. static NonnullOwnPtr<MediaCondition> from_not(NonnullOwnPtr<MediaCondition>&&);
  146. static NonnullOwnPtr<MediaCondition> from_and_list(NonnullOwnPtrVector<MediaCondition>&&);
  147. static NonnullOwnPtr<MediaCondition> from_or_list(NonnullOwnPtrVector<MediaCondition>&&);
  148. MatchResult evaluate(DOM::Window const&) const;
  149. String to_string() const;
  150. private:
  151. MediaCondition() { }
  152. Type type;
  153. Optional<MediaFeature> feature;
  154. NonnullOwnPtrVector<MediaCondition> conditions;
  155. Optional<GeneralEnclosed> general_enclosed;
  156. };
  157. class MediaQuery : public RefCounted<MediaQuery> {
  158. friend class Parser;
  159. public:
  160. ~MediaQuery() = default;
  161. // https://www.w3.org/TR/mediaqueries-4/#media-types
  162. enum class MediaType {
  163. All,
  164. Print,
  165. Screen,
  166. // Deprecated, must never match:
  167. TTY,
  168. TV,
  169. Projection,
  170. Handheld,
  171. Braille,
  172. Embossed,
  173. Aural,
  174. Speech,
  175. };
  176. static NonnullRefPtr<MediaQuery> create_not_all();
  177. static NonnullRefPtr<MediaQuery> create() { return adopt_ref(*new MediaQuery); }
  178. bool matches() const { return m_matches; }
  179. bool evaluate(DOM::Window const&);
  180. String to_string() const;
  181. private:
  182. MediaQuery() = default;
  183. // https://www.w3.org/TR/mediaqueries-4/#mq-not
  184. bool m_negated { false };
  185. MediaType m_media_type { MediaType::All };
  186. OwnPtr<MediaCondition> m_media_condition { nullptr };
  187. // Cached value, updated by evaluate()
  188. bool m_matches { false };
  189. };
  190. String serialize_a_media_query_list(NonnullRefPtrVector<MediaQuery> const&);
  191. bool is_media_feature_name(StringView name);
  192. }
  193. namespace AK {
  194. template<>
  195. struct Formatter<Web::CSS::MediaFeature> : Formatter<StringView> {
  196. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::MediaFeature const& media_feature)
  197. {
  198. return Formatter<StringView>::format(builder, media_feature.to_string());
  199. }
  200. };
  201. template<>
  202. struct Formatter<Web::CSS::MediaCondition> : Formatter<StringView> {
  203. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::MediaCondition const& media_condition)
  204. {
  205. return Formatter<StringView>::format(builder, media_condition.to_string());
  206. }
  207. };
  208. template<>
  209. struct Formatter<Web::CSS::MediaQuery> : Formatter<StringView> {
  210. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::MediaQuery const& media_query)
  211. {
  212. return Formatter<StringView>::format(builder, media_query.to_string());
  213. }
  214. };
  215. }