MediaQuery.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. // https://www.w3.org/TR/mediaqueries-4/#media-conditions
  96. struct MediaCondition {
  97. enum class Type {
  98. Single,
  99. And,
  100. Or,
  101. Not,
  102. GeneralEnclosed,
  103. };
  104. static NonnullOwnPtr<MediaCondition> from_general_enclosed(GeneralEnclosed&&);
  105. static NonnullOwnPtr<MediaCondition> from_feature(MediaFeature&&);
  106. static NonnullOwnPtr<MediaCondition> from_not(NonnullOwnPtr<MediaCondition>&&);
  107. static NonnullOwnPtr<MediaCondition> from_and_list(NonnullOwnPtrVector<MediaCondition>&&);
  108. static NonnullOwnPtr<MediaCondition> from_or_list(NonnullOwnPtrVector<MediaCondition>&&);
  109. MatchResult evaluate(DOM::Window const&) const;
  110. String to_string() const;
  111. private:
  112. MediaCondition() { }
  113. Type type;
  114. Optional<MediaFeature> feature;
  115. NonnullOwnPtrVector<MediaCondition> conditions;
  116. Optional<GeneralEnclosed> general_enclosed;
  117. };
  118. class MediaQuery : public RefCounted<MediaQuery> {
  119. friend class Parser;
  120. public:
  121. ~MediaQuery() = default;
  122. // https://www.w3.org/TR/mediaqueries-4/#media-types
  123. enum class MediaType {
  124. All,
  125. Print,
  126. Screen,
  127. // Deprecated, must never match:
  128. TTY,
  129. TV,
  130. Projection,
  131. Handheld,
  132. Braille,
  133. Embossed,
  134. Aural,
  135. Speech,
  136. };
  137. static NonnullRefPtr<MediaQuery> create_not_all();
  138. static NonnullRefPtr<MediaQuery> create() { return adopt_ref(*new MediaQuery); }
  139. bool matches() const { return m_matches; }
  140. bool evaluate(DOM::Window const&);
  141. String to_string() const;
  142. private:
  143. MediaQuery() = default;
  144. // https://www.w3.org/TR/mediaqueries-4/#mq-not
  145. bool m_negated { false };
  146. MediaType m_media_type { MediaType::All };
  147. OwnPtr<MediaCondition> m_media_condition { nullptr };
  148. // Cached value, updated by evaluate()
  149. bool m_matches { false };
  150. };
  151. String serialize_a_media_query_list(NonnullRefPtrVector<MediaQuery> const&);
  152. }
  153. namespace AK {
  154. template<>
  155. struct Formatter<Web::CSS::MediaFeature> : Formatter<StringView> {
  156. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::MediaFeature const& media_feature)
  157. {
  158. return Formatter<StringView>::format(builder, media_feature.to_string());
  159. }
  160. };
  161. template<>
  162. struct Formatter<Web::CSS::MediaCondition> : Formatter<StringView> {
  163. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::MediaCondition const& media_condition)
  164. {
  165. return Formatter<StringView>::format(builder, media_condition.to_string());
  166. }
  167. };
  168. template<>
  169. struct Formatter<Web::CSS::MediaQuery> : Formatter<StringView> {
  170. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::MediaQuery const& media_query)
  171. {
  172. return Formatter<StringView>::format(builder, media_query.to_string());
  173. }
  174. };
  175. }