Home | History | Annotate | Download | only in css
      1 /*
      2  * Copyright (C) 2011, 2012 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #ifndef CSSCalculationValue_h
     32 #define CSSCalculationValue_h
     33 
     34 #include "core/css/CSSParserValues.h"
     35 #include "core/css/CSSPrimitiveValue.h"
     36 #include "core/css/CSSValue.h"
     37 #include "core/platform/CalculationValue.h"
     38 #include "wtf/PassOwnPtr.h"
     39 #include "wtf/RefCounted.h"
     40 #include "wtf/RefPtr.h"
     41 
     42 namespace WebCore {
     43 
     44 class CSSParserValueList;
     45 class CSSValueList;
     46 class RenderStyle;
     47 class CalculationValue;
     48 class CalcExpressionNode;
     49 
     50 enum CalculationCategory {
     51     CalcNumber = 0,
     52     CalcLength,
     53     CalcPercent,
     54     CalcPercentNumber,
     55     CalcPercentLength,
     56     CalcVariable,
     57     CalcOther
     58 };
     59 
     60 class CSSCalcExpressionNode : public RefCounted<CSSCalcExpressionNode> {
     61 public:
     62     enum Type {
     63         CssCalcPrimitiveValue = 1,
     64         CssCalcBinaryOperation
     65     };
     66 
     67     virtual ~CSSCalcExpressionNode() = 0;
     68     virtual bool isZero() const = 0;
     69     virtual PassOwnPtr<CalcExpressionNode> toCalcValue(const RenderStyle*, const RenderStyle* rootStyle, double zoom = 1.0) const = 0;
     70     virtual double doubleValue() const = 0;
     71     virtual double computeLengthPx(const RenderStyle* currentStyle, const RenderStyle* rootStyle, double multiplier = 1.0, bool computingFontSize = false) const = 0;
     72     virtual String customCssText() const = 0;
     73     virtual String serializeResolvingVariables(const HashMap<AtomicString, String>&) const = 0;
     74     virtual bool hasVariableReference() const = 0;
     75     virtual bool equals(const CSSCalcExpressionNode& other) const { return m_category == other.m_category && m_isInteger == other.m_isInteger; }
     76     virtual Type type() const = 0;
     77 
     78     CalculationCategory category() const { return m_category; }
     79     virtual CSSPrimitiveValue::UnitTypes primitiveType() const = 0;
     80     bool isInteger() const { return m_isInteger; }
     81 
     82 protected:
     83     CSSCalcExpressionNode(CalculationCategory category, bool isInteger)
     84         : m_category(category)
     85         , m_isInteger(isInteger)
     86     {
     87     }
     88 
     89     CalculationCategory m_category;
     90     bool m_isInteger;
     91 };
     92 
     93 class CSSCalcValue : public CSSValue {
     94 public:
     95     static PassRefPtr<CSSCalcValue> create(CSSParserString name, CSSParserValueList*, CalculationPermittedValueRange);
     96     static PassRefPtr<CSSCalcValue> create(PassRefPtr<CSSCalcExpressionNode>, CalculationPermittedValueRange = CalculationRangeAll);
     97 
     98     static PassRefPtr<CSSCalcExpressionNode> createExpressionNode(PassRefPtr<CSSPrimitiveValue>, bool isInteger = false);
     99     static PassRefPtr<CSSCalcExpressionNode> createExpressionNode(PassRefPtr<CSSCalcExpressionNode>, PassRefPtr<CSSCalcExpressionNode>, CalcOperator);
    100 
    101     PassRefPtr<CalculationValue> toCalcValue(const RenderStyle* style, const RenderStyle* rootStyle, double zoom = 1.0) const
    102     {
    103         return CalculationValue::create(m_expression->toCalcValue(style, rootStyle, zoom), m_nonNegative ? CalculationRangeNonNegative : CalculationRangeAll);
    104     }
    105     CalculationCategory category() const { return m_expression->category(); }
    106     bool isInt() const { return m_expression->isInteger(); }
    107     double doubleValue() const;
    108     bool isNegative() const { return m_expression->doubleValue() < 0; }
    109     double computeLengthPx(const RenderStyle* currentStyle, const RenderStyle* rootStyle, double multiplier = 1.0, bool computingFontSize = false) const;
    110     CSSCalcExpressionNode* expressionNode() const { return m_expression.get(); }
    111 
    112     String customCssText() const;
    113     bool equals(const CSSCalcValue&) const;
    114     String customSerializeResolvingVariables(const HashMap<AtomicString, String>&) const;
    115     bool hasVariableReference() const;
    116 
    117 private:
    118     CSSCalcValue(PassRefPtr<CSSCalcExpressionNode> expression, CalculationPermittedValueRange range)
    119         : CSSValue(CalculationClass)
    120         , m_expression(expression)
    121         , m_nonNegative(range == CalculationRangeNonNegative)
    122     {
    123     }
    124 
    125     double clampToPermittedRange(double) const;
    126 
    127     const RefPtr<CSSCalcExpressionNode> m_expression;
    128     const bool m_nonNegative;
    129 };
    130 
    131 inline CSSCalcValue* toCSSCalcValue(CSSValue* value)
    132 {
    133     ASSERT_WITH_SECURITY_IMPLICATION(!value || value->isCalculationValue());
    134     return static_cast<CSSCalcValue*>(value);
    135 }
    136 
    137 inline const CSSCalcValue* toCSSCalcValue(const CSSValue* value)
    138 {
    139     ASSERT_WITH_SECURITY_IMPLICATION(!value || value->isCalculationValue());
    140     return static_cast<const CSSCalcValue*>(value);
    141 }
    142 
    143 } // namespace WebCore
    144 
    145 
    146 #endif // CSSCalculationValue_h
    147