File indexing completed on 2024-04-28 15:22:39

0001 /*
0002  * CSS Media Query
0003  *
0004  * Copyright (C) 2006 Kimmo Kinnunen <kimmo.t.kinnunen@nokia.com>.
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
0016  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0018  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
0019  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0020  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0021  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0022  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
0023  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0024  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0025  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 #ifndef css_mediaquery_h
0029 #define css_mediaquery_h
0030 
0031 #include "dom/dom_string.h"
0032 #include "css/css_valueimpl.h"
0033 
0034 class KHTMLPart;
0035 
0036 namespace DOM
0037 {
0038 class MediaListImpl;
0039 class ValueList;
0040 }
0041 
0042 namespace khtml
0043 {
0044 
0045 class MediaQueryExp;
0046 
0047 class MediaQuery
0048 {
0049 public:
0050     enum Restrictor {
0051         Only, Not, None
0052     };
0053 
0054     MediaQuery(Restrictor r, const DOM::DOMString &mediaType, QList<MediaQueryExp *> *exprs);
0055     ~MediaQuery();
0056 
0057     Restrictor restrictor() const
0058     {
0059         return m_restrictor;
0060     }
0061     const QList<MediaQueryExp *> *expressions() const
0062     {
0063         return m_expressions;
0064     }
0065     DOM::DOMString mediaType() const
0066     {
0067         return m_mediaType;
0068     }
0069     bool operator==(const MediaQuery &other) const;
0070     void append(MediaQueryExp *newExp)
0071     {
0072         m_expressions->append(newExp);
0073     }
0074     DOM::DOMString cssText() const;
0075 
0076 private:
0077     Restrictor m_restrictor;
0078     DOM::DOMString m_mediaType;
0079     QList<MediaQueryExp *> *m_expressions;
0080 };
0081 
0082 class CSSStyleSelector;
0083 class RenderStyle;
0084 class MediaQueryExp;
0085 
0086 /**
0087  * Class that evaluates css media queries as defined in
0088  * CSS3 Module "Media Queries" (https://www.w3.org/TR/css3-mediaqueries/)
0089  * Special constructors are needed, if simple media queries are to be
0090  * evaluated without knowledge of the medium features. This can happen
0091  * for example when parsing UA stylesheets, if evaluation is done
0092  * right after parsing.
0093  *
0094  * the boolean parameter is used to approximate results of evaluation, if
0095  * the device characteristics are not known. This can be used to prune the loading
0096  * of stylesheets to only those which are probable to match.
0097  */
0098 class MediaQueryEvaluator
0099 {
0100 public:
0101     /** Creates evaluator which evaluates only simple media queries
0102      *  Evaluator returns true for "all", and returns value of \mediaFeatureResult
0103      *  for any media features
0104      */
0105     MediaQueryEvaluator(bool mediaFeatureResult = false);
0106 
0107     /** Creates evaluator which evaluates only simple media queries
0108      *  Evaluator  returns true for acceptedMediaType and returns value of \mediafeatureResult
0109      *  for any media features
0110      */
0111     MediaQueryEvaluator(const DOM::DOMString &acceptedMediaType, bool mediaFeatureResult = false);
0112     MediaQueryEvaluator(const char *acceptedMediaType, bool mediaFeatureResult = false);
0113 
0114     /** Creates evaluator which evaluates full media queries
0115      */
0116     MediaQueryEvaluator(const DOM::DOMString &acceptedMediaType, KHTMLPart *, RenderStyle *);
0117 
0118     ~MediaQueryEvaluator();
0119 
0120     bool mediaTypeMatch(const DOM::DOMString &mediaTypeToMatch) const;
0121     bool mediaTypeMatchSpecific(const char *mediaTypeToMatch) const;
0122 
0123     /** Evaluates a list of media queries */
0124     bool eval(const DOM::MediaListImpl *, CSSStyleSelector * = nullptr) const;
0125 
0126     /** Evaluates media query subexpression, ie "and (media-feature: value)" part */
0127     bool eval(const MediaQueryExp *) const;
0128 
0129     static void cleanup();
0130 
0131 private:
0132     DOM::DOMString m_mediaType;
0133     KHTMLPart *m_part; // not owned
0134     RenderStyle *m_style; // not owned
0135     bool m_expResult;
0136 };
0137 
0138 class MediaQueryExp
0139 {
0140 public:
0141     MediaQueryExp(const DOM::DOMString &mediaFeature, DOM::ValueList *values);
0142     ~MediaQueryExp();
0143 
0144     DOM::DOMString mediaFeature() const
0145     {
0146         return m_mediaFeature;
0147     }
0148 
0149     DOM::CSSValueImpl *value() const
0150     {
0151         return m_value;
0152     }
0153 
0154     bool operator==(const MediaQueryExp &other) const
0155     {
0156         return (other.m_mediaFeature == m_mediaFeature)
0157                && ((!other.m_value && !m_value)
0158                    || (other.m_value && m_value && other.m_value->cssText() == m_value->cssText()));
0159     }
0160 
0161     bool isViewportDependent() const
0162     {
0163         return m_viewportDependent;
0164     }
0165 
0166 private:
0167     bool m_viewportDependent;
0168     DOM::DOMString m_mediaFeature;
0169     DOM::CSSValueImpl *m_value;
0170 };
0171 
0172 } // namespace
0173 
0174 #endif