1/*
2 * CSS Media Query Evaluator
3 *
4 * Copyright (C) 2006 Kimmo Kinnunen <kimmo.t.kinnunen@nokia.com>.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#pragma once
29
30#include "MediaQueryExpression.h"
31#include <wtf/WeakPtr.h>
32
33namespace WebCore {
34
35class Document;
36class Frame;
37class MediaQuerySet;
38class RenderStyle;
39class StyleResolver;
40
41struct MediaQueryResult {
42 MediaQueryExpression expression;
43 bool result;
44};
45
46// Some of the constructors are used for cases where the device characteristics are not known.
47// These can be used to prune the loading of stylesheets to only those which are not already known to not match.
48
49class MediaQueryEvaluator {
50public:
51 // Creates evaluator which evaluates only simple media queries.
52 // Evaluator returns true for "all", and returns value of \mediaFeatureResult for any media features.
53 explicit MediaQueryEvaluator(bool mediaFeatureResult = false);
54
55 // Creates evaluator which evaluates only simple media queries.
56 // Evaluator returns true for acceptedMediaType and returns value of \mediaFeatureResult for any media features.
57 MediaQueryEvaluator(const String& acceptedMediaType, bool mediaFeatureResult = false);
58
59 // Creates evaluator which evaluates full media queries.
60 WEBCORE_EXPORT MediaQueryEvaluator(const String& acceptedMediaType, const Document&, const RenderStyle*);
61
62 bool mediaTypeMatch(const String& mediaTypeToMatch) const;
63 bool mediaTypeMatchSpecific(const char* mediaTypeToMatch) const;
64
65 // Evaluates a list of media queries.
66 WEBCORE_EXPORT bool evaluate(const MediaQuerySet&, StyleResolver* = nullptr) const;
67
68 // Evaluates media query subexpression, ie "and (media-feature: value)" part.
69 bool evaluate(const MediaQueryExpression&) const;
70
71 // Evaluates a list of media queries and fills in vectors with any viewport or dark mode dependent results found.
72 bool evaluate(const MediaQuerySet&, Vector<MediaQueryResult>& viewportDependentResults, Vector<MediaQueryResult>& appearanceDependentResults) const;
73
74 static bool mediaAttributeMatches(Document&, const String& attributeValue);
75
76private:
77 String m_mediaType;
78 WeakPtr<const Document> m_document;
79 const RenderStyle* m_style { nullptr };
80 bool m_fallbackResult { false };
81};
82
83} // namespace
84