1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 /* DOM object representing values in DOM computed style */
40 #include "nsROCSSPrimitiveValue.h"
43 #include "nsDOMError.h"
45 #include "nsContentUtils.h"
46 #include "nsXPIDLString.h"
48 #include "nsPresContext.h"
50 nsROCSSPrimitiveValue::nsROCSSPrimitiveValue(PRInt32 aAppUnitsPerInch
)
51 : mType(CSS_PX
), mAppUnitsPerInch(aAppUnitsPerInch
)
57 nsROCSSPrimitiveValue::~nsROCSSPrimitiveValue()
63 nsROCSSPrimitiveValue::GetEscapedURI(nsIURI
*aURI
, PRUnichar
**aReturn
)
65 nsCAutoString specUTF8
;
66 aURI
->GetSpec(specUTF8
);
67 NS_ConvertUTF8toUTF16
spec(specUTF8
);
69 PRUint16 length
= spec
.Length();
70 PRUnichar
*escaped
= (PRUnichar
*)nsMemory::Alloc(length
* 2 * sizeof(PRUnichar
) + sizeof(PRUnichar('\0')));
73 PRUnichar
*ptr
= escaped
;
75 for (PRUint16 i
= 0; i
< length
; ++i
) {
79 case '(' : // opening parenthesis
80 case ')' : // closing parenthesis
81 case '\'': // single quote
82 case '"' : // double quote
84 case '\\': // backslash
85 // We have one of the above special characters.
86 // Prepend it with a backslash.
100 NS_IMPL_ADDREF(nsROCSSPrimitiveValue
)
101 NS_IMPL_RELEASE(nsROCSSPrimitiveValue
)
104 // QueryInterface implementation for nsROCSSPrimitiveValue
105 NS_INTERFACE_MAP_BEGIN(nsROCSSPrimitiveValue
)
106 NS_INTERFACE_MAP_ENTRY(nsIDOMCSSPrimitiveValue
)
107 NS_INTERFACE_MAP_ENTRY(nsIDOMCSSValue
)
108 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports
, nsIDOMCSSPrimitiveValue
)
109 NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(ROCSSPrimitiveValue
)
117 nsROCSSPrimitiveValue::GetCssText(nsAString
& aCssText
)
121 nsresult result
= NS_OK
;
126 float val
= nsPresContext::AppUnitsToFloatCSSPixels(mValue
.mAppUnits
);
127 tmpStr
.AppendFloat(val
);
128 tmpStr
.AppendLiteral("px");
133 const char *atomValue
;
134 mValue
.mAtom
->GetUTF8String(&atomValue
);
135 AppendUTF8toUTF16(atomValue
, tmpStr
);
139 case CSS_COUNTER
: /* FIXME: COUNTER should use an object */
141 tmpStr
.Append(mValue
.mString
);
148 GetEscapedURI(mValue
.mURI
, getter_Copies(uri
));
149 tmpStr
.Assign(NS_LITERAL_STRING("url(") +
151 NS_LITERAL_STRING(")"));
153 // XXXldb Any better ideas? It's good to have something that
154 // doesn't parse so that things round-trip "correctly".
155 tmpStr
.Assign(NS_LITERAL_STRING("url(invalid-url:)"));
161 tmpStr
.AppendLiteral("attr(");
162 tmpStr
.Append(mValue
.mString
);
163 tmpStr
.Append(PRUnichar(')'));
166 case CSS_PERCENTAGE
:
168 tmpStr
.AppendFloat(mValue
.mFloat
* 100);
169 tmpStr
.Append(PRUnichar('%'));
174 tmpStr
.AppendFloat(mValue
.mFloat
);
179 NS_ASSERTION(mValue
.mRect
, "mValue.mRect should never be null");
180 NS_NAMED_LITERAL_STRING(comma
, ", ");
181 nsCOMPtr
<nsIDOMCSSPrimitiveValue
> sideCSSValue
;
182 nsAutoString sideValue
;
183 tmpStr
.AssignLiteral("rect(");
185 result
= mValue
.mRect
->GetTop(getter_AddRefs(sideCSSValue
));
186 if (NS_FAILED(result
))
188 result
= sideCSSValue
->GetCssText(sideValue
);
189 if (NS_FAILED(result
))
191 tmpStr
.Append(sideValue
+ comma
);
193 result
= mValue
.mRect
->GetRight(getter_AddRefs(sideCSSValue
));
194 if (NS_FAILED(result
))
196 result
= sideCSSValue
->GetCssText(sideValue
);
197 if (NS_FAILED(result
))
199 tmpStr
.Append(sideValue
+ comma
);
201 result
= mValue
.mRect
->GetBottom(getter_AddRefs(sideCSSValue
));
202 if (NS_FAILED(result
))
204 result
= sideCSSValue
->GetCssText(sideValue
);
205 if (NS_FAILED(result
))
207 tmpStr
.Append(sideValue
+ comma
);
209 result
= mValue
.mRect
->GetLeft(getter_AddRefs(sideCSSValue
));
210 if (NS_FAILED(result
))
212 result
= sideCSSValue
->GetCssText(sideValue
);
213 if (NS_FAILED(result
))
215 tmpStr
.Append(sideValue
+ NS_LITERAL_STRING(")"));
220 NS_ASSERTION(mValue
.mColor
, "mValue.mColor should never be null");
221 NS_NAMED_LITERAL_STRING(comma
, ", ");
222 nsCOMPtr
<nsIDOMCSSPrimitiveValue
> colorCSSValue
;
223 nsAutoString colorValue
;
224 if (mValue
.mColor
->HasAlpha())
225 tmpStr
.AssignLiteral("rgba(");
227 tmpStr
.AssignLiteral("rgb(");
229 // get the red component
230 result
= mValue
.mColor
->GetRed(getter_AddRefs(colorCSSValue
));
231 if (NS_FAILED(result
))
233 result
= colorCSSValue
->GetCssText(colorValue
);
234 if (NS_FAILED(result
))
236 tmpStr
.Append(colorValue
+ comma
);
238 // get the green component
239 result
= mValue
.mColor
->GetGreen(getter_AddRefs(colorCSSValue
));
240 if (NS_FAILED(result
))
242 result
= colorCSSValue
->GetCssText(colorValue
);
243 if (NS_FAILED(result
))
245 tmpStr
.Append(colorValue
+ comma
);
247 // get the blue component
248 result
= mValue
.mColor
->GetBlue(getter_AddRefs(colorCSSValue
));
249 if (NS_FAILED(result
))
251 result
= colorCSSValue
->GetCssText(colorValue
);
252 if (NS_FAILED(result
))
254 tmpStr
.Append(colorValue
);
256 if (mValue
.mColor
->HasAlpha()) {
257 // get the alpha component
258 result
= mValue
.mColor
->GetAlpha(getter_AddRefs(colorCSSValue
));
259 if (NS_FAILED(result
))
261 result
= colorCSSValue
->GetCssText(colorValue
);
262 if (NS_FAILED(result
))
264 tmpStr
.Append(comma
+ colorValue
);
267 tmpStr
.Append(NS_LITERAL_STRING(")"));
287 NS_ERROR("We have a bogus value set. This should not happen");
288 return NS_ERROR_DOM_INVALID_ACCESS_ERR
;
291 if (NS_SUCCEEDED(result
)) {
292 aCssText
.Assign(tmpStr
);
300 nsROCSSPrimitiveValue::SetCssText(const nsAString
& aCssText
)
302 return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR
;
307 nsROCSSPrimitiveValue::GetCssValueType(PRUint16
* aValueType
)
309 NS_ENSURE_ARG_POINTER(aValueType
);
310 *aValueType
= nsIDOMCSSValue::CSS_PRIMITIVE_VALUE
;
315 // nsIDOMCSSPrimitiveValue
318 nsROCSSPrimitiveValue::GetPrimitiveType(PRUint16
* aPrimitiveType
)
320 NS_ENSURE_ARG_POINTER(aPrimitiveType
);
321 *aPrimitiveType
= mType
;
328 nsROCSSPrimitiveValue::SetFloatValue(PRUint16 aUnitType
, float aFloatValue
)
330 return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR
;
335 nsROCSSPrimitiveValue::GetFloatValue(PRUint16 aUnitType
, float* aReturn
)
337 NS_ENSURE_ARG_POINTER(aReturn
);
343 return NS_ERROR_DOM_INVALID_ACCESS_ERR
;
344 *aReturn
= nsPresContext::AppUnitsToFloatCSSPixels(mValue
.mAppUnits
);
348 return NS_ERROR_DOM_INVALID_ACCESS_ERR
;
349 *aReturn
= mValue
.mAppUnits
* 2.54f
/ float(mAppUnitsPerInch
);
353 return NS_ERROR_DOM_INVALID_ACCESS_ERR
;
354 *aReturn
= mValue
.mAppUnits
* 25.4f
/ float(mAppUnitsPerInch
);
358 return NS_ERROR_DOM_INVALID_ACCESS_ERR
;
359 *aReturn
= mValue
.mAppUnits
/ float(mAppUnitsPerInch
);
363 return NS_ERROR_DOM_INVALID_ACCESS_ERR
;
364 *aReturn
= mValue
.mAppUnits
* POINTS_PER_INCH_FLOAT
/
365 float(mAppUnitsPerInch
);
369 return NS_ERROR_DOM_INVALID_ACCESS_ERR
;
370 *aReturn
= mValue
.mAppUnits
* 6.0f
/ float(mAppUnitsPerInch
);
372 case CSS_PERCENTAGE
:
373 if (mType
!= CSS_PERCENTAGE
)
374 return NS_ERROR_DOM_INVALID_ACCESS_ERR
;
375 *aReturn
= mValue
.mFloat
* 100;
378 if (mType
!= CSS_NUMBER
)
379 return NS_ERROR_DOM_INVALID_ACCESS_ERR
;
380 *aReturn
= mValue
.mFloat
;
400 return NS_ERROR_DOM_INVALID_ACCESS_ERR
;
408 nsROCSSPrimitiveValue::SetStringValue(PRUint16 aStringType
,
409 const nsAString
& aStringValue
)
411 return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR
;
416 nsROCSSPrimitiveValue::GetStringValue(nsAString
& aReturn
)
420 mValue
.mAtom
->ToString(aReturn
);
424 aReturn
.Assign(mValue
.mString
);
429 mValue
.mURI
->GetSpec(spec
);
430 CopyUTF8toUTF16(spec
, aReturn
);
434 return NS_ERROR_DOM_INVALID_ACCESS_ERR
;
441 nsROCSSPrimitiveValue::GetCounterValue(nsIDOMCounter
** aReturn
)
443 return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR
;
448 nsROCSSPrimitiveValue::GetRectValue(nsIDOMRect
** aReturn
)
450 if (mType
!= CSS_RECT
) {
452 return NS_ERROR_DOM_INVALID_ACCESS_ERR
;
454 NS_ASSERTION(mValue
.mRect
, "mValue.mRect should never be null");
455 return CallQueryInterface(mValue
.mRect
, aReturn
);
460 nsROCSSPrimitiveValue::GetRGBColorValue(nsIDOMRGBColor
** aReturn
)
462 if (mType
!= CSS_RGBCOLOR
) {
464 return NS_ERROR_DOM_INVALID_ACCESS_ERR
;
466 NS_ASSERTION(mValue
.mColor
, "mValue.mColor should never be null");
467 return CallQueryInterface(mValue
.mColor
, aReturn
);