layout/style/nsROCSSPrimitiveValue.cpp

Wed, 31 Dec 2014 07:16:47 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:16:47 +0100
branch
TOR_BUG_9701
changeset 3
141e0f1194b1
permissions
-rw-r--r--

Revert simplistic fix pending revisit of Mozilla integration attempt.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /* DOM object representing values in DOM computed style */
michael@0 7
michael@0 8 #include "nsROCSSPrimitiveValue.h"
michael@0 9
michael@0 10 #include "mozilla/dom/CSSPrimitiveValueBinding.h"
michael@0 11 #include "nsPresContext.h"
michael@0 12 #include "nsStyleUtil.h"
michael@0 13 #include "nsDOMCSSRGBColor.h"
michael@0 14 #include "nsDOMCSSRect.h"
michael@0 15 #include "nsIURI.h"
michael@0 16 #include "nsError.h"
michael@0 17
michael@0 18 using namespace mozilla;
michael@0 19
michael@0 20 nsROCSSPrimitiveValue::nsROCSSPrimitiveValue()
michael@0 21 : CSSValue(), mType(CSS_PX)
michael@0 22 {
michael@0 23 mValue.mAppUnits = 0;
michael@0 24 SetIsDOMBinding();
michael@0 25 }
michael@0 26
michael@0 27
michael@0 28 nsROCSSPrimitiveValue::~nsROCSSPrimitiveValue()
michael@0 29 {
michael@0 30 Reset();
michael@0 31 }
michael@0 32
michael@0 33 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsROCSSPrimitiveValue)
michael@0 34 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsROCSSPrimitiveValue)
michael@0 35
michael@0 36
michael@0 37 // QueryInterface implementation for nsROCSSPrimitiveValue
michael@0 38 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsROCSSPrimitiveValue)
michael@0 39 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
michael@0 40 NS_INTERFACE_MAP_ENTRY(nsIDOMCSSPrimitiveValue)
michael@0 41 NS_INTERFACE_MAP_ENTRY(nsIDOMCSSValue)
michael@0 42 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, CSSValue)
michael@0 43 NS_INTERFACE_MAP_END
michael@0 44
michael@0 45 NS_IMPL_CYCLE_COLLECTION_CLASS(nsROCSSPrimitiveValue)
michael@0 46
michael@0 47 NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(nsROCSSPrimitiveValue)
michael@0 48
michael@0 49 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsROCSSPrimitiveValue)
michael@0 50 if (tmp->mType == CSS_URI) {
michael@0 51 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_RAWPTR(mValue.mURI)
michael@0 52 } else if (tmp->mType == CSS_RGBCOLOR) {
michael@0 53 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_RAWPTR(mValue.mColor)
michael@0 54 } else if (tmp->mType == CSS_RECT) {
michael@0 55 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_RAWPTR(mValue.mRect)
michael@0 56 }
michael@0 57 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
michael@0 58 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
michael@0 59
michael@0 60 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsROCSSPrimitiveValue)
michael@0 61 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
michael@0 62 tmp->Reset();
michael@0 63 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
michael@0 64
michael@0 65 JSObject*
michael@0 66 nsROCSSPrimitiveValue::WrapObject(JSContext *cx)
michael@0 67 {
michael@0 68 return dom::CSSPrimitiveValueBinding::Wrap(cx, this);
michael@0 69 }
michael@0 70
michael@0 71 // nsIDOMCSSValue
michael@0 72
michael@0 73
michael@0 74 NS_IMETHODIMP
michael@0 75 nsROCSSPrimitiveValue::GetCssText(nsAString& aCssText)
michael@0 76 {
michael@0 77 nsAutoString tmpStr;
michael@0 78 aCssText.Truncate();
michael@0 79 nsresult result = NS_OK;
michael@0 80
michael@0 81 switch (mType) {
michael@0 82 case CSS_PX :
michael@0 83 {
michael@0 84 float val = nsPresContext::AppUnitsToFloatCSSPixels(mValue.mAppUnits);
michael@0 85 nsStyleUtil::AppendCSSNumber(val, tmpStr);
michael@0 86 tmpStr.AppendLiteral("px");
michael@0 87 break;
michael@0 88 }
michael@0 89 case CSS_IDENT :
michael@0 90 {
michael@0 91 AppendUTF8toUTF16(nsCSSKeywords::GetStringValue(mValue.mKeyword),
michael@0 92 tmpStr);
michael@0 93 break;
michael@0 94 }
michael@0 95 case CSS_STRING :
michael@0 96 case CSS_COUNTER : /* FIXME: COUNTER should use an object */
michael@0 97 {
michael@0 98 tmpStr.Append(mValue.mString);
michael@0 99 break;
michael@0 100 }
michael@0 101 case CSS_URI :
michael@0 102 {
michael@0 103 if (mValue.mURI) {
michael@0 104 nsAutoCString specUTF8;
michael@0 105 mValue.mURI->GetSpec(specUTF8);
michael@0 106
michael@0 107 tmpStr.AssignLiteral("url(");
michael@0 108 nsStyleUtil::AppendEscapedCSSString(NS_ConvertUTF8toUTF16(specUTF8),
michael@0 109 tmpStr);
michael@0 110 tmpStr.AppendLiteral(")");
michael@0 111 } else {
michael@0 112 // http://dev.w3.org/csswg/css3-values/#attr defines
michael@0 113 // 'about:invalid' as the default value for url attributes,
michael@0 114 // so let's also use it here as the default computed value
michael@0 115 // for invalid URLs.
michael@0 116 tmpStr.Assign(NS_LITERAL_STRING("url(about:invalid)"));
michael@0 117 }
michael@0 118 break;
michael@0 119 }
michael@0 120 case CSS_ATTR :
michael@0 121 {
michael@0 122 tmpStr.AppendLiteral("attr(");
michael@0 123 tmpStr.Append(mValue.mString);
michael@0 124 tmpStr.Append(char16_t(')'));
michael@0 125 break;
michael@0 126 }
michael@0 127 case CSS_PERCENTAGE :
michael@0 128 {
michael@0 129 nsStyleUtil::AppendCSSNumber(mValue.mFloat * 100, tmpStr);
michael@0 130 tmpStr.Append(char16_t('%'));
michael@0 131 break;
michael@0 132 }
michael@0 133 case CSS_NUMBER :
michael@0 134 {
michael@0 135 nsStyleUtil::AppendCSSNumber(mValue.mFloat, tmpStr);
michael@0 136 break;
michael@0 137 }
michael@0 138 case CSS_NUMBER_INT32 :
michael@0 139 {
michael@0 140 tmpStr.AppendInt(mValue.mInt32);
michael@0 141 break;
michael@0 142 }
michael@0 143 case CSS_NUMBER_UINT32 :
michael@0 144 {
michael@0 145 tmpStr.AppendInt(mValue.mUint32);
michael@0 146 break;
michael@0 147 }
michael@0 148 case CSS_DEG :
michael@0 149 {
michael@0 150 nsStyleUtil::AppendCSSNumber(mValue.mFloat, tmpStr);
michael@0 151 tmpStr.AppendLiteral("deg");
michael@0 152 break;
michael@0 153 }
michael@0 154 case CSS_GRAD :
michael@0 155 {
michael@0 156 nsStyleUtil::AppendCSSNumber(mValue.mFloat, tmpStr);
michael@0 157 tmpStr.AppendLiteral("grad");
michael@0 158 break;
michael@0 159 }
michael@0 160 case CSS_RAD :
michael@0 161 {
michael@0 162 nsStyleUtil::AppendCSSNumber(mValue.mFloat, tmpStr);
michael@0 163 tmpStr.AppendLiteral("rad");
michael@0 164 break;
michael@0 165 }
michael@0 166 case CSS_TURN :
michael@0 167 {
michael@0 168 nsStyleUtil::AppendCSSNumber(mValue.mFloat, tmpStr);
michael@0 169 tmpStr.AppendLiteral("turn");
michael@0 170 break;
michael@0 171 }
michael@0 172 case CSS_RECT :
michael@0 173 {
michael@0 174 NS_ASSERTION(mValue.mRect, "mValue.mRect should never be null");
michael@0 175 NS_NAMED_LITERAL_STRING(comma, ", ");
michael@0 176 nsCOMPtr<nsIDOMCSSPrimitiveValue> sideCSSValue;
michael@0 177 nsAutoString sideValue;
michael@0 178 tmpStr.AssignLiteral("rect(");
michael@0 179 // get the top
michael@0 180 result = mValue.mRect->GetTop(getter_AddRefs(sideCSSValue));
michael@0 181 if (NS_FAILED(result))
michael@0 182 break;
michael@0 183 result = sideCSSValue->GetCssText(sideValue);
michael@0 184 if (NS_FAILED(result))
michael@0 185 break;
michael@0 186 tmpStr.Append(sideValue + comma);
michael@0 187 // get the right
michael@0 188 result = mValue.mRect->GetRight(getter_AddRefs(sideCSSValue));
michael@0 189 if (NS_FAILED(result))
michael@0 190 break;
michael@0 191 result = sideCSSValue->GetCssText(sideValue);
michael@0 192 if (NS_FAILED(result))
michael@0 193 break;
michael@0 194 tmpStr.Append(sideValue + comma);
michael@0 195 // get the bottom
michael@0 196 result = mValue.mRect->GetBottom(getter_AddRefs(sideCSSValue));
michael@0 197 if (NS_FAILED(result))
michael@0 198 break;
michael@0 199 result = sideCSSValue->GetCssText(sideValue);
michael@0 200 if (NS_FAILED(result))
michael@0 201 break;
michael@0 202 tmpStr.Append(sideValue + comma);
michael@0 203 // get the left
michael@0 204 result = mValue.mRect->GetLeft(getter_AddRefs(sideCSSValue));
michael@0 205 if (NS_FAILED(result))
michael@0 206 break;
michael@0 207 result = sideCSSValue->GetCssText(sideValue);
michael@0 208 if (NS_FAILED(result))
michael@0 209 break;
michael@0 210 tmpStr.Append(sideValue + NS_LITERAL_STRING(")"));
michael@0 211 break;
michael@0 212 }
michael@0 213 case CSS_RGBCOLOR :
michael@0 214 {
michael@0 215 NS_ASSERTION(mValue.mColor, "mValue.mColor should never be null");
michael@0 216 ErrorResult error;
michael@0 217 NS_NAMED_LITERAL_STRING(comma, ", ");
michael@0 218 nsAutoString colorValue;
michael@0 219 if (mValue.mColor->HasAlpha())
michael@0 220 tmpStr.AssignLiteral("rgba(");
michael@0 221 else
michael@0 222 tmpStr.AssignLiteral("rgb(");
michael@0 223
michael@0 224 // get the red component
michael@0 225 mValue.mColor->Red()->GetCssText(colorValue, error);
michael@0 226 if (error.Failed())
michael@0 227 break;
michael@0 228 tmpStr.Append(colorValue + comma);
michael@0 229
michael@0 230 // get the green component
michael@0 231 mValue.mColor->Green()->GetCssText(colorValue, error);
michael@0 232 if (error.Failed())
michael@0 233 break;
michael@0 234 tmpStr.Append(colorValue + comma);
michael@0 235
michael@0 236 // get the blue component
michael@0 237 mValue.mColor->Blue()->GetCssText(colorValue, error);
michael@0 238 if (error.Failed())
michael@0 239 break;
michael@0 240 tmpStr.Append(colorValue);
michael@0 241
michael@0 242 if (mValue.mColor->HasAlpha()) {
michael@0 243 // get the alpha component
michael@0 244 mValue.mColor->Alpha()->GetCssText(colorValue, error);
michael@0 245 if (error.Failed())
michael@0 246 break;
michael@0 247 tmpStr.Append(comma + colorValue);
michael@0 248 }
michael@0 249
michael@0 250 tmpStr.Append(NS_LITERAL_STRING(")"));
michael@0 251
michael@0 252 break;
michael@0 253 }
michael@0 254 case CSS_S :
michael@0 255 {
michael@0 256 nsStyleUtil::AppendCSSNumber(mValue.mFloat, tmpStr);
michael@0 257 tmpStr.AppendLiteral("s");
michael@0 258 break;
michael@0 259 }
michael@0 260 case CSS_CM :
michael@0 261 case CSS_MM :
michael@0 262 case CSS_IN :
michael@0 263 case CSS_PT :
michael@0 264 case CSS_PC :
michael@0 265 case CSS_UNKNOWN :
michael@0 266 case CSS_EMS :
michael@0 267 case CSS_EXS :
michael@0 268 case CSS_MS :
michael@0 269 case CSS_HZ :
michael@0 270 case CSS_KHZ :
michael@0 271 case CSS_DIMENSION :
michael@0 272 NS_ERROR("We have a bogus value set. This should not happen");
michael@0 273 return NS_ERROR_DOM_INVALID_ACCESS_ERR;
michael@0 274 }
michael@0 275
michael@0 276 if (NS_SUCCEEDED(result)) {
michael@0 277 aCssText.Assign(tmpStr);
michael@0 278 }
michael@0 279
michael@0 280 return NS_OK;
michael@0 281 }
michael@0 282
michael@0 283 void
michael@0 284 nsROCSSPrimitiveValue::GetCssText(nsString& aText, ErrorResult& aRv)
michael@0 285 {
michael@0 286 aRv = GetCssText(aText);
michael@0 287 }
michael@0 288
michael@0 289 NS_IMETHODIMP
michael@0 290 nsROCSSPrimitiveValue::SetCssText(const nsAString& aCssText)
michael@0 291 {
michael@0 292 return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR;
michael@0 293 }
michael@0 294
michael@0 295 void
michael@0 296 nsROCSSPrimitiveValue::SetCssText(const nsAString& aText, ErrorResult& aRv)
michael@0 297 {
michael@0 298 aRv = SetCssText(aText);
michael@0 299 }
michael@0 300
michael@0 301
michael@0 302 NS_IMETHODIMP
michael@0 303 nsROCSSPrimitiveValue::GetCssValueType(uint16_t* aValueType)
michael@0 304 {
michael@0 305 NS_ENSURE_ARG_POINTER(aValueType);
michael@0 306 *aValueType = nsIDOMCSSValue::CSS_PRIMITIVE_VALUE;
michael@0 307 return NS_OK;
michael@0 308 }
michael@0 309
michael@0 310 uint16_t
michael@0 311 nsROCSSPrimitiveValue::CssValueType() const
michael@0 312 {
michael@0 313 return nsIDOMCSSValue::CSS_PRIMITIVE_VALUE;
michael@0 314 }
michael@0 315
michael@0 316
michael@0 317 // nsIDOMCSSPrimitiveValue
michael@0 318
michael@0 319 NS_IMETHODIMP
michael@0 320 nsROCSSPrimitiveValue::GetPrimitiveType(uint16_t* aPrimitiveType)
michael@0 321 {
michael@0 322 NS_ENSURE_ARG_POINTER(aPrimitiveType);
michael@0 323 *aPrimitiveType = PrimitiveType();
michael@0 324
michael@0 325 return NS_OK;
michael@0 326 }
michael@0 327
michael@0 328
michael@0 329 NS_IMETHODIMP
michael@0 330 nsROCSSPrimitiveValue::SetFloatValue(uint16_t aUnitType, float aFloatValue)
michael@0 331 {
michael@0 332 return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR;
michael@0 333 }
michael@0 334
michael@0 335 void
michael@0 336 nsROCSSPrimitiveValue::SetFloatValue(uint16_t aType, float aVal,
michael@0 337 ErrorResult& aRv)
michael@0 338 {
michael@0 339 aRv = SetFloatValue(aType, aVal);
michael@0 340 }
michael@0 341
michael@0 342 float
michael@0 343 nsROCSSPrimitiveValue::GetFloatValue(uint16_t aUnitType, ErrorResult& aRv)
michael@0 344 {
michael@0 345 switch(aUnitType) {
michael@0 346 case CSS_PX :
michael@0 347 if (mType == CSS_PX) {
michael@0 348 return nsPresContext::AppUnitsToFloatCSSPixels(mValue.mAppUnits);
michael@0 349 }
michael@0 350
michael@0 351 break;
michael@0 352 case CSS_CM :
michael@0 353 if (mType == CSS_PX) {
michael@0 354 return mValue.mAppUnits * CM_PER_INCH_FLOAT /
michael@0 355 nsPresContext::AppUnitsPerCSSInch();
michael@0 356 }
michael@0 357
michael@0 358 break;
michael@0 359 case CSS_MM :
michael@0 360 if (mType == CSS_PX) {
michael@0 361 return mValue.mAppUnits * MM_PER_INCH_FLOAT /
michael@0 362 nsPresContext::AppUnitsPerCSSInch();
michael@0 363 }
michael@0 364
michael@0 365 break;
michael@0 366 case CSS_IN :
michael@0 367 if (mType == CSS_PX) {
michael@0 368 return mValue.mAppUnits / nsPresContext::AppUnitsPerCSSInch();
michael@0 369 }
michael@0 370
michael@0 371 break;
michael@0 372 case CSS_PT :
michael@0 373 if (mType == CSS_PX) {
michael@0 374 return mValue.mAppUnits * POINTS_PER_INCH_FLOAT /
michael@0 375 nsPresContext::AppUnitsPerCSSInch();
michael@0 376 }
michael@0 377
michael@0 378 break;
michael@0 379 case CSS_PC :
michael@0 380 if (mType == CSS_PX) {
michael@0 381 return mValue.mAppUnits * 6.0f /
michael@0 382 nsPresContext::AppUnitsPerCSSInch();
michael@0 383 }
michael@0 384
michael@0 385 break;
michael@0 386 case CSS_PERCENTAGE :
michael@0 387 if (mType == CSS_PERCENTAGE) {
michael@0 388 return mValue.mFloat * 100;
michael@0 389 }
michael@0 390
michael@0 391 break;
michael@0 392 case CSS_NUMBER :
michael@0 393 if (mType == CSS_NUMBER) {
michael@0 394 return mValue.mFloat;
michael@0 395 }
michael@0 396 if (mType == CSS_NUMBER_INT32) {
michael@0 397 return mValue.mInt32;
michael@0 398 }
michael@0 399 if (mType == CSS_NUMBER_UINT32) {
michael@0 400 return mValue.mUint32;
michael@0 401 }
michael@0 402
michael@0 403 break;
michael@0 404 case CSS_UNKNOWN :
michael@0 405 case CSS_EMS :
michael@0 406 case CSS_EXS :
michael@0 407 case CSS_DEG :
michael@0 408 case CSS_RAD :
michael@0 409 case CSS_GRAD :
michael@0 410 case CSS_MS :
michael@0 411 case CSS_S :
michael@0 412 case CSS_HZ :
michael@0 413 case CSS_KHZ :
michael@0 414 case CSS_DIMENSION :
michael@0 415 case CSS_STRING :
michael@0 416 case CSS_URI :
michael@0 417 case CSS_IDENT :
michael@0 418 case CSS_ATTR :
michael@0 419 case CSS_COUNTER :
michael@0 420 case CSS_RECT :
michael@0 421 case CSS_RGBCOLOR :
michael@0 422 break;
michael@0 423 }
michael@0 424
michael@0 425 aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR);
michael@0 426 return 0;
michael@0 427 }
michael@0 428
michael@0 429 NS_IMETHODIMP
michael@0 430 nsROCSSPrimitiveValue::GetFloatValue(uint16_t aType, float *aVal)
michael@0 431 {
michael@0 432 ErrorResult rv;
michael@0 433 *aVal = GetFloatValue(aType, rv);
michael@0 434 return rv.ErrorCode();
michael@0 435 }
michael@0 436
michael@0 437
michael@0 438 NS_IMETHODIMP
michael@0 439 nsROCSSPrimitiveValue::SetStringValue(uint16_t aStringType,
michael@0 440 const nsAString& aStringValue)
michael@0 441 {
michael@0 442 return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR;
michael@0 443 }
michael@0 444
michael@0 445 void
michael@0 446 nsROCSSPrimitiveValue::SetStringValue(uint16_t aType, const nsAString& aString,
michael@0 447 mozilla::ErrorResult& aRv)
michael@0 448 {
michael@0 449 aRv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
michael@0 450 }
michael@0 451
michael@0 452
michael@0 453 NS_IMETHODIMP
michael@0 454 nsROCSSPrimitiveValue::GetStringValue(nsAString& aReturn)
michael@0 455 {
michael@0 456 switch (mType) {
michael@0 457 case CSS_IDENT:
michael@0 458 CopyUTF8toUTF16(nsCSSKeywords::GetStringValue(mValue.mKeyword), aReturn);
michael@0 459 break;
michael@0 460 case CSS_STRING:
michael@0 461 case CSS_ATTR:
michael@0 462 aReturn.Assign(mValue.mString);
michael@0 463 break;
michael@0 464 case CSS_URI: {
michael@0 465 nsAutoCString spec;
michael@0 466 if (mValue.mURI)
michael@0 467 mValue.mURI->GetSpec(spec);
michael@0 468 CopyUTF8toUTF16(spec, aReturn);
michael@0 469 } break;
michael@0 470 default:
michael@0 471 aReturn.Truncate();
michael@0 472 return NS_ERROR_DOM_INVALID_ACCESS_ERR;
michael@0 473 }
michael@0 474 return NS_OK;
michael@0 475 }
michael@0 476
michael@0 477 void
michael@0 478 nsROCSSPrimitiveValue::GetStringValue(nsString& aString, ErrorResult& aRv)
michael@0 479 {
michael@0 480 aRv = GetStringValue(aString);
michael@0 481 }
michael@0 482
michael@0 483
michael@0 484 NS_IMETHODIMP
michael@0 485 nsROCSSPrimitiveValue::GetCounterValue(nsIDOMCounter** aReturn)
michael@0 486 {
michael@0 487 return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR;
michael@0 488 }
michael@0 489
michael@0 490 already_AddRefed<nsIDOMCounter>
michael@0 491 nsROCSSPrimitiveValue::GetCounterValue(ErrorResult& aRv)
michael@0 492 {
michael@0 493 aRv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
michael@0 494 return nullptr;
michael@0 495 }
michael@0 496
michael@0 497 nsDOMCSSRect*
michael@0 498 nsROCSSPrimitiveValue::GetRectValue(ErrorResult& aRv)
michael@0 499 {
michael@0 500 if (mType != CSS_RECT) {
michael@0 501 aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR);
michael@0 502 return nullptr;
michael@0 503 }
michael@0 504
michael@0 505 NS_ASSERTION(mValue.mRect, "mValue.mRect should never be null");
michael@0 506 return mValue.mRect;
michael@0 507 }
michael@0 508
michael@0 509 NS_IMETHODIMP
michael@0 510 nsROCSSPrimitiveValue::GetRectValue(nsIDOMRect** aRect)
michael@0 511 {
michael@0 512 ErrorResult error;
michael@0 513 NS_IF_ADDREF(*aRect = GetRectValue(error));
michael@0 514 return error.ErrorCode();
michael@0 515 }
michael@0 516
michael@0 517 nsDOMCSSRGBColor*
michael@0 518 nsROCSSPrimitiveValue::GetRGBColorValue(ErrorResult& aRv)
michael@0 519 {
michael@0 520 if (mType != CSS_RGBCOLOR) {
michael@0 521 aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR);
michael@0 522 return nullptr;
michael@0 523 }
michael@0 524
michael@0 525 NS_ASSERTION(mValue.mColor, "mValue.mColor should never be null");
michael@0 526 return mValue.mColor;
michael@0 527 }
michael@0 528
michael@0 529 void
michael@0 530 nsROCSSPrimitiveValue::SetNumber(float aValue)
michael@0 531 {
michael@0 532 Reset();
michael@0 533 mValue.mFloat = aValue;
michael@0 534 mType = CSS_NUMBER;
michael@0 535 }
michael@0 536
michael@0 537 void
michael@0 538 nsROCSSPrimitiveValue::SetNumber(int32_t aValue)
michael@0 539 {
michael@0 540 Reset();
michael@0 541 mValue.mInt32 = aValue;
michael@0 542 mType = CSS_NUMBER_INT32;
michael@0 543 }
michael@0 544
michael@0 545 void
michael@0 546 nsROCSSPrimitiveValue::SetNumber(uint32_t aValue)
michael@0 547 {
michael@0 548 Reset();
michael@0 549 mValue.mUint32 = aValue;
michael@0 550 mType = CSS_NUMBER_UINT32;
michael@0 551 }
michael@0 552
michael@0 553 void
michael@0 554 nsROCSSPrimitiveValue::SetPercent(float aValue)
michael@0 555 {
michael@0 556 Reset();
michael@0 557 mValue.mFloat = aValue;
michael@0 558 mType = CSS_PERCENTAGE;
michael@0 559 }
michael@0 560
michael@0 561 void
michael@0 562 nsROCSSPrimitiveValue::SetDegree(float aValue)
michael@0 563 {
michael@0 564 Reset();
michael@0 565 mValue.mFloat = aValue;
michael@0 566 mType = CSS_DEG;
michael@0 567 }
michael@0 568
michael@0 569 void
michael@0 570 nsROCSSPrimitiveValue::SetGrad(float aValue)
michael@0 571 {
michael@0 572 Reset();
michael@0 573 mValue.mFloat = aValue;
michael@0 574 mType = CSS_GRAD;
michael@0 575 }
michael@0 576
michael@0 577 void
michael@0 578 nsROCSSPrimitiveValue::SetRadian(float aValue)
michael@0 579 {
michael@0 580 Reset();
michael@0 581 mValue.mFloat = aValue;
michael@0 582 mType = CSS_RAD;
michael@0 583 }
michael@0 584
michael@0 585 void
michael@0 586 nsROCSSPrimitiveValue::SetTurn(float aValue)
michael@0 587 {
michael@0 588 Reset();
michael@0 589 mValue.mFloat = aValue;
michael@0 590 mType = CSS_TURN;
michael@0 591 }
michael@0 592
michael@0 593 void
michael@0 594 nsROCSSPrimitiveValue::SetAppUnits(nscoord aValue)
michael@0 595 {
michael@0 596 Reset();
michael@0 597 mValue.mAppUnits = aValue;
michael@0 598 mType = CSS_PX;
michael@0 599 }
michael@0 600
michael@0 601 void
michael@0 602 nsROCSSPrimitiveValue::SetAppUnits(float aValue)
michael@0 603 {
michael@0 604 SetAppUnits(NSToCoordRound(aValue));
michael@0 605 }
michael@0 606
michael@0 607 void
michael@0 608 nsROCSSPrimitiveValue::SetIdent(nsCSSKeyword aKeyword)
michael@0 609 {
michael@0 610 NS_PRECONDITION(aKeyword != eCSSKeyword_UNKNOWN &&
michael@0 611 0 <= aKeyword && aKeyword < eCSSKeyword_COUNT,
michael@0 612 "bad keyword");
michael@0 613 Reset();
michael@0 614 mValue.mKeyword = aKeyword;
michael@0 615 mType = CSS_IDENT;
michael@0 616 }
michael@0 617
michael@0 618 // FIXME: CSS_STRING should imply a string with "" and a need for escaping.
michael@0 619 void
michael@0 620 nsROCSSPrimitiveValue::SetString(const nsACString& aString, uint16_t aType)
michael@0 621 {
michael@0 622 Reset();
michael@0 623 mValue.mString = ToNewUnicode(aString);
michael@0 624 if (mValue.mString) {
michael@0 625 mType = aType;
michael@0 626 } else {
michael@0 627 // XXXcaa We should probably let the caller know we are out of memory
michael@0 628 mType = CSS_UNKNOWN;
michael@0 629 }
michael@0 630 }
michael@0 631
michael@0 632 // FIXME: CSS_STRING should imply a string with "" and a need for escaping.
michael@0 633 void
michael@0 634 nsROCSSPrimitiveValue::SetString(const nsAString& aString, uint16_t aType)
michael@0 635 {
michael@0 636 Reset();
michael@0 637 mValue.mString = ToNewUnicode(aString);
michael@0 638 if (mValue.mString) {
michael@0 639 mType = aType;
michael@0 640 } else {
michael@0 641 // XXXcaa We should probably let the caller know we are out of memory
michael@0 642 mType = CSS_UNKNOWN;
michael@0 643 }
michael@0 644 }
michael@0 645
michael@0 646 void
michael@0 647 nsROCSSPrimitiveValue::SetURI(nsIURI *aURI)
michael@0 648 {
michael@0 649 Reset();
michael@0 650 mValue.mURI = aURI;
michael@0 651 NS_IF_ADDREF(mValue.mURI);
michael@0 652 mType = CSS_URI;
michael@0 653 }
michael@0 654
michael@0 655 void
michael@0 656 nsROCSSPrimitiveValue::SetColor(nsDOMCSSRGBColor* aColor)
michael@0 657 {
michael@0 658 NS_PRECONDITION(aColor, "Null RGBColor being set!");
michael@0 659 Reset();
michael@0 660 mValue.mColor = aColor;
michael@0 661 if (mValue.mColor) {
michael@0 662 NS_ADDREF(mValue.mColor);
michael@0 663 mType = CSS_RGBCOLOR;
michael@0 664 }
michael@0 665 else {
michael@0 666 mType = CSS_UNKNOWN;
michael@0 667 }
michael@0 668 }
michael@0 669
michael@0 670 void
michael@0 671 nsROCSSPrimitiveValue::SetRect(nsDOMCSSRect* aRect)
michael@0 672 {
michael@0 673 NS_PRECONDITION(aRect, "Null rect being set!");
michael@0 674 Reset();
michael@0 675 mValue.mRect = aRect;
michael@0 676 if (mValue.mRect) {
michael@0 677 NS_ADDREF(mValue.mRect);
michael@0 678 mType = CSS_RECT;
michael@0 679 }
michael@0 680 else {
michael@0 681 mType = CSS_UNKNOWN;
michael@0 682 }
michael@0 683 }
michael@0 684
michael@0 685 void
michael@0 686 nsROCSSPrimitiveValue::SetTime(float aValue)
michael@0 687 {
michael@0 688 Reset();
michael@0 689 mValue.mFloat = aValue;
michael@0 690 mType = CSS_S;
michael@0 691 }
michael@0 692
michael@0 693 void
michael@0 694 nsROCSSPrimitiveValue::Reset()
michael@0 695 {
michael@0 696 switch (mType) {
michael@0 697 case CSS_IDENT:
michael@0 698 break;
michael@0 699 case CSS_STRING:
michael@0 700 case CSS_ATTR:
michael@0 701 case CSS_COUNTER: // FIXME: Counter should use an object
michael@0 702 NS_ASSERTION(mValue.mString, "Null string should never happen");
michael@0 703 nsMemory::Free(mValue.mString);
michael@0 704 mValue.mString = nullptr;
michael@0 705 break;
michael@0 706 case CSS_URI:
michael@0 707 NS_IF_RELEASE(mValue.mURI);
michael@0 708 break;
michael@0 709 case CSS_RECT:
michael@0 710 NS_ASSERTION(mValue.mRect, "Null Rect should never happen");
michael@0 711 NS_RELEASE(mValue.mRect);
michael@0 712 break;
michael@0 713 case CSS_RGBCOLOR:
michael@0 714 NS_ASSERTION(mValue.mColor, "Null RGBColor should never happen");
michael@0 715 NS_RELEASE(mValue.mColor);
michael@0 716 break;
michael@0 717 }
michael@0 718
michael@0 719 mType = CSS_UNKNOWN;
michael@0 720 }

mercurial