blob: 2f0a3b4e294782295b76dbd543459bbfb731d24d [file] [log] [blame]
bdakin1f63b912006-06-09 17:09:08 +00001/*
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
mjs@apple.com92047332014-03-15 04:08:27 +000018 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
bdakin1f63b912006-06-09 17:09:08 +000019 * 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
apavlov@chromium.orgf684aec2011-10-19 13:31:03 +000025 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
bdakin1f63b912006-06-09 17:09:08 +000026 */
27
darin@apple.com9eaaf9f2016-05-27 00:05:24 +000028#pragma once
bdakin1f63b912006-06-09 17:09:08 +000029
antti@apple.com71478ce2021-10-20 15:25:44 +000030#include "IntSize.h"
commit-queue@webkit.org50edec72017-02-13 23:06:26 +000031#include "MediaQueryExpression.h"
antti@apple.com88afb4a2018-01-17 21:53:26 +000032#include <wtf/WeakPtr.h>
bdakin1f63b912006-06-09 17:09:08 +000033
34namespace WebCore {
darin@apple.com9eaaf9f2016-05-27 00:05:24 +000035
36class Document;
antti@apple.com77874b42012-03-26 11:52:13 +000037class MediaQuerySet;
alexis.menard@openbossa.org2d5f8df2012-04-24 19:46:29 +000038class RenderStyle;
antti@apple.com4a9ad242019-11-18 17:37:15 +000039
darin@apple.com9eaaf9f2016-05-27 00:05:24 +000040struct MediaQueryResult {
41 MediaQueryExpression expression;
42 bool result;
hyatt@apple.com58f8aa02015-12-09 21:42:25 +000043};
44
antti@apple.com312cd792019-11-21 16:50:14 +000045struct 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 }
antti@apple.com61405022019-11-23 10:19:42 +000056 bool isEmpty() const { return viewport.isEmpty() && appearance.isEmpty() && accessibilitySettings.isEmpty(); }
antti@apple.com312cd792019-11-21 16:50:14 +000057};
58
antti@apple.com71478ce2021-10-20 15:25:44 +000059using MediaQueryViewportState = std::tuple<IntSize, float, bool>;
60
61MediaQueryViewportState mediaQueryViewportStateForDocument(const Document&);
62
darin@apple.com9eaaf9f2016-05-27 00:05:24 +000063// 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
ossy@webkit.org95c1bc42011-01-20 16:30:54 +000066class MediaQueryEvaluator {
bdakin1f63b912006-06-09 17:09:08 +000067public:
darin@apple.com9eaaf9f2016-05-27 00:05:24 +000068 // Creates evaluator which evaluates only simple media queries.
69 // Evaluator returns true for "all", and returns value of \mediaFeatureResult for any media features.
hayato@chromium.org9bd6bb72013-01-23 05:14:42 +000070 explicit MediaQueryEvaluator(bool mediaFeatureResult = false);
zandobersek@gmail.comf656aa62012-05-28 09:14:38 +000071
darin@apple.com9eaaf9f2016-05-27 00:05:24 +000072 // Creates evaluator which evaluates only simple media queries.
73 // Evaluator returns true for acceptedMediaType and returns value of \mediaFeatureResult for any media features.
bdakin1f63b912006-06-09 17:09:08 +000074 MediaQueryEvaluator(const String& acceptedMediaType, bool mediaFeatureResult = false);
75
darin@apple.com9eaaf9f2016-05-27 00:05:24 +000076 // Creates evaluator which evaluates full media queries.
hyatt@apple.comff850c52016-09-14 15:54:54 +000077 WEBCORE_EXPORT MediaQueryEvaluator(const String& acceptedMediaType, const Document&, const RenderStyle*);
bdakin1f63b912006-06-09 17:09:08 +000078
79 bool mediaTypeMatch(const String& mediaTypeToMatch) const;
cdumez@apple.comf78895e2022-04-28 04:03:12 +000080 bool mediaTypeMatchSpecific(ASCIILiteral mediaTypeToMatch) const;
bdakin1f63b912006-06-09 17:09:08 +000081
darin@apple.com9eaaf9f2016-05-27 00:05:24 +000082 // Evaluates media query subexpression, ie "and (media-feature: value)" part.
83 bool evaluate(const MediaQueryExpression&) const;
antti@apple.com61405022019-11-23 10:19:42 +000084 bool evaluateForChanges(const MediaQueryDynamicResults&) const;
darin@apple.com9eaaf9f2016-05-27 00:05:24 +000085
antti@apple.com3f807532019-12-17 09:51:54 +000086 enum class Mode { Normal, AlwaysMatchDynamic };
87 WEBCORE_EXPORT bool evaluate(const MediaQuerySet&, MediaQueryDynamicResults* = nullptr, Mode = Mode::Normal) const;
bdakin1f63b912006-06-09 17:09:08 +000088
yoav@yoav.ws029d3252017-05-22 22:03:18 +000089 static bool mediaAttributeMatches(Document&, const String& attributeValue);
90
bdakin1f63b912006-06-09 17:09:08 +000091private:
92 String m_mediaType;
antti@apple.comb351d972018-08-24 09:30:52 +000093 WeakPtr<const Document> m_document;
antti@apple.com5c4302b2016-04-26 18:20:09 +000094 const RenderStyle* m_style { nullptr };
darin@apple.com9eaaf9f2016-05-27 00:05:24 +000095 bool m_fallbackResult { false };
bdakin1f63b912006-06-09 17:09:08 +000096};
97
zandobersek@gmail.comf656aa62012-05-28 09:14:38 +000098} // namespace