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