Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(353)

Side by Side Diff: third_party/WebKit/Source/core/css/CSSPrimitiveValue.h

Issue 2346193002: Split CSSPrimitiveValue into CSSPrimitiveValue and CSSIdentifierValue (Closed)
Patch Set: Rebase please work Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * (C) 1999-2003 Lars Knoll (knoll@kde.org) 2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved. 3 * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
4 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org> 4 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
5 * 5 *
6 * This library is free software; you can redistribute it and/or 6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public 7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either 8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version. 9 * version 2 of the License, or (at your option) any later version.
10 * 10 *
(...skipping 11 matching lines...) Expand all
22 #ifndef CSSPrimitiveValue_h 22 #ifndef CSSPrimitiveValue_h
23 #define CSSPrimitiveValue_h 23 #define CSSPrimitiveValue_h
24 24
25 #include "core/CSSPropertyNames.h" 25 #include "core/CSSPropertyNames.h"
26 #include "core/CSSValueKeywords.h" 26 #include "core/CSSValueKeywords.h"
27 #include "core/CoreExport.h" 27 #include "core/CoreExport.h"
28 #include "core/css/CSSValue.h" 28 #include "core/css/CSSValue.h"
29 #include "wtf/BitVector.h" 29 #include "wtf/BitVector.h"
30 #include "wtf/Forward.h" 30 #include "wtf/Forward.h"
31 #include "wtf/MathExtras.h" 31 #include "wtf/MathExtras.h"
32 #include "wtf/TypeTraits.h"
33 #include "wtf/text/StringHash.h" 32 #include "wtf/text/StringHash.h"
34 #include "wtf/text/StringView.h" 33 #include "wtf/text/StringView.h"
35 34
36 namespace blink { 35 namespace blink {
37 36
38 class CSSCalcValue; 37 class CSSCalcValue;
39 class CSSToLengthConversionData; 38 class CSSToLengthConversionData;
40 class Length; 39 class Length;
41 40
42 // Dimension calculations are imprecise, often resulting in values of e.g. 41 // Dimension calculations are imprecise, often resulting in values of e.g.
43 // 44.99998. We need to go ahead and round if we're really close to the next 42 // 44.99998. We need to go ahead and round if we're really close to the next
44 // integer value. 43 // integer value.
45 template<typename T> inline T roundForImpreciseConversion(double value) 44 template<typename T> inline T roundForImpreciseConversion(double value)
46 { 45 {
47 value += (value < 0) ? -0.01 : +0.01; 46 value += (value < 0) ? -0.01 : +0.01;
48 return ((value > std::numeric_limits<T>::max()) || (value < std::numeric_lim its<T>::min())) ? 0 : static_cast<T>(value); 47 return ((value > std::numeric_limits<T>::max()) || (value < std::numeric_lim its<T>::min())) ? 0 : static_cast<T>(value);
49 } 48 }
50 49
51 template<> inline float roundForImpreciseConversion(double value) 50 template<> inline float roundForImpreciseConversion(double value)
52 { 51 {
53 double ceiledValue = ceil(value); 52 double ceiledValue = ceil(value);
54 double proximityToNextInt = ceiledValue - value; 53 double proximityToNextInt = ceiledValue - value;
55 if (proximityToNextInt <= 0.01 && value > 0) 54 if (proximityToNextInt <= 0.01 && value > 0)
56 return static_cast<float>(ceiledValue); 55 return static_cast<float>(ceiledValue);
57 if (proximityToNextInt >= 0.99 && value < 0) 56 if (proximityToNextInt >= 0.99 && value < 0)
58 return static_cast<float>(floor(value)); 57 return static_cast<float>(floor(value));
59 return static_cast<float>(value); 58 return static_cast<float>(value);
60 } 59 }
61 60
62 // CSSPrimitiveValues are immutable. This class has manual ref-counting 61 // CSSPrimitiveValue stores numeric data types (e.g. 1, 10px, 4%) and calc()
63 // of unioned types and does not have the code necessary 62 // values (e.g. calc(3px + 2em)).
64 // to handle any kind of mutations.
65 class CORE_EXPORT CSSPrimitiveValue : public CSSValue { 63 class CORE_EXPORT CSSPrimitiveValue : public CSSValue {
66 public: 64 public:
67 // These units are iterated through, so be careful when adding or changing t he order. 65 // These units are iterated through, so be careful when adding or changing t he order.
68 enum class UnitType { 66 enum class UnitType {
69 Unknown, 67 Unknown,
70 Number, 68 Number,
71 Percentage, 69 Percentage,
72 // Length units 70 // Length units
73 Ems, 71 Ems,
74 Exs, 72 Exs,
(...skipping 25 matching lines...) Expand all
100 DotsPerInch, 98 DotsPerInch,
101 DotsPerCentimeter, 99 DotsPerCentimeter,
102 // Other units 100 // Other units
103 Fraction, 101 Fraction,
104 Integer, 102 Integer,
105 Calc, 103 Calc,
106 CalcPercentageWithNumber, 104 CalcPercentageWithNumber,
107 CalcPercentageWithLength, 105 CalcPercentageWithLength,
108 CalcLengthWithNumber, 106 CalcLengthWithNumber,
109 CalcPercentageWithLengthAndNumber, 107 CalcPercentageWithLengthAndNumber,
110 ValueID,
111 108
112 // This value is used to handle quirky margins in reflow roots (body, td , and th) like WinIE. 109 // This value is used to handle quirky margins in reflow roots (body, td , and th) like WinIE.
113 // The basic idea is that a stylesheet can use the value __qem (for quir ky em) instead of em. 110 // The basic idea is that a stylesheet can use the value __qem (for quir ky em) instead of em.
114 // When the quirky value is used, if you're in quirks mode, the margin w ill collapse away 111 // When the quirky value is used, if you're in quirks mode, the margin w ill collapse away
115 // inside a table cell. This quirk is specified in the HTML spec but our impl is different. 112 // inside a table cell. This quirk is specified in the HTML spec but our impl is different.
116 // TODO: Remove this. crbug.com/443952 113 // TODO: Remove this. crbug.com/443952
117 QuirkyEms, 114 QuirkyEms,
118 }; 115 };
119 116
120 enum LengthUnitType { 117 enum LengthUnitType {
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 bool isLength() const { return isLength(typeWithCalcResolved()); } 192 bool isLength() const { return isLength(typeWithCalcResolved()); }
196 bool isNumber() const { return typeWithCalcResolved() == UnitType::Number || typeWithCalcResolved() == UnitType::Integer; } 193 bool isNumber() const { return typeWithCalcResolved() == UnitType::Number || typeWithCalcResolved() == UnitType::Integer; }
197 bool isPercentage() const { return typeWithCalcResolved() == UnitType::Perce ntage; } 194 bool isPercentage() const { return typeWithCalcResolved() == UnitType::Perce ntage; }
198 bool isPx() const { return typeWithCalcResolved() == UnitType::Pixels; } 195 bool isPx() const { return typeWithCalcResolved() == UnitType::Pixels; }
199 bool isTime() const { return type() == UnitType::Seconds || type() == UnitTy pe::Milliseconds; } 196 bool isTime() const { return type() == UnitType::Seconds || type() == UnitTy pe::Milliseconds; }
200 bool isCalculated() const { return type() == UnitType::Calc; } 197 bool isCalculated() const { return type() == UnitType::Calc; }
201 bool isCalculatedPercentageWithNumber() const { return typeWithCalcResolved( ) == UnitType::CalcPercentageWithNumber; } 198 bool isCalculatedPercentageWithNumber() const { return typeWithCalcResolved( ) == UnitType::CalcPercentageWithNumber; }
202 bool isCalculatedPercentageWithLength() const { return typeWithCalcResolved( ) == UnitType::CalcPercentageWithLength; } 199 bool isCalculatedPercentageWithLength() const { return typeWithCalcResolved( ) == UnitType::CalcPercentageWithLength; }
203 static bool isResolution(UnitType type) { return type >= UnitType::DotsPerPi xel && type <= UnitType::DotsPerCentimeter; } 200 static bool isResolution(UnitType type) { return type >= UnitType::DotsPerPi xel && type <= UnitType::DotsPerCentimeter; }
204 bool isFlex() const { return typeWithCalcResolved() == UnitType::Fraction; } 201 bool isFlex() const { return typeWithCalcResolved() == UnitType::Fraction; }
205 bool isValueID() const { return type() == UnitType::ValueID; }
206 bool colorIsDerivedFromElement() const;
207 202
208 static CSSPrimitiveValue* createIdentifier(CSSValueID);
209 static CSSPrimitiveValue* create(double value, UnitType); 203 static CSSPrimitiveValue* create(double value, UnitType);
210 static CSSPrimitiveValue* create(const Length& value, float zoom) 204 static CSSPrimitiveValue* create(const Length& value, float zoom)
211 { 205 {
212 return new CSSPrimitiveValue(value, zoom); 206 return new CSSPrimitiveValue(value, zoom);
213 } 207 }
208
209 // TODO(sashab): Remove this.
214 template<typename T> static CSSPrimitiveValue* create(T value) 210 template<typename T> static CSSPrimitiveValue* create(T value)
215 { 211 {
216 static_assert(!std::is_same<T, CSSValueID>::value, "Do not call create() with a CSSValueID; call createIdentifier() instead");
217 return new CSSPrimitiveValue(value); 212 return new CSSPrimitiveValue(value);
218 } 213 }
219 214
220 ~CSSPrimitiveValue(); 215 ~CSSPrimitiveValue();
221 216
222 UnitType typeWithCalcResolved() const; 217 UnitType typeWithCalcResolved() const;
223 218
224 double computeDegrees() const; 219 double computeDegrees() const;
225 double computeSeconds() const; 220 double computeSeconds() const;
226 221
227 // Computes a length in pixels, resolving relative lengths 222 // Computes a length in pixels, resolving relative lengths
228 template<typename T> T computeLength(const CSSToLengthConversionData&) const ; 223 template<typename T> T computeLength(const CSSToLengthConversionData&) const ;
229 224
230 // Converts to a Length (Fixed, Percent or Calculated) 225 // Converts to a Length (Fixed, Percent or Calculated)
231 Length convertToLength(const CSSToLengthConversionData&) const; 226 Length convertToLength(const CSSToLengthConversionData&) const;
232 227
233 double getDoubleValue() const; 228 double getDoubleValue() const;
234 float getFloatValue() const { return getValue<float>(); } 229 float getFloatValue() const { return getValue<float>(); }
235 int getIntValue() const { return getValue<int>(); } 230 int getIntValue() const { return getValue<int>(); }
236 template<typename T> inline T getValue() const { return clampTo<T>(getDouble Value()); } 231 template<typename T> inline T getValue() const { return clampTo<T>(getDouble Value()); }
237 232
238 CSSCalcValue* cssCalcValue() const { ASSERT(isCalculated()); return m_value. calc; } 233 CSSCalcValue* cssCalcValue() const { ASSERT(isCalculated()); return m_value. calc; }
239 234
240 CSSValueID getValueID() const { return type() == UnitType::ValueID ? m_value .valueID : CSSValueInvalid; }
241
242 template<typename T> inline T convertTo() const; // Defined in CSSPrimitiveV alueMappings.h 235 template<typename T> inline T convertTo() const; // Defined in CSSPrimitiveV alueMappings.h
243 236
244 static const char* unitTypeToString(UnitType); 237 static const char* unitTypeToString(UnitType);
245 static UnitType stringToUnitType(StringView string) 238 static UnitType stringToUnitType(StringView string)
246 { 239 {
247 if (string.is8Bit()) 240 if (string.is8Bit())
248 return stringToUnitType(string.characters8(), string.length()); 241 return stringToUnitType(string.characters8(), string.length());
249 return stringToUnitType(string.characters16(), string.length()); 242 return stringToUnitType(string.characters16(), string.length());
250 } 243 }
251 244
252 String customCSSText() const; 245 String customCSSText() const;
253 246
254 bool equals(const CSSPrimitiveValue&) const; 247 bool equals(const CSSPrimitiveValue&) const;
255 248
256 DECLARE_TRACE_AFTER_DISPATCH(); 249 DECLARE_TRACE_AFTER_DISPATCH();
257 250
258 static UnitType canonicalUnitTypeForCategory(UnitCategory); 251 static UnitType canonicalUnitTypeForCategory(UnitCategory);
259 static double conversionToCanonicalUnitsScaleFactor(UnitType); 252 static double conversionToCanonicalUnitsScaleFactor(UnitType);
260 253
261 // Returns true and populates lengthUnitType, if unitType is a length unit. Otherwise, returns false. 254 // Returns true and populates lengthUnitType, if unitType is a length unit. Otherwise, returns false.
262 static bool unitTypeToLengthUnitType(UnitType, LengthUnitType&); 255 static bool unitTypeToLengthUnitType(UnitType, LengthUnitType&);
263 static UnitType lengthUnitTypeToUnitType(LengthUnitType); 256 static UnitType lengthUnitTypeToUnitType(LengthUnitType);
264 257
265 private: 258 private:
266 CSSPrimitiveValue(CSSValueID);
267 CSSPrimitiveValue(const Length&, float zoom); 259 CSSPrimitiveValue(const Length&, float zoom);
268 CSSPrimitiveValue(double, UnitType); 260 CSSPrimitiveValue(double, UnitType);
269 261
270 template<typename T> CSSPrimitiveValue(T); // Defined in CSSPrimitiveValueMa ppings.h 262 template<typename T> CSSPrimitiveValue(T); // Defined in CSSPrimitiveValueMa ppings.h
271 263
272 template<typename T> CSSPrimitiveValue(T* val) 264 template<typename T> CSSPrimitiveValue(T* val)
273 : CSSValue(PrimitiveClass) 265 : CSSValue(PrimitiveClass)
274 { 266 {
275 init(val); 267 init(val);
276 } 268 }
277 269
278 static void create(int); // compile-time guard 270 static void create(int); // compile-time guard
279 static void create(unsigned); // compile-time guard 271 static void create(unsigned); // compile-time guard
280 template<typename T> operator T*(); // compile-time guard 272 template<typename T> operator T*(); // compile-time guard
281 273
282 // Code generated by CSSPrimitiveValueUnitTrie.cpp.tmpl 274 // Code generated by CSSPrimitiveValueUnitTrie.cpp.tmpl
283 static UnitType stringToUnitType(const LChar*, unsigned length); 275 static UnitType stringToUnitType(const LChar*, unsigned length);
284 static UnitType stringToUnitType(const UChar*, unsigned length); 276 static UnitType stringToUnitType(const UChar*, unsigned length);
285 277
286 void init(UnitType); 278 void init(UnitType);
287 void init(const Length&); 279 void init(const Length&);
288 void init(CSSCalcValue*); 280 void init(CSSCalcValue*);
289 281
290 double computeLengthDouble(const CSSToLengthConversionData&) const; 282 double computeLengthDouble(const CSSToLengthConversionData&) const;
291 283
292 inline UnitType type() const { return static_cast<UnitType>(m_primitiveUnitT ype); } 284 inline UnitType type() const { return static_cast<UnitType>(m_primitiveUnitT ype); }
293 285
294 union { 286 union {
295 CSSValueID valueID;
296 double num; 287 double num;
297 // FIXME: oilpan: Should be a member, but no support for members in unio ns. Just trace the raw ptr for now. 288 // FIXME: oilpan: Should be a member, but no support for members in unio ns. Just trace the raw ptr for now.
298 CSSCalcValue* calc; 289 CSSCalcValue* calc;
299 } m_value; 290 } m_value;
300 }; 291 };
301 292
302 using CSSLengthArray = CSSPrimitiveValue::CSSLengthArray; 293 using CSSLengthArray = CSSPrimitiveValue::CSSLengthArray;
303 294
304 DEFINE_CSS_VALUE_TYPE_CASTS(CSSPrimitiveValue, isPrimitiveValue()); 295 DEFINE_CSS_VALUE_TYPE_CASTS(CSSPrimitiveValue, isPrimitiveValue());
305 296
306 } // namespace blink 297 } // namespace blink
307 298
308 #endif // CSSPrimitiveValue_h 299 #endif // CSSPrimitiveValue_h
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/css/CSSMatrix.cpp ('k') | third_party/WebKit/Source/core/css/CSSPrimitiveValue.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698