Bug 1890793: Assert CallArgs::newTarget is not gray. r=spidermonkey-reviewers,sfink...
[gecko.git] / layout / style / nsROCSSPrimitiveValue.cpp
blob3b31a14dd20014409edbb46948235bbfb0924a91
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"
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(nsAString& aCssText) {
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_PERCENTAGE: {
40 nsStyleUtil::AppendCSSNumber(mValue.mFloat * 100, tmpStr);
41 tmpStr.Append(char16_t('%'));
42 break;
44 case CSS_NUMBER: {
45 nsStyleUtil::AppendCSSNumber(mValue.mFloat, tmpStr);
46 break;
48 case CSS_NUMBER_INT32: {
49 tmpStr.AppendInt(mValue.mInt32);
50 break;
52 case CSS_NUMBER_UINT32: {
53 tmpStr.AppendInt(mValue.mUint32);
54 break;
56 case CSS_DEG: {
57 nsStyleUtil::AppendCSSNumber(mValue.mFloat, tmpStr);
58 tmpStr.AppendLiteral("deg");
59 break;
61 case CSS_S: {
62 nsStyleUtil::AppendCSSNumber(mValue.mFloat, tmpStr);
63 tmpStr.Append('s');
64 break;
68 aCssText.Assign(tmpStr);
71 uint16_t nsROCSSPrimitiveValue::CssValueType() const {
72 return CSSValue::CSS_PRIMITIVE_VALUE;
75 void nsROCSSPrimitiveValue::SetNumber(float aValue) {
76 Reset();
77 mValue.mFloat = aValue;
78 mType = CSS_NUMBER;
81 void nsROCSSPrimitiveValue::SetNumber(int32_t aValue) {
82 Reset();
83 mValue.mInt32 = aValue;
84 mType = CSS_NUMBER_INT32;
87 void nsROCSSPrimitiveValue::SetNumber(uint32_t aValue) {
88 Reset();
89 mValue.mUint32 = aValue;
90 mType = CSS_NUMBER_UINT32;
93 void nsROCSSPrimitiveValue::SetPercent(float aValue) {
94 Reset();
95 mValue.mFloat = aValue;
96 mType = CSS_PERCENTAGE;
99 void nsROCSSPrimitiveValue::SetDegree(float aValue) {
100 Reset();
101 mValue.mFloat = aValue;
102 mType = CSS_DEG;
105 void nsROCSSPrimitiveValue::SetPixels(float aValue) {
106 Reset();
107 mValue.mFloat = aValue;
108 mType = CSS_PX;
111 void nsROCSSPrimitiveValue::SetString(const nsACString& aString) {
112 Reset();
113 mValue.mString = ToNewUnicode(aString, mozilla::fallible);
114 if (mValue.mString) {
115 mType = CSS_STRING;
116 } else {
117 // XXXcaa We should probably let the caller know we are out of memory
118 mType = CSS_UNKNOWN;
122 void nsROCSSPrimitiveValue::SetString(const nsAString& aString) {
123 Reset();
124 mValue.mString = ToNewUnicode(aString, mozilla::fallible);
125 if (mValue.mString) {
126 mType = CSS_STRING;
127 } else {
128 // XXXcaa We should probably let the caller know we are out of memory
129 mType = CSS_UNKNOWN;
133 void nsROCSSPrimitiveValue::SetTime(float aValue) {
134 Reset();
135 mValue.mFloat = aValue;
136 mType = CSS_S;
139 void nsROCSSPrimitiveValue::Reset() {
140 switch (mType) {
141 case CSS_STRING:
142 NS_ASSERTION(mValue.mString, "Null string should never happen");
143 free(mValue.mString);
144 mValue.mString = nullptr;
145 break;
148 mType = CSS_UNKNOWN;
151 uint16_t nsROCSSPrimitiveValue::PrimitiveType() {
152 // New value types were introduced but not added to CSS OM.
153 // Return CSS_UNKNOWN to avoid exposing CSS_TURN to content.
154 if (mType > CSS_RGBCOLOR) {
155 if (mType == CSS_NUMBER_INT32 || mType == CSS_NUMBER_UINT32) {
156 return CSS_NUMBER;
158 return CSS_UNKNOWN;
160 return mType;