OLD | NEW |
1 /* | 1 /* |
2 * CSS Media Query Evaluator | 2 * CSS Media Query Evaluator |
3 * | 3 * |
4 * Copyright (C) 2006 Kimmo Kinnunen <kimmo.t.kinnunen@nokia.com>. | 4 * Copyright (C) 2006 Kimmo Kinnunen <kimmo.t.kinnunen@nokia.com>. |
5 * Copyright (C) 2013 Apple Inc. All rights reserved. | 5 * Copyright (C) 2013 Apple Inc. All rights reserved. |
6 * Copyright (C) 2013 Intel Corporation. All rights reserved. | 6 * Copyright (C) 2013 Intel Corporation. All rights reserved. |
7 * | 7 * |
8 * Redistribution and use in source and binary forms, with or without | 8 * Redistribution and use in source and binary forms, with or without |
9 * modification, are permitted provided that the following conditions | 9 * modification, are permitted provided that the following conditions |
10 * are met: | 10 * are met: |
(...skipping 22 matching lines...) Expand all Loading... |
33 #include "CSSValueKeywords.h" | 33 #include "CSSValueKeywords.h" |
34 #include "MediaFeatureNames.h" | 34 #include "MediaFeatureNames.h" |
35 #include "MediaFeatures.h" | 35 #include "MediaFeatures.h" |
36 #include "MediaTypeNames.h" | 36 #include "MediaTypeNames.h" |
37 #include "core/css/CSSAspectRatioValue.h" | 37 #include "core/css/CSSAspectRatioValue.h" |
38 #include "core/css/CSSHelper.h" | 38 #include "core/css/CSSHelper.h" |
39 #include "core/css/CSSPrimitiveValue.h" | 39 #include "core/css/CSSPrimitiveValue.h" |
40 #include "core/css/CSSToLengthConversionData.h" | 40 #include "core/css/CSSToLengthConversionData.h" |
41 #include "core/css/MediaList.h" | 41 #include "core/css/MediaList.h" |
42 #include "core/css/MediaQuery.h" | 42 #include "core/css/MediaQuery.h" |
43 #include "core/css/MediaValues.h" | 43 #include "core/css/MediaValuesDynamic.h" |
44 #include "core/css/resolver/MediaQueryResult.h" | 44 #include "core/css/resolver/MediaQueryResult.h" |
45 #include "core/dom/NodeRenderStyle.h" | 45 #include "core/dom/NodeRenderStyle.h" |
46 #include "core/frame/FrameHost.h" | 46 #include "core/frame/FrameHost.h" |
47 #include "core/frame/FrameView.h" | 47 #include "core/frame/FrameView.h" |
48 #include "core/frame/LocalFrame.h" | 48 #include "core/frame/LocalFrame.h" |
49 #include "core/frame/Settings.h" | 49 #include "core/frame/Settings.h" |
50 #include "core/inspector/InspectorInstrumentation.h" | 50 #include "core/inspector/InspectorInstrumentation.h" |
51 #include "core/rendering/RenderView.h" | 51 #include "core/rendering/RenderView.h" |
52 #include "core/rendering/compositing/RenderLayerCompositor.h" | 52 #include "core/rendering/compositing/RenderLayerCompositor.h" |
53 #include "core/rendering/style/RenderStyle.h" | 53 #include "core/rendering/style/RenderStyle.h" |
(...skipping 24 matching lines...) Expand all Loading... |
78 | 78 |
79 MediaQueryEvaluator::MediaQueryEvaluator(const char* acceptedMediaType, bool med
iaFeatureResult) | 79 MediaQueryEvaluator::MediaQueryEvaluator(const char* acceptedMediaType, bool med
iaFeatureResult) |
80 : m_mediaType(acceptedMediaType) | 80 : m_mediaType(acceptedMediaType) |
81 , m_expectedResult(mediaFeatureResult) | 81 , m_expectedResult(mediaFeatureResult) |
82 { | 82 { |
83 } | 83 } |
84 | 84 |
85 MediaQueryEvaluator::MediaQueryEvaluator(const String& acceptedMediaType, LocalF
rame* frame, RenderStyle* style) | 85 MediaQueryEvaluator::MediaQueryEvaluator(const String& acceptedMediaType, LocalF
rame* frame, RenderStyle* style) |
86 : m_mediaType(acceptedMediaType) | 86 : m_mediaType(acceptedMediaType) |
87 , m_expectedResult(false) // Doesn't matter when we have m_frame and m_style
. | 87 , m_expectedResult(false) // Doesn't matter when we have m_frame and m_style
. |
88 , m_mediaValues(MediaValues::create(frame, style, MediaValues::DynamicMode)) | 88 , m_mediaValues(MediaValuesDynamic::create(frame, style)) |
89 { | 89 { |
90 } | 90 } |
91 | 91 |
92 MediaQueryEvaluator::MediaQueryEvaluator(const String& acceptedMediaType, const
MediaValues& mediaValues) | 92 MediaQueryEvaluator::MediaQueryEvaluator(const String& acceptedMediaType, const
MediaValues& mediaValues) |
93 : m_mediaType(acceptedMediaType) | 93 : m_mediaType(acceptedMediaType) |
94 , m_expectedResult(false) // Doesn't matter when we have mediaValues. | 94 , m_expectedResult(false) // Doesn't matter when we have mediaValues. |
95 , m_mediaValues(mediaValues.copy()) | 95 , m_mediaValues(mediaValues.copy()) |
96 { | 96 { |
97 } | 97 } |
98 | 98 |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 static bool gridMediaFeatureEval(CSSValue* value, MediaFeaturePrefix op, const M
ediaValues&) | 324 static bool gridMediaFeatureEval(CSSValue* value, MediaFeaturePrefix op, const M
ediaValues&) |
325 { | 325 { |
326 // if output device is bitmap, grid: 0 == true | 326 // if output device is bitmap, grid: 0 == true |
327 // assume we have bitmap device | 327 // assume we have bitmap device |
328 float number; | 328 float number; |
329 if (value && numberValue(value, number)) | 329 if (value && numberValue(value, number)) |
330 return compareValue(static_cast<int>(number), 0, op); | 330 return compareValue(static_cast<int>(number), 0, op); |
331 return false; | 331 return false; |
332 } | 332 } |
333 | 333 |
334 static bool computeLengthWithoutStyle(CSSPrimitiveValue* primitiveValue, int def
aultFontSize, int& result) | 334 static bool computeLength(CSSValue* value, const MediaValues& mediaValues, int&
result) |
335 { | |
336 // We're running in a background thread, so RenderStyle is not available. | |
337 // Nevertheless, we can evaluate length MQs with em, rem or px units. | |
338 // FIXME: Learn to support more units here, or teach CSSPrimitiveValue about
MediaValues. | |
339 unsigned short type = primitiveValue->primitiveType(); | |
340 int factor = 0; | |
341 if (type == CSSPrimitiveValue::CSS_EMS || type == CSSPrimitiveValue::CSS_REM
S) { | |
342 if (defaultFontSize > 0) | |
343 factor = defaultFontSize; | |
344 else | |
345 return false; | |
346 } else if (type == CSSPrimitiveValue::CSS_PX) { | |
347 factor = 1; | |
348 } else { | |
349 return false; | |
350 } | |
351 result = roundForImpreciseConversion<int>(primitiveValue->getDoubleValue()*f
actor); | |
352 return true; | |
353 } | |
354 | |
355 static bool computeLength(CSSValue* value, bool strict, RenderStyle* initialStyl
e, int defaultFontSize, int& result) | |
356 { | 335 { |
357 if (!value->isPrimitiveValue()) | 336 if (!value->isPrimitiveValue()) |
358 return false; | 337 return false; |
359 | 338 |
360 CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(value); | 339 CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(value); |
361 | 340 |
362 if (primitiveValue->isNumber()) { | 341 if (primitiveValue->isNumber()) { |
363 result = primitiveValue->getIntValue(); | 342 result = primitiveValue->getIntValue(); |
364 return !strict || !result; | 343 return !mediaValues.strictMode() || !result; |
365 } | 344 } |
366 | 345 |
367 if (primitiveValue->isLength()) { | 346 if (primitiveValue->isLength()) |
368 if (initialStyle) { | 347 return mediaValues.computeLength(primitiveValue->getDoubleValue(), primi
tiveValue->primitiveType(), result); |
369 // Relative (like EM) and root relative (like REM) units are always
resolved against | |
370 // the initial values for media queries, hence the two initialStyle
parameters. | |
371 // FIXME: We need to plumb viewport unit support down to here. | |
372 result = primitiveValue->computeLength<int>(CSSToLengthConversionDat
a(initialStyle, initialStyle, 0, 1.0 /* zoom */, true /* computingFontSize */)); | |
373 } else { | |
374 return computeLengthWithoutStyle(primitiveValue, defaultFontSize, re
sult); | |
375 } | |
376 return true; | |
377 } | |
378 | 348 |
379 return false; | 349 return false; |
380 } | 350 } |
381 | 351 |
382 static bool deviceHeightMediaFeatureEval(CSSValue* value, MediaFeaturePrefix op,
const MediaValues& mediaValues) | 352 static bool deviceHeightMediaFeatureEval(CSSValue* value, MediaFeaturePrefix op,
const MediaValues& mediaValues) |
383 { | 353 { |
384 if (value) { | 354 if (value) { |
385 int length; | 355 int length; |
386 return computeLength(value, mediaValues.strictMode(), mediaValues.style(
), mediaValues.defaultFontSize(), length) | 356 return computeLength(value, mediaValues, length) && compareValue(static_
cast<int>(mediaValues.deviceHeight()), length, op); |
387 && compareValue(static_cast<int>(mediaValues.deviceHeight()), length
, op); | |
388 } | 357 } |
389 // ({,min-,max-}device-height) | 358 // ({,min-,max-}device-height) |
390 // assume if we have a device, assume non-zero | 359 // assume if we have a device, assume non-zero |
391 return true; | 360 return true; |
392 } | 361 } |
393 | 362 |
394 static bool deviceWidthMediaFeatureEval(CSSValue* value, MediaFeaturePrefix op,
const MediaValues& mediaValues) | 363 static bool deviceWidthMediaFeatureEval(CSSValue* value, MediaFeaturePrefix op,
const MediaValues& mediaValues) |
395 { | 364 { |
396 if (value) { | 365 if (value) { |
397 int length; | 366 int length; |
398 return computeLength(value, mediaValues.strictMode(), mediaValues.style(
), mediaValues.defaultFontSize(), length) | 367 return computeLength(value, mediaValues, length) && compareValue(static_
cast<int>(mediaValues.deviceWidth()), length, op); |
399 && compareValue(static_cast<int>(mediaValues.deviceWidth()), length,
op); | |
400 } | 368 } |
401 // ({,min-,max-}device-width) | 369 // ({,min-,max-}device-width) |
402 // assume if we have a device, assume non-zero | 370 // assume if we have a device, assume non-zero |
403 return true; | 371 return true; |
404 } | 372 } |
405 | 373 |
406 static bool heightMediaFeatureEval(CSSValue* value, MediaFeaturePrefix op, const
MediaValues& mediaValues) | 374 static bool heightMediaFeatureEval(CSSValue* value, MediaFeaturePrefix op, const
MediaValues& mediaValues) |
407 { | 375 { |
408 int height = mediaValues.viewportHeight(); | 376 int height = mediaValues.viewportHeight(); |
409 if (value) { | 377 if (value) { |
410 int length; | 378 int length; |
411 return computeLength(value, mediaValues.strictMode(), mediaValues.style(
), mediaValues.defaultFontSize(), length) | 379 return computeLength(value, mediaValues, length) && compareValue(height,
length, op); |
412 && compareValue(height, length, op); | |
413 } | 380 } |
414 | 381 |
415 return height; | 382 return height; |
416 } | 383 } |
417 | 384 |
418 static bool widthMediaFeatureEval(CSSValue* value, MediaFeaturePrefix op, const
MediaValues& mediaValues) | 385 static bool widthMediaFeatureEval(CSSValue* value, MediaFeaturePrefix op, const
MediaValues& mediaValues) |
419 { | 386 { |
420 int width = mediaValues.viewportWidth(); | 387 int width = mediaValues.viewportWidth(); |
421 if (value) { | 388 if (value) { |
422 int length; | 389 int length; |
423 return computeLength(value, mediaValues.strictMode(), mediaValues.style(
), mediaValues.defaultFontSize(), length) | 390 return computeLength(value, mediaValues, length) && compareValue(width,
length, op); |
424 && compareValue(width, length, op); | |
425 } | 391 } |
426 | 392 |
427 return width; | 393 return width; |
428 } | 394 } |
429 | 395 |
430 // Rest of the functions are trampolines which set the prefix according to the m
edia feature expression used. | 396 // Rest of the functions are trampolines which set the prefix according to the m
edia feature expression used. |
431 | 397 |
432 static bool minColorMediaFeatureEval(CSSValue* value, MediaFeaturePrefix, const
MediaValues& mediaValues) | 398 static bool minColorMediaFeatureEval(CSSValue* value, MediaFeaturePrefix, const
MediaValues& mediaValues) |
433 { | 399 { |
434 return colorMediaFeatureEval(value, MinPrefix, mediaValues); | 400 return colorMediaFeatureEval(value, MinPrefix, mediaValues); |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
659 // Create the table. | 625 // Create the table. |
660 gFunctionMap = new FunctionMap; | 626 gFunctionMap = new FunctionMap; |
661 #define ADD_TO_FUNCTIONMAP(name) \ | 627 #define ADD_TO_FUNCTIONMAP(name) \ |
662 gFunctionMap->set(name##MediaFeature.impl(), name##MediaFeatureEval); | 628 gFunctionMap->set(name##MediaFeature.impl(), name##MediaFeatureEval); |
663 CSS_MEDIAQUERY_NAMES_FOR_EACH_MEDIAFEATURE(ADD_TO_FUNCTIONMAP); | 629 CSS_MEDIAQUERY_NAMES_FOR_EACH_MEDIAFEATURE(ADD_TO_FUNCTIONMAP); |
664 #undef ADD_TO_FUNCTIONMAP | 630 #undef ADD_TO_FUNCTIONMAP |
665 } | 631 } |
666 | 632 |
667 bool MediaQueryEvaluator::eval(const MediaQueryExp* expr) const | 633 bool MediaQueryEvaluator::eval(const MediaQueryExp* expr) const |
668 { | 634 { |
669 if (!m_mediaValues) | 635 if (!m_mediaValues || !m_mediaValues->hasValues()) |
670 return m_expectedResult; | 636 return m_expectedResult; |
671 | 637 |
672 if (!gFunctionMap) | 638 if (!gFunctionMap) |
673 createFunctionMap(); | 639 createFunctionMap(); |
674 | 640 |
675 // Call the media feature evaluation function. Assume no prefix and let | 641 // Call the media feature evaluation function. Assume no prefix and let |
676 // trampoline functions override the prefix if prefix is used. | 642 // trampoline functions override the prefix if prefix is used. |
677 EvalFunc func = gFunctionMap->get(expr->mediaFeature().impl()); | 643 EvalFunc func = gFunctionMap->get(expr->mediaFeature().impl()); |
678 if (func) | 644 if (func) |
679 return func(expr->value(), NoPrefix, *m_mediaValues); | 645 return func(expr->value(), NoPrefix, *m_mediaValues); |
680 | 646 |
681 return false; | 647 return false; |
682 } | 648 } |
683 | 649 |
684 } // namespace | 650 } // namespace |
OLD | NEW |