nsCSSDataBlock.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. * compact representation of the property-value pairs within a CSS
  7. * declaration, and the code for expanding and compacting it
  8. */
  9. #include "nsCSSDataBlock.h"
  10. #include "CSSVariableImageTable.h"
  11. #include "mozilla/css/Declaration.h"
  12. #include "mozilla/css/ImageLoader.h"
  13. #include "mozilla/MemoryReporting.h"
  14. #include "mozilla/WritingModes.h"
  15. #include "nsAutoPtr.h"
  16. #include "nsIDocument.h"
  17. #include "nsRuleData.h"
  18. #include "nsStyleContext.h"
  19. #include "nsStyleSet.h"
  20. using namespace mozilla;
  21. using namespace mozilla::css;
  22. /**
  23. * Does a fast move of aSource to aDest. The previous value in
  24. * aDest is cleanly destroyed, and aSource is cleared. Returns
  25. * true if, before the copy, the value at aSource compared unequal
  26. * to the value at aDest; false otherwise.
  27. */
  28. static bool
  29. MoveValue(nsCSSValue* aSource, nsCSSValue* aDest)
  30. {
  31. bool changed = (*aSource != *aDest);
  32. aDest->~nsCSSValue();
  33. memcpy(aDest, aSource, sizeof(nsCSSValue));
  34. new (aSource) nsCSSValue();
  35. return changed;
  36. }
  37. static bool
  38. ShouldIgnoreColors(nsRuleData *aRuleData)
  39. {
  40. return aRuleData->mLevel != SheetType::Agent &&
  41. aRuleData->mLevel != SheetType::User &&
  42. !aRuleData->mPresContext->UseDocumentColors();
  43. }
  44. /**
  45. * Tries to call |nsCSSValue::StartImageLoad()| on an image source.
  46. * Image sources are specified by |url()| or |-moz-image-rect()| function.
  47. */
  48. static void
  49. TryToStartImageLoadOnValue(const nsCSSValue& aValue, nsIDocument* aDocument,
  50. nsStyleContext* aContext, nsCSSPropertyID aProperty,
  51. bool aForTokenStream)
  52. {
  53. MOZ_ASSERT(aDocument);
  54. if (aValue.GetUnit() == eCSSUnit_URL) {
  55. // The 'mask-image' property accepts local reference URIs.
  56. // For example,
  57. // mask-image: url(#mask_id); // refer to a SVG mask element, whose id is
  58. // // "mask_id", in the current document.
  59. // For such 'mask-image' values (pointing to an in-document element),
  60. // there is no need to trigger image download.
  61. if (aProperty == eCSSProperty_mask_image) {
  62. // Filter out all fragment URLs.
  63. // Since nsCSSValue::GetURLStructValue runs much faster than
  64. // nsIURI::EqualsExceptRef bellow, we get performance gain by this
  65. // early return.
  66. URLValue* urlValue = aValue.GetURLStructValue();
  67. if (urlValue->IsLocalRef()) {
  68. return;
  69. }
  70. // Even though urlValue is not a fragment URL, it might still refer to
  71. // an internal resource.
  72. // For example, aDocument base URL is "http://foo/index.html" and
  73. // intentionally references a mask-image at
  74. // url(http://foo/index.html#mask) which still refers to a resource in
  75. // aDocument.
  76. nsIURI* imageURI = aValue.GetURLValue();
  77. if (imageURI) {
  78. nsIURI* docURI = aDocument->GetDocumentURI();
  79. bool isEqualExceptRef = false;
  80. nsresult rv = imageURI->EqualsExceptRef(docURI, &isEqualExceptRef);
  81. if (NS_SUCCEEDED(rv) && isEqualExceptRef) {
  82. return;
  83. }
  84. }
  85. }
  86. aValue.StartImageLoad(aDocument);
  87. if (aForTokenStream && aContext) {
  88. CSSVariableImageTable::Add(aContext, aProperty,
  89. aValue.GetImageStructValue());
  90. }
  91. }
  92. else if (aValue.GetUnit() == eCSSUnit_Image) {
  93. // If we already have a request, see if this document needs to clone it.
  94. imgIRequest* request = aValue.GetImageValue(nullptr);
  95. if (request) {
  96. ImageValue* imageValue = aValue.GetImageStructValue();
  97. aDocument->StyleImageLoader()->MaybeRegisterCSSImage(imageValue);
  98. if (aForTokenStream && aContext) {
  99. CSSVariableImageTable::Add(aContext, aProperty, imageValue);
  100. }
  101. }
  102. }
  103. else if (aValue.EqualsFunction(eCSSKeyword__moz_image_rect)) {
  104. nsCSSValue::Array* arguments = aValue.GetArrayValue();
  105. MOZ_ASSERT(arguments->Count() == 6, "unexpected num of arguments");
  106. const nsCSSValue& image = arguments->Item(1);
  107. TryToStartImageLoadOnValue(image, aDocument, aContext, aProperty,
  108. aForTokenStream);
  109. }
  110. }
  111. static void
  112. TryToStartImageLoad(const nsCSSValue& aValue, nsIDocument* aDocument,
  113. nsStyleContext* aContext, nsCSSPropertyID aProperty,
  114. bool aForTokenStream)
  115. {
  116. if (aValue.GetUnit() == eCSSUnit_List) {
  117. for (const nsCSSValueList* l = aValue.GetListValue(); l; l = l->mNext) {
  118. TryToStartImageLoad(l->mValue, aDocument, aContext, aProperty,
  119. aForTokenStream);
  120. }
  121. } else if (nsCSSProps::PropHasFlags(aProperty,
  122. CSS_PROPERTY_IMAGE_IS_IN_ARRAY_0)) {
  123. if (aValue.GetUnit() == eCSSUnit_Array) {
  124. TryToStartImageLoadOnValue(aValue.GetArrayValue()->Item(0), aDocument,
  125. aContext, aProperty, aForTokenStream);
  126. }
  127. } else {
  128. TryToStartImageLoadOnValue(aValue, aDocument, aContext, aProperty,
  129. aForTokenStream);
  130. }
  131. }
  132. static inline bool
  133. ShouldStartImageLoads(nsRuleData *aRuleData, nsCSSPropertyID aProperty)
  134. {
  135. // Don't initiate image loads for if-visited styles. This is
  136. // important because:
  137. // (1) it's a waste of CPU and bandwidth
  138. // (2) in some cases we'd start the image load on a style change
  139. // where we wouldn't have started the load initially, which makes
  140. // which links are visited detectable to Web pages (see bug
  141. // 557287)
  142. return !aRuleData->mStyleContext->IsStyleIfVisited() &&
  143. nsCSSProps::PropHasFlags(aProperty, CSS_PROPERTY_START_IMAGE_LOADS);
  144. }
  145. static void
  146. MapSinglePropertyInto(nsCSSPropertyID aTargetProp,
  147. const nsCSSValue* aSrcValue,
  148. nsCSSValue* aTargetValue,
  149. nsRuleData* aRuleData)
  150. {
  151. MOZ_ASSERT(!nsCSSProps::PropHasFlags(aTargetProp, CSS_PROPERTY_LOGICAL),
  152. "Can't map into a logical property");
  153. MOZ_ASSERT(aSrcValue->GetUnit() != eCSSUnit_Null, "oops");
  154. // Although aTargetValue is the nsCSSValue we are going to write into,
  155. // we also look at its value before writing into it. This is done
  156. // when aTargetValue is a token stream value, which is the case when we
  157. // have just re-parsed a property that had a variable reference (in
  158. // nsCSSParser::ParsePropertyWithVariableReferences). TryToStartImageLoad
  159. // then records any resulting ImageValue objects in the
  160. // CSSVariableImageTable, to give them the appropriate lifetime.
  161. MOZ_ASSERT(aTargetValue->GetUnit() == eCSSUnit_TokenStream ||
  162. aTargetValue->GetUnit() == eCSSUnit_Null,
  163. "aTargetValue must only be a token stream (when re-parsing "
  164. "properties with variable references) or null");
  165. if (ShouldStartImageLoads(aRuleData, aTargetProp)) {
  166. nsIDocument* doc = aRuleData->mPresContext->Document();
  167. TryToStartImageLoad(*aSrcValue, doc, aRuleData->mStyleContext,
  168. aTargetProp,
  169. aTargetValue->GetUnit() == eCSSUnit_TokenStream);
  170. }
  171. *aTargetValue = *aSrcValue;
  172. if (nsCSSProps::PropHasFlags(aTargetProp,
  173. CSS_PROPERTY_IGNORED_WHEN_COLORS_DISABLED) &&
  174. ShouldIgnoreColors(aRuleData))
  175. {
  176. if (aTargetProp == eCSSProperty_background_color) {
  177. // Force non-'transparent' background
  178. // colors to the user's default.
  179. if (aTargetValue->IsNonTransparentColor()) {
  180. aTargetValue->SetColorValue(aRuleData->mPresContext->
  181. DefaultBackgroundColor());
  182. }
  183. } else {
  184. // Ignore 'color', 'border-*-color', etc.
  185. *aTargetValue = nsCSSValue();
  186. }
  187. }
  188. }
  189. /**
  190. * If aProperty is a logical property, converts it to the equivalent physical
  191. * property based on writing mode information obtained from aRuleData's
  192. * style context.
  193. */
  194. static inline void
  195. EnsurePhysicalProperty(nsCSSPropertyID& aProperty, nsRuleData* aRuleData)
  196. {
  197. bool isAxisProperty =
  198. nsCSSProps::PropHasFlags(aProperty, CSS_PROPERTY_LOGICAL_AXIS);
  199. bool isBlock =
  200. nsCSSProps::PropHasFlags(aProperty, CSS_PROPERTY_LOGICAL_BLOCK_AXIS);
  201. int index;
  202. if (isAxisProperty) {
  203. LogicalAxis logicalAxis = isBlock ? eLogicalAxisBlock : eLogicalAxisInline;
  204. uint8_t wm = aRuleData->mStyleContext->StyleVisibility()->mWritingMode;
  205. PhysicalAxis axis =
  206. WritingMode::PhysicalAxisForLogicalAxis(wm, logicalAxis);
  207. // We rely on physical axis constants values matching the order of the
  208. // physical properties in the logical group array.
  209. static_assert(eAxisVertical == 0 && eAxisHorizontal == 1,
  210. "unexpected axis constant values");
  211. index = axis;
  212. } else {
  213. bool isEnd =
  214. nsCSSProps::PropHasFlags(aProperty, CSS_PROPERTY_LOGICAL_END_EDGE);
  215. LogicalEdge edge = isEnd ? eLogicalEdgeEnd : eLogicalEdgeStart;
  216. // We handle block axis logical properties separately to save a bit of
  217. // work that the WritingMode constructor does that is unnecessary
  218. // unless we have an inline axis property.
  219. mozilla::css::Side side;
  220. if (isBlock) {
  221. uint8_t wm = aRuleData->mStyleContext->StyleVisibility()->mWritingMode;
  222. side = WritingMode::PhysicalSideForBlockAxis(wm, edge);
  223. } else {
  224. WritingMode wm(aRuleData->mStyleContext);
  225. side = wm.PhysicalSideForInlineAxis(edge);
  226. }
  227. // We rely on the physical side constant values matching the order of
  228. // the physical properties in the logical group array.
  229. static_assert(NS_SIDE_TOP == 0 && NS_SIDE_RIGHT == 1 &&
  230. NS_SIDE_BOTTOM == 2 && NS_SIDE_LEFT == 3,
  231. "unexpected side constant values");
  232. index = side;
  233. }
  234. const nsCSSPropertyID* props = nsCSSProps::LogicalGroup(aProperty);
  235. size_t len = isAxisProperty ? 2 : 4;
  236. #ifdef DEBUG
  237. for (size_t i = 0; i < len; i++) {
  238. MOZ_ASSERT(props[i] != eCSSProperty_UNKNOWN,
  239. "unexpected logical group length");
  240. }
  241. MOZ_ASSERT(props[len] == eCSSProperty_UNKNOWN,
  242. "unexpected logical group length");
  243. #endif
  244. for (size_t i = 0; i < len; i++) {
  245. if (aRuleData->ValueFor(props[i])->GetUnit() == eCSSUnit_Null) {
  246. // A declaration of one of the logical properties in this logical
  247. // group (but maybe not aProperty) would be the winning
  248. // declaration in the cascade. This means that it's reasonably
  249. // likely that this logical property could be the winning
  250. // declaration in the cascade for some values of writing-mode,
  251. // direction, and text-orientation. (It doesn't mean that for
  252. // sure, though. For example, if this is a block-start logical
  253. // property, and all but the bottom physical property were set.
  254. // But the common case we want to hit here is logical declarations
  255. // that are completely overridden by a shorthand.)
  256. //
  257. // If this logical property could be the winning declaration in
  258. // the cascade for some values of writing-mode, direction, and
  259. // text-orientation, then we have to fault the resulting style
  260. // struct out of the rule tree. We can't cache anything on the
  261. // rule tree if it depends on data from the style context, since
  262. // data cached in the rule tree could be used with a style context
  263. // with a different value of the depended-upon data.
  264. uint8_t wm = WritingMode(aRuleData->mStyleContext).GetBits();
  265. aRuleData->mConditions.SetWritingModeDependency(wm);
  266. break;
  267. }
  268. }
  269. aProperty = props[index];
  270. }
  271. void
  272. nsCSSCompressedDataBlock::MapRuleInfoInto(nsRuleData *aRuleData) const
  273. {
  274. // If we have no data for these structs, then return immediately.
  275. // This optimization should make us return most of the time, so we
  276. // have to worry much less (although still some) about the speed of
  277. // the rest of the function.
  278. if (!(aRuleData->mSIDs & mStyleBits))
  279. return;
  280. // We process these in reverse order so that we end up mapping the
  281. // right property when one can be expressed using both logical and
  282. // physical property names.
  283. for (uint32_t i = mNumProps; i-- > 0; ) {
  284. nsCSSPropertyID iProp = PropertyAtIndex(i);
  285. if (nsCachedStyleData::GetBitForSID(nsCSSProps::kSIDTable[iProp]) &
  286. aRuleData->mSIDs) {
  287. if (nsCSSProps::PropHasFlags(iProp, CSS_PROPERTY_LOGICAL)) {
  288. EnsurePhysicalProperty(iProp, aRuleData);
  289. }
  290. nsCSSValue* target = aRuleData->ValueFor(iProp);
  291. if (target->GetUnit() == eCSSUnit_Null) {
  292. const nsCSSValue *val = ValueAtIndex(i);
  293. // In order for variable resolution to have the right information
  294. // about the stylesheet level of a value, that level needs to be
  295. // stored on the token stream. We can't do that at creation time
  296. // because the CSS parser (which creates the object) has no idea
  297. // about the stylesheet level, so we do it here instead, where
  298. // the rule walking will have just updated aRuleData.
  299. if (val->GetUnit() == eCSSUnit_TokenStream) {
  300. val->GetTokenStreamValue()->mLevel = aRuleData->mLevel;
  301. }
  302. MapSinglePropertyInto(iProp, val, target, aRuleData);
  303. }
  304. }
  305. }
  306. }
  307. const nsCSSValue*
  308. nsCSSCompressedDataBlock::ValueFor(nsCSSPropertyID aProperty) const
  309. {
  310. MOZ_ASSERT(!nsCSSProps::IsShorthand(aProperty),
  311. "Don't call for shorthands");
  312. // If we have no data for this struct, then return immediately.
  313. // This optimization should make us return most of the time, so we
  314. // have to worry much less (although still some) about the speed of
  315. // the rest of the function.
  316. if (!(nsCachedStyleData::GetBitForSID(nsCSSProps::kSIDTable[aProperty]) &
  317. mStyleBits))
  318. return nullptr;
  319. for (uint32_t i = 0; i < mNumProps; i++) {
  320. if (PropertyAtIndex(i) == aProperty) {
  321. return ValueAtIndex(i);
  322. }
  323. }
  324. return nullptr;
  325. }
  326. bool
  327. nsCSSCompressedDataBlock::TryReplaceValue(nsCSSPropertyID aProperty,
  328. nsCSSExpandedDataBlock& aFromBlock,
  329. bool *aChanged)
  330. {
  331. nsCSSValue* newValue = aFromBlock.PropertyAt(aProperty);
  332. MOZ_ASSERT(newValue && newValue->GetUnit() != eCSSUnit_Null,
  333. "cannot replace with empty value");
  334. const nsCSSValue* oldValue = ValueFor(aProperty);
  335. if (!oldValue) {
  336. *aChanged = false;
  337. return false;
  338. }
  339. *aChanged = MoveValue(newValue, const_cast<nsCSSValue*>(oldValue));
  340. aFromBlock.ClearPropertyBit(aProperty);
  341. return true;
  342. }
  343. nsCSSCompressedDataBlock*
  344. nsCSSCompressedDataBlock::Clone() const
  345. {
  346. nsAutoPtr<nsCSSCompressedDataBlock>
  347. result(new(mNumProps) nsCSSCompressedDataBlock(mNumProps));
  348. result->mStyleBits = mStyleBits;
  349. for (uint32_t i = 0; i < mNumProps; i++) {
  350. result->SetPropertyAtIndex(i, PropertyAtIndex(i));
  351. result->CopyValueToIndex(i, ValueAtIndex(i));
  352. }
  353. return result.forget();
  354. }
  355. nsCSSCompressedDataBlock::~nsCSSCompressedDataBlock()
  356. {
  357. for (uint32_t i = 0; i < mNumProps; i++) {
  358. #ifdef DEBUG
  359. (void)PropertyAtIndex(i); // this checks the property is in range
  360. #endif
  361. const nsCSSValue* val = ValueAtIndex(i);
  362. MOZ_ASSERT(val->GetUnit() != eCSSUnit_Null, "oops");
  363. val->~nsCSSValue();
  364. }
  365. }
  366. /* static */ nsCSSCompressedDataBlock*
  367. nsCSSCompressedDataBlock::CreateEmptyBlock()
  368. {
  369. nsCSSCompressedDataBlock *result = new(0) nsCSSCompressedDataBlock(0);
  370. return result;
  371. }
  372. size_t
  373. nsCSSCompressedDataBlock::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
  374. {
  375. size_t n = aMallocSizeOf(this);
  376. for (uint32_t i = 0; i < mNumProps; i++) {
  377. n += ValueAtIndex(i)->SizeOfExcludingThis(aMallocSizeOf);
  378. }
  379. return n;
  380. }
  381. bool
  382. nsCSSCompressedDataBlock::HasDefaultBorderImageSlice() const
  383. {
  384. const nsCSSValueList *slice =
  385. ValueFor(eCSSProperty_border_image_slice)->GetListValue();
  386. return !slice->mNext &&
  387. slice->mValue.GetRectValue().AllSidesEqualTo(
  388. nsCSSValue(1.0f, eCSSUnit_Percent));
  389. }
  390. bool
  391. nsCSSCompressedDataBlock::HasDefaultBorderImageWidth() const
  392. {
  393. const nsCSSRect &width =
  394. ValueFor(eCSSProperty_border_image_width)->GetRectValue();
  395. return width.AllSidesEqualTo(nsCSSValue(1.0f, eCSSUnit_Number));
  396. }
  397. bool
  398. nsCSSCompressedDataBlock::HasDefaultBorderImageOutset() const
  399. {
  400. const nsCSSRect &outset =
  401. ValueFor(eCSSProperty_border_image_outset)->GetRectValue();
  402. return outset.AllSidesEqualTo(nsCSSValue(0.0f, eCSSUnit_Number));
  403. }
  404. bool
  405. nsCSSCompressedDataBlock::HasDefaultBorderImageRepeat() const
  406. {
  407. const nsCSSValuePair &repeat =
  408. ValueFor(eCSSProperty_border_image_repeat)->GetPairValue();
  409. return repeat.BothValuesEqualTo(
  410. nsCSSValue(NS_STYLE_BORDER_IMAGE_REPEAT_STRETCH, eCSSUnit_Enumerated));
  411. }
  412. /*****************************************************************************/
  413. nsCSSExpandedDataBlock::nsCSSExpandedDataBlock()
  414. {
  415. AssertInitialState();
  416. }
  417. nsCSSExpandedDataBlock::~nsCSSExpandedDataBlock()
  418. {
  419. AssertInitialState();
  420. }
  421. void
  422. nsCSSExpandedDataBlock::DoExpand(nsCSSCompressedDataBlock *aBlock,
  423. bool aImportant)
  424. {
  425. /*
  426. * Save needless copying and allocation by copying the memory
  427. * corresponding to the stored data in the compressed block.
  428. */
  429. for (uint32_t i = 0; i < aBlock->mNumProps; i++) {
  430. nsCSSPropertyID iProp = aBlock->PropertyAtIndex(i);
  431. MOZ_ASSERT(!nsCSSProps::IsShorthand(iProp), "out of range");
  432. MOZ_ASSERT(!HasPropertyBit(iProp),
  433. "compressed block has property multiple times");
  434. SetPropertyBit(iProp);
  435. if (aImportant)
  436. SetImportantBit(iProp);
  437. const nsCSSValue* val = aBlock->ValueAtIndex(i);
  438. nsCSSValue* dest = PropertyAt(iProp);
  439. MOZ_ASSERT(val->GetUnit() != eCSSUnit_Null, "oops");
  440. MOZ_ASSERT(dest->GetUnit() == eCSSUnit_Null,
  441. "expanding into non-empty block");
  442. #ifdef NS_BUILD_REFCNT_LOGGING
  443. dest->~nsCSSValue();
  444. #endif
  445. memcpy(dest, val, sizeof(nsCSSValue));
  446. }
  447. // Set the number of properties to zero so that we don't destroy the
  448. // remnants of what we just copied.
  449. aBlock->SetNumPropsToZero();
  450. delete aBlock;
  451. }
  452. void
  453. nsCSSExpandedDataBlock::Expand(nsCSSCompressedDataBlock *aNormalBlock,
  454. nsCSSCompressedDataBlock *aImportantBlock)
  455. {
  456. MOZ_ASSERT(aNormalBlock, "unexpected null block");
  457. AssertInitialState();
  458. DoExpand(aNormalBlock, false);
  459. if (aImportantBlock) {
  460. DoExpand(aImportantBlock, true);
  461. }
  462. }
  463. void
  464. nsCSSExpandedDataBlock::ComputeNumProps(uint32_t* aNumPropsNormal,
  465. uint32_t* aNumPropsImportant)
  466. {
  467. *aNumPropsNormal = *aNumPropsImportant = 0;
  468. for (size_t iHigh = 0; iHigh < nsCSSPropertyIDSet::kChunkCount; ++iHigh) {
  469. if (!mPropertiesSet.HasPropertyInChunk(iHigh))
  470. continue;
  471. for (size_t iLow = 0; iLow < nsCSSPropertyIDSet::kBitsInChunk; ++iLow) {
  472. if (!mPropertiesSet.HasPropertyAt(iHigh, iLow))
  473. continue;
  474. #ifdef DEBUG
  475. nsCSSPropertyID iProp = nsCSSPropertyIDSet::CSSPropertyAt(iHigh, iLow);
  476. #endif
  477. MOZ_ASSERT(!nsCSSProps::IsShorthand(iProp), "out of range");
  478. MOZ_ASSERT(PropertyAt(iProp)->GetUnit() != eCSSUnit_Null,
  479. "null value while computing size");
  480. if (mPropertiesImportant.HasPropertyAt(iHigh, iLow))
  481. (*aNumPropsImportant)++;
  482. else
  483. (*aNumPropsNormal)++;
  484. }
  485. }
  486. }
  487. void
  488. nsCSSExpandedDataBlock::Compress(nsCSSCompressedDataBlock **aNormalBlock,
  489. nsCSSCompressedDataBlock **aImportantBlock,
  490. const nsTArray<uint32_t>& aOrder)
  491. {
  492. nsAutoPtr<nsCSSCompressedDataBlock> result_normal, result_important;
  493. uint32_t i_normal = 0, i_important = 0;
  494. uint32_t numPropsNormal, numPropsImportant;
  495. ComputeNumProps(&numPropsNormal, &numPropsImportant);
  496. result_normal =
  497. new(numPropsNormal) nsCSSCompressedDataBlock(numPropsNormal);
  498. if (numPropsImportant != 0) {
  499. result_important =
  500. new(numPropsImportant) nsCSSCompressedDataBlock(numPropsImportant);
  501. } else {
  502. result_important = nullptr;
  503. }
  504. /*
  505. * Save needless copying and allocation by copying the memory
  506. * corresponding to the stored data in the expanded block, and then
  507. * clearing the data in the expanded block.
  508. */
  509. for (size_t i = 0; i < aOrder.Length(); i++) {
  510. nsCSSPropertyID iProp = static_cast<nsCSSPropertyID>(aOrder[i]);
  511. if (iProp >= eCSSProperty_COUNT) {
  512. // a custom property
  513. continue;
  514. }
  515. MOZ_ASSERT(mPropertiesSet.HasProperty(iProp),
  516. "aOrder identifies a property not in the expanded "
  517. "data block");
  518. MOZ_ASSERT(!nsCSSProps::IsShorthand(iProp), "out of range");
  519. bool important = mPropertiesImportant.HasProperty(iProp);
  520. nsCSSCompressedDataBlock *result =
  521. important ? result_important : result_normal;
  522. uint32_t* ip = important ? &i_important : &i_normal;
  523. nsCSSValue* val = PropertyAt(iProp);
  524. MOZ_ASSERT(val->GetUnit() != eCSSUnit_Null,
  525. "Null value while compressing");
  526. result->SetPropertyAtIndex(*ip, iProp);
  527. result->RawCopyValueToIndex(*ip, val);
  528. new (val) nsCSSValue();
  529. (*ip)++;
  530. result->mStyleBits |=
  531. nsCachedStyleData::GetBitForSID(nsCSSProps::kSIDTable[iProp]);
  532. }
  533. MOZ_ASSERT(numPropsNormal == i_normal, "bad numProps");
  534. if (result_important) {
  535. MOZ_ASSERT(numPropsImportant == i_important, "bad numProps");
  536. }
  537. #ifdef DEBUG
  538. {
  539. // assert that we didn't have any other properties on this expanded data
  540. // block that we didn't find in aOrder
  541. uint32_t numPropsInSet = 0;
  542. for (size_t iHigh = 0; iHigh < nsCSSPropertyIDSet::kChunkCount; iHigh++) {
  543. if (!mPropertiesSet.HasPropertyInChunk(iHigh)) {
  544. continue;
  545. }
  546. for (size_t iLow = 0; iLow < nsCSSPropertyIDSet::kBitsInChunk; iLow++) {
  547. if (mPropertiesSet.HasPropertyAt(iHigh, iLow)) {
  548. numPropsInSet++;
  549. }
  550. }
  551. }
  552. MOZ_ASSERT(numPropsNormal + numPropsImportant == numPropsInSet,
  553. "aOrder missing properties from the expanded data block");
  554. }
  555. #endif
  556. ClearSets();
  557. AssertInitialState();
  558. *aNormalBlock = result_normal.forget();
  559. *aImportantBlock = result_important.forget();
  560. }
  561. void
  562. nsCSSExpandedDataBlock::AddLonghandProperty(nsCSSPropertyID aProperty,
  563. const nsCSSValue& aValue)
  564. {
  565. MOZ_ASSERT(!nsCSSProps::IsShorthand(aProperty),
  566. "property out of range");
  567. nsCSSValue& storage = *static_cast<nsCSSValue*>(PropertyAt(aProperty));
  568. storage = aValue;
  569. SetPropertyBit(aProperty);
  570. }
  571. void
  572. nsCSSExpandedDataBlock::Clear()
  573. {
  574. for (size_t iHigh = 0; iHigh < nsCSSPropertyIDSet::kChunkCount; ++iHigh) {
  575. if (!mPropertiesSet.HasPropertyInChunk(iHigh))
  576. continue;
  577. for (size_t iLow = 0; iLow < nsCSSPropertyIDSet::kBitsInChunk; ++iLow) {
  578. if (!mPropertiesSet.HasPropertyAt(iHigh, iLow))
  579. continue;
  580. nsCSSPropertyID iProp = nsCSSPropertyIDSet::CSSPropertyAt(iHigh, iLow);
  581. ClearLonghandProperty(iProp);
  582. }
  583. }
  584. AssertInitialState();
  585. }
  586. void
  587. nsCSSExpandedDataBlock::ClearProperty(nsCSSPropertyID aPropID)
  588. {
  589. if (nsCSSProps::IsShorthand(aPropID)) {
  590. CSSPROPS_FOR_SHORTHAND_SUBPROPERTIES(
  591. p, aPropID, CSSEnabledState::eIgnoreEnabledState) {
  592. ClearLonghandProperty(*p);
  593. }
  594. } else {
  595. ClearLonghandProperty(aPropID);
  596. }
  597. }
  598. void
  599. nsCSSExpandedDataBlock::ClearLonghandProperty(nsCSSPropertyID aPropID)
  600. {
  601. MOZ_ASSERT(!nsCSSProps::IsShorthand(aPropID), "out of range");
  602. ClearPropertyBit(aPropID);
  603. ClearImportantBit(aPropID);
  604. PropertyAt(aPropID)->Reset();
  605. }
  606. bool
  607. nsCSSExpandedDataBlock::TransferFromBlock(nsCSSExpandedDataBlock& aFromBlock,
  608. nsCSSPropertyID aPropID,
  609. CSSEnabledState aEnabledState,
  610. bool aIsImportant,
  611. bool aOverrideImportant,
  612. bool aMustCallValueAppended,
  613. css::Declaration* aDeclaration,
  614. nsIDocument* aSheetDocument)
  615. {
  616. if (!nsCSSProps::IsShorthand(aPropID)) {
  617. return DoTransferFromBlock(aFromBlock, aPropID,
  618. aIsImportant, aOverrideImportant,
  619. aMustCallValueAppended, aDeclaration,
  620. aSheetDocument);
  621. }
  622. // We can pass CSSEnabledState::eIgnore (here, and in ClearProperty
  623. // above) rather than a value corresponding to whether we're parsing
  624. // a UA style sheet or certified app because we assert in nsCSSProps::
  625. // AddRefTable that shorthand properties available in these contexts
  626. // also have all of their subproperties available in these contexts.
  627. bool changed = false;
  628. CSSPROPS_FOR_SHORTHAND_SUBPROPERTIES(p, aPropID, aEnabledState) {
  629. changed |= DoTransferFromBlock(aFromBlock, *p,
  630. aIsImportant, aOverrideImportant,
  631. aMustCallValueAppended, aDeclaration,
  632. aSheetDocument);
  633. }
  634. return changed;
  635. }
  636. bool
  637. nsCSSExpandedDataBlock::DoTransferFromBlock(nsCSSExpandedDataBlock& aFromBlock,
  638. nsCSSPropertyID aPropID,
  639. bool aIsImportant,
  640. bool aOverrideImportant,
  641. bool aMustCallValueAppended,
  642. css::Declaration* aDeclaration,
  643. nsIDocument* aSheetDocument)
  644. {
  645. bool changed = false;
  646. MOZ_ASSERT(aFromBlock.HasPropertyBit(aPropID), "oops");
  647. if (aIsImportant) {
  648. if (!HasImportantBit(aPropID))
  649. changed = true;
  650. SetImportantBit(aPropID);
  651. } else {
  652. if (HasImportantBit(aPropID)) {
  653. // When parsing a declaration block, an !important declaration
  654. // is not overwritten by an ordinary declaration of the same
  655. // property later in the block. However, CSSOM manipulations
  656. // come through here too, and in that case we do want to
  657. // overwrite the property.
  658. if (!aOverrideImportant) {
  659. aFromBlock.ClearLonghandProperty(aPropID);
  660. return false;
  661. }
  662. changed = true;
  663. ClearImportantBit(aPropID);
  664. }
  665. }
  666. if (aMustCallValueAppended || !HasPropertyBit(aPropID)) {
  667. aDeclaration->ValueAppended(aPropID);
  668. }
  669. if (aSheetDocument) {
  670. UseCounter useCounter = nsCSSProps::UseCounterFor(aPropID);
  671. if (useCounter != eUseCounter_UNKNOWN) {
  672. aSheetDocument->SetDocumentAndPageUseCounter(useCounter);
  673. }
  674. }
  675. SetPropertyBit(aPropID);
  676. aFromBlock.ClearPropertyBit(aPropID);
  677. /*
  678. * Save needless copying and allocation by calling the destructor in
  679. * the destination, copying memory directly, and then using placement
  680. * new.
  681. */
  682. changed |= MoveValue(aFromBlock.PropertyAt(aPropID), PropertyAt(aPropID));
  683. return changed;
  684. }
  685. void
  686. nsCSSExpandedDataBlock::MapRuleInfoInto(nsCSSPropertyID aPropID,
  687. nsRuleData* aRuleData) const
  688. {
  689. MOZ_ASSERT(!nsCSSProps::IsShorthand(aPropID));
  690. const nsCSSValue* src = PropertyAt(aPropID);
  691. MOZ_ASSERT(src->GetUnit() != eCSSUnit_Null);
  692. nsCSSPropertyID physicalProp = aPropID;
  693. if (nsCSSProps::PropHasFlags(aPropID, CSS_PROPERTY_LOGICAL)) {
  694. EnsurePhysicalProperty(physicalProp, aRuleData);
  695. }
  696. nsCSSValue* dest = aRuleData->ValueFor(physicalProp);
  697. MOZ_ASSERT(dest->GetUnit() == eCSSUnit_TokenStream &&
  698. dest->GetTokenStreamValue()->mPropertyID == aPropID);
  699. CSSVariableImageTable::ReplaceAll(aRuleData->mStyleContext, aPropID, [=] {
  700. MapSinglePropertyInto(physicalProp, src, dest, aRuleData);
  701. });
  702. }
  703. #ifdef DEBUG
  704. void
  705. nsCSSExpandedDataBlock::DoAssertInitialState()
  706. {
  707. mPropertiesSet.AssertIsEmpty("not initial state");
  708. mPropertiesImportant.AssertIsEmpty("not initial state");
  709. for (uint32_t i = 0; i < eCSSProperty_COUNT_no_shorthands; ++i) {
  710. nsCSSPropertyID prop = nsCSSPropertyID(i);
  711. MOZ_ASSERT(PropertyAt(prop)->GetUnit() == eCSSUnit_Null,
  712. "not initial state");
  713. }
  714. }
  715. #endif