nsROCSSPrimitiveValue.cpp 17 KB

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