Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: Source/core/css/MediaQueryEvaluator.cpp

Issue 240063003: Revert "A sizes attribute parser" (https://codereview.chromium.org/224733011) (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/core/css/CSSPrimitiveValue.cpp ('k') | Source/core/css/MediaQueryEvaluatorTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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/MediaValuesDynamic.h" 43 #include "core/css/MediaValues.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
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(MediaValuesDynamic::create(frame, style)) 88 , m_mediaValues(MediaValues::create(frame, style, MediaValues::DynamicMode))
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
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 computeLength(CSSValue* value, const MediaValues& mediaValues, int& result) 334 static bool computeLengthWithoutStyle(CSSPrimitiveValue* primitiveValue, int def aultFontSize, 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)
335 { 356 {
336 if (!value->isPrimitiveValue()) 357 if (!value->isPrimitiveValue())
337 return false; 358 return false;
338 359
339 CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(value); 360 CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(value);
340 361
341 if (primitiveValue->isNumber()) { 362 if (primitiveValue->isNumber()) {
342 result = primitiveValue->getIntValue(); 363 result = primitiveValue->getIntValue();
343 return !mediaValues.strictMode() || !result; 364 return !strict || !result;
344 } 365 }
345 366
346 if (primitiveValue->isLength()) 367 if (primitiveValue->isLength()) {
347 return mediaValues.computeLength(primitiveValue->getDoubleValue(), primi tiveValue->primitiveType(), result); 368 if (initialStyle) {
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 }
348 378
349 return false; 379 return false;
350 } 380 }
351 381
352 static bool deviceHeightMediaFeatureEval(CSSValue* value, MediaFeaturePrefix op, const MediaValues& mediaValues) 382 static bool deviceHeightMediaFeatureEval(CSSValue* value, MediaFeaturePrefix op, const MediaValues& mediaValues)
353 { 383 {
354 if (value) { 384 if (value) {
355 int length; 385 int length;
356 return computeLength(value, mediaValues, length) && compareValue(static_ cast<int>(mediaValues.deviceHeight()), length, op); 386 return computeLength(value, mediaValues.strictMode(), mediaValues.style( ), mediaValues.defaultFontSize(), length)
387 && compareValue(static_cast<int>(mediaValues.deviceHeight()), length , op);
357 } 388 }
358 // ({,min-,max-}device-height) 389 // ({,min-,max-}device-height)
359 // assume if we have a device, assume non-zero 390 // assume if we have a device, assume non-zero
360 return true; 391 return true;
361 } 392 }
362 393
363 static bool deviceWidthMediaFeatureEval(CSSValue* value, MediaFeaturePrefix op, const MediaValues& mediaValues) 394 static bool deviceWidthMediaFeatureEval(CSSValue* value, MediaFeaturePrefix op, const MediaValues& mediaValues)
364 { 395 {
365 if (value) { 396 if (value) {
366 int length; 397 int length;
367 return computeLength(value, mediaValues, length) && compareValue(static_ cast<int>(mediaValues.deviceWidth()), length, op); 398 return computeLength(value, mediaValues.strictMode(), mediaValues.style( ), mediaValues.defaultFontSize(), length)
399 && compareValue(static_cast<int>(mediaValues.deviceWidth()), length, op);
368 } 400 }
369 // ({,min-,max-}device-width) 401 // ({,min-,max-}device-width)
370 // assume if we have a device, assume non-zero 402 // assume if we have a device, assume non-zero
371 return true; 403 return true;
372 } 404 }
373 405
374 static bool heightMediaFeatureEval(CSSValue* value, MediaFeaturePrefix op, const MediaValues& mediaValues) 406 static bool heightMediaFeatureEval(CSSValue* value, MediaFeaturePrefix op, const MediaValues& mediaValues)
375 { 407 {
376 int height = mediaValues.viewportHeight(); 408 int height = mediaValues.viewportHeight();
377 if (value) { 409 if (value) {
378 int length; 410 int length;
379 return computeLength(value, mediaValues, length) && compareValue(height, length, op); 411 return computeLength(value, mediaValues.strictMode(), mediaValues.style( ), mediaValues.defaultFontSize(), length)
412 && compareValue(height, length, op);
380 } 413 }
381 414
382 return height; 415 return height;
383 } 416 }
384 417
385 static bool widthMediaFeatureEval(CSSValue* value, MediaFeaturePrefix op, const MediaValues& mediaValues) 418 static bool widthMediaFeatureEval(CSSValue* value, MediaFeaturePrefix op, const MediaValues& mediaValues)
386 { 419 {
387 int width = mediaValues.viewportWidth(); 420 int width = mediaValues.viewportWidth();
388 if (value) { 421 if (value) {
389 int length; 422 int length;
390 return computeLength(value, mediaValues, length) && compareValue(width, length, op); 423 return computeLength(value, mediaValues.strictMode(), mediaValues.style( ), mediaValues.defaultFontSize(), length)
424 && compareValue(width, length, op);
391 } 425 }
392 426
393 return width; 427 return width;
394 } 428 }
395 429
396 // Rest of the functions are trampolines which set the prefix according to the m edia feature expression used. 430 // Rest of the functions are trampolines which set the prefix according to the m edia feature expression used.
397 431
398 static bool minColorMediaFeatureEval(CSSValue* value, MediaFeaturePrefix, const MediaValues& mediaValues) 432 static bool minColorMediaFeatureEval(CSSValue* value, MediaFeaturePrefix, const MediaValues& mediaValues)
399 { 433 {
400 return colorMediaFeatureEval(value, MinPrefix, mediaValues); 434 return colorMediaFeatureEval(value, MinPrefix, mediaValues);
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
625 // Create the table. 659 // Create the table.
626 gFunctionMap = new FunctionMap; 660 gFunctionMap = new FunctionMap;
627 #define ADD_TO_FUNCTIONMAP(name) \ 661 #define ADD_TO_FUNCTIONMAP(name) \
628 gFunctionMap->set(name##MediaFeature.impl(), name##MediaFeatureEval); 662 gFunctionMap->set(name##MediaFeature.impl(), name##MediaFeatureEval);
629 CSS_MEDIAQUERY_NAMES_FOR_EACH_MEDIAFEATURE(ADD_TO_FUNCTIONMAP); 663 CSS_MEDIAQUERY_NAMES_FOR_EACH_MEDIAFEATURE(ADD_TO_FUNCTIONMAP);
630 #undef ADD_TO_FUNCTIONMAP 664 #undef ADD_TO_FUNCTIONMAP
631 } 665 }
632 666
633 bool MediaQueryEvaluator::eval(const MediaQueryExp* expr) const 667 bool MediaQueryEvaluator::eval(const MediaQueryExp* expr) const
634 { 668 {
635 if (!m_mediaValues || !m_mediaValues->hasValues()) 669 if (!m_mediaValues)
636 return m_expectedResult; 670 return m_expectedResult;
637 671
638 if (!gFunctionMap) 672 if (!gFunctionMap)
639 createFunctionMap(); 673 createFunctionMap();
640 674
641 // Call the media feature evaluation function. Assume no prefix and let 675 // Call the media feature evaluation function. Assume no prefix and let
642 // trampoline functions override the prefix if prefix is used. 676 // trampoline functions override the prefix if prefix is used.
643 EvalFunc func = gFunctionMap->get(expr->mediaFeature().impl()); 677 EvalFunc func = gFunctionMap->get(expr->mediaFeature().impl());
644 if (func) 678 if (func)
645 return func(expr->value(), NoPrefix, *m_mediaValues); 679 return func(expr->value(), NoPrefix, *m_mediaValues);
646 680
647 return false; 681 return false;
648 } 682 }
649 683
650 } // namespace 684 } // namespace
OLDNEW
« no previous file with comments | « Source/core/css/CSSPrimitiveValue.cpp ('k') | Source/core/css/MediaQueryEvaluatorTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698