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 /* DOM object representing values in DOM computed style */
9 #include "nsROCSSPrimitiveValue.h"
11 #include "mozilla/ErrorResult.h"
12 #include "nsPresContext.h"
13 #include "nsStyleUtil.h"
16 using namespace mozilla
;
17 using namespace mozilla::dom
;
19 nsROCSSPrimitiveValue::nsROCSSPrimitiveValue() : CSSValue(), mType(CSS_PX
) {
23 nsROCSSPrimitiveValue::~nsROCSSPrimitiveValue() { Reset(); }
25 void nsROCSSPrimitiveValue::GetCssText(nsAString
& aCssText
) {
31 nsStyleUtil::AppendCSSNumber(mValue
.mFloat
, tmpStr
);
32 tmpStr
.AppendLiteral("px");
36 tmpStr
.Append(mValue
.mString
);
39 case CSS_PERCENTAGE
: {
40 nsStyleUtil::AppendCSSNumber(mValue
.mFloat
* 100, tmpStr
);
41 tmpStr
.Append(char16_t('%'));
45 nsStyleUtil::AppendCSSNumber(mValue
.mFloat
, tmpStr
);
48 case CSS_NUMBER_INT32
: {
49 tmpStr
.AppendInt(mValue
.mInt32
);
52 case CSS_NUMBER_UINT32
: {
53 tmpStr
.AppendInt(mValue
.mUint32
);
57 nsStyleUtil::AppendCSSNumber(mValue
.mFloat
, tmpStr
);
58 tmpStr
.AppendLiteral("deg");
62 nsStyleUtil::AppendCSSNumber(mValue
.mFloat
, tmpStr
);
68 aCssText
.Assign(tmpStr
);
71 uint16_t nsROCSSPrimitiveValue::CssValueType() const {
72 return CSSValue::CSS_PRIMITIVE_VALUE
;
75 void nsROCSSPrimitiveValue::SetNumber(float aValue
) {
77 mValue
.mFloat
= aValue
;
81 void nsROCSSPrimitiveValue::SetNumber(int32_t aValue
) {
83 mValue
.mInt32
= aValue
;
84 mType
= CSS_NUMBER_INT32
;
87 void nsROCSSPrimitiveValue::SetNumber(uint32_t aValue
) {
89 mValue
.mUint32
= aValue
;
90 mType
= CSS_NUMBER_UINT32
;
93 void nsROCSSPrimitiveValue::SetPercent(float aValue
) {
95 mValue
.mFloat
= aValue
;
96 mType
= CSS_PERCENTAGE
;
99 void nsROCSSPrimitiveValue::SetDegree(float aValue
) {
101 mValue
.mFloat
= aValue
;
105 void nsROCSSPrimitiveValue::SetAppUnits(nscoord aValue
) {
106 SetPixels(nsPresContext::AppUnitsToFloatCSSPixels(aValue
));
109 void nsROCSSPrimitiveValue::SetPixels(float aValue
) {
111 mValue
.mFloat
= aValue
;
115 void nsROCSSPrimitiveValue::SetAppUnits(float aValue
) {
116 SetAppUnits(NSToCoordRound(aValue
));
119 void nsROCSSPrimitiveValue::SetString(const nsACString
& aString
) {
121 mValue
.mString
= ToNewUnicode(aString
, mozilla::fallible
);
122 if (mValue
.mString
) {
125 // XXXcaa We should probably let the caller know we are out of memory
130 void nsROCSSPrimitiveValue::SetString(const nsAString
& aString
) {
132 mValue
.mString
= ToNewUnicode(aString
, mozilla::fallible
);
133 if (mValue
.mString
) {
136 // XXXcaa We should probably let the caller know we are out of memory
141 void nsROCSSPrimitiveValue::SetTime(float aValue
) {
143 mValue
.mFloat
= aValue
;
147 void nsROCSSPrimitiveValue::Reset() {
150 NS_ASSERTION(mValue
.mString
, "Null string should never happen");
151 free(mValue
.mString
);
152 mValue
.mString
= nullptr;
159 uint16_t nsROCSSPrimitiveValue::PrimitiveType() {
160 // New value types were introduced but not added to CSS OM.
161 // Return CSS_UNKNOWN to avoid exposing CSS_TURN to content.
162 if (mType
> CSS_RGBCOLOR
) {
163 if (mType
== CSS_NUMBER_INT32
|| mType
== CSS_NUMBER_UINT32
) {