bdakin | 1f63b91 | 2006-06-09 17:09:08 +0000 | [diff] [blame] | 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 |
mjs@apple.com | 9204733 | 2014-03-15 04:08:27 +0000 | [diff] [blame] | 18 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
bdakin | 1f63b91 | 2006-06-09 17:09:08 +0000 | [diff] [blame] | 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 |
apavlov@chromium.org | f684aec | 2011-10-19 13:31:03 +0000 | [diff] [blame] | 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
bdakin | 1f63b91 | 2006-06-09 17:09:08 +0000 | [diff] [blame] | 26 | */ |
| 27 | |
darin@apple.com | 9eaaf9f | 2016-05-27 00:05:24 +0000 | [diff] [blame] | 28 | #pragma once |
bdakin | 1f63b91 | 2006-06-09 17:09:08 +0000 | [diff] [blame] | 29 | |
antti@apple.com | 71478ce | 2021-10-20 15:25:44 +0000 | [diff] [blame] | 30 | #include "IntSize.h" |
commit-queue@webkit.org | 50edec7 | 2017-02-13 23:06:26 +0000 | [diff] [blame] | 31 | #include "MediaQueryExpression.h" |
antti@apple.com | 88afb4a | 2018-01-17 21:53:26 +0000 | [diff] [blame] | 32 | #include <wtf/WeakPtr.h> |
bdakin | 1f63b91 | 2006-06-09 17:09:08 +0000 | [diff] [blame] | 33 | |
| 34 | namespace WebCore { |
darin@apple.com | 9eaaf9f | 2016-05-27 00:05:24 +0000 | [diff] [blame] | 35 | |
| 36 | class Document; |
antti@apple.com | 77874b4 | 2012-03-26 11:52:13 +0000 | [diff] [blame] | 37 | class MediaQuerySet; |
alexis.menard@openbossa.org | 2d5f8df | 2012-04-24 19:46:29 +0000 | [diff] [blame] | 38 | class RenderStyle; |
antti@apple.com | 4a9ad24 | 2019-11-18 17:37:15 +0000 | [diff] [blame] | 39 | |
darin@apple.com | 9eaaf9f | 2016-05-27 00:05:24 +0000 | [diff] [blame] | 40 | struct MediaQueryResult { |
| 41 | MediaQueryExpression expression; |
| 42 | bool result; |
hyatt@apple.com | 58f8aa0 | 2015-12-09 21:42:25 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
antti@apple.com | 312cd79 | 2019-11-21 16:50:14 +0000 | [diff] [blame] | 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 | } |
antti@apple.com | 6140502 | 2019-11-23 10:19:42 +0000 | [diff] [blame] | 56 | bool isEmpty() const { return viewport.isEmpty() && appearance.isEmpty() && accessibilitySettings.isEmpty(); } |
antti@apple.com | 312cd79 | 2019-11-21 16:50:14 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
antti@apple.com | 71478ce | 2021-10-20 15:25:44 +0000 | [diff] [blame] | 59 | using MediaQueryViewportState = std::tuple<IntSize, float, bool>; |
| 60 | |
| 61 | MediaQueryViewportState mediaQueryViewportStateForDocument(const Document&); |
| 62 | |
darin@apple.com | 9eaaf9f | 2016-05-27 00:05:24 +0000 | [diff] [blame] | 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 | |
ossy@webkit.org | 95c1bc4 | 2011-01-20 16:30:54 +0000 | [diff] [blame] | 66 | class MediaQueryEvaluator { |
bdakin | 1f63b91 | 2006-06-09 17:09:08 +0000 | [diff] [blame] | 67 | public: |
darin@apple.com | 9eaaf9f | 2016-05-27 00:05:24 +0000 | [diff] [blame] | 68 | // 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.org | 9bd6bb7 | 2013-01-23 05:14:42 +0000 | [diff] [blame] | 70 | explicit MediaQueryEvaluator(bool mediaFeatureResult = false); |
zandobersek@gmail.com | f656aa6 | 2012-05-28 09:14:38 +0000 | [diff] [blame] | 71 | |
darin@apple.com | 9eaaf9f | 2016-05-27 00:05:24 +0000 | [diff] [blame] | 72 | // Creates evaluator which evaluates only simple media queries. |
| 73 | // Evaluator returns true for acceptedMediaType and returns value of \mediaFeatureResult for any media features. |
bdakin | 1f63b91 | 2006-06-09 17:09:08 +0000 | [diff] [blame] | 74 | MediaQueryEvaluator(const String& acceptedMediaType, bool mediaFeatureResult = false); |
| 75 | |
darin@apple.com | 9eaaf9f | 2016-05-27 00:05:24 +0000 | [diff] [blame] | 76 | // Creates evaluator which evaluates full media queries. |
hyatt@apple.com | ff850c5 | 2016-09-14 15:54:54 +0000 | [diff] [blame] | 77 | WEBCORE_EXPORT MediaQueryEvaluator(const String& acceptedMediaType, const Document&, const RenderStyle*); |
bdakin | 1f63b91 | 2006-06-09 17:09:08 +0000 | [diff] [blame] | 78 | |
| 79 | bool mediaTypeMatch(const String& mediaTypeToMatch) const; |
cdumez@apple.com | f78895e | 2022-04-28 04:03:12 +0000 | [diff] [blame] | 80 | bool mediaTypeMatchSpecific(ASCIILiteral mediaTypeToMatch) const; |
bdakin | 1f63b91 | 2006-06-09 17:09:08 +0000 | [diff] [blame] | 81 | |
darin@apple.com | 9eaaf9f | 2016-05-27 00:05:24 +0000 | [diff] [blame] | 82 | // Evaluates media query subexpression, ie "and (media-feature: value)" part. |
| 83 | bool evaluate(const MediaQueryExpression&) const; |
antti@apple.com | 6140502 | 2019-11-23 10:19:42 +0000 | [diff] [blame] | 84 | bool evaluateForChanges(const MediaQueryDynamicResults&) const; |
darin@apple.com | 9eaaf9f | 2016-05-27 00:05:24 +0000 | [diff] [blame] | 85 | |
antti@apple.com | 3f80753 | 2019-12-17 09:51:54 +0000 | [diff] [blame] | 86 | enum class Mode { Normal, AlwaysMatchDynamic }; |
| 87 | WEBCORE_EXPORT bool evaluate(const MediaQuerySet&, MediaQueryDynamicResults* = nullptr, Mode = Mode::Normal) const; |
bdakin | 1f63b91 | 2006-06-09 17:09:08 +0000 | [diff] [blame] | 88 | |
yoav@yoav.ws | 029d325 | 2017-05-22 22:03:18 +0000 | [diff] [blame] | 89 | static bool mediaAttributeMatches(Document&, const String& attributeValue); |
| 90 | |
bdakin | 1f63b91 | 2006-06-09 17:09:08 +0000 | [diff] [blame] | 91 | private: |
| 92 | String m_mediaType; |
antti@apple.com | b351d97 | 2018-08-24 09:30:52 +0000 | [diff] [blame] | 93 | WeakPtr<const Document> m_document; |
antti@apple.com | 5c4302b | 2016-04-26 18:20:09 +0000 | [diff] [blame] | 94 | const RenderStyle* m_style { nullptr }; |
darin@apple.com | 9eaaf9f | 2016-05-27 00:05:24 +0000 | [diff] [blame] | 95 | bool m_fallbackResult { false }; |
bdakin | 1f63b91 | 2006-06-09 17:09:08 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
zandobersek@gmail.com | f656aa6 | 2012-05-28 09:14:38 +0000 | [diff] [blame] | 98 | } // namespace |