nsRuleNode.h 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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. /*
  6. * a node in the lexicographic tree of rules that match an element,
  7. * responsible for converting the rules' information into computed style
  8. */
  9. #ifndef nsRuleNode_h___
  10. #define nsRuleNode_h___
  11. #include "mozilla/ArenaObjectID.h"
  12. #include "mozilla/LinkedList.h"
  13. #include "mozilla/PodOperations.h"
  14. #include "mozilla/RangedArray.h"
  15. #include "mozilla/RuleNodeCacheConditions.h"
  16. #include "mozilla/SheetType.h"
  17. #include "nsPresContext.h"
  18. #include "nsStyleStruct.h"
  19. class nsCSSPropertyIDSet;
  20. class nsCSSValue;
  21. class nsIStyleRule;
  22. class nsStyleContext;
  23. class nsStyleCoord;
  24. struct nsCSSRect;
  25. struct nsCSSValueList;
  26. struct nsCSSValuePairList;
  27. struct nsRuleData;
  28. struct nsInheritedStyleData
  29. {
  30. mozilla::RangedArray<void*,
  31. nsStyleStructID_Inherited_Start,
  32. nsStyleStructID_Inherited_Count> mStyleStructs;
  33. void* operator new(size_t sz, nsPresContext* aContext) {
  34. return aContext->PresShell()->
  35. AllocateByObjectID(mozilla::eArenaObjectID_nsInheritedStyleData, sz);
  36. }
  37. void DestroyStructs(uint64_t aBits, nsPresContext* aContext) {
  38. #define STYLE_STRUCT_INHERITED(name, checkdata_cb) \
  39. void *name##Data = mStyleStructs[eStyleStruct_##name]; \
  40. if (name##Data && !(aBits & NS_STYLE_INHERIT_BIT(name))) \
  41. static_cast<nsStyle##name*>(name##Data)->Destroy(aContext);
  42. #define STYLE_STRUCT_RESET(name, checkdata_cb)
  43. #include "nsStyleStructList.h"
  44. #undef STYLE_STRUCT_INHERITED
  45. #undef STYLE_STRUCT_RESET
  46. }
  47. void Destroy(uint64_t aBits, nsPresContext* aContext) {
  48. DestroyStructs(aBits, aContext);
  49. aContext->PresShell()->
  50. FreeByObjectID(mozilla::eArenaObjectID_nsInheritedStyleData, this);
  51. }
  52. nsInheritedStyleData() {
  53. for (nsStyleStructID i = nsStyleStructID_Inherited_Start;
  54. i < nsStyleStructID_Inherited_Start + nsStyleStructID_Inherited_Count;
  55. i = nsStyleStructID(i + 1)) {
  56. mStyleStructs[i] = nullptr;
  57. }
  58. }
  59. };
  60. struct nsResetStyleData
  61. {
  62. mozilla::RangedArray<void*,
  63. nsStyleStructID_Reset_Start,
  64. nsStyleStructID_Reset_Count> mStyleStructs;
  65. nsResetStyleData()
  66. {
  67. for (nsStyleStructID i = nsStyleStructID_Reset_Start;
  68. i < nsStyleStructID_Reset_Start + nsStyleStructID_Reset_Count;
  69. i = nsStyleStructID(i + 1)) {
  70. mStyleStructs[i] = nullptr;
  71. }
  72. }
  73. void* operator new(size_t sz, nsPresContext* aContext) {
  74. return aContext->PresShell()->
  75. AllocateByObjectID(mozilla::eArenaObjectID_nsResetStyleData, sz);
  76. }
  77. void Destroy(uint64_t aBits, nsPresContext* aContext) {
  78. #define STYLE_STRUCT_RESET(name, checkdata_cb) \
  79. void *name##Data = mStyleStructs[eStyleStruct_##name]; \
  80. if (name##Data && !(aBits & NS_STYLE_INHERIT_BIT(name))) \
  81. static_cast<nsStyle##name*>(name##Data)->Destroy(aContext);
  82. #define STYLE_STRUCT_INHERITED(name, checkdata_cb)
  83. #include "nsStyleStructList.h"
  84. #undef STYLE_STRUCT_RESET
  85. #undef STYLE_STRUCT_INHERITED
  86. aContext->PresShell()->
  87. FreeByObjectID(mozilla::eArenaObjectID_nsResetStyleData, this);
  88. }
  89. };
  90. struct nsConditionalResetStyleData
  91. {
  92. static uint32_t GetBitForSID(const nsStyleStructID aSID) {
  93. return 1 << aSID;
  94. }
  95. struct Entry
  96. {
  97. Entry(const mozilla::RuleNodeCacheConditions& aConditions,
  98. void* aStyleStruct,
  99. Entry* aNext)
  100. : mConditions(aConditions), mStyleStruct(aStyleStruct), mNext(aNext) {}
  101. void* operator new(size_t sz, nsPresContext* aContext) {
  102. return aContext->PresShell()->AllocateByObjectID(
  103. mozilla::eArenaObjectID_nsConditionalResetStyleDataEntry, sz);
  104. }
  105. const mozilla::RuleNodeCacheConditions mConditions;
  106. void* const mStyleStruct;
  107. Entry* const mNext;
  108. };
  109. // Each entry is either a pointer to a style struct or a
  110. // pointer to an Entry object. A bit in mConditionalBits
  111. // means that it is an Entry.
  112. mozilla::RangedArray<void*,
  113. nsStyleStructID_Reset_Start,
  114. nsStyleStructID_Reset_Count> mEntries;
  115. uint32_t mConditionalBits;
  116. nsConditionalResetStyleData()
  117. {
  118. for (nsStyleStructID i = nsStyleStructID_Reset_Start;
  119. i < nsStyleStructID_Reset_Start + nsStyleStructID_Reset_Count;
  120. i = nsStyleStructID(i + 1)) {
  121. mEntries[i] = nullptr;
  122. }
  123. mConditionalBits = 0;
  124. }
  125. void* operator new(size_t sz, nsPresContext* aContext) {
  126. return aContext->PresShell()->AllocateByObjectID(
  127. mozilla::eArenaObjectID_nsConditionalResetStyleData, sz);
  128. }
  129. void* GetStyleData(nsStyleStructID aSID) const {
  130. if (mConditionalBits & GetBitForSID(aSID)) {
  131. return nullptr;
  132. }
  133. return mEntries[aSID];
  134. }
  135. void* GetStyleData(nsStyleStructID aSID,
  136. nsStyleContext* aStyleContext,
  137. bool aCanComputeData) const {
  138. if (!(mConditionalBits & GetBitForSID(aSID))) {
  139. return mEntries[aSID];
  140. }
  141. if (!aCanComputeData) {
  142. // If aCanComputeData is false, then any previously-computed data
  143. // would have been cached on the style context. Therefore it's
  144. // unnecessary to check the conditional data. It's also
  145. // incorrect, because calling e->mConditions.Matches() below could
  146. // cause additional structs to be computed, which is incorrect
  147. // during CalcStyleDifference.
  148. return nullptr;
  149. }
  150. return GetConditionalStyleData(aSID, aStyleContext);
  151. }
  152. private:
  153. // non-inline helper for GetStyleData
  154. void* GetConditionalStyleData(nsStyleStructID aSID,
  155. nsStyleContext* aStyleContext) const;
  156. public:
  157. void SetStyleData(nsStyleStructID aSID, void* aStyleStruct) {
  158. MOZ_ASSERT(!(mConditionalBits & GetBitForSID(aSID)),
  159. "rule node should not have unconditional and conditional style "
  160. "data for a given struct");
  161. mConditionalBits &= ~GetBitForSID(aSID);
  162. mEntries[aSID] = aStyleStruct;
  163. }
  164. void SetStyleData(nsStyleStructID aSID,
  165. nsPresContext* aPresContext,
  166. void* aStyleStruct,
  167. const mozilla::RuleNodeCacheConditions& aConditions) {
  168. if (!(mConditionalBits & GetBitForSID(aSID))) {
  169. MOZ_ASSERT(!mEntries[aSID],
  170. "rule node should not have unconditional and conditional "
  171. "style data for a given struct");
  172. mEntries[aSID] = nullptr;
  173. }
  174. MOZ_ASSERT(aConditions.CacheableWithDependencies(),
  175. "don't call SetStyleData with a cache key that has no "
  176. "conditions or is uncacheable");
  177. #ifdef DEBUG
  178. for (Entry* e = static_cast<Entry*>(mEntries[aSID]); e; e = e->mNext) {
  179. NS_WARNING_ASSERTION(e->mConditions != aConditions,
  180. "wasteful to have duplicate conditional style data");
  181. }
  182. #endif
  183. mConditionalBits |= GetBitForSID(aSID);
  184. mEntries[aSID] =
  185. new (aPresContext) Entry(aConditions, aStyleStruct,
  186. static_cast<Entry*>(mEntries[aSID]));
  187. }
  188. void Destroy(uint64_t aBits, nsPresContext* aContext) {
  189. #define STYLE_STRUCT_RESET(name, checkdata_cb) \
  190. void* name##Ptr = mEntries[eStyleStruct_##name]; \
  191. if (name##Ptr) { \
  192. if (!(mConditionalBits & NS_STYLE_INHERIT_BIT(name))) { \
  193. if (!(aBits & NS_STYLE_INHERIT_BIT(name))) { \
  194. static_cast<nsStyle##name*>(name##Ptr)->Destroy(aContext); \
  195. } \
  196. } else { \
  197. Entry* e = static_cast<Entry*>(name##Ptr); \
  198. MOZ_ASSERT(e, "if mConditionalBits bit is set, we must have at least " \
  199. "one conditional style struct"); \
  200. do { \
  201. static_cast<nsStyle##name*>(e->mStyleStruct)->Destroy(aContext); \
  202. Entry* next = e->mNext; \
  203. aContext->PresShell()->FreeByObjectID( \
  204. mozilla::eArenaObjectID_nsConditionalResetStyleDataEntry, e); \
  205. e = next; \
  206. } while (e); \
  207. } \
  208. }
  209. #define STYLE_STRUCT_INHERITED(name, checkdata_cb)
  210. #include "nsStyleStructList.h"
  211. #undef STYLE_STRUCT_RESET
  212. #undef STYLE_STRUCT_INHERITED
  213. aContext->PresShell()->FreeByObjectID(
  214. mozilla::eArenaObjectID_nsConditionalResetStyleData, this);
  215. }
  216. };
  217. struct nsCachedStyleData
  218. {
  219. nsInheritedStyleData* mInheritedData;
  220. nsConditionalResetStyleData* mResetData;
  221. static bool IsReset(const nsStyleStructID aSID) {
  222. MOZ_ASSERT(0 <= aSID && aSID < nsStyleStructID_Length,
  223. "must be an inherited or reset SID");
  224. return nsStyleStructID_Reset_Start <= aSID;
  225. }
  226. static bool IsInherited(const nsStyleStructID aSID) {
  227. return !IsReset(aSID);
  228. }
  229. static uint32_t GetBitForSID(const nsStyleStructID aSID) {
  230. return nsConditionalResetStyleData::GetBitForSID(aSID);
  231. }
  232. void* NS_FASTCALL GetStyleData(const nsStyleStructID aSID) {
  233. if (IsReset(aSID)) {
  234. if (mResetData) {
  235. return mResetData->GetStyleData(aSID);
  236. }
  237. } else {
  238. if (mInheritedData) {
  239. return mInheritedData->mStyleStructs[aSID];
  240. }
  241. }
  242. return nullptr;
  243. }
  244. void* NS_FASTCALL GetStyleData(const nsStyleStructID aSID,
  245. nsStyleContext* aStyleContext,
  246. bool aCanComputeData) {
  247. if (IsReset(aSID)) {
  248. if (mResetData) {
  249. return mResetData->GetStyleData(aSID, aStyleContext, aCanComputeData);
  250. }
  251. } else {
  252. if (mInheritedData) {
  253. return mInheritedData->mStyleStructs[aSID];
  254. }
  255. }
  256. return nullptr;
  257. }
  258. void NS_FASTCALL SetStyleData(const nsStyleStructID aSID,
  259. nsPresContext *aPresContext, void *aData) {
  260. if (IsReset(aSID)) {
  261. if (!mResetData) {
  262. mResetData = new (aPresContext) nsConditionalResetStyleData;
  263. }
  264. mResetData->SetStyleData(aSID, aData);
  265. } else {
  266. if (!mInheritedData) {
  267. mInheritedData = new (aPresContext) nsInheritedStyleData;
  268. }
  269. mInheritedData->mStyleStructs[aSID] = aData;
  270. }
  271. }
  272. // Typesafe and faster versions of the above
  273. #define STYLE_STRUCT_INHERITED(name_, checkdata_cb_) \
  274. nsStyle##name_ * NS_FASTCALL GetStyle##name_ () { \
  275. return mInheritedData ? static_cast<nsStyle##name_*>( \
  276. mInheritedData->mStyleStructs[eStyleStruct_##name_]) : nullptr; \
  277. }
  278. #define STYLE_STRUCT_RESET(name_, checkdata_cb_) \
  279. nsStyle##name_ * NS_FASTCALL GetStyle##name_ (nsStyleContext* aContext, \
  280. bool aCanComputeData) { \
  281. return mResetData ? static_cast<nsStyle##name_*>( \
  282. mResetData->GetStyleData(eStyleStruct_##name_, aContext, \
  283. aCanComputeData)) \
  284. : nullptr; \
  285. }
  286. #include "nsStyleStructList.h"
  287. #undef STYLE_STRUCT_RESET
  288. #undef STYLE_STRUCT_INHERITED
  289. void Destroy(uint64_t aBits, nsPresContext* aContext) {
  290. if (mResetData)
  291. mResetData->Destroy(aBits, aContext);
  292. if (mInheritedData)
  293. mInheritedData->Destroy(aBits, aContext);
  294. mResetData = nullptr;
  295. mInheritedData = nullptr;
  296. }
  297. nsCachedStyleData() :mInheritedData(nullptr), mResetData(nullptr) {}
  298. ~nsCachedStyleData() {}
  299. };
  300. /**
  301. * nsRuleNode is a node in a lexicographic tree (the "rule tree")
  302. * indexed by style rules (implementations of nsIStyleRule).
  303. *
  304. * The rule tree is owned by the nsStyleSet and is destroyed when the
  305. * presentation of the document goes away. Its entries are reference-
  306. * counted, with strong references held by child nodes, style structs
  307. * and (for the root), the style set. Rule nodes are not immediately
  308. * destroyed when their reference-count drops to zero, but are instead
  309. * destroyed during a GC sweep.
  310. *
  311. * An nsStyleContext, which represents the computed style data for an
  312. * element, points to an nsRuleNode. The path from the root of the rule
  313. * tree to the nsStyleContext's mRuleNode gives the list of the rules
  314. * matched, from least important in the cascading order to most
  315. * important in the cascading order.
  316. *
  317. * The reason for using a lexicographic tree is that it allows for
  318. * sharing of style data, which saves both memory (for storing the
  319. * computed style data) and time (for computing them). This sharing
  320. * depends on the computed style data being stored in structs (nsStyle*)
  321. * that contain only properties that are inherited by default
  322. * ("inherited structs") or structs that contain only properties that
  323. * are not inherited by default ("reset structs"). The optimization
  324. * depends on the normal case being that style rules specify relatively
  325. * few properties and even that elements generally have relatively few
  326. * properties specified. This allows sharing in the following ways:
  327. * 1. [mainly reset structs] When a style data struct will contain the
  328. * same computed value for any elements that match the same set of
  329. * rules (common for reset structs), it can be stored on the
  330. * nsRuleNode instead of on the nsStyleContext.
  331. * 2. [only? reset structs] When (1) occurs, and an nsRuleNode doesn't
  332. * have any rules that change the values in the struct, the
  333. * nsRuleNode can share that struct with its parent nsRuleNode.
  334. * 3. [mainly inherited structs] When an element doesn't match any
  335. * rules that change the value of a property (or, in the edge case,
  336. * when all the values specified are 'inherit'), the nsStyleContext
  337. * can use the same nsStyle* struct as its parent nsStyleContext.
  338. *
  339. * Since the data represented by an nsIStyleRule are immutable, the data
  340. * represented by an nsRuleNode are also immutable.
  341. */
  342. enum nsFontSizeType {
  343. eFontSize_HTML = 1,
  344. eFontSize_CSS = 2
  345. };
  346. // Note: This LinkedListElement is used for storing unused nodes in the
  347. // linked list on nsStyleSet. We use mNextSibling for the singly-linked
  348. // sibling list.
  349. class nsRuleNode : public mozilla::LinkedListElement<nsRuleNode> {
  350. public:
  351. enum RuleDetail {
  352. eRuleNone, // No props have been specified at all.
  353. eRulePartialReset, // At least one prop with a non-"inherit" value
  354. // has been specified. No props have been
  355. // specified with an "inherit" value. At least
  356. // one prop remains unspecified.
  357. eRulePartialMixed, // At least one prop with a non-"inherit" value
  358. // has been specified. Some props may also have
  359. // been specified with an "inherit" value. At
  360. // least one prop remains unspecified.
  361. eRulePartialInherited, // Only props with "inherit" values have
  362. // have been specified. At least one prop
  363. // remains unspecified.
  364. eRuleFullReset, // All props have been specified. None has an
  365. // "inherit" value.
  366. eRuleFullMixed, // All props have been specified. At least one has
  367. // a non-"inherit" value.
  368. eRuleFullInherited // All props have been specified with "inherit"
  369. // values.
  370. };
  371. private:
  372. nsPresContext* const mPresContext; // Our pres context.
  373. const RefPtr<nsRuleNode> mParent; // A pointer to the parent node in the tree.
  374. // This enables us to walk backwards from the
  375. // most specific rule matched to the least
  376. // specific rule (which is the optimal order to
  377. // use for lookups of style properties.
  378. const nsCOMPtr<nsIStyleRule> mRule; // A pointer to our specific rule.
  379. nsRuleNode* mNextSibling; // This value should be used only by the
  380. // parent, since the parent may store
  381. // children in a hash, which means this
  382. // pointer is not meaningful. Order of
  383. // siblings is also not meaningful.
  384. struct Key {
  385. nsIStyleRule* mRule;
  386. mozilla::SheetType mLevel;
  387. bool mIsImportantRule;
  388. Key(nsIStyleRule* aRule, mozilla::SheetType aLevel, bool aIsImportantRule)
  389. : mRule(aRule), mLevel(aLevel), mIsImportantRule(aIsImportantRule)
  390. {}
  391. bool operator==(const Key& aOther) const
  392. {
  393. return mRule == aOther.mRule &&
  394. mLevel == aOther.mLevel &&
  395. mIsImportantRule == aOther.mIsImportantRule;
  396. }
  397. bool operator!=(const Key& aOther) const
  398. {
  399. return !(*this == aOther);
  400. }
  401. };
  402. static PLDHashNumber
  403. ChildrenHashHashKey(const void *aKey);
  404. static bool
  405. ChildrenHashMatchEntry(const PLDHashEntryHdr *aHdr, const void *aKey);
  406. static const PLDHashTableOps ChildrenHashOps;
  407. Key GetKey() const {
  408. return Key(mRule, GetLevel(), IsImportantRule());
  409. }
  410. // The children of this node are stored in either a hashtable or list
  411. // that maps from rules to our nsRuleNode children. When matching
  412. // rules, we use this mapping to transition from node to node
  413. // (constructing new nodes as needed to flesh out the tree).
  414. union {
  415. void* asVoid;
  416. nsRuleNode* asList;
  417. PLDHashTable* asHash;
  418. } mChildren; // Accessed only through the methods below.
  419. enum {
  420. kTypeMask = 0x1,
  421. kListType = 0x0,
  422. kHashType = 0x1
  423. };
  424. enum {
  425. // Maximum to have in a list before converting to a hashtable.
  426. // XXX Need to optimize this.
  427. kMaxChildrenInList = 32
  428. };
  429. bool HaveChildren() const {
  430. return mChildren.asVoid != nullptr;
  431. }
  432. bool ChildrenAreHashed() {
  433. return (intptr_t(mChildren.asVoid) & kTypeMask) == kHashType;
  434. }
  435. nsRuleNode* ChildrenList() {
  436. return mChildren.asList;
  437. }
  438. nsRuleNode** ChildrenListPtr() {
  439. return &mChildren.asList;
  440. }
  441. PLDHashTable* ChildrenHash() {
  442. return (PLDHashTable*) (intptr_t(mChildren.asHash) & ~intptr_t(kTypeMask));
  443. }
  444. void SetChildrenList(nsRuleNode *aList) {
  445. NS_ASSERTION(!(intptr_t(aList) & kTypeMask),
  446. "pointer not 2-byte aligned");
  447. mChildren.asList = aList;
  448. }
  449. void SetChildrenHash(PLDHashTable *aHashtable) {
  450. NS_ASSERTION(!(intptr_t(aHashtable) & kTypeMask),
  451. "pointer not 2-byte aligned");
  452. mChildren.asHash = (PLDHashTable*)(intptr_t(aHashtable) | kHashType);
  453. }
  454. void ConvertChildrenToHash(int32_t aNumKids);
  455. void RemoveChild(nsRuleNode* aNode);
  456. nsCachedStyleData mStyleData; // Any data we cached on the rule node.
  457. uint32_t mDependentBits; // Used to cache the fact that we can look up
  458. // cached data under a parent rule.
  459. uint32_t mNoneBits; // Used to cache the fact that the branch to this
  460. // node specifies no non-inherited data for a
  461. // given struct type. (This usually implies that
  462. // the entire branch specifies no non-inherited
  463. // data, although not necessarily, if a
  464. // non-inherited value is overridden by an
  465. // explicit 'inherit' value.) For example, if an
  466. // entire rule branch specifies no color
  467. // information, then a bit will be set along every
  468. // rule node on that branch, so that you can break
  469. // out of the rule tree early and just inherit
  470. // from the parent style context. The presence of
  471. // this bit means we should just get inherited
  472. // data from the parent style context, and it is
  473. // never used for reset structs since their
  474. // Compute*Data functions don't initialize from
  475. // inherited data.
  476. // Reference count. Style contexts hold strong references to their rule node,
  477. // and rule nodes hold strong references to their parent.
  478. //
  479. // When the refcount drops to zero, we don't necessarily free the node.
  480. // Instead, we notify the style set, which performs periodic sweeps.
  481. uint32_t mRefCnt;
  482. public:
  483. // Infallible overloaded new operator that allocates from a presShell arena.
  484. void* operator new(size_t sz, nsPresContext* aContext);
  485. void Destroy();
  486. // Implemented in nsStyleSet.h, since it needs to know about nsStyleSet.
  487. inline void AddRef();
  488. // Implemented in nsStyleSet.h, since it needs to know about nsStyleSet.
  489. inline void Release();
  490. protected:
  491. void PropagateDependentBit(nsStyleStructID aSID, nsRuleNode* aHighestNode,
  492. void* aStruct);
  493. void PropagateNoneBit(uint32_t aBit, nsRuleNode* aHighestNode);
  494. static void PropagateGrandancestorBit(nsStyleContext* aContext,
  495. nsStyleContext* aContextInheritedFrom);
  496. const void* SetDefaultOnRoot(const nsStyleStructID aSID,
  497. nsStyleContext* aContext);
  498. /**
  499. * Resolves any property values in aRuleData for a given style struct that
  500. * have eCSSUnit_TokenStream values, by resolving them against the computed
  501. * variable values on the style context and re-parsing the property.
  502. *
  503. * @return Whether any properties with eCSSUnit_TokenStream values were
  504. * encountered.
  505. */
  506. static bool ResolveVariableReferences(const nsStyleStructID aSID,
  507. nsRuleData* aRuleData,
  508. nsStyleContext* aContext);
  509. const void*
  510. WalkRuleTree(const nsStyleStructID aSID, nsStyleContext* aContext);
  511. const void*
  512. ComputeDisplayData(void* aStartStruct,
  513. const nsRuleData* aRuleData,
  514. nsStyleContext* aContext, nsRuleNode* aHighestNode,
  515. RuleDetail aRuleDetail,
  516. const mozilla::RuleNodeCacheConditions aConditions);
  517. const void*
  518. ComputeVisibilityData(void* aStartStruct,
  519. const nsRuleData* aRuleData,
  520. nsStyleContext* aContext, nsRuleNode* aHighestNode,
  521. RuleDetail aRuleDetail,
  522. const mozilla::RuleNodeCacheConditions aConditions);
  523. const void*
  524. ComputeFontData(void* aStartStruct,
  525. const nsRuleData* aRuleData,
  526. nsStyleContext* aContext, nsRuleNode* aHighestNode,
  527. RuleDetail aRuleDetail,
  528. const mozilla::RuleNodeCacheConditions aConditions);
  529. const void*
  530. ComputeColorData(void* aStartStruct,
  531. const nsRuleData* aRuleData,
  532. nsStyleContext* aContext, nsRuleNode* aHighestNode,
  533. RuleDetail aRuleDetail,
  534. const mozilla::RuleNodeCacheConditions aConditions);
  535. const void*
  536. ComputeBackgroundData(void* aStartStruct,
  537. const nsRuleData* aRuleData,
  538. nsStyleContext* aContext, nsRuleNode* aHighestNode,
  539. RuleDetail aRuleDetail,
  540. const mozilla::RuleNodeCacheConditions aConditions);
  541. const void*
  542. ComputeMarginData(void* aStartStruct,
  543. const nsRuleData* aRuleData,
  544. nsStyleContext* aContext, nsRuleNode* aHighestNode,
  545. RuleDetail aRuleDetail,
  546. const mozilla::RuleNodeCacheConditions aConditions);
  547. const void*
  548. ComputeBorderData(void* aStartStruct,
  549. const nsRuleData* aRuleData,
  550. nsStyleContext* aContext, nsRuleNode* aHighestNode,
  551. RuleDetail aRuleDetail,
  552. const mozilla::RuleNodeCacheConditions aConditions);
  553. const void*
  554. ComputePaddingData(void* aStartStruct,
  555. const nsRuleData* aRuleData,
  556. nsStyleContext* aContext, nsRuleNode* aHighestNode,
  557. RuleDetail aRuleDetail,
  558. const mozilla::RuleNodeCacheConditions aConditions);
  559. const void*
  560. ComputeOutlineData(void* aStartStruct,
  561. const nsRuleData* aRuleData,
  562. nsStyleContext* aContext, nsRuleNode* aHighestNode,
  563. RuleDetail aRuleDetail,
  564. const mozilla::RuleNodeCacheConditions aConditions);
  565. const void*
  566. ComputeListData(void* aStartStruct,
  567. const nsRuleData* aRuleData,
  568. nsStyleContext* aContext, nsRuleNode* aHighestNode,
  569. RuleDetail aRuleDetail,
  570. const mozilla::RuleNodeCacheConditions aConditions);
  571. const void*
  572. ComputePositionData(void* aStartStruct,
  573. const nsRuleData* aRuleData,
  574. nsStyleContext* aContext, nsRuleNode* aHighestNode,
  575. RuleDetail aRuleDetail,
  576. const mozilla::RuleNodeCacheConditions aConditions);
  577. const void*
  578. ComputeTableData(void* aStartStruct,
  579. const nsRuleData* aRuleData,
  580. nsStyleContext* aContext, nsRuleNode* aHighestNode,
  581. RuleDetail aRuleDetail,
  582. const mozilla::RuleNodeCacheConditions aConditions);
  583. const void*
  584. ComputeTableBorderData(void* aStartStruct,
  585. const nsRuleData* aRuleData,
  586. nsStyleContext* aContext, nsRuleNode* aHighestNode,
  587. RuleDetail aRuleDetail,
  588. const mozilla::RuleNodeCacheConditions aConditions);
  589. const void*
  590. ComputeContentData(void* aStartStruct,
  591. const nsRuleData* aRuleData,
  592. nsStyleContext* aContext, nsRuleNode* aHighestNode,
  593. RuleDetail aRuleDetail,
  594. const mozilla::RuleNodeCacheConditions aConditions);
  595. const void*
  596. ComputeTextData(void* aStartStruct,
  597. const nsRuleData* aRuleData,
  598. nsStyleContext* aContext, nsRuleNode* aHighestNode,
  599. RuleDetail aRuleDetail,
  600. const mozilla::RuleNodeCacheConditions aConditions);
  601. const void*
  602. ComputeTextResetData(void* aStartStruct,
  603. const nsRuleData* aRuleData,
  604. nsStyleContext* aContext, nsRuleNode* aHighestNode,
  605. RuleDetail aRuleDetail,
  606. const mozilla::RuleNodeCacheConditions aConditions);
  607. const void*
  608. ComputeUserInterfaceData(void* aStartStruct,
  609. const nsRuleData* aRuleData,
  610. nsStyleContext* aContext,
  611. nsRuleNode* aHighestNode,
  612. RuleDetail aRuleDetail,
  613. const mozilla::RuleNodeCacheConditions aConditions);
  614. const void*
  615. ComputeUIResetData(void* aStartStruct,
  616. const nsRuleData* aRuleData,
  617. nsStyleContext* aContext, nsRuleNode* aHighestNode,
  618. RuleDetail aRuleDetail,
  619. const mozilla::RuleNodeCacheConditions aConditions);
  620. const void*
  621. ComputeXULData(void* aStartStruct,
  622. const nsRuleData* aRuleData,
  623. nsStyleContext* aContext, nsRuleNode* aHighestNode,
  624. RuleDetail aRuleDetail,
  625. const mozilla::RuleNodeCacheConditions aConditions);
  626. const void*
  627. ComputeColumnData(void* aStartStruct,
  628. const nsRuleData* aRuleData,
  629. nsStyleContext* aContext, nsRuleNode* aHighestNode,
  630. RuleDetail aRuleDetail,
  631. const mozilla::RuleNodeCacheConditions aConditions);
  632. const void*
  633. ComputeSVGData(void* aStartStruct,
  634. const nsRuleData* aRuleData,
  635. nsStyleContext* aContext, nsRuleNode* aHighestNode,
  636. RuleDetail aRuleDetail,
  637. const mozilla::RuleNodeCacheConditions aConditions);
  638. const void*
  639. ComputeSVGResetData(void* aStartStruct,
  640. const nsRuleData* aRuleData,
  641. nsStyleContext* aContext, nsRuleNode* aHighestNode,
  642. RuleDetail aRuleDetail,
  643. const mozilla::RuleNodeCacheConditions aConditions);
  644. const void*
  645. ComputeVariablesData(void* aStartStruct,
  646. const nsRuleData* aRuleData,
  647. nsStyleContext* aContext, nsRuleNode* aHighestNode,
  648. RuleDetail aRuleDetail,
  649. const mozilla::RuleNodeCacheConditions aConditions);
  650. const void*
  651. ComputeEffectsData(void* aStartStruct,
  652. const nsRuleData* aRuleData,
  653. nsStyleContext* aContext, nsRuleNode* aHighestNode,
  654. RuleDetail aRuleDetail,
  655. const mozilla::RuleNodeCacheConditions aConditions);
  656. // helpers for |ComputeFontData| that need access to |mNoneBits|:
  657. static void SetFontSize(nsPresContext* aPresContext,
  658. nsStyleContext* aContext,
  659. const nsRuleData* aRuleData,
  660. const nsStyleFont* aFont,
  661. const nsStyleFont* aParentFont,
  662. nscoord* aSize,
  663. const nsFont& aSystemFont,
  664. nscoord aParentSize,
  665. nscoord aScriptLevelAdjustedParentSize,
  666. bool aUsedStartStruct,
  667. bool aAtRoot,
  668. mozilla::RuleNodeCacheConditions& aConditions);
  669. static void SetFont(nsPresContext* aPresContext,
  670. nsStyleContext* aContext,
  671. uint8_t aGenericFontID,
  672. const nsRuleData* aRuleData,
  673. const nsStyleFont* aParentFont,
  674. nsStyleFont* aFont,
  675. bool aStartStruct,
  676. mozilla::RuleNodeCacheConditions& aConditions);
  677. static void SetGenericFont(nsPresContext* aPresContext,
  678. nsStyleContext* aContext,
  679. uint8_t aGenericFontID,
  680. nsStyleFont* aFont);
  681. inline RuleDetail CheckSpecifiedProperties(const nsStyleStructID aSID,
  682. const nsRuleData* aRuleData);
  683. private:
  684. nsRuleNode(nsPresContext* aPresContext, nsRuleNode* aParent,
  685. nsIStyleRule* aRule, mozilla::SheetType aLevel, bool aIsImportant);
  686. ~nsRuleNode();
  687. public:
  688. // This is infallible; it will never return nullptr.
  689. static already_AddRefed<nsRuleNode> CreateRootNode(nsPresContext* aPresContext);
  690. static void EnsureBlockDisplay(mozilla::StyleDisplay& display,
  691. bool aConvertListItem = false);
  692. static void EnsureInlineDisplay(mozilla::StyleDisplay& display);
  693. // Transition never returns null; on out of memory it'll just return |this|.
  694. nsRuleNode* Transition(nsIStyleRule* aRule, mozilla::SheetType aLevel,
  695. bool aIsImportantRule);
  696. nsRuleNode* GetParent() const { return mParent; }
  697. bool IsRoot() const { return mParent == nullptr; }
  698. // Return the root of the rule tree that this rule node is in.
  699. nsRuleNode* RuleTree();
  700. const nsRuleNode* RuleTree() const {
  701. return const_cast<nsRuleNode*>(this)->RuleTree();
  702. }
  703. mozilla::SheetType GetLevel() const {
  704. NS_ASSERTION(!IsRoot(), "can't call on root");
  705. return mozilla::SheetType(
  706. (mDependentBits & NS_RULE_NODE_LEVEL_MASK) >>
  707. NS_RULE_NODE_LEVEL_SHIFT);
  708. }
  709. bool IsImportantRule() const {
  710. NS_ASSERTION(!IsRoot(), "can't call on root");
  711. return (mDependentBits & NS_RULE_NODE_IS_IMPORTANT) != 0;
  712. }
  713. /**
  714. * Has this rule node at some time in its lifetime been the mRuleNode
  715. * of some style context (as opposed to only being the ancestor of
  716. * some style context's mRuleNode)?
  717. */
  718. void SetUsedDirectly();
  719. bool IsUsedDirectly() const {
  720. return (mDependentBits & NS_RULE_NODE_USED_DIRECTLY) != 0;
  721. }
  722. /**
  723. * Is the mRule of this rule node an AnimValuesStyleRule?
  724. */
  725. void SetIsAnimationRule() {
  726. MOZ_ASSERT(!HaveChildren() ||
  727. (mDependentBits & NS_RULE_NODE_IS_ANIMATION_RULE),
  728. "SetIsAnimationRule must only set the IS_ANIMATION_RULE bit "
  729. "before the rule node has children");
  730. mDependentBits |= NS_RULE_NODE_IS_ANIMATION_RULE;
  731. mNoneBits |= NS_RULE_NODE_HAS_ANIMATION_DATA;
  732. }
  733. bool IsAnimationRule() const {
  734. return (mDependentBits & NS_RULE_NODE_IS_ANIMATION_RULE) != 0;
  735. }
  736. /**
  737. * Is the mRule of this rule node or any of its ancestors an
  738. * AnimValuesStyleRule?
  739. */
  740. bool HasAnimationData() const {
  741. return (mNoneBits & NS_RULE_NODE_HAS_ANIMATION_DATA) != 0;
  742. }
  743. // NOTE: Does not |AddRef|. Null only for the root.
  744. nsIStyleRule* GetRule() const { return mRule; }
  745. // NOTE: Does not |AddRef|. Never null.
  746. nsPresContext* PresContext() const { return mPresContext; }
  747. const void* GetStyleData(nsStyleStructID aSID,
  748. nsStyleContext* aContext,
  749. bool aComputeData);
  750. void GetDiscretelyAnimatedCSSValue(nsCSSPropertyID aProperty,
  751. nsCSSValue* aValue);
  752. // See comments in GetStyleData for an explanation of what the
  753. // code below does.
  754. #define STYLE_STRUCT_INHERITED(name_, checkdata_cb_) \
  755. template<bool aComputeData> \
  756. const nsStyle##name_* \
  757. GetStyle##name_(nsStyleContext* aContext, uint64_t& aContextStyleBits) \
  758. { \
  759. NS_ASSERTION(IsUsedDirectly(), \
  760. "if we ever call this on rule nodes that aren't used " \
  761. "directly, we should adjust handling of mDependentBits " \
  762. "in some way."); \
  763. MOZ_ASSERT(!ContextHasCachedData(aContext, eStyleStruct_##name_), \
  764. "style context should not have cached data for struct"); \
  765. \
  766. const nsStyle##name_ *data; \
  767. \
  768. /* Never use cached data for animated style inside a pseudo-element; */ \
  769. /* see comment on cacheability in AnimValuesStyleRule::MapRuleInfoInto */ \
  770. if (!(HasAnimationData() && ParentHasPseudoElementData(aContext))) { \
  771. data = mStyleData.GetStyle##name_(); \
  772. if (data != nullptr) { \
  773. /* For inherited structs, mark the struct (which will be set on */ \
  774. /* the context by our caller) as not being owned by the context. */ \
  775. /* Normally this would be aContext->AddStyleBit(), but aContext is */ \
  776. /* an incomplete type here, so we work around that with a param. */ \
  777. aContextStyleBits |= NS_STYLE_INHERIT_BIT(name_); \
  778. /* Our caller will cache the data on the style context. */ \
  779. return data; \
  780. } \
  781. } \
  782. \
  783. if (!aComputeData) \
  784. return nullptr; \
  785. \
  786. data = static_cast<const nsStyle##name_ *> \
  787. (WalkRuleTree(eStyleStruct_##name_, aContext)); \
  788. \
  789. MOZ_ASSERT(data, "should have aborted on out-of-memory"); \
  790. return data; \
  791. }
  792. #define STYLE_STRUCT_RESET(name_, checkdata_cb_) \
  793. template<bool aComputeData> \
  794. const nsStyle##name_* \
  795. GetStyle##name_(nsStyleContext* aContext) \
  796. { \
  797. NS_ASSERTION(IsUsedDirectly(), \
  798. "if we ever call this on rule nodes that aren't used " \
  799. "directly, we should adjust handling of mDependentBits " \
  800. "in some way."); \
  801. MOZ_ASSERT(!ContextHasCachedData(aContext, eStyleStruct_##name_), \
  802. "style context should not have cached data for struct"); \
  803. \
  804. const nsStyle##name_ *data; \
  805. \
  806. /* Never use cached data for animated style inside a pseudo-element; */ \
  807. /* see comment on cacheability in AnimValuesStyleRule::MapRuleInfoInto */ \
  808. if (!(HasAnimationData() && ParentHasPseudoElementData(aContext))) { \
  809. data = mStyleData.GetStyle##name_(aContext, aComputeData); \
  810. if (MOZ_LIKELY(data != nullptr)) { \
  811. if (HasAnimationData()) { \
  812. /* If we have animation data, the struct should be cached on the */ \
  813. /* style context so that we can peek the struct. */ \
  814. /* See comment in AnimValuesStyleRule::MapRuleInfoInto. */ \
  815. StoreStyleOnContext(aContext, \
  816. eStyleStruct_##name_, \
  817. const_cast<nsStyle##name_*>(data)); \
  818. } \
  819. return data; \
  820. } \
  821. } \
  822. \
  823. if (!aComputeData) \
  824. return nullptr; \
  825. \
  826. data = static_cast<const nsStyle##name_ *> \
  827. (WalkRuleTree(eStyleStruct_##name_, aContext)); \
  828. \
  829. MOZ_ASSERT(data, "should have aborted on out-of-memory"); \
  830. return data; \
  831. }
  832. #include "nsStyleStructList.h"
  833. #undef STYLE_STRUCT_RESET
  834. #undef STYLE_STRUCT_INHERITED
  835. static bool
  836. HasAuthorSpecifiedRules(nsStyleContext* aStyleContext,
  837. uint32_t ruleTypeMask,
  838. bool aAuthorColorsAllowed);
  839. /**
  840. * Fill in to aPropertiesOverridden all of the properties in aProperties
  841. * that, for this rule node, have a declaration that is higher than the
  842. * animation level in the CSS Cascade.
  843. */
  844. static void
  845. ComputePropertiesOverridingAnimation(
  846. const nsTArray<nsCSSPropertyID>& aProperties,
  847. nsStyleContext* aStyleContext,
  848. nsCSSPropertyIDSet& aPropertiesOverridden);
  849. // Expose this so media queries can use it
  850. static nscoord CalcLengthWithInitialFont(nsPresContext* aPresContext,
  851. const nsCSSValue& aValue);
  852. // Expose this so nsTransformFunctions can use it.
  853. static nscoord CalcLength(const nsCSSValue& aValue,
  854. nsStyleContext* aStyleContext,
  855. nsPresContext* aPresContext,
  856. mozilla::RuleNodeCacheConditions& aConditions);
  857. struct ComputedCalc {
  858. nscoord mLength;
  859. float mPercent;
  860. ComputedCalc(nscoord aLength, float aPercent)
  861. : mLength(aLength), mPercent(aPercent) {}
  862. };
  863. static ComputedCalc
  864. SpecifiedCalcToComputedCalc(const nsCSSValue& aValue,
  865. nsStyleContext* aStyleContext,
  866. nsPresContext* aPresContext,
  867. mozilla::RuleNodeCacheConditions& aConditions);
  868. // Compute the value of an nsStyleCoord that IsCalcUnit().
  869. // (Values that don't require aPercentageBasis should be handled
  870. // inside nsRuleNode rather than through this API.)
  871. // @note the caller is expected to handle percentage of an indefinite size
  872. // and NOT call this method with aPercentageBasis == NS_UNCONSTRAINEDSIZE.
  873. // @note the return value may be negative, e.g. for "calc(a - b%)"
  874. static nscoord ComputeComputedCalc(const nsStyleCoord& aCoord,
  875. nscoord aPercentageBasis);
  876. // Compute the value of an nsStyleCoord that is either a coord, a
  877. // percent, or a calc expression.
  878. // @note the caller is expected to handle percentage of an indefinite size
  879. // and NOT call this method with aPercentageBasis == NS_UNCONSTRAINEDSIZE.
  880. // @note the return value may be negative, e.g. for "calc(a - b%)"
  881. static nscoord ComputeCoordPercentCalc(const nsStyleCoord& aCoord,
  882. nscoord aPercentageBasis);
  883. // Return whether the rule tree for which this node is the root has
  884. // cached data such that we need to do dynamic change handling for
  885. // changes that change the results of media queries or require
  886. // rebuilding all style data.
  887. bool TreeHasCachedData() const {
  888. NS_ASSERTION(IsRoot(), "should only be called on root of rule tree");
  889. return HaveChildren() || mStyleData.mInheritedData || mStyleData.mResetData;
  890. }
  891. // Note that this will return false if we have cached conditional
  892. // style structs.
  893. bool NodeHasCachedUnconditionalData(const nsStyleStructID aSID) {
  894. return !!mStyleData.GetStyleData(aSID);
  895. }
  896. static void ComputeFontFeatures(const nsCSSValuePairList *aFeaturesList,
  897. nsTArray<gfxFontFeature>& aFeatureSettings);
  898. static nscoord CalcFontPointSize(int32_t aHTMLSize, int32_t aBasePointSize,
  899. nsPresContext* aPresContext,
  900. nsFontSizeType aFontSizeType = eFontSize_HTML);
  901. static nscoord FindNextSmallerFontSize(nscoord aFontSize, int32_t aBasePointSize,
  902. nsPresContext* aPresContext,
  903. nsFontSizeType aFontSizeType = eFontSize_HTML);
  904. static nscoord FindNextLargerFontSize(nscoord aFontSize, int32_t aBasePointSize,
  905. nsPresContext* aPresContext,
  906. nsFontSizeType aFontSizeType = eFontSize_HTML);
  907. /**
  908. * @param aValue The color value, returned from nsCSSParser::ParseColorString
  909. * @param aPresContext Presentation context whose preferences are used
  910. * for certain enumerated colors
  911. * @param aStyleContext Style context whose color is used for 'currentColor'
  912. *
  913. * @note aPresContext and aStyleContext may be null, but in that case, fully
  914. * opaque black will be returned for the values that rely on these
  915. * objects to compute the color. (For example, -moz-hyperlinktext.)
  916. *
  917. * @return false if we fail to extract a color; this will not happen if both
  918. * aPresContext and aStyleContext are non-null
  919. */
  920. static bool ComputeColor(const nsCSSValue& aValue,
  921. nsPresContext* aPresContext,
  922. nsStyleContext* aStyleContext,
  923. nscolor& aResult);
  924. static bool ParentHasPseudoElementData(nsStyleContext* aContext);
  925. static void ComputeTimingFunction(const nsCSSValue& aValue,
  926. nsTimingFunction& aResult);
  927. // Fill unspecified layers by cycling through their values
  928. // till they all are of length aMaxItemCount
  929. static void FillAllBackgroundLists(nsStyleImageLayers& aLayers,
  930. uint32_t aMaxItemCount);
  931. static void FillAllMaskLists(nsStyleImageLayers& aLayers,
  932. uint32_t aMaxItemCount);
  933. private:
  934. #ifdef DEBUG
  935. // non-inline helper function to allow assertions without incomplete
  936. // type errors
  937. bool ContextHasCachedData(nsStyleContext* aContext, nsStyleStructID aSID);
  938. #endif
  939. // Store style struct on the style context and tell the style context
  940. // that it doesn't own the data
  941. static void StoreStyleOnContext(nsStyleContext* aContext,
  942. nsStyleStructID aSID,
  943. void* aStruct);
  944. };
  945. #endif