MediaQuery.h 7.1 KB

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