1 | /*
|
---|
2 | * CSS Media Query Evaluator
|
---|
3 | *
|
---|
4 | * Copyright (C) 2006 Kimmo Kinnunen <[email protected]>.
|
---|
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 "IntSize.h"
|
---|
31 | #include "MediaQueryExpression.h"
|
---|
32 | #include <wtf/WeakPtr.h>
|
---|
33 |
|
---|
34 | namespace WebCore {
|
---|
35 |
|
---|
36 | class Document;
|
---|
37 | class MediaQuerySet;
|
---|
38 | class RenderStyle;
|
---|
39 |
|
---|
40 | struct MediaQueryResult {
|
---|
41 | MediaQueryExpression expression;
|
---|
42 | bool result;
|
---|
43 | };
|
---|
44 |
|
---|
45 | struct MediaQueryDynamicResults {
|
---|
46 | Vector<MediaQueryResult> viewport;
|
---|
47 | Vector<MediaQueryResult> appearance;
|
---|
48 | Vector<MediaQueryResult> accessibilitySettings;
|
---|
49 |
|
---|
50 | void append(const MediaQueryDynamicResults& other)
|
---|
51 | {
|
---|
52 | viewport.appendVector(other.viewport);
|
---|
53 | appearance.appendVector(other.appearance);
|
---|
54 | accessibilitySettings.appendVector(other.accessibilitySettings);
|
---|
55 | }
|
---|
56 | bool isEmpty() const { return viewport.isEmpty() && appearance.isEmpty() && accessibilitySettings.isEmpty(); }
|
---|
57 | };
|
---|
58 |
|
---|
59 | using MediaQueryViewportState = std::tuple<IntSize, float, bool>;
|
---|
60 |
|
---|
61 | MediaQueryViewportState mediaQueryViewportStateForDocument(const Document&);
|
---|
62 |
|
---|
63 | // Some of the constructors are used for cases where the device characteristics are not known.
|
---|
64 | // These can be used to prune the loading of stylesheets to only those which are not already known to not match.
|
---|
65 |
|
---|
66 | class MediaQueryEvaluator {
|
---|
67 | public:
|
---|
68 | // Creates evaluator which evaluates only simple media queries.
|
---|
69 | // Evaluator returns true for "all", and returns value of \mediaFeatureResult for any media features.
|
---|
70 | explicit MediaQueryEvaluator(bool mediaFeatureResult = false);
|
---|
71 |
|
---|
72 | // Creates evaluator which evaluates only simple media queries.
|
---|
73 | // Evaluator returns true for acceptedMediaType and returns value of \mediaFeatureResult for any media features.
|
---|
74 | MediaQueryEvaluator(const String& acceptedMediaType, bool mediaFeatureResult = false);
|
---|
75 |
|
---|
76 | // Creates evaluator which evaluates full media queries.
|
---|
77 | WEBCORE_EXPORT MediaQueryEvaluator(const String& acceptedMediaType, const Document&, const RenderStyle*);
|
---|
78 |
|
---|
79 | bool mediaTypeMatch(const String& mediaTypeToMatch) const;
|
---|
80 | bool mediaTypeMatchSpecific(ASCIILiteral mediaTypeToMatch) const;
|
---|
81 |
|
---|
82 | // Evaluates media query subexpression, ie "and (media-feature: value)" part.
|
---|
83 | bool evaluate(const MediaQueryExpression&) const;
|
---|
84 | bool evaluateForChanges(const MediaQueryDynamicResults&) const;
|
---|
85 |
|
---|
86 | enum class Mode { Normal, AlwaysMatchDynamic };
|
---|
87 | WEBCORE_EXPORT bool evaluate(const MediaQuerySet&, MediaQueryDynamicResults* = nullptr, Mode = Mode::Normal) const;
|
---|
88 |
|
---|
89 | static bool mediaAttributeMatches(Document&, const String& attributeValue);
|
---|
90 |
|
---|
91 | private:
|
---|
92 | String m_mediaType;
|
---|
93 | WeakPtr<const Document> m_document;
|
---|
94 | const RenderStyle* m_style { nullptr };
|
---|
95 | bool m_fallbackResult { false };
|
---|
96 | };
|
---|
97 |
|
---|
98 | } // namespace
|
---|