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 "nsPresContext.h"
12 #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(nsString
& aCssText
, ErrorResult
& aRv
) {
31 nsStyleUtil::AppendCSSNumber(mValue
.mFloat
, tmpStr
);
32 tmpStr
.AppendLiteral("px");
36 tmpStr
.Append(mValue
.mString
);
41 nsAutoCString specUTF8
;
42 nsresult rv
= mValue
.mURI
->GetSpec(specUTF8
);
44 aRv
.ThrowInvalidStateError("Can't get URL string for url()");
48 tmpStr
.AssignLiteral("url(");
49 nsStyleUtil::AppendEscapedCSSString(NS_ConvertUTF8toUTF16(specUTF8
),
53 // http://dev.w3.org/csswg/css3-values/#attr defines
54 // 'about:invalid' as the default value for url attributes,
55 // so let's also use it here as the default computed value
57 tmpStr
.AssignLiteral(u
"url(about:invalid)");
61 case CSS_PERCENTAGE
: {
62 nsStyleUtil::AppendCSSNumber(mValue
.mFloat
* 100, tmpStr
);
63 tmpStr
.Append(char16_t('%'));
67 nsStyleUtil::AppendCSSNumber(mValue
.mFloat
, tmpStr
);
70 case CSS_NUMBER_INT32
: {
71 tmpStr
.AppendInt(mValue
.mInt32
);
74 case CSS_NUMBER_UINT32
: {
75 tmpStr
.AppendInt(mValue
.mUint32
);
79 nsStyleUtil::AppendCSSNumber(mValue
.mFloat
, tmpStr
);
80 tmpStr
.AppendLiteral("deg");
84 nsStyleUtil::AppendCSSNumber(mValue
.mFloat
, tmpStr
);
100 NS_ERROR("We have a bogus value set. This should not happen");
101 aRv
.ThrowInvalidAccessError("Unexpected value in computed style");
105 aCssText
.Assign(tmpStr
);
108 uint16_t nsROCSSPrimitiveValue::CssValueType() const {
109 return CSSValue::CSS_PRIMITIVE_VALUE
;
112 void nsROCSSPrimitiveValue::SetNumber(float aValue
) {
114 mValue
.mFloat
= aValue
;
118 void nsROCSSPrimitiveValue::SetNumber(int32_t aValue
) {
120 mValue
.mInt32
= aValue
;
121 mType
= CSS_NUMBER_INT32
;
124 void nsROCSSPrimitiveValue::SetNumber(uint32_t aValue
) {
126 mValue
.mUint32
= aValue
;
127 mType
= CSS_NUMBER_UINT32
;
130 void nsROCSSPrimitiveValue::SetPercent(float aValue
) {
132 mValue
.mFloat
= aValue
;
133 mType
= CSS_PERCENTAGE
;
136 void nsROCSSPrimitiveValue::SetDegree(float aValue
) {
138 mValue
.mFloat
= aValue
;
142 void nsROCSSPrimitiveValue::SetAppUnits(nscoord aValue
) {
143 SetPixels(nsPresContext::AppUnitsToFloatCSSPixels(aValue
));
146 void nsROCSSPrimitiveValue::SetPixels(float aValue
) {
148 mValue
.mFloat
= aValue
;
152 void nsROCSSPrimitiveValue::SetAppUnits(float aValue
) {
153 SetAppUnits(NSToCoordRound(aValue
));
156 void nsROCSSPrimitiveValue::SetString(const nsACString
& aString
) {
158 mValue
.mString
= ToNewUnicode(aString
, mozilla::fallible
);
159 if (mValue
.mString
) {
162 // XXXcaa We should probably let the caller know we are out of memory
167 void nsROCSSPrimitiveValue::SetString(const nsAString
& aString
) {
169 mValue
.mString
= ToNewUnicode(aString
, mozilla::fallible
);
170 if (mValue
.mString
) {
173 // XXXcaa We should probably let the caller know we are out of memory
178 void nsROCSSPrimitiveValue::SetURI(nsIURI
* aURI
) {
181 NS_IF_ADDREF(mValue
.mURI
);
185 void nsROCSSPrimitiveValue::SetTime(float aValue
) {
187 mValue
.mFloat
= aValue
;
191 void nsROCSSPrimitiveValue::Reset() {
194 NS_ASSERTION(mValue
.mString
, "Null string should never happen");
195 free(mValue
.mString
);
196 mValue
.mString
= nullptr;
199 NS_IF_RELEASE(mValue
.mURI
);
206 uint16_t nsROCSSPrimitiveValue::PrimitiveType() {
207 // New value types were introduced but not added to CSS OM.
208 // Return CSS_UNKNOWN to avoid exposing CSS_TURN to content.
209 if (mType
> CSS_RGBCOLOR
) {
210 if (mType
== CSS_NUMBER_INT32
|| mType
== CSS_NUMBER_UINT32
) {