Chromium Code Reviews| Index: Source/core/css/MediaQueryExp.cpp |
| diff --git a/Source/core/css/MediaQueryExp.cpp b/Source/core/css/MediaQueryExp.cpp |
| index 6009840c23f68cab6a33b98a552094149a43581c..84eb3f6bc7ac5dc5f09e02359cac4499d2988691 100644 |
| --- a/Source/core/css/MediaQueryExp.cpp |
| +++ b/Source/core/css/MediaQueryExp.cpp |
| @@ -32,29 +32,39 @@ |
| #include "CSSValueKeywords.h" |
| #include "core/css/CSSAspectRatioValue.h" |
| -#include "core/css/parser/BisonCSSParser.h" |
| +#include "core/css/CSSParserValues.h" |
| #include "core/css/CSSPrimitiveValue.h" |
| +#include "core/html/parser/HTMLParserIdioms.h" |
| #include "wtf/text/StringBuilder.h" |
| namespace WebCore { |
| -static inline bool featureWithCSSValueID(const AtomicString& mediaFeature, const CSSParserValue* value) |
| +using namespace MediaFeatureNames; |
| + |
| +// Since the comparisons we do here are on static strings and since most of the strings are not equal, |
| +// we use this function to only compare the StringImpl addresses. |
| +static inline bool stringImplEquals(const String& mediaFeature, const String& otherMediaFeature) |
| +{ |
| + return (mediaFeature.impl() == otherMediaFeature.impl()); |
| +} |
|
abarth-chromium
2014/03/06 16:59:58
I'm still not convinced this optimization is impor
abarth-chromium
2014/03/06 17:05:45
Maybe add a general equal codepath that checks isA
|
| + |
| +static inline bool featureWithCSSValueID(const String& mediaFeature, const CSSParserValue* value) |
| { |
| if (!value->id) |
| return false; |
| - return mediaFeature == MediaFeatureNames::orientationMediaFeature |
| - || mediaFeature == MediaFeatureNames::viewModeMediaFeature |
| - || mediaFeature == MediaFeatureNames::pointerMediaFeature |
| - || mediaFeature == MediaFeatureNames::scanMediaFeature; |
| + return stringImplEquals(mediaFeature, orientationMediaFeature) |
| + || stringImplEquals(mediaFeature, viewModeMediaFeature) |
| + || stringImplEquals(mediaFeature, pointerMediaFeature) |
| + || stringImplEquals(mediaFeature, scanMediaFeature); |
| } |
| -static inline bool featureWithValidIdent(const AtomicString& mediaFeature, CSSValueID ident) |
| +static inline bool featureWithValidIdent(const String& mediaFeature, CSSValueID ident) |
| { |
| - if (mediaFeature == MediaFeatureNames::orientationMediaFeature) |
| + if (stringImplEquals(mediaFeature, orientationMediaFeature)) |
| return ident == CSSValuePortrait || ident == CSSValueLandscape; |
| - if (mediaFeature == MediaFeatureNames::viewModeMediaFeature) { |
| + if (stringImplEquals(mediaFeature, viewModeMediaFeature)) { |
| switch (ident) { |
| case CSSValueWindowed: |
| case CSSValueFloating: |
| @@ -67,132 +77,133 @@ static inline bool featureWithValidIdent(const AtomicString& mediaFeature, CSSVa |
| } |
| } |
| - if (mediaFeature == MediaFeatureNames::pointerMediaFeature) |
| + if (stringImplEquals(mediaFeature, pointerMediaFeature)) |
| return ident == CSSValueNone || ident == CSSValueCoarse || ident == CSSValueFine; |
| - if (mediaFeature == MediaFeatureNames::scanMediaFeature) |
| + if (stringImplEquals(mediaFeature, scanMediaFeature)) |
| return ident == CSSValueInterlace || ident == CSSValueProgressive; |
| ASSERT_NOT_REACHED(); |
| return false; |
| } |
| -static inline bool featureWithValidPositiveLength(const AtomicString& mediaFeature, const CSSParserValue* value) |
| +static inline bool featureWithValidPositiveLength(const String& mediaFeature, const CSSParserValue* value) |
| { |
| if (!(((value->unit >= CSSPrimitiveValue::CSS_EMS && value->unit <= CSSPrimitiveValue::CSS_PC) || value->unit == CSSPrimitiveValue::CSS_REMS) || (value->unit == CSSPrimitiveValue::CSS_NUMBER && !(value->fValue))) || value->fValue < 0) |
| return false; |
| - return mediaFeature == MediaFeatureNames::heightMediaFeature |
| - || mediaFeature == MediaFeatureNames::maxHeightMediaFeature |
| - || mediaFeature == MediaFeatureNames::minHeightMediaFeature |
| - || mediaFeature == MediaFeatureNames::widthMediaFeature |
| - || mediaFeature == MediaFeatureNames::maxWidthMediaFeature |
| - || mediaFeature == MediaFeatureNames::minWidthMediaFeature |
| - || mediaFeature == MediaFeatureNames::deviceHeightMediaFeature |
| - || mediaFeature == MediaFeatureNames::maxDeviceHeightMediaFeature |
| - || mediaFeature == MediaFeatureNames::minDeviceHeightMediaFeature |
| - || mediaFeature == MediaFeatureNames::deviceWidthMediaFeature |
| - || mediaFeature == MediaFeatureNames::maxDeviceWidthMediaFeature |
| - || mediaFeature == MediaFeatureNames::minDeviceWidthMediaFeature; |
| + |
| + return stringImplEquals(mediaFeature, heightMediaFeature) |
| + || stringImplEquals(mediaFeature, maxHeightMediaFeature) |
| + || stringImplEquals(mediaFeature, minHeightMediaFeature) |
| + || stringImplEquals(mediaFeature, widthMediaFeature) |
| + || stringImplEquals(mediaFeature, maxWidthMediaFeature) |
| + || stringImplEquals(mediaFeature, minWidthMediaFeature) |
| + || stringImplEquals(mediaFeature, deviceHeightMediaFeature) |
| + || stringImplEquals(mediaFeature, maxDeviceHeightMediaFeature) |
| + || stringImplEquals(mediaFeature, minDeviceHeightMediaFeature) |
| + || stringImplEquals(mediaFeature, deviceWidthMediaFeature) |
| + || stringImplEquals(mediaFeature, minDeviceWidthMediaFeature) |
| + || stringImplEquals(mediaFeature, maxDeviceWidthMediaFeature); |
| } |
| -static inline bool featureWithValidDensity(const AtomicString& mediaFeature, const CSSParserValue* value) |
| +static inline bool featureWithValidDensity(const String& mediaFeature, const CSSParserValue* value) |
| { |
| if ((value->unit != CSSPrimitiveValue::CSS_DPPX && value->unit != CSSPrimitiveValue::CSS_DPI && value->unit != CSSPrimitiveValue::CSS_DPCM) || value->fValue <= 0) |
| return false; |
| - return mediaFeature == MediaFeatureNames::resolutionMediaFeature |
| - || mediaFeature == MediaFeatureNames::maxResolutionMediaFeature |
| - || mediaFeature == MediaFeatureNames::minResolutionMediaFeature; |
| + return stringImplEquals(mediaFeature, resolutionMediaFeature) |
| + || stringImplEquals(mediaFeature, minResolutionMediaFeature) |
| + || stringImplEquals(mediaFeature, maxResolutionMediaFeature); |
| } |
| -static inline bool featureWithPositiveInteger(const AtomicString& mediaFeature, const CSSParserValue* value) |
| +static inline bool featureWithPositiveInteger(const String& mediaFeature, const CSSParserValue* value) |
| { |
| if (!value->isInt || value->fValue < 0) |
| return false; |
| - return mediaFeature == MediaFeatureNames::colorMediaFeature |
| - || mediaFeature == MediaFeatureNames::maxColorMediaFeature |
| - || mediaFeature == MediaFeatureNames::minColorMediaFeature |
| - || mediaFeature == MediaFeatureNames::colorIndexMediaFeature |
| - || mediaFeature == MediaFeatureNames::maxColorIndexMediaFeature |
| - || mediaFeature == MediaFeatureNames::minColorIndexMediaFeature |
| - || mediaFeature == MediaFeatureNames::monochromeMediaFeature |
| - || mediaFeature == MediaFeatureNames::minMonochromeMediaFeature |
| - || mediaFeature == MediaFeatureNames::maxMonochromeMediaFeature; |
| + return stringImplEquals(mediaFeature, colorMediaFeature) |
| + || stringImplEquals(mediaFeature, maxColorMediaFeature) |
| + || stringImplEquals(mediaFeature, minColorMediaFeature) |
| + || stringImplEquals(mediaFeature, colorIndexMediaFeature) |
| + || stringImplEquals(mediaFeature, maxColorIndexMediaFeature) |
| + || stringImplEquals(mediaFeature, minColorIndexMediaFeature) |
| + || stringImplEquals(mediaFeature, monochromeMediaFeature) |
| + || stringImplEquals(mediaFeature, maxMonochromeMediaFeature) |
| + || stringImplEquals(mediaFeature, minMonochromeMediaFeature); |
| } |
| -static inline bool featureWithPositiveNumber(const AtomicString& mediaFeature, const CSSParserValue* value) |
| +static inline bool featureWithPositiveNumber(const String& mediaFeature, const CSSParserValue* value) |
| { |
| if (value->unit != CSSPrimitiveValue::CSS_NUMBER || value->fValue < 0) |
| return false; |
| - return mediaFeature == MediaFeatureNames::transform2dMediaFeature |
| - || mediaFeature == MediaFeatureNames::transform3dMediaFeature |
| - || mediaFeature == MediaFeatureNames::animationMediaFeature |
| - || mediaFeature == MediaFeatureNames::devicePixelRatioMediaFeature |
| - || mediaFeature == MediaFeatureNames::maxDevicePixelRatioMediaFeature |
| - || mediaFeature == MediaFeatureNames::minDevicePixelRatioMediaFeature; |
| + return stringImplEquals(mediaFeature, transform2dMediaFeature) |
| + || stringImplEquals(mediaFeature, transform3dMediaFeature) |
| + || stringImplEquals(mediaFeature, animationMediaFeature) |
| + || stringImplEquals(mediaFeature, devicePixelRatioMediaFeature) |
| + || stringImplEquals(mediaFeature, maxDevicePixelRatioMediaFeature) |
| + || stringImplEquals(mediaFeature, minDevicePixelRatioMediaFeature); |
| } |
| -static inline bool featureWithZeroOrOne(const AtomicString& mediaFeature, const CSSParserValue* value) |
| +static inline bool featureWithZeroOrOne(const String& mediaFeature, const CSSParserValue* value) |
| { |
| if (!value->isInt || !(value->fValue == 1 || !value->fValue)) |
| return false; |
| - return mediaFeature == MediaFeatureNames::gridMediaFeature |
| - || mediaFeature == MediaFeatureNames::hoverMediaFeature; |
| + return stringImplEquals(mediaFeature, gridMediaFeature) |
| + || stringImplEquals(mediaFeature, hoverMediaFeature); |
| } |
| -static inline bool featureWithAspectRatio(const AtomicString& mediaFeature) |
| +static inline bool featureWithAspectRatio(const String& mediaFeature) |
| { |
| - return mediaFeature == MediaFeatureNames::aspectRatioMediaFeature |
| - || mediaFeature == MediaFeatureNames::deviceAspectRatioMediaFeature |
| - || mediaFeature == MediaFeatureNames::minAspectRatioMediaFeature |
| - || mediaFeature == MediaFeatureNames::maxAspectRatioMediaFeature |
| - || mediaFeature == MediaFeatureNames::minDeviceAspectRatioMediaFeature |
| - || mediaFeature == MediaFeatureNames::maxDeviceAspectRatioMediaFeature; |
| + return stringImplEquals(mediaFeature, aspectRatioMediaFeature) |
| + || stringImplEquals(mediaFeature, deviceAspectRatioMediaFeature) |
| + || stringImplEquals(mediaFeature, minAspectRatioMediaFeature) |
| + || stringImplEquals(mediaFeature, maxAspectRatioMediaFeature) |
| + || stringImplEquals(mediaFeature, minDeviceAspectRatioMediaFeature) |
| + || stringImplEquals(mediaFeature, maxDeviceAspectRatioMediaFeature); |
| } |
| -static inline bool featureWithoutValue(const AtomicString& mediaFeature) |
| +static inline bool featureWithoutValue(const String& mediaFeature) |
| { |
| // Media features that are prefixed by min/max cannot be used without a value. |
| - return mediaFeature == MediaFeatureNames::monochromeMediaFeature |
| - || mediaFeature == MediaFeatureNames::colorMediaFeature |
| - || mediaFeature == MediaFeatureNames::colorIndexMediaFeature |
| - || mediaFeature == MediaFeatureNames::gridMediaFeature |
| - || mediaFeature == MediaFeatureNames::heightMediaFeature |
| - || mediaFeature == MediaFeatureNames::widthMediaFeature |
| - || mediaFeature == MediaFeatureNames::deviceHeightMediaFeature |
| - || mediaFeature == MediaFeatureNames::deviceWidthMediaFeature |
| - || mediaFeature == MediaFeatureNames::orientationMediaFeature |
| - || mediaFeature == MediaFeatureNames::aspectRatioMediaFeature |
| - || mediaFeature == MediaFeatureNames::deviceAspectRatioMediaFeature |
| - || mediaFeature == MediaFeatureNames::hoverMediaFeature |
| - || mediaFeature == MediaFeatureNames::transform2dMediaFeature |
| - || mediaFeature == MediaFeatureNames::transform3dMediaFeature |
| - || mediaFeature == MediaFeatureNames::animationMediaFeature |
| - || mediaFeature == MediaFeatureNames::viewModeMediaFeature |
| - || mediaFeature == MediaFeatureNames::pointerMediaFeature |
| - || mediaFeature == MediaFeatureNames::devicePixelRatioMediaFeature |
| - || mediaFeature == MediaFeatureNames::resolutionMediaFeature |
| - || mediaFeature == MediaFeatureNames::scanMediaFeature; |
| + return stringImplEquals(mediaFeature, monochromeMediaFeature) |
| + || stringImplEquals(mediaFeature, colorMediaFeature) |
| + || stringImplEquals(mediaFeature, colorIndexMediaFeature) |
| + || stringImplEquals(mediaFeature, gridMediaFeature) |
| + || stringImplEquals(mediaFeature, heightMediaFeature) |
| + || stringImplEquals(mediaFeature, widthMediaFeature) |
| + || stringImplEquals(mediaFeature, deviceHeightMediaFeature) |
| + || stringImplEquals(mediaFeature, deviceWidthMediaFeature) |
| + || stringImplEquals(mediaFeature, orientationMediaFeature) |
| + || stringImplEquals(mediaFeature, aspectRatioMediaFeature) |
| + || stringImplEquals(mediaFeature, deviceAspectRatioMediaFeature) |
| + || stringImplEquals(mediaFeature, hoverMediaFeature) |
| + || stringImplEquals(mediaFeature, transform2dMediaFeature) |
| + || stringImplEquals(mediaFeature, transform3dMediaFeature) |
| + || stringImplEquals(mediaFeature, animationMediaFeature) |
| + || stringImplEquals(mediaFeature, viewModeMediaFeature) |
| + || stringImplEquals(mediaFeature, pointerMediaFeature) |
| + || stringImplEquals(mediaFeature, devicePixelRatioMediaFeature) |
| + || stringImplEquals(mediaFeature, resolutionMediaFeature) |
| + || stringImplEquals(mediaFeature, scanMediaFeature); |
| } |
| bool MediaQueryExp::isViewportDependent() const |
| { |
| - return m_mediaFeature == MediaFeatureNames::widthMediaFeature |
| - || m_mediaFeature == MediaFeatureNames::heightMediaFeature |
| - || m_mediaFeature == MediaFeatureNames::minWidthMediaFeature |
| - || m_mediaFeature == MediaFeatureNames::minHeightMediaFeature |
| - || m_mediaFeature == MediaFeatureNames::maxWidthMediaFeature |
| - || m_mediaFeature == MediaFeatureNames::maxHeightMediaFeature |
| - || m_mediaFeature == MediaFeatureNames::orientationMediaFeature |
| - || m_mediaFeature == MediaFeatureNames::aspectRatioMediaFeature |
| - || m_mediaFeature == MediaFeatureNames::minAspectRatioMediaFeature |
| - || m_mediaFeature == MediaFeatureNames::devicePixelRatioMediaFeature |
| - || m_mediaFeature == MediaFeatureNames::resolutionMediaFeature |
| - || m_mediaFeature == MediaFeatureNames::maxAspectRatioMediaFeature; |
| + return stringImplEquals(m_mediaFeature, widthMediaFeature) |
| + || stringImplEquals(m_mediaFeature, heightMediaFeature) |
| + || stringImplEquals(m_mediaFeature, minWidthMediaFeature) |
| + || stringImplEquals(m_mediaFeature, minHeightMediaFeature) |
| + || stringImplEquals(m_mediaFeature, maxWidthMediaFeature) |
| + || stringImplEquals(m_mediaFeature, maxHeightMediaFeature) |
| + || stringImplEquals(m_mediaFeature, orientationMediaFeature) |
| + || stringImplEquals(m_mediaFeature, aspectRatioMediaFeature) |
| + || stringImplEquals(m_mediaFeature, minAspectRatioMediaFeature) |
| + || stringImplEquals(m_mediaFeature, devicePixelRatioMediaFeature) |
| + || stringImplEquals(m_mediaFeature, resolutionMediaFeature) |
| + || stringImplEquals(m_mediaFeature, maxAspectRatioMediaFeature); |
| } |
| MediaQueryExp::MediaQueryExp(const MediaQueryExp& other) |
| @@ -201,47 +212,48 @@ MediaQueryExp::MediaQueryExp(const MediaQueryExp& other) |
| { |
| } |
| -MediaQueryExp::MediaQueryExp(const AtomicString& mediaFeature, PassRefPtrWillBeRawPtr<CSSValue> value) |
| +MediaQueryExp::MediaQueryExp(const String& mediaFeature, PassRefPtrWillBeRawPtr<CSSValue> value) |
| : m_mediaFeature(mediaFeature) |
| , m_value(value) |
| { |
| } |
| -PassOwnPtrWillBeRawPtr<MediaQueryExp> MediaQueryExp::create(const AtomicString& mediaFeature, CSSParserValueList* valueList) |
| +PassOwnPtrWillBeRawPtr<MediaQueryExp> MediaQueryExp::create(const String& mediaFeature, CSSParserValueList* valueList) |
| { |
| RefPtrWillBeRawPtr<CSSValue> cssValue; |
| bool isValid = false; |
| + String lowerMediaFeature = attemptStaticStringCreation(mediaFeature.lower()); |
| // Create value for media query expression that must have 1 or more values. |
| - if (valueList) { |
| + if (valueList && valueList->size() > 0) { |
| if (valueList->size() == 1) { |
| CSSParserValue* value = valueList->current(); |
| - if (featureWithCSSValueID(mediaFeature, value)) { |
| + if (featureWithCSSValueID(lowerMediaFeature, value)) { |
| // Media features that use CSSValueIDs. |
| cssValue = CSSPrimitiveValue::createIdentifier(value->id); |
| - if (!featureWithValidIdent(mediaFeature, toCSSPrimitiveValue(cssValue.get())->getValueID())) |
| + if (!featureWithValidIdent(lowerMediaFeature, toCSSPrimitiveValue(cssValue.get())->getValueID())) |
| cssValue.clear(); |
| - } else if (featureWithValidDensity(mediaFeature, value)) { |
| + } else if (featureWithValidDensity(lowerMediaFeature, value)) { |
| // Media features that must have non-negative <density>, ie. dppx, dpi or dpcm. |
| cssValue = CSSPrimitiveValue::create(value->fValue, (CSSPrimitiveValue::UnitTypes) value->unit); |
| - } else if (featureWithValidPositiveLength(mediaFeature, value)) { |
| + } else if (featureWithValidPositiveLength(lowerMediaFeature, value)) { |
| // Media features that must have non-negative <lenght> or number value. |
| cssValue = CSSPrimitiveValue::create(value->fValue, (CSSPrimitiveValue::UnitTypes) value->unit); |
| - } else if (featureWithPositiveInteger(mediaFeature, value)) { |
| + } else if (featureWithPositiveInteger(lowerMediaFeature, value)) { |
| // Media features that must have non-negative integer value. |
| cssValue = CSSPrimitiveValue::create(value->fValue, CSSPrimitiveValue::CSS_NUMBER); |
| - } else if (featureWithPositiveNumber(mediaFeature, value)) { |
| + } else if (featureWithPositiveNumber(lowerMediaFeature, value)) { |
| // Media features that must have non-negative number value. |
| cssValue = CSSPrimitiveValue::create(value->fValue, CSSPrimitiveValue::CSS_NUMBER); |
| - } else if (featureWithZeroOrOne(mediaFeature, value)) { |
| + } else if (featureWithZeroOrOne(lowerMediaFeature, value)) { |
| // Media features that must have (0|1) value. |
| cssValue = CSSPrimitiveValue::create(value->fValue, CSSPrimitiveValue::CSS_NUMBER); |
| } |
| isValid = cssValue; |
| - } else if (valueList->size() == 3 && featureWithAspectRatio(mediaFeature)) { |
| + } else if (valueList->size() == 3 && featureWithAspectRatio(lowerMediaFeature)) { |
| // Create list of values. |
| // Currently accepts only <integer>/<integer>. |
| // Applicable to device-aspect-ratio and aspec-ratio. |
| @@ -267,14 +279,14 @@ PassOwnPtrWillBeRawPtr<MediaQueryExp> MediaQueryExp::create(const AtomicString& |
| if (isValid) |
| cssValue = CSSAspectRatioValue::create(numeratorValue, denominatorValue); |
| } |
| - } else if (featureWithoutValue(mediaFeature)) { |
| + } else if (featureWithoutValue(lowerMediaFeature)) { |
| isValid = true; |
| } |
| if (!isValid) |
| return nullptr; |
| - return adoptPtrWillBeNoop(new MediaQueryExp(mediaFeature, cssValue)); |
| + return adoptPtrWillBeNoop(new MediaQueryExp(lowerMediaFeature, cssValue)); |
| } |
| MediaQueryExp::~MediaQueryExp() |