nsROCSSPrimitiveValue.cpp (3933B)
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/. */ 6 7 /* DOM object representing values in DOM computed style */ 8 9 #include "nsROCSSPrimitiveValue.h" 10 11 #include "mozilla/ErrorResult.h" 12 #include "nsError.h" 13 #include "nsPresContext.h" 14 #include "nsStyleUtil.h" 15 16 using namespace mozilla; 17 using namespace mozilla::dom; 18 19 nsROCSSPrimitiveValue::nsROCSSPrimitiveValue() : CSSValue(), mType(CSS_PX) { 20 mValue.mFloat = 0.0f; 21 } 22 23 nsROCSSPrimitiveValue::~nsROCSSPrimitiveValue() { Reset(); } 24 25 void nsROCSSPrimitiveValue::GetCssText(nsAString& aCssText) { 26 nsAutoString tmpStr; 27 aCssText.Truncate(); 28 29 switch (mType) { 30 case CSS_PX: { 31 nsStyleUtil::AppendCSSNumber(mValue.mFloat, tmpStr); 32 tmpStr.AppendLiteral("px"); 33 break; 34 } 35 case CSS_STRING: { 36 tmpStr.Append(mValue.mString); 37 break; 38 } 39 case CSS_PERCENTAGE: { 40 nsStyleUtil::AppendCSSNumber(mValue.mFloat * 100, tmpStr); 41 tmpStr.Append(char16_t('%')); 42 break; 43 } 44 case CSS_NUMBER: { 45 nsStyleUtil::AppendCSSNumber(mValue.mFloat, tmpStr); 46 break; 47 } 48 case CSS_NUMBER_INT32: { 49 tmpStr.AppendInt(mValue.mInt32); 50 break; 51 } 52 case CSS_NUMBER_UINT32: { 53 tmpStr.AppendInt(mValue.mUint32); 54 break; 55 } 56 case CSS_DEG: { 57 nsStyleUtil::AppendCSSNumber(mValue.mFloat, tmpStr); 58 tmpStr.AppendLiteral("deg"); 59 break; 60 } 61 case CSS_S: { 62 nsStyleUtil::AppendCSSNumber(mValue.mFloat, tmpStr); 63 tmpStr.Append('s'); 64 break; 65 } 66 } 67 68 aCssText.Assign(tmpStr); 69 } 70 71 uint16_t nsROCSSPrimitiveValue::CssValueType() const { 72 return CSSValue::CSS_PRIMITIVE_VALUE; 73 } 74 75 void nsROCSSPrimitiveValue::SetNumber(float aValue) { 76 Reset(); 77 mValue.mFloat = aValue; 78 mType = CSS_NUMBER; 79 } 80 81 void nsROCSSPrimitiveValue::SetNumber(int32_t aValue) { 82 Reset(); 83 mValue.mInt32 = aValue; 84 mType = CSS_NUMBER_INT32; 85 } 86 87 void nsROCSSPrimitiveValue::SetNumber(uint32_t aValue) { 88 Reset(); 89 mValue.mUint32 = aValue; 90 mType = CSS_NUMBER_UINT32; 91 } 92 93 void nsROCSSPrimitiveValue::SetPercent(float aValue) { 94 Reset(); 95 mValue.mFloat = aValue; 96 mType = CSS_PERCENTAGE; 97 } 98 99 void nsROCSSPrimitiveValue::SetDegree(float aValue) { 100 Reset(); 101 mValue.mFloat = aValue; 102 mType = CSS_DEG; 103 } 104 105 void nsROCSSPrimitiveValue::SetPixels(float aValue) { 106 Reset(); 107 mValue.mFloat = aValue; 108 mType = CSS_PX; 109 } 110 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; 119 } 120 } 121 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; 130 } 131 } 132 133 void nsROCSSPrimitiveValue::SetTime(float aValue) { 134 Reset(); 135 mValue.mFloat = aValue; 136 mType = CSS_S; 137 } 138 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; 146 } 147 148 mType = CSS_UNKNOWN; 149 } 150 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; 157 } 158 return CSS_UNKNOWN; 159 } 160 return mType; 161 }