michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* DOM object representing values in DOM computed style */ michael@0: michael@0: #include "nsROCSSPrimitiveValue.h" michael@0: michael@0: #include "mozilla/dom/CSSPrimitiveValueBinding.h" michael@0: #include "nsPresContext.h" michael@0: #include "nsStyleUtil.h" michael@0: #include "nsDOMCSSRGBColor.h" michael@0: #include "nsDOMCSSRect.h" michael@0: #include "nsIURI.h" michael@0: #include "nsError.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: nsROCSSPrimitiveValue::nsROCSSPrimitiveValue() michael@0: : CSSValue(), mType(CSS_PX) michael@0: { michael@0: mValue.mAppUnits = 0; michael@0: SetIsDOMBinding(); michael@0: } michael@0: michael@0: michael@0: nsROCSSPrimitiveValue::~nsROCSSPrimitiveValue() michael@0: { michael@0: Reset(); michael@0: } michael@0: michael@0: NS_IMPL_CYCLE_COLLECTING_ADDREF(nsROCSSPrimitiveValue) michael@0: NS_IMPL_CYCLE_COLLECTING_RELEASE(nsROCSSPrimitiveValue) michael@0: michael@0: michael@0: // QueryInterface implementation for nsROCSSPrimitiveValue michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsROCSSPrimitiveValue) michael@0: NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY michael@0: NS_INTERFACE_MAP_ENTRY(nsIDOMCSSPrimitiveValue) michael@0: NS_INTERFACE_MAP_ENTRY(nsIDOMCSSValue) michael@0: NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, CSSValue) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_CLASS(nsROCSSPrimitiveValue) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(nsROCSSPrimitiveValue) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsROCSSPrimitiveValue) michael@0: if (tmp->mType == CSS_URI) { michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_RAWPTR(mValue.mURI) michael@0: } else if (tmp->mType == CSS_RGBCOLOR) { michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_RAWPTR(mValue.mColor) michael@0: } else if (tmp->mType == CSS_RECT) { michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_RAWPTR(mValue.mRect) michael@0: } michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsROCSSPrimitiveValue) michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER michael@0: tmp->Reset(); michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_END michael@0: michael@0: JSObject* michael@0: nsROCSSPrimitiveValue::WrapObject(JSContext *cx) michael@0: { michael@0: return dom::CSSPrimitiveValueBinding::Wrap(cx, this); michael@0: } michael@0: michael@0: // nsIDOMCSSValue michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsROCSSPrimitiveValue::GetCssText(nsAString& aCssText) michael@0: { michael@0: nsAutoString tmpStr; michael@0: aCssText.Truncate(); michael@0: nsresult result = NS_OK; michael@0: michael@0: switch (mType) { michael@0: case CSS_PX : michael@0: { michael@0: float val = nsPresContext::AppUnitsToFloatCSSPixels(mValue.mAppUnits); michael@0: nsStyleUtil::AppendCSSNumber(val, tmpStr); michael@0: tmpStr.AppendLiteral("px"); michael@0: break; michael@0: } michael@0: case CSS_IDENT : michael@0: { michael@0: AppendUTF8toUTF16(nsCSSKeywords::GetStringValue(mValue.mKeyword), michael@0: tmpStr); michael@0: break; michael@0: } michael@0: case CSS_STRING : michael@0: case CSS_COUNTER : /* FIXME: COUNTER should use an object */ michael@0: { michael@0: tmpStr.Append(mValue.mString); michael@0: break; michael@0: } michael@0: case CSS_URI : michael@0: { michael@0: if (mValue.mURI) { michael@0: nsAutoCString specUTF8; michael@0: mValue.mURI->GetSpec(specUTF8); michael@0: michael@0: tmpStr.AssignLiteral("url("); michael@0: nsStyleUtil::AppendEscapedCSSString(NS_ConvertUTF8toUTF16(specUTF8), michael@0: tmpStr); michael@0: tmpStr.AppendLiteral(")"); michael@0: } else { michael@0: // http://dev.w3.org/csswg/css3-values/#attr defines michael@0: // 'about:invalid' as the default value for url attributes, michael@0: // so let's also use it here as the default computed value michael@0: // for invalid URLs. michael@0: tmpStr.Assign(NS_LITERAL_STRING("url(about:invalid)")); michael@0: } michael@0: break; michael@0: } michael@0: case CSS_ATTR : michael@0: { michael@0: tmpStr.AppendLiteral("attr("); michael@0: tmpStr.Append(mValue.mString); michael@0: tmpStr.Append(char16_t(')')); michael@0: break; michael@0: } michael@0: case CSS_PERCENTAGE : michael@0: { michael@0: nsStyleUtil::AppendCSSNumber(mValue.mFloat * 100, tmpStr); michael@0: tmpStr.Append(char16_t('%')); michael@0: break; michael@0: } michael@0: case CSS_NUMBER : michael@0: { michael@0: nsStyleUtil::AppendCSSNumber(mValue.mFloat, tmpStr); michael@0: break; michael@0: } michael@0: case CSS_NUMBER_INT32 : michael@0: { michael@0: tmpStr.AppendInt(mValue.mInt32); michael@0: break; michael@0: } michael@0: case CSS_NUMBER_UINT32 : michael@0: { michael@0: tmpStr.AppendInt(mValue.mUint32); michael@0: break; michael@0: } michael@0: case CSS_DEG : michael@0: { michael@0: nsStyleUtil::AppendCSSNumber(mValue.mFloat, tmpStr); michael@0: tmpStr.AppendLiteral("deg"); michael@0: break; michael@0: } michael@0: case CSS_GRAD : michael@0: { michael@0: nsStyleUtil::AppendCSSNumber(mValue.mFloat, tmpStr); michael@0: tmpStr.AppendLiteral("grad"); michael@0: break; michael@0: } michael@0: case CSS_RAD : michael@0: { michael@0: nsStyleUtil::AppendCSSNumber(mValue.mFloat, tmpStr); michael@0: tmpStr.AppendLiteral("rad"); michael@0: break; michael@0: } michael@0: case CSS_TURN : michael@0: { michael@0: nsStyleUtil::AppendCSSNumber(mValue.mFloat, tmpStr); michael@0: tmpStr.AppendLiteral("turn"); michael@0: break; michael@0: } michael@0: case CSS_RECT : michael@0: { michael@0: NS_ASSERTION(mValue.mRect, "mValue.mRect should never be null"); michael@0: NS_NAMED_LITERAL_STRING(comma, ", "); michael@0: nsCOMPtr sideCSSValue; michael@0: nsAutoString sideValue; michael@0: tmpStr.AssignLiteral("rect("); michael@0: // get the top michael@0: result = mValue.mRect->GetTop(getter_AddRefs(sideCSSValue)); michael@0: if (NS_FAILED(result)) michael@0: break; michael@0: result = sideCSSValue->GetCssText(sideValue); michael@0: if (NS_FAILED(result)) michael@0: break; michael@0: tmpStr.Append(sideValue + comma); michael@0: // get the right michael@0: result = mValue.mRect->GetRight(getter_AddRefs(sideCSSValue)); michael@0: if (NS_FAILED(result)) michael@0: break; michael@0: result = sideCSSValue->GetCssText(sideValue); michael@0: if (NS_FAILED(result)) michael@0: break; michael@0: tmpStr.Append(sideValue + comma); michael@0: // get the bottom michael@0: result = mValue.mRect->GetBottom(getter_AddRefs(sideCSSValue)); michael@0: if (NS_FAILED(result)) michael@0: break; michael@0: result = sideCSSValue->GetCssText(sideValue); michael@0: if (NS_FAILED(result)) michael@0: break; michael@0: tmpStr.Append(sideValue + comma); michael@0: // get the left michael@0: result = mValue.mRect->GetLeft(getter_AddRefs(sideCSSValue)); michael@0: if (NS_FAILED(result)) michael@0: break; michael@0: result = sideCSSValue->GetCssText(sideValue); michael@0: if (NS_FAILED(result)) michael@0: break; michael@0: tmpStr.Append(sideValue + NS_LITERAL_STRING(")")); michael@0: break; michael@0: } michael@0: case CSS_RGBCOLOR : michael@0: { michael@0: NS_ASSERTION(mValue.mColor, "mValue.mColor should never be null"); michael@0: ErrorResult error; michael@0: NS_NAMED_LITERAL_STRING(comma, ", "); michael@0: nsAutoString colorValue; michael@0: if (mValue.mColor->HasAlpha()) michael@0: tmpStr.AssignLiteral("rgba("); michael@0: else michael@0: tmpStr.AssignLiteral("rgb("); michael@0: michael@0: // get the red component michael@0: mValue.mColor->Red()->GetCssText(colorValue, error); michael@0: if (error.Failed()) michael@0: break; michael@0: tmpStr.Append(colorValue + comma); michael@0: michael@0: // get the green component michael@0: mValue.mColor->Green()->GetCssText(colorValue, error); michael@0: if (error.Failed()) michael@0: break; michael@0: tmpStr.Append(colorValue + comma); michael@0: michael@0: // get the blue component michael@0: mValue.mColor->Blue()->GetCssText(colorValue, error); michael@0: if (error.Failed()) michael@0: break; michael@0: tmpStr.Append(colorValue); michael@0: michael@0: if (mValue.mColor->HasAlpha()) { michael@0: // get the alpha component michael@0: mValue.mColor->Alpha()->GetCssText(colorValue, error); michael@0: if (error.Failed()) michael@0: break; michael@0: tmpStr.Append(comma + colorValue); michael@0: } michael@0: michael@0: tmpStr.Append(NS_LITERAL_STRING(")")); michael@0: michael@0: break; michael@0: } michael@0: case CSS_S : michael@0: { michael@0: nsStyleUtil::AppendCSSNumber(mValue.mFloat, tmpStr); michael@0: tmpStr.AppendLiteral("s"); michael@0: break; michael@0: } michael@0: case CSS_CM : michael@0: case CSS_MM : michael@0: case CSS_IN : michael@0: case CSS_PT : michael@0: case CSS_PC : michael@0: case CSS_UNKNOWN : michael@0: case CSS_EMS : michael@0: case CSS_EXS : michael@0: case CSS_MS : michael@0: case CSS_HZ : michael@0: case CSS_KHZ : michael@0: case CSS_DIMENSION : michael@0: NS_ERROR("We have a bogus value set. This should not happen"); michael@0: return NS_ERROR_DOM_INVALID_ACCESS_ERR; michael@0: } michael@0: michael@0: if (NS_SUCCEEDED(result)) { michael@0: aCssText.Assign(tmpStr); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: nsROCSSPrimitiveValue::GetCssText(nsString& aText, ErrorResult& aRv) michael@0: { michael@0: aRv = GetCssText(aText); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsROCSSPrimitiveValue::SetCssText(const nsAString& aCssText) michael@0: { michael@0: return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR; michael@0: } michael@0: michael@0: void michael@0: nsROCSSPrimitiveValue::SetCssText(const nsAString& aText, ErrorResult& aRv) michael@0: { michael@0: aRv = SetCssText(aText); michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsROCSSPrimitiveValue::GetCssValueType(uint16_t* aValueType) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aValueType); michael@0: *aValueType = nsIDOMCSSValue::CSS_PRIMITIVE_VALUE; michael@0: return NS_OK; michael@0: } michael@0: michael@0: uint16_t michael@0: nsROCSSPrimitiveValue::CssValueType() const michael@0: { michael@0: return nsIDOMCSSValue::CSS_PRIMITIVE_VALUE; michael@0: } michael@0: michael@0: michael@0: // nsIDOMCSSPrimitiveValue michael@0: michael@0: NS_IMETHODIMP michael@0: nsROCSSPrimitiveValue::GetPrimitiveType(uint16_t* aPrimitiveType) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aPrimitiveType); michael@0: *aPrimitiveType = PrimitiveType(); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsROCSSPrimitiveValue::SetFloatValue(uint16_t aUnitType, float aFloatValue) michael@0: { michael@0: return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR; michael@0: } michael@0: michael@0: void michael@0: nsROCSSPrimitiveValue::SetFloatValue(uint16_t aType, float aVal, michael@0: ErrorResult& aRv) michael@0: { michael@0: aRv = SetFloatValue(aType, aVal); michael@0: } michael@0: michael@0: float michael@0: nsROCSSPrimitiveValue::GetFloatValue(uint16_t aUnitType, ErrorResult& aRv) michael@0: { michael@0: switch(aUnitType) { michael@0: case CSS_PX : michael@0: if (mType == CSS_PX) { michael@0: return nsPresContext::AppUnitsToFloatCSSPixels(mValue.mAppUnits); michael@0: } michael@0: michael@0: break; michael@0: case CSS_CM : michael@0: if (mType == CSS_PX) { michael@0: return mValue.mAppUnits * CM_PER_INCH_FLOAT / michael@0: nsPresContext::AppUnitsPerCSSInch(); michael@0: } michael@0: michael@0: break; michael@0: case CSS_MM : michael@0: if (mType == CSS_PX) { michael@0: return mValue.mAppUnits * MM_PER_INCH_FLOAT / michael@0: nsPresContext::AppUnitsPerCSSInch(); michael@0: } michael@0: michael@0: break; michael@0: case CSS_IN : michael@0: if (mType == CSS_PX) { michael@0: return mValue.mAppUnits / nsPresContext::AppUnitsPerCSSInch(); michael@0: } michael@0: michael@0: break; michael@0: case CSS_PT : michael@0: if (mType == CSS_PX) { michael@0: return mValue.mAppUnits * POINTS_PER_INCH_FLOAT / michael@0: nsPresContext::AppUnitsPerCSSInch(); michael@0: } michael@0: michael@0: break; michael@0: case CSS_PC : michael@0: if (mType == CSS_PX) { michael@0: return mValue.mAppUnits * 6.0f / michael@0: nsPresContext::AppUnitsPerCSSInch(); michael@0: } michael@0: michael@0: break; michael@0: case CSS_PERCENTAGE : michael@0: if (mType == CSS_PERCENTAGE) { michael@0: return mValue.mFloat * 100; michael@0: } michael@0: michael@0: break; michael@0: case CSS_NUMBER : michael@0: if (mType == CSS_NUMBER) { michael@0: return mValue.mFloat; michael@0: } michael@0: if (mType == CSS_NUMBER_INT32) { michael@0: return mValue.mInt32; michael@0: } michael@0: if (mType == CSS_NUMBER_UINT32) { michael@0: return mValue.mUint32; michael@0: } michael@0: michael@0: break; michael@0: case CSS_UNKNOWN : michael@0: case CSS_EMS : michael@0: case CSS_EXS : michael@0: case CSS_DEG : michael@0: case CSS_RAD : michael@0: case CSS_GRAD : michael@0: case CSS_MS : michael@0: case CSS_S : michael@0: case CSS_HZ : michael@0: case CSS_KHZ : michael@0: case CSS_DIMENSION : michael@0: case CSS_STRING : michael@0: case CSS_URI : michael@0: case CSS_IDENT : michael@0: case CSS_ATTR : michael@0: case CSS_COUNTER : michael@0: case CSS_RECT : michael@0: case CSS_RGBCOLOR : michael@0: break; michael@0: } michael@0: michael@0: aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR); michael@0: return 0; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsROCSSPrimitiveValue::GetFloatValue(uint16_t aType, float *aVal) michael@0: { michael@0: ErrorResult rv; michael@0: *aVal = GetFloatValue(aType, rv); michael@0: return rv.ErrorCode(); michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsROCSSPrimitiveValue::SetStringValue(uint16_t aStringType, michael@0: const nsAString& aStringValue) michael@0: { michael@0: return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR; michael@0: } michael@0: michael@0: void michael@0: nsROCSSPrimitiveValue::SetStringValue(uint16_t aType, const nsAString& aString, michael@0: mozilla::ErrorResult& aRv) michael@0: { michael@0: aRv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR); michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsROCSSPrimitiveValue::GetStringValue(nsAString& aReturn) michael@0: { michael@0: switch (mType) { michael@0: case CSS_IDENT: michael@0: CopyUTF8toUTF16(nsCSSKeywords::GetStringValue(mValue.mKeyword), aReturn); michael@0: break; michael@0: case CSS_STRING: michael@0: case CSS_ATTR: michael@0: aReturn.Assign(mValue.mString); michael@0: break; michael@0: case CSS_URI: { michael@0: nsAutoCString spec; michael@0: if (mValue.mURI) michael@0: mValue.mURI->GetSpec(spec); michael@0: CopyUTF8toUTF16(spec, aReturn); michael@0: } break; michael@0: default: michael@0: aReturn.Truncate(); michael@0: return NS_ERROR_DOM_INVALID_ACCESS_ERR; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: nsROCSSPrimitiveValue::GetStringValue(nsString& aString, ErrorResult& aRv) michael@0: { michael@0: aRv = GetStringValue(aString); michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsROCSSPrimitiveValue::GetCounterValue(nsIDOMCounter** aReturn) michael@0: { michael@0: return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR; michael@0: } michael@0: michael@0: already_AddRefed michael@0: nsROCSSPrimitiveValue::GetCounterValue(ErrorResult& aRv) michael@0: { michael@0: aRv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR); michael@0: return nullptr; michael@0: } michael@0: michael@0: nsDOMCSSRect* michael@0: nsROCSSPrimitiveValue::GetRectValue(ErrorResult& aRv) michael@0: { michael@0: if (mType != CSS_RECT) { michael@0: aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR); michael@0: return nullptr; michael@0: } michael@0: michael@0: NS_ASSERTION(mValue.mRect, "mValue.mRect should never be null"); michael@0: return mValue.mRect; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsROCSSPrimitiveValue::GetRectValue(nsIDOMRect** aRect) michael@0: { michael@0: ErrorResult error; michael@0: NS_IF_ADDREF(*aRect = GetRectValue(error)); michael@0: return error.ErrorCode(); michael@0: } michael@0: michael@0: nsDOMCSSRGBColor* michael@0: nsROCSSPrimitiveValue::GetRGBColorValue(ErrorResult& aRv) michael@0: { michael@0: if (mType != CSS_RGBCOLOR) { michael@0: aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR); michael@0: return nullptr; michael@0: } michael@0: michael@0: NS_ASSERTION(mValue.mColor, "mValue.mColor should never be null"); michael@0: return mValue.mColor; michael@0: } michael@0: michael@0: void michael@0: nsROCSSPrimitiveValue::SetNumber(float aValue) michael@0: { michael@0: Reset(); michael@0: mValue.mFloat = aValue; michael@0: mType = CSS_NUMBER; michael@0: } michael@0: michael@0: void michael@0: nsROCSSPrimitiveValue::SetNumber(int32_t aValue) michael@0: { michael@0: Reset(); michael@0: mValue.mInt32 = aValue; michael@0: mType = CSS_NUMBER_INT32; michael@0: } michael@0: michael@0: void michael@0: nsROCSSPrimitiveValue::SetNumber(uint32_t aValue) michael@0: { michael@0: Reset(); michael@0: mValue.mUint32 = aValue; michael@0: mType = CSS_NUMBER_UINT32; michael@0: } michael@0: michael@0: void michael@0: nsROCSSPrimitiveValue::SetPercent(float aValue) michael@0: { michael@0: Reset(); michael@0: mValue.mFloat = aValue; michael@0: mType = CSS_PERCENTAGE; michael@0: } michael@0: michael@0: void michael@0: nsROCSSPrimitiveValue::SetDegree(float aValue) michael@0: { michael@0: Reset(); michael@0: mValue.mFloat = aValue; michael@0: mType = CSS_DEG; michael@0: } michael@0: michael@0: void michael@0: nsROCSSPrimitiveValue::SetGrad(float aValue) michael@0: { michael@0: Reset(); michael@0: mValue.mFloat = aValue; michael@0: mType = CSS_GRAD; michael@0: } michael@0: michael@0: void michael@0: nsROCSSPrimitiveValue::SetRadian(float aValue) michael@0: { michael@0: Reset(); michael@0: mValue.mFloat = aValue; michael@0: mType = CSS_RAD; michael@0: } michael@0: michael@0: void michael@0: nsROCSSPrimitiveValue::SetTurn(float aValue) michael@0: { michael@0: Reset(); michael@0: mValue.mFloat = aValue; michael@0: mType = CSS_TURN; michael@0: } michael@0: michael@0: void michael@0: nsROCSSPrimitiveValue::SetAppUnits(nscoord aValue) michael@0: { michael@0: Reset(); michael@0: mValue.mAppUnits = aValue; michael@0: mType = CSS_PX; michael@0: } michael@0: michael@0: void michael@0: nsROCSSPrimitiveValue::SetAppUnits(float aValue) michael@0: { michael@0: SetAppUnits(NSToCoordRound(aValue)); michael@0: } michael@0: michael@0: void michael@0: nsROCSSPrimitiveValue::SetIdent(nsCSSKeyword aKeyword) michael@0: { michael@0: NS_PRECONDITION(aKeyword != eCSSKeyword_UNKNOWN && michael@0: 0 <= aKeyword && aKeyword < eCSSKeyword_COUNT, michael@0: "bad keyword"); michael@0: Reset(); michael@0: mValue.mKeyword = aKeyword; michael@0: mType = CSS_IDENT; michael@0: } michael@0: michael@0: // FIXME: CSS_STRING should imply a string with "" and a need for escaping. michael@0: void michael@0: nsROCSSPrimitiveValue::SetString(const nsACString& aString, uint16_t aType) michael@0: { michael@0: Reset(); michael@0: mValue.mString = ToNewUnicode(aString); michael@0: if (mValue.mString) { michael@0: mType = aType; michael@0: } else { michael@0: // XXXcaa We should probably let the caller know we are out of memory michael@0: mType = CSS_UNKNOWN; michael@0: } michael@0: } michael@0: michael@0: // FIXME: CSS_STRING should imply a string with "" and a need for escaping. michael@0: void michael@0: nsROCSSPrimitiveValue::SetString(const nsAString& aString, uint16_t aType) michael@0: { michael@0: Reset(); michael@0: mValue.mString = ToNewUnicode(aString); michael@0: if (mValue.mString) { michael@0: mType = aType; michael@0: } else { michael@0: // XXXcaa We should probably let the caller know we are out of memory michael@0: mType = CSS_UNKNOWN; michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsROCSSPrimitiveValue::SetURI(nsIURI *aURI) michael@0: { michael@0: Reset(); michael@0: mValue.mURI = aURI; michael@0: NS_IF_ADDREF(mValue.mURI); michael@0: mType = CSS_URI; michael@0: } michael@0: michael@0: void michael@0: nsROCSSPrimitiveValue::SetColor(nsDOMCSSRGBColor* aColor) michael@0: { michael@0: NS_PRECONDITION(aColor, "Null RGBColor being set!"); michael@0: Reset(); michael@0: mValue.mColor = aColor; michael@0: if (mValue.mColor) { michael@0: NS_ADDREF(mValue.mColor); michael@0: mType = CSS_RGBCOLOR; michael@0: } michael@0: else { michael@0: mType = CSS_UNKNOWN; michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsROCSSPrimitiveValue::SetRect(nsDOMCSSRect* aRect) michael@0: { michael@0: NS_PRECONDITION(aRect, "Null rect being set!"); michael@0: Reset(); michael@0: mValue.mRect = aRect; michael@0: if (mValue.mRect) { michael@0: NS_ADDREF(mValue.mRect); michael@0: mType = CSS_RECT; michael@0: } michael@0: else { michael@0: mType = CSS_UNKNOWN; michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsROCSSPrimitiveValue::SetTime(float aValue) michael@0: { michael@0: Reset(); michael@0: mValue.mFloat = aValue; michael@0: mType = CSS_S; michael@0: } michael@0: michael@0: void michael@0: nsROCSSPrimitiveValue::Reset() michael@0: { michael@0: switch (mType) { michael@0: case CSS_IDENT: michael@0: break; michael@0: case CSS_STRING: michael@0: case CSS_ATTR: michael@0: case CSS_COUNTER: // FIXME: Counter should use an object michael@0: NS_ASSERTION(mValue.mString, "Null string should never happen"); michael@0: nsMemory::Free(mValue.mString); michael@0: mValue.mString = nullptr; michael@0: break; michael@0: case CSS_URI: michael@0: NS_IF_RELEASE(mValue.mURI); michael@0: break; michael@0: case CSS_RECT: michael@0: NS_ASSERTION(mValue.mRect, "Null Rect should never happen"); michael@0: NS_RELEASE(mValue.mRect); michael@0: break; michael@0: case CSS_RGBCOLOR: michael@0: NS_ASSERTION(mValue.mColor, "Null RGBColor should never happen"); michael@0: NS_RELEASE(mValue.mColor); michael@0: break; michael@0: } michael@0: michael@0: mType = CSS_UNKNOWN; michael@0: }