Bug 1698238 return default dictionary from GetUserMediaRequest#getConstraints() if...
[gecko.git] / layout / style / nsROCSSPrimitiveValue.cpp
blob21043bb55117a6ca02d99d3f9bc44051d25db4a0
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"
13 #include "nsIURI.h"
14 #include "nsError.h"
16 using namespace mozilla;
17 using namespace mozilla::dom;
19 nsROCSSPrimitiveValue::nsROCSSPrimitiveValue() : CSSValue(), mType(CSS_PX) {
20 mValue.mFloat = 0.0f;
23 nsROCSSPrimitiveValue::~nsROCSSPrimitiveValue() { Reset(); }
25 void nsROCSSPrimitiveValue::GetCssText(nsString& aCssText, ErrorResult& aRv) {
26 nsAutoString tmpStr;
27 aCssText.Truncate();
29 switch (mType) {
30 case CSS_PX: {
31 nsStyleUtil::AppendCSSNumber(mValue.mFloat, tmpStr);
32 tmpStr.AppendLiteral("px");
33 break;
35 case CSS_STRING: {
36 tmpStr.Append(mValue.mString);
37 break;
39 case CSS_URI: {
40 if (mValue.mURI) {
41 nsAutoCString specUTF8;
42 nsresult rv = mValue.mURI->GetSpec(specUTF8);
43 if (NS_FAILED(rv)) {
44 aRv.ThrowInvalidStateError("Can't get URL string for url()");
45 return;
48 tmpStr.AssignLiteral("url(");
49 nsStyleUtil::AppendEscapedCSSString(NS_ConvertUTF8toUTF16(specUTF8),
50 tmpStr);
51 tmpStr.Append(')');
52 } else {
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
56 // for invalid URLs.
57 tmpStr.AssignLiteral(u"url(about:invalid)");
59 break;
61 case CSS_PERCENTAGE: {
62 nsStyleUtil::AppendCSSNumber(mValue.mFloat * 100, tmpStr);
63 tmpStr.Append(char16_t('%'));
64 break;
66 case CSS_NUMBER: {
67 nsStyleUtil::AppendCSSNumber(mValue.mFloat, tmpStr);
68 break;
70 case CSS_NUMBER_INT32: {
71 tmpStr.AppendInt(mValue.mInt32);
72 break;
74 case CSS_NUMBER_UINT32: {
75 tmpStr.AppendInt(mValue.mUint32);
76 break;
78 case CSS_DEG: {
79 nsStyleUtil::AppendCSSNumber(mValue.mFloat, tmpStr);
80 tmpStr.AppendLiteral("deg");
81 break;
83 case CSS_S: {
84 nsStyleUtil::AppendCSSNumber(mValue.mFloat, tmpStr);
85 tmpStr.Append('s');
86 break;
88 case CSS_CM:
89 case CSS_MM:
90 case CSS_IN:
91 case CSS_PT:
92 case CSS_PC:
93 case CSS_UNKNOWN:
94 case CSS_EMS:
95 case CSS_EXS:
96 case CSS_MS:
97 case CSS_HZ:
98 case CSS_KHZ:
99 case CSS_DIMENSION:
100 NS_ERROR("We have a bogus value set. This should not happen");
101 aRv.ThrowInvalidAccessError("Unexpected value in computed style");
102 return;
105 aCssText.Assign(tmpStr);
108 uint16_t nsROCSSPrimitiveValue::CssValueType() const {
109 return CSSValue::CSS_PRIMITIVE_VALUE;
112 void nsROCSSPrimitiveValue::SetNumber(float aValue) {
113 Reset();
114 mValue.mFloat = aValue;
115 mType = CSS_NUMBER;
118 void nsROCSSPrimitiveValue::SetNumber(int32_t aValue) {
119 Reset();
120 mValue.mInt32 = aValue;
121 mType = CSS_NUMBER_INT32;
124 void nsROCSSPrimitiveValue::SetNumber(uint32_t aValue) {
125 Reset();
126 mValue.mUint32 = aValue;
127 mType = CSS_NUMBER_UINT32;
130 void nsROCSSPrimitiveValue::SetPercent(float aValue) {
131 Reset();
132 mValue.mFloat = aValue;
133 mType = CSS_PERCENTAGE;
136 void nsROCSSPrimitiveValue::SetDegree(float aValue) {
137 Reset();
138 mValue.mFloat = aValue;
139 mType = CSS_DEG;
142 void nsROCSSPrimitiveValue::SetAppUnits(nscoord aValue) {
143 SetPixels(nsPresContext::AppUnitsToFloatCSSPixels(aValue));
146 void nsROCSSPrimitiveValue::SetPixels(float aValue) {
147 Reset();
148 mValue.mFloat = aValue;
149 mType = CSS_PX;
152 void nsROCSSPrimitiveValue::SetAppUnits(float aValue) {
153 SetAppUnits(NSToCoordRound(aValue));
156 void nsROCSSPrimitiveValue::SetString(const nsACString& aString) {
157 Reset();
158 mValue.mString = ToNewUnicode(aString, mozilla::fallible);
159 if (mValue.mString) {
160 mType = CSS_STRING;
161 } else {
162 // XXXcaa We should probably let the caller know we are out of memory
163 mType = CSS_UNKNOWN;
167 void nsROCSSPrimitiveValue::SetString(const nsAString& aString) {
168 Reset();
169 mValue.mString = ToNewUnicode(aString, mozilla::fallible);
170 if (mValue.mString) {
171 mType = CSS_STRING;
172 } else {
173 // XXXcaa We should probably let the caller know we are out of memory
174 mType = CSS_UNKNOWN;
178 void nsROCSSPrimitiveValue::SetURI(nsIURI* aURI) {
179 Reset();
180 mValue.mURI = aURI;
181 NS_IF_ADDREF(mValue.mURI);
182 mType = CSS_URI;
185 void nsROCSSPrimitiveValue::SetTime(float aValue) {
186 Reset();
187 mValue.mFloat = aValue;
188 mType = CSS_S;
191 void nsROCSSPrimitiveValue::Reset() {
192 switch (mType) {
193 case CSS_STRING:
194 NS_ASSERTION(mValue.mString, "Null string should never happen");
195 free(mValue.mString);
196 mValue.mString = nullptr;
197 break;
198 case CSS_URI:
199 NS_IF_RELEASE(mValue.mURI);
200 break;
203 mType = CSS_UNKNOWN;
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) {
211 return CSS_NUMBER;
213 return CSS_UNKNOWN;
215 return mType;