2 * Copyright (C) 2011, 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
31 #ifndef CSSCalculationValue_h
32 #define CSSCalculationValue_h
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"
44 class CSSParserValueList;
46 class CalculationValue;
47 class CalcExpressionNode;
50 enum CalculationCategory {
59 class CSSCalcExpressionNode : public RefCountedWillBeGarbageCollected<CSSCalcExpressionNode> {
63 CssCalcPrimitiveValue = 1,
64 CssCalcBinaryOperation
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 bool equals(const CSSCalcExpressionNode& other) const { return m_category == other.m_category && m_isInteger == other.m_isInteger; }
74 virtual Type type() const = 0;
76 CalculationCategory category() const { return m_category; }
77 virtual CSSPrimitiveValue::UnitTypes primitiveType() const = 0;
78 bool isInteger() const { return m_isInteger; }
80 virtual void trace(Visitor*) { }
83 CSSCalcExpressionNode(CalculationCategory category, bool isInteger)
84 : m_category(category)
85 , m_isInteger(isInteger)
89 CalculationCategory m_category;
93 class CSSCalcValue : public CSSValue {
95 static PassRefPtrWillBeRawPtr<CSSCalcValue> create(CSSParserString name, CSSParserValueList*, ValueRange);
96 static PassRefPtrWillBeRawPtr<CSSCalcValue> create(PassRefPtrWillBeRawPtr<CSSCalcExpressionNode>, ValueRange = ValueRangeAll);
97 static PassRefPtrWillBeRawPtr<CSSCalcValue> create(const CalculationValue* value, float zoom) { return adoptRefCountedWillBeRefCountedGarbageCollected(new CSSCalcValue(value, zoom)); }
99 static PassRefPtrWillBeRawPtr<CSSCalcExpressionNode> createExpressionNode(PassRefPtrWillBeRawPtr<CSSPrimitiveValue>, bool isInteger = false);
100 static PassRefPtrWillBeRawPtr<CSSCalcExpressionNode> createExpressionNode(PassRefPtrWillBeRawPtr<CSSCalcExpressionNode>, PassRefPtrWillBeRawPtr<CSSCalcExpressionNode>, CalcOperator);
101 static PassRefPtrWillBeRawPtr<CSSCalcExpressionNode> createExpressionNode(const CalcExpressionNode*, float zoom);
102 static PassRefPtrWillBeRawPtr<CSSCalcExpressionNode> createExpressionNode(const Length&, float zoom);
104 PassRefPtr<CalculationValue> toCalcValue(const CSSToLengthConversionData& conversionData) const
106 return CalculationValue::create(m_expression->toCalcValue(conversionData), m_nonNegative ? ValueRangeNonNegative : ValueRangeAll);
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(); }
116 String customCSSText() const;
117 bool equals(const CSSCalcValue&) const;
119 void traceAfterDispatch(Visitor*);
122 CSSCalcValue(PassRefPtrWillBeRawPtr<CSSCalcExpressionNode> expression, ValueRange range)
123 : CSSValue(CalculationClass)
124 , m_expression(expression)
125 , m_nonNegative(range == ValueRangeNonNegative)
128 CSSCalcValue(const CalculationValue* value, float zoom)
129 : CSSValue(CalculationClass)
130 , m_expression(createExpressionNode(value->expression(), zoom))
131 , m_nonNegative(value->isNonNegative())
135 double clampToPermittedRange(double) const;
137 const RefPtrWillBeMember<CSSCalcExpressionNode> m_expression;
138 const bool m_nonNegative;
141 DEFINE_CSS_VALUE_TYPE_CASTS(CSSCalcValue, isCalcValue());
143 } // namespace WebCore
146 #endif // CSSCalculationValue_h