1 /* 2 * Copyright (C) 2013 Google Inc. All rights reserved. 3 * 4 * * Redistributions of source code must retain the above copyright 5 * notice, this list of conditions and the following disclaimer. 6 * * Redistributions in binary form must reproduce the above 7 * copyright notice, this list of conditions and the following disclaimer 8 * in the documentation and/or other materials provided with the 9 * distribution. 10 * * Neither the name of Google Inc. nor the names of its 11 * contributors may be used to endorse or promote products derived from 12 * this software without specific prior written permission. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #ifndef StyleBuilderConverter_h 28 #define StyleBuilderConverter_h 29 30 #include "core/css/CSSValue.h" 31 #include "core/css/resolver/StyleResolverState.h" 32 #include "core/rendering/RenderView.h" 33 #include "core/rendering/style/ShadowList.h" 34 #include "core/svg/SVGLength.h" 35 #include "platform/LengthSize.h" 36 37 namespace WebCore { 38 39 // Note that we assume the parser only allows valid CSSValue types. 40 41 class StyleBuilderConverter { 42 public: 43 static String convertFragmentIdentifier(StyleResolverState&, CSSValue*); 44 template <typename T> static T convertComputedLength(StyleResolverState&, CSSValue*); 45 template <typename T> static T convertLineWidth(StyleResolverState&, CSSValue*); 46 static Length convertLength(StyleResolverState&, CSSValue*); 47 static Length convertLengthOrAuto(StyleResolverState&, CSSValue*); 48 static Length convertLengthSizing(StyleResolverState&, CSSValue*); 49 static Length convertLengthMaxSizing(StyleResolverState&, CSSValue*); 50 static LengthPoint convertLengthPoint(StyleResolverState&, CSSValue*); 51 static float convertNumberOrPercentage(StyleResolverState&, CSSValue*); 52 static LengthSize convertRadius(StyleResolverState&, CSSValue*); 53 static PassRefPtr<ShadowList> convertShadow(StyleResolverState&, CSSValue*); 54 static float convertSpacing(StyleResolverState&, CSSValue*); 55 template <CSSValueID IdForNone> static AtomicString convertString(StyleResolverState&, CSSValue*); 56 static SVGLength convertSVGLength(StyleResolverState&, CSSValue*); 57 }; 58 59 template <typename T> 60 T StyleBuilderConverter::convertComputedLength(StyleResolverState& state, CSSValue* value) 61 { 62 return toCSSPrimitiveValue(value)->computeLength<T>(state.cssToLengthConversionData()); 63 } 64 65 template <typename T> 66 T StyleBuilderConverter::convertLineWidth(StyleResolverState& state, CSSValue* value) 67 { 68 CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(value); 69 CSSValueID valueID = primitiveValue->getValueID(); 70 if (valueID == CSSValueThin) 71 return 1; 72 if (valueID == CSSValueMedium) 73 return 3; 74 if (valueID == CSSValueThick) 75 return 5; 76 if (primitiveValue->isViewportPercentageLength()) 77 return intValueForLength(primitiveValue->viewportPercentageLength(), 0, state.document().renderView()); 78 if (valueID == CSSValueInvalid) { 79 // Any original result that was >= 1 should not be allowed to fall below 1. 80 // This keeps border lines from vanishing. 81 T result = primitiveValue->computeLength<T>(state.cssToLengthConversionData()); 82 if (state.style()->effectiveZoom() < 1.0f && result < 1.0) { 83 T originalLength = primitiveValue->computeLength<T>(state.cssToLengthConversionData().copyWithAdjustedZoom(1.0)); 84 if (originalLength >= 1.0) 85 return 1.0; 86 } 87 return result; 88 } 89 ASSERT_NOT_REACHED(); 90 return 0; 91 } 92 93 template <CSSValueID IdForNone> 94 AtomicString StyleBuilderConverter::convertString(StyleResolverState&, CSSValue* value) 95 { 96 CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(value); 97 if (primitiveValue->getValueID() == IdForNone) 98 return nullAtom; 99 return primitiveValue->getStringValue(); 100 } 101 102 } // namespace WebCore 103 104 #endif 105