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 "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 CalculationValue;
     47 class CalcExpressionNode;
     48 class Length;
     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 CSSToLengthConversionData&) const = 0;
     70     virtual double doubleValue() const = 0;
     71     virtual double computeLengthPx(const CSSToLengthConversionData&) 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*, ValueRange);
     96     static PassRefPtr<CSSCalcValue> create(PassRefPtr<CSSCalcExpressionNode>, ValueRange = ValueRangeAll);
     97     static PassRefPtr<CSSCalcValue> create(const CalculationValue* value, float zoom) { return adoptRef(new CSSCalcValue(value, zoom)); }
     98 
     99     static PassRefPtr<CSSCalcExpressionNode> createExpressionNode(PassRefPtr<CSSPrimitiveValue>, bool isInteger = false);
    100     static PassRefPtr<CSSCalcExpressionNode> createExpressionNode(PassRefPtr<CSSCalcExpressionNode>, PassRefPtr<CSSCalcExpressionNode>, CalcOperator);
    101     static PassRefPtr<CSSCalcExpressionNode> createExpressionNode(const CalcExpressionNode*, float zoom);
    102     static PassRefPtr<CSSCalcExpressionNode> createExpressionNode(const Length&, float zoom);
    103 
    104     PassRefPtr<CalculationValue> toCalcValue(const CSSToLengthConversionData& conversionData) const
    105     {
    106         return CalculationValue::create(m_expression->toCalcValue(conversionData), m_nonNegative ? ValueRangeNonNegative : ValueRangeAll);
    107     }
    108     CalculationCategory category() const { return m_expression->category(); }
    109     bool isInt() const { return m_expression->isInteger(); }
    110     double doubleValue() const;
    111     bool isNegative() const { return m_expression->doubleValue() < 0; }
    112     ValueRange permittedValueRange() { return m_nonNegative ? ValueRangeNonNegative : ValueRangeAll; }
    113     double computeLengthPx(const CSSToLengthConversionData&) const;
    114     CSSCalcExpressionNode* expressionNode() const { return m_expression.get(); }
    115 
    116     String customCSSText() const;
    117     bool equals(const CSSCalcValue&) const;
    118     String customSerializeResolvingVariables(const HashMap<AtomicString, String>&) const;
    119     bool hasVariableReference() const;
    120 
    121 private:
    122     CSSCalcValue(PassRefPtr<CSSCalcExpressionNode> expression, ValueRange range)
    123         : CSSValue(CalculationClass)
    124         , m_expression(expression)
    125         , m_nonNegative(range == ValueRangeNonNegative)
    126     {
    127     }
    128     CSSCalcValue(const CalculationValue* value, float zoom)
    129         : CSSValue(CalculationClass)
    130         , m_expression(createExpressionNode(value->expression(), zoom))
    131         , m_nonNegative(value->isNonNegative())
    132     {
    133     }
    134 
    135     double clampToPermittedRange(double) const;
    136 
    137     const RefPtr<CSSCalcExpressionNode> m_expression;
    138     const bool m_nonNegative;
    139 };
    140 
    141 DEFINE_CSS_VALUE_TYPE_CASTS(CSSCalcValue, isCalcValue());
    142 
    143 } // namespace WebCore
    144 
    145 
    146 #endif // CSSCalculationValue_h
    147