nsCSSValue.h 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944
  1. /* -*- Mode: C++; tab-width: 2; 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. /* representation of simple property values within CSS declarations */
  6. #ifndef nsCSSValue_h___
  7. #define nsCSSValue_h___
  8. #include <type_traits>
  9. #include "mozilla/Attributes.h"
  10. #include "mozilla/MemoryReporting.h"
  11. #include "mozilla/SheetType.h"
  12. #include "mozilla/StyleComplexColor.h"
  13. #include "mozilla/UniquePtr.h"
  14. #include "nsIPrincipal.h"
  15. #include "nsIURI.h"
  16. #include "nsCOMPtr.h"
  17. #include "nsCSSKeywords.h"
  18. #include "nsCSSPropertyID.h"
  19. #include "nsCSSProps.h"
  20. #include "nsColor.h"
  21. #include "nsCoord.h"
  22. #include "nsProxyRelease.h"
  23. #include "nsRefPtrHashtable.h"
  24. #include "nsString.h"
  25. #include "nsStringBuffer.h"
  26. #include "nsTArray.h"
  27. #include "nsStyleConsts.h"
  28. #include "nsStyleCoord.h"
  29. #include "gfxFontFamilyList.h"
  30. class imgRequestProxy;
  31. class nsIContent;
  32. class nsIDocument;
  33. class nsIPrincipal;
  34. class nsIURI;
  35. class nsPresContext;
  36. template <class T>
  37. class nsPtrHashKey;
  38. namespace mozilla {
  39. class CSSStyleSheet;
  40. } // namespace mozilla
  41. // Deletes a linked list iteratively to avoid blowing up the stack (bug 456196).
  42. #define NS_CSS_DELETE_LIST_MEMBER(type_, ptr_, member_) \
  43. { \
  44. type_ *cur = (ptr_)->member_; \
  45. (ptr_)->member_ = nullptr; \
  46. while (cur) { \
  47. type_ *dlm_next = cur->member_; \
  48. cur->member_ = nullptr; \
  49. delete cur; \
  50. cur = dlm_next; \
  51. } \
  52. }
  53. // Ditto, but use NS_RELEASE instead of 'delete' (bug 1221902).
  54. #define NS_CSS_NS_RELEASE_LIST_MEMBER(type_, ptr_, member_) \
  55. { \
  56. type_ *cur = (ptr_)->member_; \
  57. (ptr_)->member_ = nullptr; \
  58. while (cur) { \
  59. type_ *dlm_next = cur->member_; \
  60. cur->member_ = nullptr; \
  61. NS_RELEASE(cur); \
  62. cur = dlm_next; \
  63. } \
  64. }
  65. // Clones a linked list iteratively to avoid blowing up the stack.
  66. // If it fails to clone the entire list then 'to_' is deleted and
  67. // we return null.
  68. #define NS_CSS_CLONE_LIST_MEMBER(type_, from_, member_, to_, args_) \
  69. { \
  70. type_ *dest = (to_); \
  71. (to_)->member_ = nullptr; \
  72. for (const type_ *src = (from_)->member_; src; src = src->member_) { \
  73. type_ *clm_clone = src->Clone args_; \
  74. if (!clm_clone) { \
  75. delete (to_); \
  76. return nullptr; \
  77. } \
  78. dest->member_ = clm_clone; \
  79. dest = clm_clone; \
  80. } \
  81. }
  82. namespace mozilla {
  83. namespace css {
  84. struct URLValueData
  85. {
  86. protected:
  87. // Methods are not inline because using an nsIPrincipal means requiring
  88. // caps, which leads to REQUIRES hell, since this header is included all
  89. // over.
  90. // For both constructors aString must not be null.
  91. // For both constructors aOriginPrincipal must not be null.
  92. // Construct with a base URI; this will create the actual URI lazily from
  93. // aString and aBaseURI.
  94. URLValueData(nsStringBuffer* aString,
  95. already_AddRefed<PtrHolder<nsIURI>> aBaseURI,
  96. already_AddRefed<PtrHolder<nsIURI>> aReferrer,
  97. already_AddRefed<PtrHolder<nsIPrincipal>> aOriginPricinpal);
  98. // Construct with the actual URI.
  99. URLValueData(already_AddRefed<PtrHolder<nsIURI>> aURI,
  100. nsStringBuffer* aString,
  101. already_AddRefed<PtrHolder<nsIURI>> aBaseURI,
  102. already_AddRefed<PtrHolder<nsIURI>> aReferrer,
  103. already_AddRefed<PtrHolder<nsIPrincipal>> aOriginPrincipal);
  104. public:
  105. // Returns true iff all fields of the two URLValueData objects are equal.
  106. //
  107. // Only safe to call on the main thread, since this will call Equals on the
  108. // nsIURI and nsIPrincipal objects stored on the URLValueData objects.
  109. bool Equals(const URLValueData& aOther) const;
  110. // Returns true iff we know for sure, by comparing the mBaseURI pointer,
  111. // the specified url() value mString, and the mIsLocalRef, that these
  112. // two URLValueData objects represent the same computed url() value.
  113. //
  114. // Doesn't look at mReferrer or mOriginPrincipal.
  115. //
  116. // Safe to call from any thread.
  117. bool DefinitelyEqualURIs(const URLValueData& aOther) const;
  118. // Smae as DefinitelyEqualURIs but additionally compares the nsIPrincipal
  119. // pointers of the two URLValueData objects.
  120. bool DefinitelyEqualURIsAndPrincipal(const URLValueData& aOther) const;
  121. nsIURI* GetURI() const;
  122. bool IsLocalRef() const { return mIsLocalRef; }
  123. NS_INLINE_DECL_THREADSAFE_REFCOUNTING(URLValueData)
  124. // When matching a url with mIsLocalRef set, resolve it against aURI;
  125. // Otherwise, ignore aURL and return mURL directly.
  126. already_AddRefed<nsIURI> ResolveLocalRef(nsIURI* aURI) const;
  127. already_AddRefed<nsIURI> ResolveLocalRef(nsIContent* aContent) const;
  128. // Serializes mURI as a computed URI value, taking into account mIsLocalRef
  129. // and serializing just the fragment if true.
  130. void GetSourceString(nsString& aRef) const;
  131. bool EqualsExceptRef(nsIURI* aURI) const;
  132. private:
  133. // mURI stores the lazily resolved URI. This may be null if the URI is
  134. // invalid, even once resolved.
  135. mutable PtrHandle<nsIURI> mURI;
  136. public:
  137. PtrHandle<nsIURI> mBaseURI;
  138. RefPtr<nsStringBuffer> mString;
  139. PtrHandle<nsIURI> mReferrer;
  140. PtrHandle<nsIPrincipal> mOriginPrincipal;
  141. private:
  142. mutable bool mURIResolved;
  143. // mIsLocalRef is set when url starts with a U+0023 number sign(#) character.
  144. bool mIsLocalRef;
  145. protected:
  146. virtual ~URLValueData() = default;
  147. size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
  148. private:
  149. URLValueData(const URLValueData& aOther) = delete;
  150. URLValueData& operator=(const URLValueData& aOther) = delete;
  151. };
  152. struct URLValue final : public URLValueData
  153. {
  154. // These two constructors are safe to call only on the main thread.
  155. URLValue(nsStringBuffer* aString, nsIURI* aBaseURI, nsIURI* aReferrer,
  156. nsIPrincipal* aOriginPrincipal);
  157. URLValue(nsIURI* aURI, nsStringBuffer* aString, nsIURI* aBaseURI,
  158. nsIURI* aReferrer, nsIPrincipal* aOriginPrincipal);
  159. // This constructor is safe to call from any thread.
  160. URLValue(nsStringBuffer* aString,
  161. already_AddRefed<PtrHolder<nsIURI>> aBaseURI,
  162. already_AddRefed<PtrHolder<nsIURI>> aReferrer,
  163. already_AddRefed<PtrHolder<nsIPrincipal>> aOriginPrincipal)
  164. : URLValueData(aString, Move(aBaseURI), Move(aReferrer),
  165. Move(aOriginPrincipal)) {}
  166. URLValue(const URLValue&) = delete;
  167. URLValue& operator=(const URLValue&) = delete;
  168. size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
  169. };
  170. struct ImageValue final : public URLValueData
  171. {
  172. // Not making the constructor and destructor inline because that would
  173. // force us to include imgIRequest.h, which leads to REQUIRES hell, since
  174. // this header is included all over.
  175. // aString must not be null.
  176. //
  177. // This constructor is only safe to call from the main thread.
  178. ImageValue(nsIURI* aURI, nsStringBuffer* aString, nsIURI* aBaseURI,
  179. nsIURI* aReferrer, nsIPrincipal* aOriginPrincipal,
  180. nsIDocument* aDocument);
  181. // This constructor is safe to call from any thread, but Initialize
  182. // must be called later for the object to be useful.
  183. ImageValue(nsStringBuffer* aString,
  184. already_AddRefed<PtrHolder<nsIURI>> aBaseURI,
  185. already_AddRefed<PtrHolder<nsIURI>> aReferrer,
  186. already_AddRefed<PtrHolder<nsIPrincipal>> aOriginPrincipal);
  187. ImageValue(const ImageValue&) = delete;
  188. ImageValue& operator=(const ImageValue&) = delete;
  189. void Initialize(nsIDocument* aDocument);
  190. // XXXheycam We should have our own SizeOfIncludingThis method.
  191. protected:
  192. ~ImageValue();
  193. public:
  194. // Inherit Equals from URLValueData
  195. nsRefPtrHashtable<nsPtrHashKey<nsIDocument>, imgRequestProxy> mRequests;
  196. private:
  197. #ifdef DEBUG
  198. bool mInitialized = false;
  199. #endif
  200. };
  201. struct GridNamedArea {
  202. nsString mName;
  203. uint32_t mColumnStart;
  204. uint32_t mColumnEnd;
  205. uint32_t mRowStart;
  206. uint32_t mRowEnd;
  207. };
  208. struct GridTemplateAreasValue final {
  209. // Parsed value
  210. nsTArray<GridNamedArea> mNamedAreas;
  211. // Original <string> values. Length gives the number of rows,
  212. // content makes serialization easier.
  213. nsTArray<nsString> mTemplates;
  214. // How many columns grid-template-areas contributes to the explicit grid.
  215. // http://dev.w3.org/csswg/css-grid/#explicit-grid
  216. uint32_t mNColumns;
  217. // How many rows grid-template-areas contributes to the explicit grid.
  218. // http://dev.w3.org/csswg/css-grid/#explicit-grid
  219. uint32_t NRows() const {
  220. return mTemplates.Length();
  221. }
  222. GridTemplateAreasValue()
  223. : mNColumns(0)
  224. // Default constructors for mNamedAreas and mTemplates: empty arrays.
  225. {
  226. }
  227. bool operator==(const GridTemplateAreasValue& aOther) const
  228. {
  229. return mTemplates == aOther.mTemplates;
  230. }
  231. bool operator!=(const GridTemplateAreasValue& aOther) const
  232. {
  233. return !(*this == aOther);
  234. }
  235. NS_INLINE_DECL_REFCOUNTING(GridTemplateAreasValue)
  236. size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
  237. private:
  238. // Private destructor to make sure this isn't used as a stack variable
  239. // or member variable.
  240. ~GridTemplateAreasValue()
  241. {
  242. }
  243. GridTemplateAreasValue(const GridTemplateAreasValue& aOther) = delete;
  244. GridTemplateAreasValue&
  245. operator=(const GridTemplateAreasValue& aOther) = delete;
  246. };
  247. class FontFamilyListRefCnt final : public FontFamilyList {
  248. public:
  249. FontFamilyListRefCnt()
  250. : FontFamilyList()
  251. {
  252. MOZ_COUNT_CTOR(FontFamilyListRefCnt);
  253. }
  254. explicit FontFamilyListRefCnt(FontFamilyType aGenericType)
  255. : FontFamilyList(aGenericType)
  256. {
  257. MOZ_COUNT_CTOR(FontFamilyListRefCnt);
  258. }
  259. FontFamilyListRefCnt(const nsAString& aFamilyName,
  260. QuotedName aQuoted)
  261. : FontFamilyList(aFamilyName, aQuoted)
  262. {
  263. MOZ_COUNT_CTOR(FontFamilyListRefCnt);
  264. }
  265. FontFamilyListRefCnt(const FontFamilyListRefCnt& aOther)
  266. : FontFamilyList(aOther)
  267. {
  268. MOZ_COUNT_CTOR(FontFamilyListRefCnt);
  269. }
  270. NS_INLINE_DECL_REFCOUNTING(FontFamilyListRefCnt);
  271. private:
  272. ~FontFamilyListRefCnt() {
  273. MOZ_COUNT_DTOR(FontFamilyListRefCnt);
  274. }
  275. };
  276. struct RGBAColorData
  277. {
  278. // 1.0 means 100% for all components, but the value may fall outside
  279. // the range of [0.0, 1.0], so it is necessary to clamp them when
  280. // converting to nscolor.
  281. float mR;
  282. float mG;
  283. float mB;
  284. float mA;
  285. RGBAColorData() = default;
  286. MOZ_IMPLICIT RGBAColorData(nscolor aColor)
  287. : mR(NS_GET_R(aColor) * (1.0f / 255.0f))
  288. , mG(NS_GET_G(aColor) * (1.0f / 255.0f))
  289. , mB(NS_GET_B(aColor) * (1.0f / 255.0f))
  290. , mA(NS_GET_A(aColor) * (1.0f / 255.0f))
  291. {}
  292. RGBAColorData(float aR, float aG, float aB, float aA)
  293. : mR(aR), mG(aG), mB(aB), mA(aA) {}
  294. bool operator==(const RGBAColorData& aOther) const
  295. {
  296. return mR == aOther.mR && mG == aOther.mG &&
  297. mB == aOther.mB && mA == aOther.mA;
  298. }
  299. bool operator!=(const RGBAColorData& aOther) const
  300. {
  301. return !(*this == aOther);
  302. }
  303. nscolor ToColor() const
  304. {
  305. return NS_RGBA(ClampColor(mR * 255.0f),
  306. ClampColor(mG * 255.0f),
  307. ClampColor(mB * 255.0f),
  308. ClampColor(mA * 255.0f));
  309. }
  310. RGBAColorData WithAlpha(float aAlpha) const
  311. {
  312. RGBAColorData result = *this;
  313. result.mA = aAlpha;
  314. return result;
  315. }
  316. };
  317. struct ComplexColorData
  318. {
  319. RGBAColorData mColor;
  320. float mForegroundRatio;
  321. ComplexColorData() = default;
  322. ComplexColorData(const RGBAColorData& aColor, float aForegroundRatio)
  323. : mColor(aColor), mForegroundRatio(aForegroundRatio) {}
  324. ComplexColorData(nscolor aColor, float aForegroundRatio)
  325. : mColor(aColor), mForegroundRatio(aForegroundRatio) {}
  326. explicit ComplexColorData(const StyleComplexColor& aColor)
  327. : mColor(aColor.mColor)
  328. , mForegroundRatio(aColor.mForegroundRatio * (1.0f / 255.0f)) {}
  329. bool operator==(const ComplexColorData& aOther) const
  330. {
  331. return mForegroundRatio == aOther.mForegroundRatio &&
  332. (IsCurrentColor() || mColor == aOther.mColor);
  333. }
  334. bool operator!=(const ComplexColorData& aOther) const
  335. {
  336. return !(*this == aOther);
  337. }
  338. bool IsCurrentColor() const { return mForegroundRatio >= 1.0f; }
  339. bool IsNumericColor() const { return mForegroundRatio <= 0.0f; }
  340. StyleComplexColor ToComplexColor() const
  341. {
  342. return {mColor.ToColor(), ClampColor(mForegroundRatio * 255.0f)};
  343. }
  344. };
  345. struct ComplexColorValue final : public ComplexColorData
  346. {
  347. // Just redirect any parameter to the data struct.
  348. template<typename... Args>
  349. explicit ComplexColorValue(Args&&... aArgs)
  350. : ComplexColorData(Forward<Args>(aArgs)...) {}
  351. ComplexColorValue(const ComplexColorValue&) = delete;
  352. NS_INLINE_DECL_REFCOUNTING(ComplexColorValue)
  353. size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const;
  354. private:
  355. ~ComplexColorValue() {}
  356. };
  357. } // namespace css
  358. } // namespace mozilla
  359. enum nsCSSUnit {
  360. eCSSUnit_Null = 0, // (n/a) null unit, value is not specified
  361. eCSSUnit_Auto = 1, // (n/a) value is algorithmic
  362. eCSSUnit_Inherit = 2, // (n/a) value is inherited
  363. eCSSUnit_Initial = 3, // (n/a) value is default UA value
  364. eCSSUnit_Unset = 4, // (n/a) value equivalent to 'initial' if on a reset property, 'inherit' otherwise
  365. eCSSUnit_None = 5, // (n/a) value is none
  366. eCSSUnit_Normal = 6, // (n/a) value is normal (algorithmic, different than auto)
  367. eCSSUnit_System_Font = 7, // (n/a) value is -moz-use-system-font
  368. eCSSUnit_All = 8, // (n/a) value is all
  369. eCSSUnit_Dummy = 9, // (n/a) a fake but specified value, used
  370. // only in temporary values
  371. eCSSUnit_DummyInherit = 10, // (n/a) a fake but specified value, used
  372. // only in temporary values
  373. eCSSUnit_String = 11, // (char16_t*) a string value
  374. eCSSUnit_Ident = 12, // (char16_t*) a string value
  375. eCSSUnit_Attr = 14, // (char16_t*) a attr(string) value
  376. eCSSUnit_Local_Font = 15, // (char16_t*) a local font name
  377. eCSSUnit_Font_Format = 16, // (char16_t*) a font format name
  378. eCSSUnit_Element = 17, // (char16_t*) an element id
  379. eCSSUnit_Array = 20, // (nsCSSValue::Array*) a list of values
  380. eCSSUnit_Counter = 21, // (nsCSSValue::Array*) a counter(string,[string]) value
  381. eCSSUnit_Counters = 22, // (nsCSSValue::Array*) a counters(string,string[,string]) value
  382. eCSSUnit_Cubic_Bezier = 23, // (nsCSSValue::Array*) a list of float values
  383. eCSSUnit_Steps = 24, // (nsCSSValue::Array*) a list of (integer, enumerated)
  384. eCSSUnit_Symbols = 25, // (nsCSSValue::Array*) a symbols(enumerated, symbols) value
  385. eCSSUnit_Function = 26, // (nsCSSValue::Array*) a function with
  386. // parameters. First elem of array is name,
  387. // an nsCSSKeyword as eCSSUnit_Enumerated,
  388. // the rest of the values are arguments.
  389. // The top level of a calc() expression is eCSSUnit_Calc. All
  390. // remaining eCSSUnit_Calc_* units only occur inside these toplevel
  391. // calc values.
  392. // eCSSUnit_Calc has an array with exactly 1 element. eCSSUnit_Calc
  393. // exists so we can distinguish calc(2em) from 2em as specified values
  394. // (but we drop this distinction for nsStyleCoord when we store
  395. // computed values).
  396. eCSSUnit_Calc = 30, // (nsCSSValue::Array*) calc() value
  397. // Plus, Minus, Times_* and Divided have arrays with exactly 2
  398. // elements. a + b + c + d is grouped as ((a + b) + c) + d
  399. eCSSUnit_Calc_Plus = 31, // (nsCSSValue::Array*) + node within calc()
  400. eCSSUnit_Calc_Minus = 32, // (nsCSSValue::Array*) - within calc
  401. eCSSUnit_Calc_Times_L = 33, // (nsCSSValue::Array*) num * val within calc
  402. eCSSUnit_Calc_Times_R = 34, // (nsCSSValue::Array*) val * num within calc
  403. eCSSUnit_Calc_Divided = 35, // (nsCSSValue::Array*) / within calc
  404. eCSSUnit_URL = 40, // (nsCSSValue::URL*) value
  405. eCSSUnit_Image = 41, // (nsCSSValue::Image*) value
  406. eCSSUnit_Gradient = 42, // (nsCSSValueGradient*) value
  407. eCSSUnit_TokenStream = 43, // (nsCSSValueTokenStream*) value
  408. eCSSUnit_GridTemplateAreas = 44, // (GridTemplateAreasValue*)
  409. // for grid-template-areas
  410. eCSSUnit_Pair = 50, // (nsCSSValuePair*) pair of values
  411. eCSSUnit_Triplet = 51, // (nsCSSValueTriplet*) triplet of values
  412. eCSSUnit_Rect = 52, // (nsCSSRect*) rectangle (four values)
  413. eCSSUnit_List = 53, // (nsCSSValueList*) list of values
  414. eCSSUnit_ListDep = 54, // (nsCSSValueList*) same as List
  415. // but does not own the list
  416. eCSSUnit_SharedList = 55, // (nsCSSValueSharedList*) same as list
  417. // but reference counted and shared
  418. eCSSUnit_PairList = 56, // (nsCSSValuePairList*) list of value pairs
  419. eCSSUnit_PairListDep = 57, // (nsCSSValuePairList*) same as PairList
  420. // but does not own the list
  421. eCSSUnit_FontFamilyList = 58, // (FontFamilyList*) value
  422. eCSSUnit_Integer = 70, // (int) simple value
  423. eCSSUnit_Enumerated = 71, // (int) value has enumerated meaning
  424. eCSSUnit_EnumColor = 80, // (int) enumerated color (kColorKTable)
  425. eCSSUnit_RGBColor = 81, // (nscolor) an opaque RGBA value specified as rgb()
  426. eCSSUnit_RGBAColor = 82, // (nscolor) an RGBA value specified as rgba()
  427. eCSSUnit_HexColor = 83, // (nscolor) an opaque RGBA value specified as #rrggbb
  428. eCSSUnit_ShortHexColor = 84, // (nscolor) an opaque RGBA value specified as #rgb
  429. eCSSUnit_HexColorAlpha = 85, // (nscolor) an opaque RGBA value specified as #rrggbbaa
  430. eCSSUnit_ShortHexColorAlpha = 86, // (nscolor) an opaque RGBA value specified as #rgba
  431. eCSSUnit_PercentageRGBColor = 87, // (nsCSSValueFloatColor*) an opaque
  432. // RGBA value specified as rgb() with
  433. // percentage components. Values over
  434. // 100% are allowed.
  435. eCSSUnit_PercentageRGBAColor = 88, // (nsCSSValueFloatColor*) an RGBA value
  436. // specified as rgba() with percentage
  437. // components. Values over 100% are
  438. // allowed.
  439. eCSSUnit_HSLColor = 89, // (nsCSSValueFloatColor*)
  440. eCSSUnit_HSLAColor = 90, // (nsCSSValueFloatColor*)
  441. eCSSUnit_ComplexColor = 91, // (ComplexColorValue*)
  442. eCSSUnit_Percent = 100, // (float) 1.0 == 100%) value is percentage of something
  443. eCSSUnit_Number = 101, // (float) value is numeric (usually multiplier, different behavior than percent)
  444. // Physical length units
  445. eCSSUnit_PhysicalMillimeter = 200, // (float) 1/25.4 inch
  446. // Length units - relative
  447. // Viewport relative measure
  448. eCSSUnit_ViewportWidth = 700, // (float) 1% of the width of the initial containing block
  449. eCSSUnit_ViewportHeight = 701, // (float) 1% of the height of the initial containing block
  450. eCSSUnit_ViewportMin = 702, // (float) smaller of ViewportWidth and ViewportHeight
  451. eCSSUnit_ViewportMax = 703, // (float) larger of ViewportWidth and ViewportHeight
  452. // Font relative measure
  453. eCSSUnit_EM = 800, // (float) == current font size
  454. eCSSUnit_XHeight = 801, // (float) distance from top of lower case x to baseline
  455. eCSSUnit_Char = 802, // (float) number of characters, used for width with monospace font
  456. eCSSUnit_RootEM = 803, // (float) == root element font size
  457. // Screen relative measure
  458. eCSSUnit_Point = 900, // (float) 4/3 of a CSS pixel
  459. eCSSUnit_Inch = 901, // (float) 96 CSS pixels
  460. eCSSUnit_Millimeter = 902, // (float) 96/25.4 CSS pixels
  461. eCSSUnit_Centimeter = 903, // (float) 96/2.54 CSS pixels
  462. eCSSUnit_Pica = 904, // (float) 12 points == 16 CSS pixls
  463. eCSSUnit_Quarter = 905, // (float) 96/101.6 CSS pixels
  464. eCSSUnit_Pixel = 906, // (float) CSS pixel unit
  465. // Angular units
  466. eCSSUnit_Degree = 1000, // (float) 360 per circle
  467. eCSSUnit_Grad = 1001, // (float) 400 per circle
  468. eCSSUnit_Radian = 1002, // (float) 2*pi per circle
  469. eCSSUnit_Turn = 1003, // (float) 1 per circle
  470. // Frequency units
  471. eCSSUnit_Hertz = 2000, // (float) 1/seconds
  472. eCSSUnit_Kilohertz = 2001, // (float) 1000 Hertz
  473. // Time units
  474. eCSSUnit_Seconds = 3000, // (float) Standard time
  475. eCSSUnit_Milliseconds = 3001, // (float) 1/1000 second
  476. // Flexible fraction (CSS Grid)
  477. eCSSUnit_FlexFraction = 4000 // (float) Fraction of free space
  478. };
  479. struct nsCSSValueGradient;
  480. struct nsCSSValuePair;
  481. struct nsCSSValuePair_heap;
  482. struct nsCSSValueTokenStream;
  483. struct nsCSSRect;
  484. struct nsCSSRect_heap;
  485. struct nsCSSValueList;
  486. struct nsCSSValueList_heap;
  487. struct nsCSSValueSharedList;
  488. struct nsCSSValuePairList;
  489. struct nsCSSValuePairList_heap;
  490. struct nsCSSValueTriplet;
  491. struct nsCSSValueTriplet_heap;
  492. class nsCSSValueFloatColor;
  493. class nsCSSValue {
  494. public:
  495. struct Array;
  496. friend struct Array;
  497. friend struct mozilla::css::URLValueData;
  498. friend struct mozilla::css::ImageValue;
  499. // for valueless units only (null, auto, inherit, none, all, normal)
  500. explicit nsCSSValue(nsCSSUnit aUnit = eCSSUnit_Null)
  501. : mUnit(aUnit)
  502. {
  503. MOZ_ASSERT(aUnit <= eCSSUnit_DummyInherit, "not a valueless unit");
  504. }
  505. nsCSSValue(int32_t aValue, nsCSSUnit aUnit);
  506. nsCSSValue(float aValue, nsCSSUnit aUnit);
  507. nsCSSValue(const nsString& aValue, nsCSSUnit aUnit);
  508. nsCSSValue(Array* aArray, nsCSSUnit aUnit);
  509. explicit nsCSSValue(mozilla::css::URLValue* aValue);
  510. explicit nsCSSValue(mozilla::css::ImageValue* aValue);
  511. explicit nsCSSValue(nsCSSValueGradient* aValue);
  512. explicit nsCSSValue(nsCSSValueTokenStream* aValue);
  513. explicit nsCSSValue(mozilla::css::GridTemplateAreasValue* aValue);
  514. explicit nsCSSValue(mozilla::css::FontFamilyListRefCnt* aValue);
  515. nsCSSValue(const nsCSSValue& aCopy);
  516. nsCSSValue(nsCSSValue&& aOther)
  517. : mUnit(aOther.mUnit)
  518. , mValue(aOther.mValue)
  519. {
  520. aOther.mUnit = eCSSUnit_Null;
  521. }
  522. ~nsCSSValue() { Reset(); }
  523. nsCSSValue& operator=(const nsCSSValue& aCopy);
  524. nsCSSValue& operator=(nsCSSValue&& aCopy);
  525. bool operator==(const nsCSSValue& aOther) const;
  526. bool operator!=(const nsCSSValue& aOther) const
  527. {
  528. return !(*this == aOther);
  529. }
  530. // Enum for AppendToString's aValueSerialization argument.
  531. enum Serialization { eNormalized, eAuthorSpecified };
  532. /**
  533. * Serialize |this| as a specified value for |aProperty| and append
  534. * it to |aResult|.
  535. */
  536. void AppendToString(nsCSSPropertyID aProperty, nsAString& aResult,
  537. Serialization aValueSerialization) const;
  538. nsCSSUnit GetUnit() const { return mUnit; }
  539. bool IsLengthUnit() const
  540. { return eCSSUnit_PhysicalMillimeter <= mUnit && mUnit <= eCSSUnit_Pixel; }
  541. bool IsLengthPercentCalcUnit() const
  542. { return IsLengthUnit() || mUnit == eCSSUnit_Percent || IsCalcUnit(); }
  543. /**
  544. * A "fixed" length unit is one that means a specific physical length
  545. * which we try to match based on the physical characteristics of an
  546. * output device.
  547. */
  548. bool IsFixedLengthUnit() const
  549. { return mUnit == eCSSUnit_PhysicalMillimeter; }
  550. /**
  551. * What the spec calls relative length units is, for us, split
  552. * between relative length units and pixel length units.
  553. *
  554. * A "relative" length unit is a multiple of some derived metric,
  555. * such as a font em-size, which itself was controlled by an input CSS
  556. * length. Relative length units should not be scaled by zooming, since
  557. * the underlying CSS length would already have been scaled.
  558. */
  559. bool IsRelativeLengthUnit() const
  560. { return eCSSUnit_EM <= mUnit && mUnit <= eCSSUnit_RootEM; }
  561. /**
  562. * A "pixel" length unit is a some multiple of CSS pixels.
  563. */
  564. static bool IsPixelLengthUnit(nsCSSUnit aUnit)
  565. { return eCSSUnit_Point <= aUnit && aUnit <= eCSSUnit_Pixel; }
  566. bool IsPixelLengthUnit() const
  567. { return IsPixelLengthUnit(mUnit); }
  568. static bool IsPercentLengthUnit(nsCSSUnit aUnit)
  569. { return aUnit == eCSSUnit_Percent; }
  570. bool IsPercentLengthUnit()
  571. { return IsPercentLengthUnit(mUnit); }
  572. static bool IsFloatUnit(nsCSSUnit aUnit)
  573. { return eCSSUnit_Number <= aUnit; }
  574. bool IsAngularUnit() const
  575. { return eCSSUnit_Degree <= mUnit && mUnit <= eCSSUnit_Turn; }
  576. bool IsFrequencyUnit() const
  577. { return eCSSUnit_Hertz <= mUnit && mUnit <= eCSSUnit_Kilohertz; }
  578. bool IsTimeUnit() const
  579. { return eCSSUnit_Seconds <= mUnit && mUnit <= eCSSUnit_Milliseconds; }
  580. bool IsCalcUnit() const
  581. { return eCSSUnit_Calc <= mUnit && mUnit <= eCSSUnit_Calc_Divided; }
  582. bool UnitHasStringValue() const
  583. { return eCSSUnit_String <= mUnit && mUnit <= eCSSUnit_Element; }
  584. bool UnitHasArrayValue() const
  585. { return eCSSUnit_Array <= mUnit && mUnit <= eCSSUnit_Calc_Divided; }
  586. // Checks for the nsCSSValue being of a particular type of color unit:
  587. //
  588. // - IsIntegerColorUnit returns true for:
  589. // eCSSUnit_RGBColor -- rgb(int,int,int)
  590. // eCSSUnit_RGBAColor -- rgba(int,int,int,float)
  591. // eCSSUnit_HexColor -- #rrggbb
  592. // eCSSUnit_ShortHexColor -- #rgb
  593. // eCSSUnit_HexColorAlpha -- #rrggbbaa
  594. // eCSSUnit_ShortHexColorAlpha -- #rgba
  595. //
  596. // - IsFloatColorUnit returns true for:
  597. // eCSSUnit_PercentageRGBColor -- rgb(%,%,%)
  598. // eCSSUnit_PercentageRGBAColor -- rgba(%,%,%,float)
  599. // eCSSUnit_HSLColor -- hsl(float,%,%)
  600. // eCSSUnit_HSLAColor -- hsla(float,%,%,float)
  601. //
  602. // - IsNumericColorUnit returns true for any of the above units.
  603. //
  604. // Note that color keywords and system colors are represented by
  605. // eCSSUnit_EnumColor and eCSSUnit_Ident.
  606. bool IsIntegerColorUnit() const { return IsIntegerColorUnit(mUnit); }
  607. bool IsFloatColorUnit() const { return IsFloatColorUnit(mUnit); }
  608. bool IsNumericColorUnit() const { return IsNumericColorUnit(mUnit); }
  609. static bool IsIntegerColorUnit(nsCSSUnit aUnit)
  610. { return eCSSUnit_RGBColor <= aUnit && aUnit <= eCSSUnit_ShortHexColorAlpha; }
  611. static bool IsFloatColorUnit(nsCSSUnit aUnit)
  612. { return eCSSUnit_PercentageRGBColor <= aUnit &&
  613. aUnit <= eCSSUnit_HSLAColor; }
  614. static bool IsNumericColorUnit(nsCSSUnit aUnit)
  615. { return IsIntegerColorUnit(aUnit) || IsFloatColorUnit(aUnit); }
  616. int32_t GetIntValue() const
  617. {
  618. MOZ_ASSERT(mUnit == eCSSUnit_Integer ||
  619. mUnit == eCSSUnit_Enumerated ||
  620. mUnit == eCSSUnit_EnumColor,
  621. "not an int value");
  622. return mValue.mInt;
  623. }
  624. nsCSSKeyword GetKeywordValue() const
  625. {
  626. MOZ_ASSERT(mUnit == eCSSUnit_Enumerated, "not a keyword value");
  627. return static_cast<nsCSSKeyword>(mValue.mInt);
  628. }
  629. float GetPercentValue() const
  630. {
  631. MOZ_ASSERT(mUnit == eCSSUnit_Percent, "not a percent value");
  632. return mValue.mFloat;
  633. }
  634. float GetFloatValue() const
  635. {
  636. MOZ_ASSERT(eCSSUnit_Number <= mUnit, "not a float value");
  637. MOZ_ASSERT(!mozilla::IsNaN(mValue.mFloat));
  638. return mValue.mFloat;
  639. }
  640. float GetAngleValue() const
  641. {
  642. MOZ_ASSERT(eCSSUnit_Degree <= mUnit && mUnit <= eCSSUnit_Turn,
  643. "not an angle value");
  644. return mValue.mFloat;
  645. }
  646. // Converts any angle to radians.
  647. double GetAngleValueInRadians() const;
  648. // Converts any angle to degrees.
  649. double GetAngleValueInDegrees() const;
  650. nsAString& GetStringValue(nsAString& aBuffer) const
  651. {
  652. MOZ_ASSERT(UnitHasStringValue(), "not a string value");
  653. aBuffer.Truncate();
  654. uint32_t len = NS_strlen(GetBufferValue(mValue.mString));
  655. mValue.mString->ToString(len, aBuffer);
  656. return aBuffer;
  657. }
  658. const char16_t* GetStringBufferValue() const
  659. {
  660. MOZ_ASSERT(UnitHasStringValue(), "not a string value");
  661. return GetBufferValue(mValue.mString);
  662. }
  663. nscolor GetColorValue() const;
  664. bool IsNonTransparentColor() const;
  665. mozilla::StyleComplexColor GetStyleComplexColorValue() const
  666. {
  667. MOZ_ASSERT(mUnit == eCSSUnit_ComplexColor);
  668. return mValue.mComplexColor->ToComplexColor();
  669. }
  670. Array* GetArrayValue() const
  671. {
  672. MOZ_ASSERT(UnitHasArrayValue(), "not an array value");
  673. return mValue.mArray;
  674. }
  675. nsIURI* GetURLValue() const
  676. {
  677. MOZ_ASSERT(mUnit == eCSSUnit_URL || mUnit == eCSSUnit_Image,
  678. "not a URL value");
  679. return mUnit == eCSSUnit_URL ?
  680. mValue.mURL->GetURI() : mValue.mImage->GetURI();
  681. }
  682. nsCSSValueGradient* GetGradientValue() const
  683. {
  684. MOZ_ASSERT(mUnit == eCSSUnit_Gradient, "not a gradient value");
  685. return mValue.mGradient;
  686. }
  687. nsCSSValueTokenStream* GetTokenStreamValue() const
  688. {
  689. MOZ_ASSERT(mUnit == eCSSUnit_TokenStream, "not a token stream value");
  690. return mValue.mTokenStream;
  691. }
  692. nsCSSValueSharedList* GetSharedListValue() const
  693. {
  694. MOZ_ASSERT(mUnit == eCSSUnit_SharedList, "not a shared list value");
  695. return mValue.mSharedList;
  696. }
  697. mozilla::FontFamilyList* GetFontFamilyListValue() const
  698. {
  699. MOZ_ASSERT(mUnit == eCSSUnit_FontFamilyList,
  700. "not a font family list value");
  701. NS_ASSERTION(mValue.mFontFamilyList != nullptr,
  702. "font family list value should never be null");
  703. return mValue.mFontFamilyList;
  704. }
  705. // bodies of these are below
  706. inline nsCSSValuePair& GetPairValue();
  707. inline const nsCSSValuePair& GetPairValue() const;
  708. inline nsCSSRect& GetRectValue();
  709. inline const nsCSSRect& GetRectValue() const;
  710. inline nsCSSValueList* GetListValue();
  711. inline const nsCSSValueList* GetListValue() const;
  712. inline nsCSSValuePairList* GetPairListValue();
  713. inline const nsCSSValuePairList* GetPairListValue() const;
  714. inline nsCSSValueTriplet& GetTripletValue();
  715. inline const nsCSSValueTriplet& GetTripletValue() const;
  716. mozilla::css::URLValue* GetURLStructValue() const
  717. {
  718. // Not allowing this for Image values, because if the caller takes
  719. // a ref to them they won't be able to delete them properly.
  720. MOZ_ASSERT(mUnit == eCSSUnit_URL, "not a URL value");
  721. return mValue.mURL;
  722. }
  723. mozilla::css::ImageValue* GetImageStructValue() const
  724. {
  725. MOZ_ASSERT(mUnit == eCSSUnit_Image, "not an Image value");
  726. return mValue.mImage;
  727. }
  728. mozilla::css::GridTemplateAreasValue* GetGridTemplateAreas() const
  729. {
  730. MOZ_ASSERT(mUnit == eCSSUnit_GridTemplateAreas,
  731. "not a grid-template-areas value");
  732. return mValue.mGridTemplateAreas;
  733. }
  734. const char16_t* GetOriginalURLValue() const
  735. {
  736. MOZ_ASSERT(mUnit == eCSSUnit_URL || mUnit == eCSSUnit_Image,
  737. "not a URL value");
  738. return GetBufferValue(mUnit == eCSSUnit_URL ?
  739. mValue.mURL->mString :
  740. mValue.mImage->mString);
  741. }
  742. // Not making this inline because that would force us to include
  743. // imgIRequest.h, which leads to REQUIRES hell, since this header is included
  744. // all over.
  745. imgRequestProxy* GetImageValue(nsIDocument* aDocument) const;
  746. // Like GetImageValue, but additionally will pass the imgRequestProxy
  747. // through nsContentUtils::GetStaticRequest if aPresContent is static.
  748. already_AddRefed<imgRequestProxy> GetPossiblyStaticImageValue(
  749. nsIDocument* aDocument, nsPresContext* aPresContext) const;
  750. nscoord GetFixedLength(nsPresContext* aPresContext) const;
  751. nscoord GetPixelLength() const;
  752. nsCSSValueFloatColor* GetFloatColorValue() const
  753. {
  754. MOZ_ASSERT(IsFloatColorUnit(), "not a float color value");
  755. return mValue.mFloatColor;
  756. }
  757. void Reset() // sets to null
  758. {
  759. if (mUnit != eCSSUnit_Null)
  760. DoReset();
  761. }
  762. private:
  763. void DoReset();
  764. public:
  765. void SetIntValue(int32_t aValue, nsCSSUnit aUnit);
  766. template<typename T,
  767. typename = typename std::enable_if<std::is_enum<T>::value>::type>
  768. void SetIntValue(T aValue, nsCSSUnit aUnit)
  769. {
  770. static_assert(mozilla::EnumTypeFitsWithin<T, int32_t>::value,
  771. "aValue must be an enum that fits within mValue.mInt");
  772. SetIntValue(static_cast<int32_t>(aValue), aUnit);
  773. }
  774. void SetPercentValue(float aValue);
  775. void SetFloatValue(float aValue, nsCSSUnit aUnit);
  776. void SetStringValue(const nsString& aValue, nsCSSUnit aUnit);
  777. void SetColorValue(nscolor aValue);
  778. void SetIntegerColorValue(nscolor aValue, nsCSSUnit aUnit);
  779. // converts the nscoord to pixels
  780. void SetIntegerCoordValue(nscoord aCoord);
  781. void SetFloatColorValue(float aComponent1,
  782. float aComponent2,
  783. float aComponent3,
  784. float aAlpha, nsCSSUnit aUnit);
  785. void SetRGBAColorValue(const mozilla::css::RGBAColorData& aValue);
  786. void SetComplexColorValue(
  787. already_AddRefed<mozilla::css::ComplexColorValue> aValue);
  788. void SetArrayValue(nsCSSValue::Array* aArray, nsCSSUnit aUnit);
  789. void SetURLValue(mozilla::css::URLValue* aURI);
  790. void SetImageValue(mozilla::css::ImageValue* aImage);
  791. void SetGradientValue(nsCSSValueGradient* aGradient);
  792. void SetTokenStreamValue(nsCSSValueTokenStream* aTokenStream);
  793. void SetGridTemplateAreas(mozilla::css::GridTemplateAreasValue* aValue);
  794. void SetFontFamilyListValue(mozilla::css::FontFamilyListRefCnt* aFontListValue);
  795. void SetPairValue(const nsCSSValuePair* aPair);
  796. void SetPairValue(const nsCSSValue& xValue, const nsCSSValue& yValue);
  797. void SetSharedListValue(nsCSSValueSharedList* aList);
  798. void SetDependentListValue(nsCSSValueList* aList);
  799. void SetDependentPairListValue(nsCSSValuePairList* aList);
  800. void SetTripletValue(const nsCSSValueTriplet* aTriplet);
  801. void SetTripletValue(const nsCSSValue& xValue, const nsCSSValue& yValue, const nsCSSValue& zValue);
  802. void SetAutoValue();
  803. void SetInheritValue();
  804. void SetInitialValue();
  805. void SetUnsetValue();
  806. void SetNoneValue();
  807. void SetAllValue();
  808. void SetNormalValue();
  809. void SetSystemFontValue();
  810. void SetDummyValue();
  811. void SetDummyInheritValue();
  812. // Converts an nsStyleCoord::CalcValue back into a CSSValue
  813. void SetCalcValue(const nsStyleCoord::CalcValue* aCalc);
  814. // These are a little different - they allocate storage for you and
  815. // return a handle.
  816. nsCSSRect& SetRectValue();
  817. nsCSSValueList* SetListValue();
  818. nsCSSValuePairList* SetPairListValue();
  819. // These take ownership of the passed-in resource.
  820. void AdoptListValue(mozilla::UniquePtr<nsCSSValueList> aValue);
  821. void AdoptPairListValue(mozilla::UniquePtr<nsCSSValuePairList> aValue);
  822. void StartImageLoad(nsIDocument* aDocument) const; // Only pretend const
  823. // Initializes as a function value with the specified function id.
  824. Array* InitFunction(nsCSSKeyword aFunctionId, uint32_t aNumArgs);
  825. // Checks if this is a function value with the specified function id.
  826. bool EqualsFunction(nsCSSKeyword aFunctionId) const;
  827. // Returns an already addrefed buffer. Guaranteed to return non-null.
  828. // (Will abort on allocation failure.)
  829. static already_AddRefed<nsStringBuffer>
  830. BufferFromString(const nsString& aValue);
  831. size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
  832. static void
  833. AppendSidesShorthandToString(const nsCSSPropertyID aProperties[],
  834. const nsCSSValue* aValues[],
  835. nsAString& aString,
  836. Serialization aSerialization);
  837. static void
  838. AppendBasicShapeRadiusToString(const nsCSSPropertyID aProperties[],
  839. const nsCSSValue* aValues[],
  840. nsAString& aResult,
  841. Serialization aValueSerialization);
  842. static void
  843. AppendAlignJustifyValueToString(int32_t aValue, nsAString& aResult);
  844. private:
  845. static const char16_t* GetBufferValue(nsStringBuffer* aBuffer) {
  846. return static_cast<char16_t*>(aBuffer->Data());
  847. }
  848. void AppendPolygonToString(nsCSSPropertyID aProperty, nsAString& aResult,
  849. Serialization aValueSerialization) const;
  850. void AppendPositionCoordinateToString(const nsCSSValue& aValue,
  851. nsCSSPropertyID aProperty,
  852. nsAString& aResult,
  853. Serialization aSerialization) const;
  854. void AppendCircleOrEllipseToString(
  855. nsCSSKeyword aFunctionId,
  856. nsCSSPropertyID aProperty, nsAString& aResult,
  857. Serialization aValueSerialization) const;
  858. void AppendBasicShapePositionToString(
  859. nsAString& aResult,
  860. Serialization aValueSerialization) const;
  861. void AppendInsetToString(nsCSSPropertyID aProperty, nsAString& aResult,
  862. Serialization aValueSerialization) const;
  863. protected:
  864. nsCSSUnit mUnit;
  865. union {
  866. int32_t mInt;
  867. float mFloat;
  868. // Note: the capacity of the buffer may exceed the length of the string.
  869. // If we're of a string type, mString is not null.
  870. nsStringBuffer* MOZ_OWNING_REF mString;
  871. nscolor mColor;
  872. Array* MOZ_OWNING_REF mArray;
  873. mozilla::css::URLValue* MOZ_OWNING_REF mURL;
  874. mozilla::css::ImageValue* MOZ_OWNING_REF mImage;
  875. mozilla::css::GridTemplateAreasValue* MOZ_OWNING_REF mGridTemplateAreas;
  876. nsCSSValueGradient* MOZ_OWNING_REF mGradient;
  877. nsCSSValueTokenStream* MOZ_OWNING_REF mTokenStream;
  878. nsCSSValuePair_heap* MOZ_OWNING_REF mPair;
  879. nsCSSRect_heap* MOZ_OWNING_REF mRect;
  880. nsCSSValueTriplet_heap* MOZ_OWNING_REF mTriplet;
  881. nsCSSValueList_heap* MOZ_OWNING_REF mList;
  882. nsCSSValueList* mListDependent;
  883. nsCSSValueSharedList* MOZ_OWNING_REF mSharedList;
  884. nsCSSValuePairList_heap* MOZ_OWNING_REF mPairList;
  885. nsCSSValuePairList* mPairListDependent;
  886. nsCSSValueFloatColor* MOZ_OWNING_REF mFloatColor;
  887. mozilla::css::FontFamilyListRefCnt* MOZ_OWNING_REF mFontFamilyList;
  888. mozilla::css::ComplexColorValue* MOZ_OWNING_REF mComplexColor;
  889. } mValue;
  890. };
  891. struct nsCSSValue::Array final {
  892. // return |Array| with reference count of zero
  893. static Array* Create(size_t aItemCount) {
  894. return new (aItemCount) Array(aItemCount);
  895. }
  896. nsCSSValue& operator[](size_t aIndex) {
  897. MOZ_ASSERT(aIndex < mCount, "out of range");
  898. return mArray[aIndex];
  899. }
  900. const nsCSSValue& operator[](size_t aIndex) const {
  901. MOZ_ASSERT(aIndex < mCount, "out of range");
  902. return mArray[aIndex];
  903. }
  904. nsCSSValue& Item(size_t aIndex) { return (*this)[aIndex]; }
  905. const nsCSSValue& Item(size_t aIndex) const { return (*this)[aIndex]; }
  906. size_t Count() const { return mCount; }
  907. // callers depend on the items being contiguous
  908. nsCSSValue* ItemStorage() {
  909. return this->First();
  910. }
  911. bool operator==(const Array& aOther) const
  912. {
  913. if (mCount != aOther.mCount)
  914. return false;
  915. for (size_t i = 0; i < mCount; ++i)
  916. if ((*this)[i] != aOther[i])
  917. return false;
  918. return true;
  919. }
  920. // XXXdholbert This uses a size_t ref count. Should we use a variant
  921. // of NS_INLINE_DECL_REFCOUNTING that takes a type as an argument?
  922. void AddRef() {
  923. if (mRefCnt == size_t(-1)) { // really want SIZE_MAX
  924. NS_WARNING("refcount overflow, leaking nsCSSValue::Array");
  925. return;
  926. }
  927. ++mRefCnt;
  928. NS_LOG_ADDREF(this, mRefCnt, "nsCSSValue::Array", sizeof(*this));
  929. }
  930. void Release() {
  931. if (mRefCnt == size_t(-1)) { // really want SIZE_MAX
  932. NS_WARNING("refcount overflow, leaking nsCSSValue::Array");
  933. return;
  934. }
  935. --mRefCnt;
  936. NS_LOG_RELEASE(this, mRefCnt, "nsCSSValue::Array");
  937. if (mRefCnt == 0)
  938. delete this;
  939. }
  940. private:
  941. size_t mRefCnt;
  942. const size_t mCount;
  943. // This must be the last sub-object, since we extend this array to
  944. // be of size mCount; it needs to be a sub-object so it gets proper
  945. // alignment.
  946. nsCSSValue mArray[1];
  947. void* operator new(size_t aSelfSize, size_t aItemCount) CPP_THROW_NEW {
  948. MOZ_ASSERT(aItemCount > 0, "cannot have a 0 item count");
  949. return ::operator new(aSelfSize + sizeof(nsCSSValue) * (aItemCount - 1));
  950. }
  951. void operator delete(void* aPtr) { ::operator delete(aPtr); }
  952. nsCSSValue* First() { return mArray; }
  953. const nsCSSValue* First() const { return mArray; }
  954. #define CSSVALUE_LIST_FOR_EXTRA_VALUES(var) \
  955. for (nsCSSValue *var = First() + 1, *var##_end = First() + mCount; \
  956. var != var##_end; ++var)
  957. explicit Array(size_t aItemCount)
  958. : mRefCnt(0)
  959. , mCount(aItemCount)
  960. {
  961. MOZ_COUNT_CTOR(nsCSSValue::Array);
  962. CSSVALUE_LIST_FOR_EXTRA_VALUES(val) {
  963. new (val) nsCSSValue();
  964. }
  965. }
  966. ~Array()
  967. {
  968. MOZ_COUNT_DTOR(nsCSSValue::Array);
  969. CSSVALUE_LIST_FOR_EXTRA_VALUES(val) {
  970. val->~nsCSSValue();
  971. }
  972. }
  973. size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
  974. #undef CSSVALUE_LIST_FOR_EXTRA_VALUES
  975. private:
  976. Array(const Array& aOther) = delete;
  977. Array& operator=(const Array& aOther) = delete;
  978. };
  979. // Prefer nsCSSValue::Array for lists of fixed size.
  980. struct nsCSSValueList {
  981. nsCSSValueList() : mNext(nullptr) { MOZ_COUNT_CTOR(nsCSSValueList); }
  982. ~nsCSSValueList();
  983. nsCSSValueList* Clone() const; // makes a deep copy. Infallible.
  984. void CloneInto(nsCSSValueList* aList) const; // makes a deep copy into aList
  985. void AppendToString(nsCSSPropertyID aProperty, nsAString& aResult,
  986. nsCSSValue::Serialization aValueSerialization) const;
  987. static bool Equal(const nsCSSValueList* aList1,
  988. const nsCSSValueList* aList2);
  989. size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
  990. nsCSSValue mValue;
  991. nsCSSValueList* mNext;
  992. private:
  993. nsCSSValueList(const nsCSSValueList& aCopy) // makes a shallow copy
  994. : mValue(aCopy.mValue), mNext(nullptr)
  995. {
  996. MOZ_COUNT_CTOR(nsCSSValueList);
  997. }
  998. // We don't want operator== or operator!= because they wouldn't be
  999. // null-safe, which is generally what we need. Use |Equal| method
  1000. // above instead.
  1001. bool operator==(nsCSSValueList const& aOther) const = delete;
  1002. bool operator!=(const nsCSSValueList& aOther) const = delete;
  1003. };
  1004. // nsCSSValueList_heap differs from nsCSSValueList only in being
  1005. // refcounted. It should not be necessary to use this class directly;
  1006. // it's an implementation detail of nsCSSValue.
  1007. struct nsCSSValueList_heap final : public nsCSSValueList {
  1008. NS_INLINE_DECL_REFCOUNTING(nsCSSValueList_heap)
  1009. size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
  1010. private:
  1011. // Private destructor, to discourage deletion outside of Release():
  1012. ~nsCSSValueList_heap()
  1013. {
  1014. }
  1015. };
  1016. // This is a reference counted list value. Note that the object is
  1017. // a wrapper for the reference count and a pointer to the head of the
  1018. // list, whereas the other list types (such as nsCSSValueList) do
  1019. // not have such a wrapper.
  1020. struct nsCSSValueSharedList final {
  1021. nsCSSValueSharedList()
  1022. : mHead(nullptr)
  1023. {
  1024. MOZ_COUNT_CTOR(nsCSSValueSharedList);
  1025. }
  1026. // Takes ownership of aList.
  1027. explicit nsCSSValueSharedList(nsCSSValueList* aList)
  1028. : mHead(aList)
  1029. {
  1030. MOZ_COUNT_CTOR(nsCSSValueSharedList);
  1031. }
  1032. private:
  1033. // Private destructor, to discourage deletion outside of Release():
  1034. ~nsCSSValueSharedList();
  1035. public:
  1036. NS_INLINE_DECL_THREADSAFE_REFCOUNTING(nsCSSValueSharedList)
  1037. void AppendToString(nsCSSPropertyID aProperty, nsAString& aResult,
  1038. nsCSSValue::Serialization aValueSerialization) const;
  1039. bool operator==(nsCSSValueSharedList const& aOther) const;
  1040. bool operator!=(const nsCSSValueSharedList& aOther) const
  1041. { return !(*this == aOther); }
  1042. size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
  1043. nsCSSValueList* mHead;
  1044. };
  1045. // This has to be here so that the relationship between nsCSSValueList
  1046. // and nsCSSValueList_heap is visible.
  1047. inline nsCSSValueList*
  1048. nsCSSValue::GetListValue()
  1049. {
  1050. if (mUnit == eCSSUnit_List)
  1051. return mValue.mList;
  1052. else {
  1053. MOZ_ASSERT(mUnit == eCSSUnit_ListDep, "not a list value");
  1054. return mValue.mListDependent;
  1055. }
  1056. }
  1057. inline const nsCSSValueList*
  1058. nsCSSValue::GetListValue() const
  1059. {
  1060. if (mUnit == eCSSUnit_List)
  1061. return mValue.mList;
  1062. else {
  1063. MOZ_ASSERT(mUnit == eCSSUnit_ListDep, "not a list value");
  1064. return mValue.mListDependent;
  1065. }
  1066. }
  1067. struct nsCSSRect {
  1068. nsCSSRect(void);
  1069. nsCSSRect(const nsCSSRect& aCopy);
  1070. ~nsCSSRect();
  1071. void AppendToString(nsCSSPropertyID aProperty, nsAString& aResult,
  1072. nsCSSValue::Serialization aValueSerialization) const;
  1073. bool operator==(const nsCSSRect& aOther) const {
  1074. return mTop == aOther.mTop &&
  1075. mRight == aOther.mRight &&
  1076. mBottom == aOther.mBottom &&
  1077. mLeft == aOther.mLeft;
  1078. }
  1079. bool operator!=(const nsCSSRect& aOther) const {
  1080. return mTop != aOther.mTop ||
  1081. mRight != aOther.mRight ||
  1082. mBottom != aOther.mBottom ||
  1083. mLeft != aOther.mLeft;
  1084. }
  1085. void SetAllSidesTo(const nsCSSValue& aValue);
  1086. bool AllSidesEqualTo(const nsCSSValue& aValue) const {
  1087. return mTop == aValue &&
  1088. mRight == aValue &&
  1089. mBottom == aValue &&
  1090. mLeft == aValue;
  1091. }
  1092. void Reset() {
  1093. mTop.Reset();
  1094. mRight.Reset();
  1095. mBottom.Reset();
  1096. mLeft.Reset();
  1097. }
  1098. bool HasValue() const {
  1099. return
  1100. mTop.GetUnit() != eCSSUnit_Null ||
  1101. mRight.GetUnit() != eCSSUnit_Null ||
  1102. mBottom.GetUnit() != eCSSUnit_Null ||
  1103. mLeft.GetUnit() != eCSSUnit_Null;
  1104. }
  1105. nsCSSValue mTop;
  1106. nsCSSValue mRight;
  1107. nsCSSValue mBottom;
  1108. nsCSSValue mLeft;
  1109. typedef nsCSSValue nsCSSRect::*side_type;
  1110. static const side_type sides[4];
  1111. };
  1112. // nsCSSRect_heap differs from nsCSSRect only in being
  1113. // refcounted. It should not be necessary to use this class directly;
  1114. // it's an implementation detail of nsCSSValue.
  1115. struct nsCSSRect_heap final : public nsCSSRect {
  1116. NS_INLINE_DECL_REFCOUNTING(nsCSSRect_heap)
  1117. size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
  1118. private:
  1119. // Private destructor, to discourage deletion outside of Release():
  1120. ~nsCSSRect_heap()
  1121. {
  1122. }
  1123. };
  1124. // This has to be here so that the relationship between nsCSSRect
  1125. // and nsCSSRect_heap is visible.
  1126. inline nsCSSRect&
  1127. nsCSSValue::GetRectValue()
  1128. {
  1129. MOZ_ASSERT(mUnit == eCSSUnit_Rect, "not a rect value");
  1130. return *mValue.mRect;
  1131. }
  1132. inline const nsCSSRect&
  1133. nsCSSValue::GetRectValue() const
  1134. {
  1135. MOZ_ASSERT(mUnit == eCSSUnit_Rect, "not a rect value");
  1136. return *mValue.mRect;
  1137. }
  1138. struct nsCSSValuePair {
  1139. nsCSSValuePair()
  1140. {
  1141. MOZ_COUNT_CTOR(nsCSSValuePair);
  1142. }
  1143. explicit nsCSSValuePair(nsCSSUnit aUnit)
  1144. : mXValue(aUnit), mYValue(aUnit)
  1145. {
  1146. MOZ_COUNT_CTOR(nsCSSValuePair);
  1147. }
  1148. nsCSSValuePair(const nsCSSValue& aXValue, const nsCSSValue& aYValue)
  1149. : mXValue(aXValue), mYValue(aYValue)
  1150. {
  1151. MOZ_COUNT_CTOR(nsCSSValuePair);
  1152. }
  1153. nsCSSValuePair(const nsCSSValuePair& aCopy)
  1154. : mXValue(aCopy.mXValue), mYValue(aCopy.mYValue)
  1155. {
  1156. MOZ_COUNT_CTOR(nsCSSValuePair);
  1157. }
  1158. ~nsCSSValuePair()
  1159. {
  1160. MOZ_COUNT_DTOR(nsCSSValuePair);
  1161. }
  1162. nsCSSValuePair& operator=(const nsCSSValuePair& aOther) {
  1163. mXValue = aOther.mXValue;
  1164. mYValue = aOther.mYValue;
  1165. return *this;
  1166. }
  1167. bool operator==(const nsCSSValuePair& aOther) const {
  1168. return mXValue == aOther.mXValue &&
  1169. mYValue == aOther.mYValue;
  1170. }
  1171. bool operator!=(const nsCSSValuePair& aOther) const {
  1172. return mXValue != aOther.mXValue ||
  1173. mYValue != aOther.mYValue;
  1174. }
  1175. bool BothValuesEqualTo(const nsCSSValue& aValue) const {
  1176. return mXValue == aValue &&
  1177. mYValue == aValue;
  1178. }
  1179. void SetBothValuesTo(const nsCSSValue& aValue) {
  1180. mXValue = aValue;
  1181. mYValue = aValue;
  1182. }
  1183. void Reset() {
  1184. mXValue.Reset();
  1185. mYValue.Reset();
  1186. }
  1187. bool HasValue() const {
  1188. return mXValue.GetUnit() != eCSSUnit_Null ||
  1189. mYValue.GetUnit() != eCSSUnit_Null;
  1190. }
  1191. void AppendToString(nsCSSPropertyID aProperty, nsAString& aResult,
  1192. nsCSSValue::Serialization aValueSerialization) const;
  1193. size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
  1194. nsCSSValue mXValue;
  1195. nsCSSValue mYValue;
  1196. };
  1197. // nsCSSValuePair_heap differs from nsCSSValuePair only in being
  1198. // refcounted. It should not be necessary to use this class directly;
  1199. // it's an implementation detail of nsCSSValue.
  1200. struct nsCSSValuePair_heap final : public nsCSSValuePair {
  1201. // forward constructor
  1202. nsCSSValuePair_heap(const nsCSSValue& aXValue, const nsCSSValue& aYValue)
  1203. : nsCSSValuePair(aXValue, aYValue)
  1204. {}
  1205. NS_INLINE_DECL_REFCOUNTING(nsCSSValuePair_heap)
  1206. size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
  1207. private:
  1208. // Private destructor, to discourage deletion outside of Release():
  1209. ~nsCSSValuePair_heap()
  1210. {
  1211. }
  1212. };
  1213. struct nsCSSValueTriplet {
  1214. nsCSSValueTriplet()
  1215. {
  1216. MOZ_COUNT_CTOR(nsCSSValueTriplet);
  1217. }
  1218. explicit nsCSSValueTriplet(nsCSSUnit aUnit)
  1219. : mXValue(aUnit), mYValue(aUnit), mZValue(aUnit)
  1220. {
  1221. MOZ_COUNT_CTOR(nsCSSValueTriplet);
  1222. }
  1223. nsCSSValueTriplet(const nsCSSValue& aXValue,
  1224. const nsCSSValue& aYValue,
  1225. const nsCSSValue& aZValue)
  1226. : mXValue(aXValue), mYValue(aYValue), mZValue(aZValue)
  1227. {
  1228. MOZ_COUNT_CTOR(nsCSSValueTriplet);
  1229. }
  1230. nsCSSValueTriplet(const nsCSSValueTriplet& aCopy)
  1231. : mXValue(aCopy.mXValue), mYValue(aCopy.mYValue), mZValue(aCopy.mZValue)
  1232. {
  1233. MOZ_COUNT_CTOR(nsCSSValueTriplet);
  1234. }
  1235. ~nsCSSValueTriplet()
  1236. {
  1237. MOZ_COUNT_DTOR(nsCSSValueTriplet);
  1238. }
  1239. bool operator==(const nsCSSValueTriplet& aOther) const {
  1240. return mXValue == aOther.mXValue &&
  1241. mYValue == aOther.mYValue &&
  1242. mZValue == aOther.mZValue;
  1243. }
  1244. bool operator!=(const nsCSSValueTriplet& aOther) const {
  1245. return mXValue != aOther.mXValue ||
  1246. mYValue != aOther.mYValue ||
  1247. mZValue != aOther.mZValue;
  1248. }
  1249. bool AllValuesEqualTo(const nsCSSValue& aValue) const {
  1250. return mXValue == aValue &&
  1251. mYValue == aValue &&
  1252. mZValue == aValue;
  1253. }
  1254. void SetAllValuesTo(const nsCSSValue& aValue) {
  1255. mXValue = aValue;
  1256. mYValue = aValue;
  1257. mZValue = aValue;
  1258. }
  1259. void Reset() {
  1260. mXValue.Reset();
  1261. mYValue.Reset();
  1262. mZValue.Reset();
  1263. }
  1264. bool HasValue() const {
  1265. return mXValue.GetUnit() != eCSSUnit_Null ||
  1266. mYValue.GetUnit() != eCSSUnit_Null ||
  1267. mZValue.GetUnit() != eCSSUnit_Null;
  1268. }
  1269. void AppendToString(nsCSSPropertyID aProperty, nsAString& aResult,
  1270. nsCSSValue::Serialization aValueSerialization) const;
  1271. nsCSSValue mXValue;
  1272. nsCSSValue mYValue;
  1273. nsCSSValue mZValue;
  1274. };
  1275. // nsCSSValueTriplet_heap differs from nsCSSValueTriplet only in being
  1276. // refcounted. It should not be necessary to use this class directly;
  1277. // it's an implementation detail of nsCSSValue.
  1278. struct nsCSSValueTriplet_heap final : public nsCSSValueTriplet {
  1279. // forward constructor
  1280. nsCSSValueTriplet_heap(const nsCSSValue& aXValue, const nsCSSValue& aYValue, const nsCSSValue& aZValue)
  1281. : nsCSSValueTriplet(aXValue, aYValue, aZValue)
  1282. {}
  1283. NS_INLINE_DECL_REFCOUNTING(nsCSSValueTriplet_heap)
  1284. size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
  1285. private:
  1286. // Private destructor, to discourage deletion outside of Release():
  1287. ~nsCSSValueTriplet_heap()
  1288. {
  1289. }
  1290. };
  1291. // This has to be here so that the relationship between nsCSSValuePair
  1292. // and nsCSSValuePair_heap is visible.
  1293. inline nsCSSValuePair&
  1294. nsCSSValue::GetPairValue()
  1295. {
  1296. MOZ_ASSERT(mUnit == eCSSUnit_Pair, "not a pair value");
  1297. return *mValue.mPair;
  1298. }
  1299. inline const nsCSSValuePair&
  1300. nsCSSValue::GetPairValue() const
  1301. {
  1302. MOZ_ASSERT(mUnit == eCSSUnit_Pair, "not a pair value");
  1303. return *mValue.mPair;
  1304. }
  1305. inline nsCSSValueTriplet&
  1306. nsCSSValue::GetTripletValue()
  1307. {
  1308. MOZ_ASSERT(mUnit == eCSSUnit_Triplet, "not a triplet value");
  1309. return *mValue.mTriplet;
  1310. }
  1311. inline const nsCSSValueTriplet&
  1312. nsCSSValue::GetTripletValue() const
  1313. {
  1314. MOZ_ASSERT(mUnit == eCSSUnit_Triplet, "not a triplet value");
  1315. return *mValue.mTriplet;
  1316. }
  1317. // Maybe should be replaced with nsCSSValueList and nsCSSValue::Array?
  1318. struct nsCSSValuePairList {
  1319. nsCSSValuePairList() : mNext(nullptr) { MOZ_COUNT_CTOR(nsCSSValuePairList); }
  1320. ~nsCSSValuePairList();
  1321. nsCSSValuePairList* Clone() const; // makes a deep copy. Infallible.
  1322. void AppendToString(nsCSSPropertyID aProperty, nsAString& aResult,
  1323. nsCSSValue::Serialization aValueSerialization) const;
  1324. static bool Equal(const nsCSSValuePairList* aList1,
  1325. const nsCSSValuePairList* aList2);
  1326. size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
  1327. nsCSSValue mXValue;
  1328. nsCSSValue mYValue;
  1329. nsCSSValuePairList* mNext;
  1330. private:
  1331. nsCSSValuePairList(const nsCSSValuePairList& aCopy) // makes a shallow copy
  1332. : mXValue(aCopy.mXValue), mYValue(aCopy.mYValue), mNext(nullptr)
  1333. {
  1334. MOZ_COUNT_CTOR(nsCSSValuePairList);
  1335. }
  1336. // We don't want operator== or operator!= because they wouldn't be
  1337. // null-safe, which is generally what we need. Use |Equal| method
  1338. // above instead.
  1339. bool operator==(const nsCSSValuePairList& aOther) const = delete;
  1340. bool operator!=(const nsCSSValuePairList& aOther) const = delete;
  1341. };
  1342. // nsCSSValuePairList_heap differs from nsCSSValuePairList only in being
  1343. // refcounted. It should not be necessary to use this class directly;
  1344. // it's an implementation detail of nsCSSValue.
  1345. struct nsCSSValuePairList_heap final : public nsCSSValuePairList {
  1346. NS_INLINE_DECL_REFCOUNTING(nsCSSValuePairList_heap)
  1347. size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
  1348. private:
  1349. // Private destructor, to discourage deletion outside of Release():
  1350. ~nsCSSValuePairList_heap()
  1351. {
  1352. }
  1353. };
  1354. // This has to be here so that the relationship between nsCSSValuePairList
  1355. // and nsCSSValuePairList_heap is visible.
  1356. inline nsCSSValuePairList*
  1357. nsCSSValue::GetPairListValue()
  1358. {
  1359. if (mUnit == eCSSUnit_PairList)
  1360. return mValue.mPairList;
  1361. else {
  1362. MOZ_ASSERT (mUnit == eCSSUnit_PairListDep, "not a pairlist value");
  1363. return mValue.mPairListDependent;
  1364. }
  1365. }
  1366. inline const nsCSSValuePairList*
  1367. nsCSSValue::GetPairListValue() const
  1368. {
  1369. if (mUnit == eCSSUnit_PairList)
  1370. return mValue.mPairList;
  1371. else {
  1372. MOZ_ASSERT (mUnit == eCSSUnit_PairListDep, "not a pairlist value");
  1373. return mValue.mPairListDependent;
  1374. }
  1375. }
  1376. struct nsCSSValueGradientStop {
  1377. public:
  1378. nsCSSValueGradientStop();
  1379. // needed to keep bloat logs happy when we use the TArray
  1380. // in nsCSSValueGradient
  1381. nsCSSValueGradientStop(const nsCSSValueGradientStop& aOther);
  1382. ~nsCSSValueGradientStop();
  1383. nsCSSValue mLocation;
  1384. nsCSSValue mColor;
  1385. // If mIsInterpolationHint is true, there is no color, just
  1386. // a location.
  1387. bool mIsInterpolationHint;
  1388. bool operator==(const nsCSSValueGradientStop& aOther) const
  1389. {
  1390. return (mLocation == aOther.mLocation &&
  1391. mIsInterpolationHint == aOther.mIsInterpolationHint &&
  1392. (mIsInterpolationHint || mColor == aOther.mColor));
  1393. }
  1394. bool operator!=(const nsCSSValueGradientStop& aOther) const
  1395. {
  1396. return !(*this == aOther);
  1397. }
  1398. size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
  1399. };
  1400. struct nsCSSValueGradient final {
  1401. nsCSSValueGradient(bool aIsRadial, bool aIsRepeating);
  1402. // true if gradient is radial, false if it is linear
  1403. bool mIsRadial;
  1404. bool mIsRepeating;
  1405. bool mIsLegacySyntax;
  1406. bool mIsExplicitSize;
  1407. // line position and angle
  1408. nsCSSValuePair mBgPos;
  1409. nsCSSValue mAngle;
  1410. // Only meaningful if mIsRadial is true
  1411. private:
  1412. nsCSSValue mRadialValues[2];
  1413. public:
  1414. nsCSSValue& GetRadialShape()
  1415. {
  1416. MOZ_ASSERT(!mIsExplicitSize);
  1417. return mRadialValues[0];
  1418. }
  1419. const nsCSSValue& GetRadialShape() const
  1420. {
  1421. MOZ_ASSERT(!mIsExplicitSize);
  1422. return mRadialValues[0];
  1423. }
  1424. nsCSSValue& GetRadialSize()
  1425. {
  1426. MOZ_ASSERT(!mIsExplicitSize);
  1427. return mRadialValues[1];
  1428. }
  1429. const nsCSSValue& GetRadialSize() const
  1430. {
  1431. MOZ_ASSERT(!mIsExplicitSize);
  1432. return mRadialValues[1];
  1433. }
  1434. nsCSSValue& GetRadiusX()
  1435. {
  1436. MOZ_ASSERT(mIsExplicitSize);
  1437. return mRadialValues[0];
  1438. }
  1439. const nsCSSValue& GetRadiusX() const
  1440. {
  1441. MOZ_ASSERT(mIsExplicitSize);
  1442. return mRadialValues[0];
  1443. }
  1444. nsCSSValue& GetRadiusY()
  1445. {
  1446. MOZ_ASSERT(mIsExplicitSize);
  1447. return mRadialValues[1];
  1448. }
  1449. const nsCSSValue& GetRadiusY() const
  1450. {
  1451. MOZ_ASSERT(mIsExplicitSize);
  1452. return mRadialValues[1];
  1453. }
  1454. InfallibleTArray<nsCSSValueGradientStop> mStops;
  1455. bool operator==(const nsCSSValueGradient& aOther) const
  1456. {
  1457. if (mIsRadial != aOther.mIsRadial ||
  1458. mIsRepeating != aOther.mIsRepeating ||
  1459. mIsLegacySyntax != aOther.mIsLegacySyntax ||
  1460. mIsExplicitSize != aOther.mIsExplicitSize ||
  1461. mBgPos != aOther.mBgPos ||
  1462. mAngle != aOther.mAngle ||
  1463. mRadialValues[0] != aOther.mRadialValues[0] ||
  1464. mRadialValues[1] != aOther.mRadialValues[1])
  1465. return false;
  1466. if (mStops.Length() != aOther.mStops.Length())
  1467. return false;
  1468. for (uint32_t i = 0; i < mStops.Length(); i++) {
  1469. if (mStops[i] != aOther.mStops[i])
  1470. return false;
  1471. }
  1472. return true;
  1473. }
  1474. bool operator!=(const nsCSSValueGradient& aOther) const
  1475. {
  1476. return !(*this == aOther);
  1477. }
  1478. NS_INLINE_DECL_REFCOUNTING(nsCSSValueGradient)
  1479. size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
  1480. private:
  1481. // Private destructor, to discourage deletion outside of Release():
  1482. ~nsCSSValueGradient()
  1483. {
  1484. }
  1485. nsCSSValueGradient(const nsCSSValueGradient& aOther) = delete;
  1486. nsCSSValueGradient& operator=(const nsCSSValueGradient& aOther) = delete;
  1487. };
  1488. // A string value used primarily to represent variable references.
  1489. //
  1490. // Animation code, specifically the KeyframeUtils class, also uses this
  1491. // type as a container for various string values including:
  1492. //
  1493. // * Shorthand property values
  1494. // * Shorthand sentinel values used for testing failure conditions
  1495. // * Invalid longhand property values
  1496. //
  1497. // For the most part, the above values are not passed to functions that
  1498. // manipulate nsCSSValue objects in a generic fashion. Instead KeyframeUtils
  1499. // extracts the string from the nsCSSValueTokenStream and passes that around
  1500. // instead. The single exception is nsCSSValue::AppendToString which we use
  1501. // to serialize the string contained in the nsCSSValueTokenStream by ensuring
  1502. // the mShorthandPropertyID is set to eCSSProperty_UNKNOWN.
  1503. struct nsCSSValueTokenStream final {
  1504. nsCSSValueTokenStream();
  1505. private:
  1506. // Private destructor, to discourage deletion outside of Release():
  1507. ~nsCSSValueTokenStream();
  1508. public:
  1509. bool operator==(const nsCSSValueTokenStream& aOther) const
  1510. {
  1511. bool eq;
  1512. return mPropertyID == aOther.mPropertyID &&
  1513. mShorthandPropertyID == aOther.mShorthandPropertyID &&
  1514. mTokenStream.Equals(aOther.mTokenStream) &&
  1515. mLevel == aOther.mLevel &&
  1516. (mBaseURI == aOther.mBaseURI ||
  1517. (mBaseURI && aOther.mBaseURI &&
  1518. NS_SUCCEEDED(mBaseURI->Equals(aOther.mBaseURI, &eq)) &&
  1519. eq)) &&
  1520. (mSheetURI == aOther.mSheetURI ||
  1521. (mSheetURI && aOther.mSheetURI &&
  1522. NS_SUCCEEDED(mSheetURI->Equals(aOther.mSheetURI, &eq)) &&
  1523. eq)) &&
  1524. (mSheetPrincipal == aOther.mSheetPrincipal ||
  1525. (mSheetPrincipal && aOther.mSheetPrincipal &&
  1526. NS_SUCCEEDED(mSheetPrincipal->Equals(aOther.mSheetPrincipal,
  1527. &eq)) &&
  1528. eq));
  1529. }
  1530. bool operator!=(const nsCSSValueTokenStream& aOther) const
  1531. {
  1532. return !(*this == aOther);
  1533. }
  1534. NS_INLINE_DECL_REFCOUNTING(nsCSSValueTokenStream)
  1535. size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
  1536. // The property that has mTokenStream as its unparsed specified value.
  1537. // When a variable reference is used in a shorthand property, a
  1538. // TokenStream value is stored as the specified value for each of its
  1539. // component longhand properties.
  1540. nsCSSPropertyID mPropertyID;
  1541. // The shorthand property that had a value with a variable reference,
  1542. // which caused the longhand property identified by mPropertyID to have
  1543. // a TokenStream value.
  1544. nsCSSPropertyID mShorthandPropertyID;
  1545. // The unparsed CSS corresponding to the specified value of the property.
  1546. // When the value of a shorthand property has a variable reference, the
  1547. // same mTokenStream value is used on each of the nsCSSValueTokenStream
  1548. // objects that will be set by parsing the shorthand.
  1549. nsString mTokenStream;
  1550. nsCOMPtr<nsIURI> mBaseURI;
  1551. nsCOMPtr<nsIURI> mSheetURI;
  1552. nsCOMPtr<nsIPrincipal> mSheetPrincipal;
  1553. // XXX Should store sheet here (see Bug 952338)
  1554. // mozilla::CSSStyleSheet* mSheet;
  1555. uint32_t mLineNumber;
  1556. uint32_t mLineOffset;
  1557. mozilla::SheetType mLevel;
  1558. private:
  1559. nsCSSValueTokenStream(const nsCSSValueTokenStream& aOther) = delete;
  1560. nsCSSValueTokenStream& operator=(const nsCSSValueTokenStream& aOther) = delete;
  1561. };
  1562. class nsCSSValueFloatColor final {
  1563. public:
  1564. nsCSSValueFloatColor(float aComponent1, float aComponent2, float aComponent3,
  1565. float aAlpha)
  1566. : mComponent1(aComponent1)
  1567. , mComponent2(aComponent2)
  1568. , mComponent3(aComponent3)
  1569. , mAlpha(aAlpha)
  1570. {
  1571. MOZ_COUNT_CTOR(nsCSSValueFloatColor);
  1572. }
  1573. private:
  1574. // Private destructor, to discourage deletion outside of Release():
  1575. ~nsCSSValueFloatColor()
  1576. {
  1577. MOZ_COUNT_DTOR(nsCSSValueFloatColor);
  1578. }
  1579. public:
  1580. bool operator==(nsCSSValueFloatColor& aOther) const;
  1581. nscolor GetColorValue(nsCSSUnit aUnit) const;
  1582. float Comp1() const { return mComponent1; }
  1583. float Comp2() const { return mComponent2; }
  1584. float Comp3() const { return mComponent3; }
  1585. float Alpha() const { return mAlpha; }
  1586. bool IsNonTransparentColor() const;
  1587. void AppendToString(nsCSSUnit aUnit, nsAString& aResult) const;
  1588. size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
  1589. NS_INLINE_DECL_REFCOUNTING(nsCSSValueFloatColor)
  1590. private:
  1591. // The range of each component is.
  1592. // [0, 1] for HSLColor and HSLAColor. mComponent1 for hue, mComponent2 for
  1593. // saturation, mComponent3 for lightness.
  1594. // [0, 1] for saturation and lightness
  1595. // represents [0%, 100%].
  1596. // [0, 1] for hue represents
  1597. // [0deg, 360deg].
  1598. //
  1599. // [-float::max(), float::max()] for PercentageRGBColor, PercentageRGBAColor.
  1600. // 1.0 means 100%.
  1601. float mComponent1;
  1602. float mComponent2;
  1603. float mComponent3;
  1604. float mAlpha;
  1605. nsCSSValueFloatColor(const nsCSSValueFloatColor& aOther) = delete;
  1606. nsCSSValueFloatColor& operator=(const nsCSSValueFloatColor& aOther)
  1607. = delete;
  1608. };
  1609. struct nsCSSCornerSizes {
  1610. nsCSSCornerSizes(void);
  1611. nsCSSCornerSizes(const nsCSSCornerSizes& aCopy);
  1612. ~nsCSSCornerSizes();
  1613. // argument is a "full corner" constant from nsStyleConsts.h
  1614. nsCSSValue const & GetCorner(uint32_t aCorner) const {
  1615. return this->*corners[aCorner];
  1616. }
  1617. nsCSSValue & GetCorner(uint32_t aCorner) {
  1618. return this->*corners[aCorner];
  1619. }
  1620. bool operator==(const nsCSSCornerSizes& aOther) const {
  1621. NS_FOR_CSS_FULL_CORNERS(corner) {
  1622. if (this->GetCorner(corner) != aOther.GetCorner(corner))
  1623. return false;
  1624. }
  1625. return true;
  1626. }
  1627. bool operator!=(const nsCSSCornerSizes& aOther) const {
  1628. NS_FOR_CSS_FULL_CORNERS(corner) {
  1629. if (this->GetCorner(corner) != aOther.GetCorner(corner))
  1630. return true;
  1631. }
  1632. return false;
  1633. }
  1634. bool HasValue() const {
  1635. NS_FOR_CSS_FULL_CORNERS(corner) {
  1636. if (this->GetCorner(corner).GetUnit() != eCSSUnit_Null)
  1637. return true;
  1638. }
  1639. return false;
  1640. }
  1641. void Reset();
  1642. nsCSSValue mTopLeft;
  1643. nsCSSValue mTopRight;
  1644. nsCSSValue mBottomRight;
  1645. nsCSSValue mBottomLeft;
  1646. protected:
  1647. typedef nsCSSValue nsCSSCornerSizes::*corner_type;
  1648. static const corner_type corners[4];
  1649. };
  1650. #endif /* nsCSSValue_h___ */