1/*
2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
4 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22#pragma once
23
24#include "CSSPropertyNames.h"
25#include "CSSValue.h"
26#include "CSSValueKeywords.h"
27#include "Color.h"
28#include "ExceptionOr.h"
29#include "LayoutUnit.h"
30#include <utility>
31#include <wtf/Forward.h>
32#include <wtf/MathExtras.h>
33
34namespace WebCore {
35
36class CSSBasicShape;
37class CSSCalcValue;
38class CSSToLengthConversionData;
39class Counter;
40class DeprecatedCSSOMPrimitiveValue;
41class Pair;
42class Quad;
43class RGBColor;
44class Rect;
45class RenderStyle;
46
47struct CSSFontFamily;
48struct Length;
49struct LengthSize;
50
51// Max/min values for CSS, needs to slightly smaller/larger than the true max/min values to allow for rounding without overflowing.
52// Subtract two (rather than one) to allow for values to be converted to float and back without exceeding the LayoutUnit::max.
53const int maxValueForCssLength = intMaxForLayoutUnit - 2;
54const int minValueForCssLength = intMinForLayoutUnit + 2;
55
56// Dimension calculations are imprecise, often resulting in values of e.g.
57// 44.99998. We need to round if we're really close to the next integer value.
58template<typename T> inline T roundForImpreciseConversion(double value)
59{
60 value += (value < 0) ? -0.01 : +0.01;
61 return ((value > std::numeric_limits<T>::max()) || (value < std::numeric_limits<T>::min())) ? 0 : static_cast<T>(value);
62}
63
64template<> inline float roundForImpreciseConversion(double value)
65{
66 double ceiledValue = ceil(value);
67 double proximityToNextInt = ceiledValue - value;
68 if (proximityToNextInt <= 0.01 && value > 0)
69 return static_cast<float>(ceiledValue);
70 if (proximityToNextInt >= 0.99 && value < 0)
71 return static_cast<float>(floor(value));
72 return static_cast<float>(value);
73}
74
75class CSSPrimitiveValue final : public CSSValue {
76public:
77 enum UnitType {
78 CSS_UNKNOWN = 0,
79 CSS_NUMBER = 1,
80 CSS_PERCENTAGE = 2,
81 CSS_EMS = 3,
82 CSS_EXS = 4,
83 CSS_PX = 5,
84 CSS_CM = 6,
85 CSS_MM = 7,
86 CSS_IN = 8,
87 CSS_PT = 9,
88 CSS_PC = 10,
89 CSS_DEG = 11,
90 CSS_RAD = 12,
91 CSS_GRAD = 13,
92 CSS_MS = 14,
93 CSS_S = 15,
94 CSS_HZ = 16,
95 CSS_KHZ = 17,
96 CSS_DIMENSION = 18,
97 CSS_STRING = 19,
98 CSS_URI = 20,
99 CSS_IDENT = 21,
100 CSS_ATTR = 22,
101 CSS_COUNTER = 23,
102 CSS_RECT = 24,
103 CSS_RGBCOLOR = 25,
104 // From CSS Values and Units. Viewport-percentage Lengths (vw/vh/vmin/vmax).
105 CSS_VW = 26,
106 CSS_VH = 27,
107 CSS_VMIN = 28,
108 CSS_VMAX = 29,
109 CSS_DPPX = 30,
110 CSS_DPI = 31,
111 CSS_DPCM = 32,
112 CSS_FR = 33,
113 CSS_PAIR = 100, // We envision this being exposed as a means of getting computed style values for pairs (border-spacing/radius, background-position, etc.)
114 CSS_UNICODE_RANGE = 102,
115
116 // These are from CSS3 Values and Units, but that isn't a finished standard yet
117 CSS_TURN = 107,
118 CSS_REMS = 108,
119 CSS_CHS = 109,
120
121 // This is used internally for counter names (as opposed to counter values)
122 CSS_COUNTER_NAME = 110,
123
124 // This is used by the CSS Shapes draft
125 CSS_SHAPE = 111,
126
127 // Used by border images.
128 CSS_QUAD = 112,
129
130 CSS_CALC = 113,
131 CSS_CALC_PERCENTAGE_WITH_NUMBER = 114,
132 CSS_CALC_PERCENTAGE_WITH_LENGTH = 115,
133
134 CSS_FONT_FAMILY = 116,
135
136 CSS_PROPERTY_ID = 117,
137 CSS_VALUE_ID = 118,
138
139 // This value is used to handle quirky margins in reflow roots (body, td, and th) like WinIE.
140 // The basic idea is that a stylesheet can use the value __qem (for quirky em) instead of em.
141 // When the quirky value is used, if you're in quirks mode, the margin will collapse away
142 // inside a table cell. This quirk is specified in the HTML spec but our impl is different.
143 CSS_QUIRKY_EMS = 120
144 };
145
146 // This enum follows the CSSParser::Units enum augmented with UNIT_FREQUENCY for frequencies.
147 enum UnitCategory {
148 UNumber,
149 UPercent,
150 ULength,
151 UAngle,
152 UTime,
153 UFrequency,
154#if ENABLE(CSS_IMAGE_RESOLUTION) || ENABLE(RESOLUTION_MEDIA_QUERY)
155 UResolution,
156#endif
157 UOther
158 };
159 static UnitCategory unitCategory(UnitType);
160
161 bool isAngle() const;
162 bool isAttr() const { return m_primitiveUnitType == CSS_ATTR; }
163 bool isCounter() const { return m_primitiveUnitType == CSS_COUNTER; }
164 bool isFontIndependentLength() const { return m_primitiveUnitType >= CSS_PX && m_primitiveUnitType <= CSS_PC; }
165 static bool isFontRelativeLength(UnitType);
166 bool isFontRelativeLength() const { return isFontRelativeLength(static_cast<UnitType>(m_primitiveUnitType)); }
167
168 bool isQuirkyEms() const { return primitiveType() == UnitType::CSS_QUIRKY_EMS; }
169
170 static bool isViewportPercentageLength(UnitType type) { return type >= CSS_VW && type <= CSS_VMAX; }
171 bool isViewportPercentageLength() const { return isViewportPercentageLength(static_cast<UnitType>(m_primitiveUnitType)); }
172
173 static bool isLength(UnitType);
174 bool isLength() const { return isLength(static_cast<UnitType>(primitiveType())); }
175 bool isNumber() const { return primitiveType() == CSS_NUMBER; }
176 bool isPercentage() const { return primitiveType() == CSS_PERCENTAGE; }
177 bool isPx() const { return primitiveType() == CSS_PX; }
178 bool isRect() const { return m_primitiveUnitType == CSS_RECT; }
179 bool isPair() const { return m_primitiveUnitType == CSS_PAIR; }
180 bool isPropertyID() const { return m_primitiveUnitType == CSS_PROPERTY_ID; }
181 bool isRGBColor() const { return m_primitiveUnitType == CSS_RGBCOLOR; }
182 bool isShape() const { return m_primitiveUnitType == CSS_SHAPE; }
183 bool isString() const { return m_primitiveUnitType == CSS_STRING; }
184 bool isFontFamily() const { return m_primitiveUnitType == CSS_FONT_FAMILY; }
185 bool isTime() const { return m_primitiveUnitType == CSS_S || m_primitiveUnitType == CSS_MS; }
186 bool isURI() const { return m_primitiveUnitType == CSS_URI; }
187 bool isCalculated() const { return m_primitiveUnitType == CSS_CALC; }
188 bool isCalculatedPercentageWithNumber() const { return primitiveType() == CSS_CALC_PERCENTAGE_WITH_NUMBER; }
189 bool isCalculatedPercentageWithLength() const { return primitiveType() == CSS_CALC_PERCENTAGE_WITH_LENGTH; }
190 bool isDotsPerInch() const { return primitiveType() == CSS_DPI; }
191 bool isDotsPerPixel() const { return primitiveType() == CSS_DPPX; }
192 bool isDotsPerCentimeter() const { return primitiveType() == CSS_DPCM; }
193
194 static bool isResolution(UnitType);
195 bool isResolution() const { return isResolution(static_cast<UnitType>(primitiveType())); }
196 bool isViewportPercentageWidth() const { return m_primitiveUnitType == CSS_VW; }
197 bool isViewportPercentageHeight() const { return m_primitiveUnitType == CSS_VH; }
198 bool isViewportPercentageMax() const { return m_primitiveUnitType == CSS_VMAX; }
199 bool isViewportPercentageMin() const { return m_primitiveUnitType == CSS_VMIN; }
200 bool isValueID() const { return m_primitiveUnitType == CSS_VALUE_ID; }
201 bool isFlex() const { return primitiveType() == CSS_FR; }
202
203 static Ref<CSSPrimitiveValue> createIdentifier(CSSValueID valueID) { return adoptRef(*new CSSPrimitiveValue(valueID)); }
204 static Ref<CSSPrimitiveValue> createIdentifier(CSSPropertyID propertyID) { return adoptRef(*new CSSPrimitiveValue(propertyID)); }
205
206 static Ref<CSSPrimitiveValue> create(double value, UnitType type) { return adoptRef(*new CSSPrimitiveValue(value, type)); }
207 static Ref<CSSPrimitiveValue> create(const String& value, UnitType type) { return adoptRef(*new CSSPrimitiveValue(value, type)); }
208 static Ref<CSSPrimitiveValue> create(const Length& value, const RenderStyle& style) { return adoptRef(*new CSSPrimitiveValue(value, style)); }
209 static Ref<CSSPrimitiveValue> create(const LengthSize& value, const RenderStyle& style) { return adoptRef(*new CSSPrimitiveValue(value, style)); }
210
211 template<typename T> static Ref<CSSPrimitiveValue> create(T&&);
212
213 // This value is used to handle quirky margins in reflow roots (body, td, and th) like WinIE.
214 // The basic idea is that a stylesheet can use the value __qem (for quirky em) instead of em.
215 // When the quirky value is used, if you're in quirks mode, the margin will collapse away
216 // inside a table cell.
217 static Ref<CSSPrimitiveValue> createAllowingMarginQuirk(double value, UnitType);
218
219 ~CSSPrimitiveValue();
220
221 void cleanup();
222
223 WEBCORE_EXPORT unsigned short primitiveType() const;
224 WEBCORE_EXPORT ExceptionOr<void> setFloatValue(unsigned short unitType, double floatValue);
225 WEBCORE_EXPORT ExceptionOr<float> getFloatValue(unsigned short unitType) const;
226 WEBCORE_EXPORT ExceptionOr<void> setStringValue(unsigned short stringType, const String& stringValue);
227 WEBCORE_EXPORT ExceptionOr<String> getStringValue() const;
228 WEBCORE_EXPORT ExceptionOr<Counter&> getCounterValue() const;
229 WEBCORE_EXPORT ExceptionOr<Rect&> getRectValue() const;
230 WEBCORE_EXPORT ExceptionOr<Ref<RGBColor>> getRGBColorValue() const;
231
232 double computeDegrees() const;
233
234 enum TimeUnit { Seconds, Milliseconds };
235 template<typename T, TimeUnit timeUnit> T computeTime() const;
236
237 template<typename T> T computeLength(const CSSToLengthConversionData&) const;
238 template<int> Length convertToLength(const CSSToLengthConversionData&) const;
239
240 bool convertingToLengthRequiresNonNullStyle(int lengthConversion) const;
241
242 double doubleValue(UnitType) const;
243 double doubleValue() const;
244
245 template<typename T> inline T value(UnitType type) const { return clampTo<T>(doubleValue(type)); }
246 template<typename T> inline T value() const { return clampTo<T>(doubleValue()); }
247
248 float floatValue(UnitType type) const { return value<float>(type); }
249 float floatValue() const { return value<float>(); }
250
251 int intValue(UnitType type) const { return value<int>(type); }
252 int intValue() const { return value<int>(); }
253
254 WEBCORE_EXPORT String stringValue() const;
255
256 const Color& color() const { ASSERT(m_primitiveUnitType == CSS_RGBCOLOR); return *m_value.color; }
257 Counter* counterValue() const { return m_primitiveUnitType != CSS_COUNTER ? nullptr : m_value.counter; }
258 CSSCalcValue* cssCalcValue() const { return m_primitiveUnitType != CSS_CALC ? nullptr : m_value.calc; }
259 const CSSFontFamily& fontFamily() const { ASSERT(m_primitiveUnitType == CSS_FONT_FAMILY); return *m_value.fontFamily; }
260 Pair* pairValue() const { return m_primitiveUnitType != CSS_PAIR ? nullptr : m_value.pair; }
261 CSSPropertyID propertyID() const { return m_primitiveUnitType == CSS_PROPERTY_ID ? m_value.propertyID : CSSPropertyInvalid; }
262 Quad* quadValue() const { return m_primitiveUnitType != CSS_QUAD ? nullptr : m_value.quad; }
263 Rect* rectValue() const { return m_primitiveUnitType != CSS_RECT ? nullptr : m_value.rect; }
264 CSSBasicShape* shapeValue() const { return m_primitiveUnitType != CSS_SHAPE ? nullptr : m_value.shape; }
265 CSSValueID valueID() const { return m_primitiveUnitType == CSS_VALUE_ID ? m_value.valueID : CSSValueInvalid; }
266
267 template<typename T> inline operator T() const; // Defined in CSSPrimitiveValueMappings.h
268
269 String customCSSText() const;
270
271 // FIXME-NEWPARSER: Can ditch the boolean and just use the unit type once old parser is gone.
272 bool isQuirkValue() const { return m_isQuirkValue || primitiveType() == CSS_QUIRKY_EMS; }
273
274 bool equals(const CSSPrimitiveValue&) const;
275
276 static UnitType canonicalUnitTypeForCategory(UnitCategory);
277 static double conversionToCanonicalUnitsScaleFactor(UnitType);
278
279 static double computeNonCalcLengthDouble(const CSSToLengthConversionData&, UnitType, double value);
280
281 Ref<DeprecatedCSSOMPrimitiveValue> createDeprecatedCSSOMPrimitiveWrapper(CSSStyleDeclaration&) const;
282
283 void collectDirectComputationalDependencies(HashSet<CSSPropertyID>&) const;
284 void collectDirectRootComputationalDependencies(HashSet<CSSPropertyID>&) const;
285
286private:
287 friend class CSSValuePool;
288 friend LazyNeverDestroyed<CSSPrimitiveValue>;
289
290 CSSPrimitiveValue(CSSValueID);
291 CSSPrimitiveValue(CSSPropertyID);
292 CSSPrimitiveValue(const Color&);
293 CSSPrimitiveValue(const Length&);
294 CSSPrimitiveValue(const Length&, const RenderStyle&);
295 CSSPrimitiveValue(const LengthSize&, const RenderStyle&);
296 CSSPrimitiveValue(const String&, UnitType);
297 CSSPrimitiveValue(double, UnitType);
298
299 template<typename T> CSSPrimitiveValue(T); // Defined in CSSPrimitiveValueMappings.h
300 template<typename T> CSSPrimitiveValue(RefPtr<T>&&);
301 template<typename T> CSSPrimitiveValue(Ref<T>&&);
302
303 static void create(int); // compile-time guard
304 static void create(unsigned); // compile-time guard
305 template<typename T> operator T*(); // compile-time guard
306
307 void init(const Length&);
308 void init(const LengthSize&, const RenderStyle&);
309 void init(Ref<CSSBasicShape>&&);
310 void init(RefPtr<CSSCalcValue>&&);
311 void init(Ref<Counter>&&);
312 void init(Ref<Pair>&&);
313 void init(Ref<Quad>&&);
314 void init(Ref<Rect>&&);
315
316 Optional<double> doubleValueInternal(UnitType targetUnitType) const;
317
318 double computeLengthDouble(const CSSToLengthConversionData&) const;
319
320 ALWAYS_INLINE String formatNumberForCustomCSSText() const;
321 NEVER_INLINE String formatNumberValue(StringView) const;
322
323 union {
324 CSSPropertyID propertyID;
325 CSSValueID valueID;
326 double num;
327 StringImpl* string;
328 Counter* counter;
329 Rect* rect;
330 Quad* quad;
331 const Color* color;
332 Pair* pair;
333 CSSBasicShape* shape;
334 CSSCalcValue* calc;
335 const CSSFontFamily* fontFamily;
336 } m_value;
337};
338
339inline bool CSSPrimitiveValue::isAngle() const
340{
341 auto primitiveType = this->primitiveType();
342 return primitiveType == CSS_DEG
343 || primitiveType == CSS_RAD
344 || primitiveType == CSS_GRAD
345 || primitiveType == CSS_TURN;
346}
347
348inline bool CSSPrimitiveValue::isFontRelativeLength(UnitType type)
349{
350 return type == CSS_EMS
351 || type == CSS_EXS
352 || type == CSS_REMS
353 || type == CSS_CHS
354 || type == CSS_QUIRKY_EMS;
355}
356
357inline bool CSSPrimitiveValue::isLength(UnitType type)
358{
359 return (type >= CSS_EMS && type <= CSS_PC)
360 || type == CSS_REMS
361 || type == CSS_CHS
362 || isViewportPercentageLength(type)
363 || type == CSS_QUIRKY_EMS;
364}
365
366inline bool CSSPrimitiveValue::isResolution(UnitType type)
367{
368 return type >= CSS_DPPX && type <= CSS_DPCM;
369}
370
371template<typename T> inline Ref<CSSPrimitiveValue> CSSPrimitiveValue::create(T&& value)
372{
373 return adoptRef(*new CSSPrimitiveValue(std::forward<T>(value)));
374}
375
376inline Ref<CSSPrimitiveValue> CSSPrimitiveValue::createAllowingMarginQuirk(double value, UnitType type)
377{
378 auto result = adoptRef(*new CSSPrimitiveValue(value, type));
379 result->m_isQuirkValue = true;
380 return result;
381}
382
383template<typename T, CSSPrimitiveValue::TimeUnit timeUnit> inline T CSSPrimitiveValue::computeTime() const
384{
385 if (timeUnit == Seconds && primitiveType() == CSS_S)
386 return value<T>();
387 if (timeUnit == Seconds && primitiveType() == CSS_MS)
388 return value<T>() / 1000;
389 if (timeUnit == Milliseconds && primitiveType() == CSS_MS)
390 return value<T>();
391 if (timeUnit == Milliseconds && primitiveType() == CSS_S)
392 return value<T>() * 1000;
393 ASSERT_NOT_REACHED();
394 return 0;
395}
396
397template<typename T> inline CSSPrimitiveValue::CSSPrimitiveValue(RefPtr<T>&& value)
398 : CSSValue(PrimitiveClass)
399{
400 init(WTFMove(value));
401}
402
403template<typename T> inline CSSPrimitiveValue::CSSPrimitiveValue(Ref<T>&& value)
404 : CSSValue(PrimitiveClass)
405{
406 init(WTFMove(value));
407}
408
409} // namespace WebCore
410
411SPECIALIZE_TYPE_TRAITS_CSS_VALUE(CSSPrimitiveValue, isPrimitiveValue())
412