2 * CSS Media Query Evaluator
4 * Copyright (C) 2006 Kimmo Kinnunen <kimmo.t.kinnunen@nokia.com>.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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 COMPUTER, 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.
29 #include "MediaQueryEvaluator.h"
32 #include "ChromeClient.h"
33 #include "CSSPrimitiveValue.h"
34 #include "CSSStyleSelector.h"
35 #include "CSSValueList.h"
36 #include "FloatRect.h"
38 #include "FrameView.h"
40 #include "MediaFeatureNames.h"
41 #include "MediaList.h"
42 #include "MediaQuery.h"
43 #include "MediaQueryExp.h"
44 #include "NodeRenderStyle.h"
46 #include "RenderView.h"
47 #include "RenderStyle.h"
48 #include "PlatformScreen.h"
49 #include <wtf/HashMap.h>
51 #if ENABLE(3D_RENDERING) && USE(ACCELERATED_COMPOSITING)
52 #include "RenderLayerCompositor.h"
57 using namespace MediaFeatureNames;
59 enum MediaFeaturePrefix { MinPrefix, MaxPrefix, NoPrefix };
61 typedef bool (*EvalFunc)(CSSValue*, RenderStyle*, Frame*, MediaFeaturePrefix);
62 typedef HashMap<AtomicStringImpl*, EvalFunc> FunctionMap;
63 static FunctionMap* gFunctionMap;
66 * FIXME: following media features are not implemented: color_index, scan, resolution
68 * color_index, min-color-index, max_color_index: It's unknown how to retrieve
69 * the information if the display mode is indexed
70 * scan: The "scan" media feature describes the scanning process of
71 * tv output devices. It's unknown how to retrieve this information from
73 * resolution, min-resolution, max-resolution: css parser doesn't seem to
74 * support CSS_DIMENSION
77 MediaQueryEvaluator::MediaQueryEvaluator(bool mediaFeatureResult)
80 , m_expResult(mediaFeatureResult)
84 MediaQueryEvaluator:: MediaQueryEvaluator(const String& acceptedMediaType, bool mediaFeatureResult)
85 : m_mediaType(acceptedMediaType)
88 , m_expResult(mediaFeatureResult)
92 MediaQueryEvaluator:: MediaQueryEvaluator(const char* acceptedMediaType, bool mediaFeatureResult)
93 : m_mediaType(acceptedMediaType)
96 , m_expResult(mediaFeatureResult)
100 MediaQueryEvaluator:: MediaQueryEvaluator(const String& acceptedMediaType, Frame* frame, RenderStyle* style)
101 : m_mediaType(acceptedMediaType)
104 , m_expResult(false) // doesn't matter when we have m_frame and m_style
108 MediaQueryEvaluator::~MediaQueryEvaluator()
112 bool MediaQueryEvaluator::mediaTypeMatch(const String& mediaTypeToMatch) const
114 return mediaTypeToMatch.isEmpty()
115 || equalIgnoringCase(mediaTypeToMatch, "all")
116 || equalIgnoringCase(mediaTypeToMatch, m_mediaType);
119 bool MediaQueryEvaluator::mediaTypeMatchSpecific(const char* mediaTypeToMatch) const
121 // Like mediaTypeMatch, but without the special cases for "" and "all".
122 ASSERT(mediaTypeToMatch);
123 ASSERT(mediaTypeToMatch[0] != '\0');
124 ASSERT(!equalIgnoringCase(mediaTypeToMatch, String("all")));
125 return equalIgnoringCase(mediaTypeToMatch, m_mediaType);
128 static bool applyRestrictor(MediaQuery::Restrictor r, bool value)
130 return r == MediaQuery::Not ? !value : value;
133 bool MediaQueryEvaluator::eval(const MediaList* mediaList, CSSStyleSelector* styleSelector) const
138 const Vector<MediaQuery*>& queries = mediaList->mediaQueries();
140 return true; // empty query list evaluates to true
142 // iterate over queries, stop if any of them eval to true (OR semantics)
144 for (size_t i = 0; i < queries.size() && !result; ++i) {
145 MediaQuery* query = queries.at(i);
147 if (query->ignored())
150 if (mediaTypeMatch(query->mediaType())) {
151 const Vector<MediaQueryExp*>* exps = query->expressions();
152 // iterate through expressions, stop if any of them eval to false
155 for (; j < exps->size(); ++j) {
156 bool exprResult = eval(exps->at(j));
157 if (styleSelector && exps->at(j)->isViewportDependent())
158 styleSelector->addViewportDependentMediaQueryResult(exps->at(j), exprResult);
163 // assume true if we are at the end of the list,
164 // otherwise assume false
165 result = applyRestrictor(query->restrictor(), exps->size() == j);
167 result = applyRestrictor(query->restrictor(), false);
173 static bool parseAspectRatio(CSSValue* value, int& h, int& v)
175 if (value->isValueList()) {
176 CSSValueList* valueList = static_cast<CSSValueList*>(value);
177 if (valueList->length() == 3) {
178 CSSValue* i0 = valueList->itemWithoutBoundsCheck(0);
179 CSSValue* i1 = valueList->itemWithoutBoundsCheck(1);
180 CSSValue* i2 = valueList->itemWithoutBoundsCheck(2);
181 if (i0->isPrimitiveValue() && static_cast<CSSPrimitiveValue*>(i0)->primitiveType() == CSSPrimitiveValue::CSS_NUMBER
182 && i1->isPrimitiveValue() && static_cast<CSSPrimitiveValue*>(i1)->primitiveType() == CSSPrimitiveValue::CSS_STRING
183 && i2->isPrimitiveValue() && static_cast<CSSPrimitiveValue*>(i2)->primitiveType() == CSSPrimitiveValue::CSS_NUMBER) {
184 String str = static_cast<CSSPrimitiveValue*>(i1)->getStringValue();
185 if (!str.isNull() && str.length() == 1 && str[0] == '/') {
186 h = static_cast<CSSPrimitiveValue*>(i0)->getIntValue(CSSPrimitiveValue::CSS_NUMBER);
187 v = static_cast<CSSPrimitiveValue*>(i2)->getIntValue(CSSPrimitiveValue::CSS_NUMBER);
197 bool compareValue(T a, T b, MediaFeaturePrefix op)
210 static bool numberValue(CSSValue* value, float& result)
212 if (value->isPrimitiveValue()
213 && static_cast<CSSPrimitiveValue*>(value)->primitiveType() == CSSPrimitiveValue::CSS_NUMBER) {
214 result = static_cast<CSSPrimitiveValue*>(value)->getFloatValue(CSSPrimitiveValue::CSS_NUMBER);
220 static bool colorMediaFeatureEval(CSSValue* value, RenderStyle*, Frame* frame, MediaFeaturePrefix op)
222 int bitsPerComponent = screenDepthPerComponent(frame->page()->mainFrame()->view());
225 return numberValue(value, number) && compareValue(bitsPerComponent, static_cast<int>(number), op);
227 return bitsPerComponent != 0;
230 static bool monochromeMediaFeatureEval(CSSValue* value, RenderStyle* style, Frame* frame, MediaFeaturePrefix op)
232 if (!screenIsMonochrome(frame->page()->mainFrame()->view())) {
235 return numberValue(value, number) && compareValue(0, static_cast<int>(number), op);
240 return colorMediaFeatureEval(value, style, frame, op);
243 static bool orientationMediaFeatureEval(CSSValue* value, RenderStyle*, Frame* frame, MediaFeaturePrefix)
245 // A missing parameter should fail
249 FrameView* view = frame->view();
250 int width = view->layoutWidth();
251 int height = view->layoutHeight();
252 if (width > height) // Square viewport is portrait
253 return "landscape" == static_cast<CSSPrimitiveValue*>(value)->getStringValue();
254 return "portrait" == static_cast<CSSPrimitiveValue*>(value)->getStringValue();
257 static bool aspect_ratioMediaFeatureEval(CSSValue* value, RenderStyle*, Frame* frame, MediaFeaturePrefix op)
260 FrameView* view = frame->view();
261 int width = view->layoutWidth();
262 int height = view->layoutHeight();
265 if (parseAspectRatio(value, h, v))
266 return v != 0 && compareValue(width * v, height * h, op);
270 // ({,min-,max-}aspect-ratio)
271 // assume if we have a device, its aspect ratio is non-zero
275 static bool device_aspect_ratioMediaFeatureEval(CSSValue* value, RenderStyle*, Frame* frame, MediaFeaturePrefix op)
278 FloatRect sg = screenRect(frame->page()->mainFrame()->view());
281 if (parseAspectRatio(value, h, v))
282 return v != 0 && compareValue(static_cast<int>(sg.width()) * v, static_cast<int>(sg.height()) * h, op);
286 // ({,min-,max-}device-aspect-ratio)
287 // assume if we have a device, its aspect ratio is non-zero
291 static bool device_pixel_ratioMediaFeatureEval(CSSValue *value, RenderStyle*, Frame* frame, MediaFeaturePrefix op)
294 return value->isPrimitiveValue() && compareValue(frame->page()->chrome()->scaleFactor(), static_cast<CSSPrimitiveValue*>(value)->getFloatValue(), op);
296 return frame->page()->chrome()->scaleFactor() != 0;
299 static bool gridMediaFeatureEval(CSSValue* value, RenderStyle*, Frame*, MediaFeaturePrefix op)
301 // if output device is bitmap, grid: 0 == true
302 // assume we have bitmap device
304 if (value && numberValue(value, number))
305 return compareValue(static_cast<int>(number), 0, op);
309 static bool device_heightMediaFeatureEval(CSSValue* value, RenderStyle* style, Frame* frame, MediaFeaturePrefix op)
312 FloatRect sg = screenRect(frame->page()->mainFrame()->view());
313 RenderStyle* rootStyle = frame->document()->documentElement()->renderStyle();
314 return value->isPrimitiveValue() && compareValue(static_cast<int>(sg.height()), static_cast<CSSPrimitiveValue*>(value)->computeLengthInt(style, rootStyle), op);
316 // ({,min-,max-}device-height)
317 // assume if we have a device, assume non-zero
321 static bool device_widthMediaFeatureEval(CSSValue* value, RenderStyle* style, Frame* frame, MediaFeaturePrefix op)
324 FloatRect sg = screenRect(frame->page()->mainFrame()->view());
325 RenderStyle* rootStyle = frame->document()->documentElement()->renderStyle();
326 return value->isPrimitiveValue() && compareValue(static_cast<int>(sg.width()), static_cast<CSSPrimitiveValue*>(value)->computeLengthInt(style, rootStyle), op);
328 // ({,min-,max-}device-width)
329 // assume if we have a device, assume non-zero
333 static bool heightMediaFeatureEval(CSSValue* value, RenderStyle* style, Frame* frame, MediaFeaturePrefix op)
335 FrameView* view = frame->view();
336 RenderStyle* rootStyle = frame->document()->documentElement()->renderStyle();
339 return value->isPrimitiveValue() && compareValue(view->layoutHeight(), static_cast<CSSPrimitiveValue*>(value)->computeLengthInt(style, rootStyle), op);
341 return view->layoutHeight() != 0;
344 static bool widthMediaFeatureEval(CSSValue* value, RenderStyle* style, Frame* frame, MediaFeaturePrefix op)
346 FrameView* view = frame->view();
347 RenderStyle* rootStyle = frame->document()->documentElement()->renderStyle();
350 return value->isPrimitiveValue() && compareValue(view->layoutWidth(), static_cast<CSSPrimitiveValue*>(value)->computeLengthInt(style, rootStyle), op);
352 return view->layoutWidth() != 0;
355 // rest of the functions are trampolines which set the prefix according to the media feature expression used
357 static bool min_colorMediaFeatureEval(CSSValue* value, RenderStyle* style, Frame* frame, MediaFeaturePrefix)
359 return colorMediaFeatureEval(value, style, frame, MinPrefix);
362 static bool max_colorMediaFeatureEval(CSSValue* value, RenderStyle* style, Frame* frame, MediaFeaturePrefix)
364 return colorMediaFeatureEval(value, style, frame, MaxPrefix);
367 static bool min_monochromeMediaFeatureEval(CSSValue* value, RenderStyle* style, Frame* frame, MediaFeaturePrefix)
369 return monochromeMediaFeatureEval(value, style, frame, MinPrefix);
372 static bool max_monochromeMediaFeatureEval(CSSValue* value, RenderStyle* style, Frame* frame, MediaFeaturePrefix)
374 return monochromeMediaFeatureEval(value, style, frame, MaxPrefix);
377 static bool min_aspect_ratioMediaFeatureEval(CSSValue* value, RenderStyle* style, Frame* frame, MediaFeaturePrefix)
379 return aspect_ratioMediaFeatureEval(value, style, frame, MinPrefix);
382 static bool max_aspect_ratioMediaFeatureEval(CSSValue* value, RenderStyle* style, Frame* frame, MediaFeaturePrefix)
384 return aspect_ratioMediaFeatureEval(value, style, frame, MaxPrefix);
387 static bool min_device_aspect_ratioMediaFeatureEval(CSSValue* value, RenderStyle* style, Frame* frame, MediaFeaturePrefix)
389 return device_aspect_ratioMediaFeatureEval(value, style, frame, MinPrefix);
392 static bool max_device_aspect_ratioMediaFeatureEval(CSSValue* value, RenderStyle* style, Frame* frame, MediaFeaturePrefix)
394 return device_aspect_ratioMediaFeatureEval(value, style, frame, MaxPrefix);
397 static bool min_device_pixel_ratioMediaFeatureEval(CSSValue* value, RenderStyle* style, Frame* frame, MediaFeaturePrefix)
399 return device_pixel_ratioMediaFeatureEval(value, style, frame, MinPrefix);
402 static bool max_device_pixel_ratioMediaFeatureEval(CSSValue* value, RenderStyle* style, Frame* frame, MediaFeaturePrefix)
404 return device_pixel_ratioMediaFeatureEval(value, style, frame, MaxPrefix);
407 static bool min_heightMediaFeatureEval(CSSValue* value, RenderStyle* style, Frame* frame, MediaFeaturePrefix)
409 return heightMediaFeatureEval(value, style, frame, MinPrefix);
412 static bool max_heightMediaFeatureEval(CSSValue* value, RenderStyle* style, Frame* frame, MediaFeaturePrefix)
414 return heightMediaFeatureEval(value, style, frame, MaxPrefix);
417 static bool min_widthMediaFeatureEval(CSSValue* value, RenderStyle* style, Frame* frame, MediaFeaturePrefix)
419 return widthMediaFeatureEval(value, style, frame, MinPrefix);
422 static bool max_widthMediaFeatureEval(CSSValue* value, RenderStyle* style, Frame* frame, MediaFeaturePrefix)
424 return widthMediaFeatureEval(value, style, frame, MaxPrefix);
427 static bool min_device_heightMediaFeatureEval(CSSValue* value, RenderStyle* style, Frame* frame, MediaFeaturePrefix)
429 return device_heightMediaFeatureEval(value, style, frame, MinPrefix);
432 static bool max_device_heightMediaFeatureEval(CSSValue* value, RenderStyle* style, Frame* frame, MediaFeaturePrefix)
434 return device_heightMediaFeatureEval(value, style, frame, MaxPrefix);
437 static bool min_device_widthMediaFeatureEval(CSSValue* value, RenderStyle* style, Frame* frame, MediaFeaturePrefix)
439 return device_widthMediaFeatureEval(value, style, frame, MinPrefix);
442 static bool max_device_widthMediaFeatureEval(CSSValue* value, RenderStyle* style, Frame* frame, MediaFeaturePrefix)
444 return device_widthMediaFeatureEval(value, style, frame, MaxPrefix);
447 static bool animationMediaFeatureEval(CSSValue* value, RenderStyle*, Frame*, MediaFeaturePrefix op)
451 return numberValue(value, number) && compareValue(1, static_cast<int>(number), op);
456 static bool transitionMediaFeatureEval(CSSValue* value, RenderStyle*, Frame*, MediaFeaturePrefix op)
460 return numberValue(value, number) && compareValue(1, static_cast<int>(number), op);
465 static bool transform_2dMediaFeatureEval(CSSValue* value, RenderStyle*, Frame*, MediaFeaturePrefix op)
469 return numberValue(value, number) && compareValue(1, static_cast<int>(number), op);
474 static bool transform_3dMediaFeatureEval(CSSValue* value, RenderStyle*, Frame* frame, MediaFeaturePrefix op)
476 bool returnValueIfNoParameter;
479 #if ENABLE(3D_RENDERING)
480 bool threeDEnabled = false;
481 #if USE(ACCELERATED_COMPOSITING)
482 if (RenderView* view = frame->contentRenderer())
483 threeDEnabled = view->compositor()->hasAcceleratedCompositing();
486 returnValueIfNoParameter = threeDEnabled;
487 have3dRendering = threeDEnabled ? 1 : 0;
490 returnValueIfNoParameter = false;
496 return numberValue(value, number) && compareValue(have3dRendering, static_cast<int>(number), op);
498 return returnValueIfNoParameter;
501 static bool view_modeMediaFeatureEval(CSSValue* value, RenderStyle*, Frame* frame, MediaFeaturePrefix op)
506 return Page::stringToViewMode(static_cast<CSSPrimitiveValue*>(value)->getStringValue()) == frame->page()->viewMode();
509 static void createFunctionMap()
512 gFunctionMap = new FunctionMap;
513 #define ADD_TO_FUNCTIONMAP(name, str) \
514 gFunctionMap->set(name##MediaFeature.impl(), name##MediaFeatureEval);
515 CSS_MEDIAQUERY_NAMES_FOR_EACH_MEDIAFEATURE(ADD_TO_FUNCTIONMAP);
516 #undef ADD_TO_FUNCTIONMAP
519 bool MediaQueryEvaluator::eval(const MediaQueryExp* expr) const
521 if (!m_frame || !m_style)
524 if (!expr->isValid())
530 // call the media feature evaluation function. Assume no prefix
531 // and let trampoline functions override the prefix if prefix is
533 EvalFunc func = gFunctionMap->get(expr->mediaFeature().impl());
535 return func(expr->value(), m_style, m_frame, NoPrefix);