1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* representation of simple property values within CSS declarations */
9 #ifndef nsCSSValue_h___
10 #define nsCSSValue_h___
12 #include "mozilla/Attributes.h"
13 #include "mozilla/CORSMode.h"
14 #include "mozilla/EnumTypeTraits.h"
15 #include "mozilla/MemoryReporting.h"
16 #include "mozilla/ServoBindingTypes.h"
17 #include "mozilla/URLExtraData.h"
18 #include "mozilla/UniquePtr.h"
23 #include <type_traits>
25 // XXX Avoid including this here by moving function bodies to the cpp file
26 #include "mozilla/FloatingPoint.h"
28 class imgRequestProxy
;
37 struct RawServoCssUrlData
;
41 } // namespace mozilla
43 // Forward declaration copied here since ServoBindings.h #includes nsCSSValue.h.
45 mozilla::URLExtraData
* Servo_CssUrlData_GetExtraData(const RawServoCssUrlData
*);
46 bool Servo_CssUrlData_IsLocalRef(const RawServoCssUrlData
* url
);
49 enum nsCSSUnit
: uint32_t {
50 eCSSUnit_Null
= 0, // (n/a) null unit, value is not specified
52 eCSSUnit_Percent
= 100, // (1.0 == 100%) value is percentage of
54 eCSSUnit_Number
= 101, // value is numeric (usually multiplier,
55 // different behavior than percent)
57 // Font relative measure
58 eCSSUnit_EM
= 800, // == current font size
59 eCSSUnit_XHeight
= 801, // distance from top of lower case x to
61 eCSSUnit_Char
= 802, // number of characters, used for width with
63 eCSSUnit_RootEM
= 803, // == root element font size
64 eCSSUnit_Ideographic
= 804, // == CJK water ideograph width
65 eCSSUnit_CapHeight
= 805, // == Capital letter height
67 // Screen relative measure
68 eCSSUnit_Point
= 900, // 4/3 of a CSS pixel
69 eCSSUnit_Inch
= 901, // 96 CSS pixels
70 eCSSUnit_Millimeter
= 902, // 96/25.4 CSS pixels
71 eCSSUnit_Centimeter
= 903, // 96/2.54 CSS pixels
72 eCSSUnit_Pica
= 904, // 12 points == 16 CSS pixls
73 eCSSUnit_Quarter
= 905, // 96/101.6 CSS pixels
74 eCSSUnit_Pixel
= 906, // CSS pixel unit
76 // Viewport percentage lengths
83 struct nsCSSValuePair
;
84 struct nsCSSValuePair_heap
;
85 struct nsCSSValueList
;
86 struct nsCSSValueList_heap
;
87 struct nsCSSValueSharedList
;
88 struct nsCSSValuePairList
;
89 struct nsCSSValuePairList_heap
;
93 explicit nsCSSValue() : mUnit(eCSSUnit_Null
) {}
95 nsCSSValue(float aValue
, nsCSSUnit aUnit
);
96 nsCSSValue(const nsCSSValue
& aCopy
);
97 nsCSSValue(nsCSSValue
&& aOther
) : mUnit(aOther
.mUnit
), mValue(aOther
.mValue
) {
98 aOther
.mUnit
= eCSSUnit_Null
;
101 nsCSSValue
& operator=(const nsCSSValue
& aCopy
);
102 nsCSSValue
& operator=(nsCSSValue
&& aCopy
);
103 bool operator==(const nsCSSValue
& aOther
) const;
105 bool operator!=(const nsCSSValue
& aOther
) const { return !(*this == aOther
); }
107 nsCSSUnit
GetUnit() const { return mUnit
; }
108 bool IsLengthUnit() const {
109 return eCSSUnit_EM
<= mUnit
&& mUnit
<= eCSSUnit_Pixel
;
112 * A "pixel" length unit is a some multiple of CSS pixels.
114 static bool IsPixelLengthUnit(nsCSSUnit aUnit
) {
115 return eCSSUnit_Point
<= aUnit
&& aUnit
<= eCSSUnit_Pixel
;
117 bool IsPixelLengthUnit() const { return IsPixelLengthUnit(mUnit
); }
118 static bool IsPercentLengthUnit(nsCSSUnit aUnit
) {
119 return aUnit
== eCSSUnit_Percent
;
121 static bool IsFloatUnit(nsCSSUnit aUnit
) { return eCSSUnit_Number
<= aUnit
; }
123 float GetPercentValue() const {
124 MOZ_ASSERT(mUnit
== eCSSUnit_Percent
, "not a percent value");
128 float GetFloatValue() const {
129 MOZ_ASSERT(eCSSUnit_Number
<= mUnit
, "not a float value");
130 MOZ_ASSERT(!std::isnan(mValue
));
134 nscoord
GetPixelLength() const;
136 void Reset() { mUnit
= eCSSUnit_Null
; }
137 ~nsCSSValue() { Reset(); }
140 void SetPercentValue(float aValue
);
141 void SetFloatValue(float aValue
, nsCSSUnit aUnit
);
148 #endif /* nsCSSValue_h___ */