Bug 461267, bump version to 3.0.5pre/1.9.0.5pre, p=joduinn, r=nthomas
[mozilla-1.9.git] / layout / style / nsROCSSPrimitiveValue.cpp
blobe5c333165bf88979198450d168144911b6163ad9
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
13 * License.
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.
22 * Contributor(s):
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"
42 #include "nsCOMPtr.h"
43 #include "nsDOMError.h"
44 #include "prprf.h"
45 #include "nsContentUtils.h"
46 #include "nsXPIDLString.h"
47 #include "nsCRT.h"
48 #include "nsPresContext.h"
50 nsROCSSPrimitiveValue::nsROCSSPrimitiveValue(PRInt32 aAppUnitsPerInch)
51 : mType(CSS_PX), mAppUnitsPerInch(aAppUnitsPerInch)
53 mValue.mAppUnits = 0;
57 nsROCSSPrimitiveValue::~nsROCSSPrimitiveValue()
59 Reset();
62 void
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')));
72 if (escaped) {
73 PRUnichar *ptr = escaped;
75 for (PRUint16 i = 0; i < length; ++i) {
76 switch (spec[i]) {
77 case ' ' : // space
78 case '\t': // tab
79 case '(' : // opening parenthesis
80 case ')' : // closing parenthesis
81 case '\'': // single quote
82 case '"' : // double quote
83 case ',' : // comma
84 case '\\': // backslash
85 // We have one of the above special characters.
86 // Prepend it with a backslash.
87 *ptr++ = '\\';
88 break;
89 default:
90 break;
92 *ptr++ = spec[i];
94 *ptr = 0;
96 *aReturn = escaped;
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)
110 NS_INTERFACE_MAP_END
113 // nsIDOMCSSValue
116 NS_IMETHODIMP
117 nsROCSSPrimitiveValue::GetCssText(nsAString& aCssText)
119 nsAutoString tmpStr;
120 aCssText.Truncate();
121 nsresult result = NS_OK;
123 switch (mType) {
124 case CSS_PX :
126 float val = nsPresContext::AppUnitsToFloatCSSPixels(mValue.mAppUnits);
127 tmpStr.AppendFloat(val);
128 tmpStr.AppendLiteral("px");
129 break;
131 case CSS_IDENT :
133 const char *atomValue;
134 mValue.mAtom->GetUTF8String(&atomValue);
135 AppendUTF8toUTF16(atomValue, tmpStr);
136 break;
138 case CSS_STRING :
139 case CSS_COUNTER : /* FIXME: COUNTER should use an object */
141 tmpStr.Append(mValue.mString);
142 break;
144 case CSS_URI :
146 nsXPIDLString uri;
147 if (mValue.mURI) {
148 GetEscapedURI(mValue.mURI, getter_Copies(uri));
149 tmpStr.Assign(NS_LITERAL_STRING("url(") +
150 uri +
151 NS_LITERAL_STRING(")"));
152 } else {
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:)"));
157 break;
159 case CSS_ATTR :
161 tmpStr.AppendLiteral("attr(");
162 tmpStr.Append(mValue.mString);
163 tmpStr.Append(PRUnichar(')'));
164 break;
166 case CSS_PERCENTAGE :
168 tmpStr.AppendFloat(mValue.mFloat * 100);
169 tmpStr.Append(PRUnichar('%'));
170 break;
172 case CSS_NUMBER :
174 tmpStr.AppendFloat(mValue.mFloat);
175 break;
177 case CSS_RECT :
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(");
184 // get the top
185 result = mValue.mRect->GetTop(getter_AddRefs(sideCSSValue));
186 if (NS_FAILED(result))
187 break;
188 result = sideCSSValue->GetCssText(sideValue);
189 if (NS_FAILED(result))
190 break;
191 tmpStr.Append(sideValue + comma);
192 // get the right
193 result = mValue.mRect->GetRight(getter_AddRefs(sideCSSValue));
194 if (NS_FAILED(result))
195 break;
196 result = sideCSSValue->GetCssText(sideValue);
197 if (NS_FAILED(result))
198 break;
199 tmpStr.Append(sideValue + comma);
200 // get the bottom
201 result = mValue.mRect->GetBottom(getter_AddRefs(sideCSSValue));
202 if (NS_FAILED(result))
203 break;
204 result = sideCSSValue->GetCssText(sideValue);
205 if (NS_FAILED(result))
206 break;
207 tmpStr.Append(sideValue + comma);
208 // get the left
209 result = mValue.mRect->GetLeft(getter_AddRefs(sideCSSValue));
210 if (NS_FAILED(result))
211 break;
212 result = sideCSSValue->GetCssText(sideValue);
213 if (NS_FAILED(result))
214 break;
215 tmpStr.Append(sideValue + NS_LITERAL_STRING(")"));
216 break;
218 case CSS_RGBCOLOR :
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(");
226 else
227 tmpStr.AssignLiteral("rgb(");
229 // get the red component
230 result = mValue.mColor->GetRed(getter_AddRefs(colorCSSValue));
231 if (NS_FAILED(result))
232 break;
233 result = colorCSSValue->GetCssText(colorValue);
234 if (NS_FAILED(result))
235 break;
236 tmpStr.Append(colorValue + comma);
238 // get the green component
239 result = mValue.mColor->GetGreen(getter_AddRefs(colorCSSValue));
240 if (NS_FAILED(result))
241 break;
242 result = colorCSSValue->GetCssText(colorValue);
243 if (NS_FAILED(result))
244 break;
245 tmpStr.Append(colorValue + comma);
247 // get the blue component
248 result = mValue.mColor->GetBlue(getter_AddRefs(colorCSSValue));
249 if (NS_FAILED(result))
250 break;
251 result = colorCSSValue->GetCssText(colorValue);
252 if (NS_FAILED(result))
253 break;
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))
260 break;
261 result = colorCSSValue->GetCssText(colorValue);
262 if (NS_FAILED(result))
263 break;
264 tmpStr.Append(comma + colorValue);
267 tmpStr.Append(NS_LITERAL_STRING(")"));
269 break;
271 case CSS_CM :
272 case CSS_MM :
273 case CSS_IN :
274 case CSS_PT :
275 case CSS_PC :
276 case CSS_UNKNOWN :
277 case CSS_EMS :
278 case CSS_EXS :
279 case CSS_DEG :
280 case CSS_RAD :
281 case CSS_GRAD :
282 case CSS_MS :
283 case CSS_S :
284 case CSS_HZ :
285 case CSS_KHZ :
286 case CSS_DIMENSION :
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);
295 return NS_OK;
299 NS_IMETHODIMP
300 nsROCSSPrimitiveValue::SetCssText(const nsAString& aCssText)
302 return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR;
306 NS_IMETHODIMP
307 nsROCSSPrimitiveValue::GetCssValueType(PRUint16* aValueType)
309 NS_ENSURE_ARG_POINTER(aValueType);
310 *aValueType = nsIDOMCSSValue::CSS_PRIMITIVE_VALUE;
311 return NS_OK;
315 // nsIDOMCSSPrimitiveValue
317 NS_IMETHODIMP
318 nsROCSSPrimitiveValue::GetPrimitiveType(PRUint16* aPrimitiveType)
320 NS_ENSURE_ARG_POINTER(aPrimitiveType);
321 *aPrimitiveType = mType;
323 return NS_OK;
327 NS_IMETHODIMP
328 nsROCSSPrimitiveValue::SetFloatValue(PRUint16 aUnitType, float aFloatValue)
330 return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR;
334 NS_IMETHODIMP
335 nsROCSSPrimitiveValue::GetFloatValue(PRUint16 aUnitType, float* aReturn)
337 NS_ENSURE_ARG_POINTER(aReturn);
338 *aReturn = 0;
340 switch(aUnitType) {
341 case CSS_PX :
342 if (mType != CSS_PX)
343 return NS_ERROR_DOM_INVALID_ACCESS_ERR;
344 *aReturn = nsPresContext::AppUnitsToFloatCSSPixels(mValue.mAppUnits);
345 break;
346 case CSS_CM :
347 if (mType != CSS_PX)
348 return NS_ERROR_DOM_INVALID_ACCESS_ERR;
349 *aReturn = mValue.mAppUnits * 2.54f / float(mAppUnitsPerInch);
350 break;
351 case CSS_MM :
352 if (mType != CSS_PX)
353 return NS_ERROR_DOM_INVALID_ACCESS_ERR;
354 *aReturn = mValue.mAppUnits * 25.4f / float(mAppUnitsPerInch);
355 break;
356 case CSS_IN :
357 if (mType != CSS_PX)
358 return NS_ERROR_DOM_INVALID_ACCESS_ERR;
359 *aReturn = mValue.mAppUnits / float(mAppUnitsPerInch);
360 break;
361 case CSS_PT :
362 if (mType != CSS_PX)
363 return NS_ERROR_DOM_INVALID_ACCESS_ERR;
364 *aReturn = mValue.mAppUnits * POINTS_PER_INCH_FLOAT /
365 float(mAppUnitsPerInch);
366 break;
367 case CSS_PC :
368 if (mType != CSS_PX)
369 return NS_ERROR_DOM_INVALID_ACCESS_ERR;
370 *aReturn = mValue.mAppUnits * 6.0f / float(mAppUnitsPerInch);
371 break;
372 case CSS_PERCENTAGE :
373 if (mType != CSS_PERCENTAGE)
374 return NS_ERROR_DOM_INVALID_ACCESS_ERR;
375 *aReturn = mValue.mFloat * 100;
376 break;
377 case CSS_NUMBER :
378 if (mType != CSS_NUMBER)
379 return NS_ERROR_DOM_INVALID_ACCESS_ERR;
380 *aReturn = mValue.mFloat;
381 break;
382 case CSS_UNKNOWN :
383 case CSS_EMS :
384 case CSS_EXS :
385 case CSS_DEG :
386 case CSS_RAD :
387 case CSS_GRAD :
388 case CSS_MS :
389 case CSS_S :
390 case CSS_HZ :
391 case CSS_KHZ :
392 case CSS_DIMENSION :
393 case CSS_STRING :
394 case CSS_URI :
395 case CSS_IDENT :
396 case CSS_ATTR :
397 case CSS_COUNTER :
398 case CSS_RECT :
399 case CSS_RGBCOLOR :
400 return NS_ERROR_DOM_INVALID_ACCESS_ERR;
403 return NS_OK;
407 NS_IMETHODIMP
408 nsROCSSPrimitiveValue::SetStringValue(PRUint16 aStringType,
409 const nsAString& aStringValue)
411 return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR;
415 NS_IMETHODIMP
416 nsROCSSPrimitiveValue::GetStringValue(nsAString& aReturn)
418 switch (mType) {
419 case CSS_IDENT:
420 mValue.mAtom->ToString(aReturn);
421 break;
422 case CSS_STRING:
423 case CSS_ATTR:
424 aReturn.Assign(mValue.mString);
425 break;
426 case CSS_URI: {
427 nsCAutoString spec;
428 if (mValue.mURI)
429 mValue.mURI->GetSpec(spec);
430 CopyUTF8toUTF16(spec, aReturn);
431 } break;
432 default:
433 aReturn.Truncate();
434 return NS_ERROR_DOM_INVALID_ACCESS_ERR;
436 return NS_OK;
440 NS_IMETHODIMP
441 nsROCSSPrimitiveValue::GetCounterValue(nsIDOMCounter** aReturn)
443 return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR;
447 NS_IMETHODIMP
448 nsROCSSPrimitiveValue::GetRectValue(nsIDOMRect** aReturn)
450 if (mType != CSS_RECT) {
451 *aReturn = nsnull;
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);
459 NS_IMETHODIMP
460 nsROCSSPrimitiveValue::GetRGBColorValue(nsIDOMRGBColor** aReturn)
462 if (mType != CSS_RGBCOLOR) {
463 *aReturn = nsnull;
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);