/* * Copyright (c) 2021, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include #include #include namespace Web::CSS { // https://www.w3.org/TR/mediaqueries-4/#typedef-mf-value class MediaFeatureValue { public: explicit MediaFeatureValue(ValueID ident) : m_value(move(ident)) { } explicit MediaFeatureValue(Length length) : m_value(move(length)) { } explicit MediaFeatureValue(Ratio ratio) : m_value(move(ratio)) { } explicit MediaFeatureValue(Resolution resolution) : m_value(move(resolution)) { } explicit MediaFeatureValue(float number) : m_value(number) { } String to_string() const; bool is_ident() const { return m_value.has(); } bool is_length() const { return m_value.has(); } bool is_number() const { return m_value.has(); } bool is_ratio() const { return m_value.has(); } bool is_resolution() const { return m_value.has(); } bool is_same_type(MediaFeatureValue const& other) const; ValueID const& ident() const { VERIFY(is_ident()); return m_value.get(); } Length const& length() const { VERIFY(is_length()); return m_value.get(); } Ratio const& ratio() const { VERIFY(is_ratio()); return m_value.get(); } Resolution const& resolution() const { VERIFY(is_resolution()); return m_value.get(); } float number() const { VERIFY(is_number()); return m_value.get(); } private: Variant m_value; }; // https://www.w3.org/TR/mediaqueries-4/#mq-features class MediaFeature { public: enum class Comparison { Equal, LessThan, LessThanOrEqual, GreaterThan, GreaterThanOrEqual, }; // Corresponds to `` grammar static MediaFeature boolean(MediaFeatureID id) { return MediaFeature(Type::IsTrue, id); } // Corresponds to `` grammar static MediaFeature plain(MediaFeatureID id, MediaFeatureValue value) { return MediaFeature(Type::ExactValue, move(id), move(value)); } static MediaFeature min(MediaFeatureID id, MediaFeatureValue value) { return MediaFeature(Type::MinValue, id, move(value)); } static MediaFeature max(MediaFeatureID id, MediaFeatureValue value) { return MediaFeature(Type::MaxValue, id, move(value)); } // Corresponds to `` grammar, with a single comparison static MediaFeature half_range(MediaFeatureValue value, Comparison comparison, MediaFeatureID id) { MediaFeature feature { Type::Range, id }; feature.m_range = Range { .left_value = value, .left_comparison = comparison, }; return feature; } // Corresponds to `` grammar, with two comparisons static MediaFeature range(MediaFeatureValue left_value, Comparison left_comparison, MediaFeatureID id, Comparison right_comparison, MediaFeatureValue right_value) { MediaFeature feature { Type::Range, id }; feature.m_range = Range { .left_value = left_value, .left_comparison = left_comparison, .right_comparison = right_comparison, .right_value = right_value, }; return feature; } bool evaluate(HTML::Window const&) const; String to_string() const; private: enum class Type { IsTrue, ExactValue, MinValue, MaxValue, Range, }; MediaFeature(Type type, MediaFeatureID id, Optional value = {}) : m_type(type) , m_id(move(id)) , m_value(move(value)) { } static bool compare(HTML::Window const& window, MediaFeatureValue left, Comparison comparison, MediaFeatureValue right); struct Range { MediaFeatureValue left_value; Comparison left_comparison; Optional right_comparison {}; Optional right_value {}; }; Type m_type; MediaFeatureID m_id; Optional m_value {}; Optional m_range {}; }; // https://www.w3.org/TR/mediaqueries-4/#media-conditions struct MediaCondition { enum class Type { Single, And, Or, Not, GeneralEnclosed, }; // Only used in parsing enum class AllowOr { No = 0, Yes = 1, }; static NonnullOwnPtr from_general_enclosed(GeneralEnclosed&&); static NonnullOwnPtr from_feature(MediaFeature&&); static NonnullOwnPtr from_not(NonnullOwnPtr&&); static NonnullOwnPtr from_and_list(NonnullOwnPtrVector&&); static NonnullOwnPtr from_or_list(NonnullOwnPtrVector&&); MatchResult evaluate(HTML::Window const&) const; String to_string() const; private: MediaCondition() = default; Type type; Optional feature; NonnullOwnPtrVector conditions; Optional general_enclosed; }; class MediaQuery : public RefCounted { friend class Parser::Parser; public: ~MediaQuery() = default; // https://www.w3.org/TR/mediaqueries-4/#media-types enum class MediaType { All, Print, Screen, Unknown, // Deprecated, must never match: TTY, TV, Projection, Handheld, Braille, Embossed, Aural, Speech, }; static NonnullRefPtr create_not_all(); static NonnullRefPtr create() { return adopt_ref(*new MediaQuery); } bool matches() const { return m_matches; } bool evaluate(HTML::Window const&); String to_string() const; private: MediaQuery() = default; // https://www.w3.org/TR/mediaqueries-4/#mq-not bool m_negated { false }; MediaType m_media_type { MediaType::All }; OwnPtr m_media_condition { nullptr }; // Cached value, updated by evaluate() bool m_matches { false }; }; String serialize_a_media_query_list(NonnullRefPtrVector const&); bool is_media_feature_name(StringView name); MediaQuery::MediaType media_type_from_string(StringView); StringView to_string(MediaQuery::MediaType); } namespace AK { template<> struct Formatter : Formatter { ErrorOr format(FormatBuilder& builder, Web::CSS::MediaFeature const& media_feature) { return Formatter::format(builder, media_feature.to_string()); } }; template<> struct Formatter : Formatter { ErrorOr format(FormatBuilder& builder, Web::CSS::MediaCondition const& media_condition) { return Formatter::format(builder, media_condition.to_string()); } }; template<> struct Formatter : Formatter { ErrorOr format(FormatBuilder& builder, Web::CSS::MediaQuery const& media_query) { return Formatter::format(builder, media_query.to_string()); } }; }