MediaQuery.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/StyleValue.h>
  14. namespace Web::CSS {
  15. class MediaQuery : public RefCounted<MediaQuery> {
  16. friend class Parser;
  17. public:
  18. ~MediaQuery() { }
  19. // https://www.w3.org/TR/mediaqueries-4/#media-types
  20. enum class MediaType {
  21. All,
  22. Print,
  23. Screen,
  24. // Deprecated, must never match:
  25. TTY,
  26. TV,
  27. Projection,
  28. Handheld,
  29. Braille,
  30. Embossed,
  31. Aural,
  32. Speech,
  33. };
  34. // https://www.w3.org/TR/mediaqueries-4/#mq-features
  35. struct MediaFeature {
  36. // FIXME: Implement range syntax: https://www.w3.org/TR/mediaqueries-4/#mq-ranges
  37. enum class Type {
  38. IsTrue,
  39. ExactValue,
  40. MinValue,
  41. MaxValue,
  42. };
  43. Type type;
  44. FlyString name;
  45. RefPtr<StyleValue> value { nullptr };
  46. String to_string() const;
  47. };
  48. // https://www.w3.org/TR/mediaqueries-4/#media-conditions
  49. struct MediaCondition {
  50. enum class Type {
  51. Single,
  52. And,
  53. Or,
  54. Not,
  55. };
  56. Type type;
  57. MediaFeature feature;
  58. NonnullOwnPtrVector<MediaCondition> conditions;
  59. String to_string() const;
  60. };
  61. static NonnullRefPtr<MediaQuery> create_not_all();
  62. static NonnullRefPtr<MediaQuery> create() { return adopt_ref(*new MediaQuery); }
  63. String to_string() const;
  64. private:
  65. MediaQuery() { }
  66. // https://www.w3.org/TR/mediaqueries-4/#mq-not
  67. bool m_negated { false };
  68. MediaType m_media_type { MediaType::All };
  69. OwnPtr<MediaCondition> m_media_condition { nullptr };
  70. };
  71. }
  72. namespace AK {
  73. template<>
  74. struct Formatter<Web::CSS::MediaQuery::MediaFeature> : Formatter<StringView> {
  75. void format(FormatBuilder& builder, Web::CSS::MediaQuery::MediaFeature const& media_feature)
  76. {
  77. Formatter<StringView>::format(builder, media_feature.to_string());
  78. }
  79. };
  80. template<>
  81. struct Formatter<Web::CSS::MediaQuery::MediaCondition> : Formatter<StringView> {
  82. void format(FormatBuilder& builder, Web::CSS::MediaQuery::MediaCondition const& media_condition)
  83. {
  84. Formatter<StringView>::format(builder, media_condition.to_string());
  85. }
  86. };
  87. template<>
  88. struct Formatter<Web::CSS::MediaQuery> : Formatter<StringView> {
  89. void format(FormatBuilder& builder, Web::CSS::MediaQuery const& media_query)
  90. {
  91. Formatter<StringView>::format(builder, media_query.to_string());
  92. }
  93. };
  94. }