Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / layout / style / nsCSSDataBlock.cpp
blob2d8f89d238b0573d7b1156f60a9cd48c071e9068
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is nsCSSDataBlock.cpp.
17 * The Initial Developer of the Original Code is L. David Baron.
18 * Portions created by the Initial Developer are Copyright (C) 2003
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * L. David Baron <dbaron@dbaron.org> (original author)
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 * compact representation of the property-value pairs within a CSS
40 * declaration, and the code for expanding and compacting it
43 #include "nsCSSDataBlock.h"
44 #include "mozilla/css/Declaration.h"
45 #include "nsRuleData.h"
46 #include "nsStyleSet.h"
47 #include "nsStyleContext.h"
49 namespace css = mozilla::css;
52 * nsCSSCompressedDataBlock holds property-value pairs corresponding
53 * to CSS declaration blocks. Each pair is stored in a CDBValueStorage
54 * object; these objects form an array at the end of the data block.
57 struct CDBValueStorage {
58 nsCSSProperty property;
59 nsCSSValue value;
62 enum {
63 CDBValueStorage_advance = sizeof(CDBValueStorage)
67 * Define a bunch of utility functions for getting the property or any
68 * of the value types when the cursor is at the beginning of the storage
69 * for the property-value pair. The versions taking a non-const cursor
70 * argument return a reference so that the caller can assign into the
71 * result.
74 inline nsCSSProperty& PropertyAtCursor(char *aCursor) {
75 return *reinterpret_cast<nsCSSProperty*>(aCursor);
78 inline nsCSSProperty PropertyAtCursor(const char *aCursor) {
79 return *reinterpret_cast<const nsCSSProperty*>(aCursor);
82 inline nsCSSValue* ValueAtCursor(char *aCursor) {
83 return & reinterpret_cast<CDBValueStorage*>(aCursor)->value;
86 inline const nsCSSValue* ValueAtCursor(const char *aCursor) {
87 return & reinterpret_cast<const CDBValueStorage*>(aCursor)->value;
90 /**
91 * Does a fast move of aSource to aDest. The previous value in
92 * aDest is cleanly destroyed, and aSource is cleared. Returns
93 * true if, before the copy, the value at aSource compared unequal
94 * to the value at aDest; false otherwise.
96 static PRBool
97 MoveValue(nsCSSValue* aSource, nsCSSValue* aDest)
99 PRBool changed = (*aSource != *aDest);
100 aDest->~nsCSSValue();
101 memcpy(aDest, aSource, sizeof(nsCSSValue));
102 new (aSource) nsCSSValue();
103 return changed;
106 static PRBool
107 ShouldIgnoreColors(nsRuleData *aRuleData)
109 return aRuleData->mLevel != nsStyleSet::eAgentSheet &&
110 aRuleData->mLevel != nsStyleSet::eUserSheet &&
111 !aRuleData->mPresContext->UseDocumentColors();
115 * Tries to call |nsCSSValue::StartImageLoad()| on an image source.
116 * Image sources are specified by |url()| or |-moz-image-rect()| function.
118 static void
119 TryToStartImageLoadOnValue(const nsCSSValue& aValue, nsIDocument* aDocument)
121 if (aValue.GetUnit() == eCSSUnit_URL) {
122 aValue.StartImageLoad(aDocument);
124 else if (aValue.EqualsFunction(eCSSKeyword__moz_image_rect)) {
125 nsCSSValue::Array* arguments = aValue.GetArrayValue();
126 NS_ABORT_IF_FALSE(arguments->Count() == 6, "unexpected num of arguments");
128 const nsCSSValue& image = arguments->Item(1);
129 if (image.GetUnit() == eCSSUnit_URL)
130 image.StartImageLoad(aDocument);
134 static void
135 TryToStartImageLoad(const nsCSSValue& aValue, nsIDocument* aDocument,
136 nsCSSProperty aProperty)
138 if (aValue.GetUnit() == eCSSUnit_List) {
139 for (const nsCSSValueList* l = aValue.GetListValue(); l; l = l->mNext) {
140 TryToStartImageLoad(l->mValue, aDocument, aProperty);
142 } else if (nsCSSProps::PropHasFlags(aProperty,
143 CSS_PROPERTY_IMAGE_IS_IN_ARRAY_0)) {
144 if (aValue.GetUnit() == eCSSUnit_Array) {
145 TryToStartImageLoadOnValue(aValue.GetArrayValue()->Item(0), aDocument);
147 } else {
148 TryToStartImageLoadOnValue(aValue, aDocument);
152 static inline PRBool
153 ShouldStartImageLoads(nsRuleData *aRuleData, nsCSSProperty aProperty)
155 // Don't initiate image loads for if-visited styles. This is
156 // important because:
157 // (1) it's a waste of CPU and bandwidth
158 // (2) in some cases we'd start the image load on a style change
159 // where we wouldn't have started the load initially, which makes
160 // which links are visited detectable to Web pages (see bug
161 // 557287)
162 return !aRuleData->mStyleContext->IsStyleIfVisited() &&
163 nsCSSProps::PropHasFlags(aProperty, CSS_PROPERTY_START_IMAGE_LOADS);
166 void
167 nsCSSCompressedDataBlock::MapRuleInfoInto(nsRuleData *aRuleData) const
169 // If we have no data for these structs, then return immediately.
170 // This optimization should make us return most of the time, so we
171 // have to worry much less (although still some) about the speed of
172 // the rest of the function.
173 if (!(aRuleData->mSIDs & mStyleBits))
174 return;
176 nsIDocument* doc = aRuleData->mPresContext->Document();
178 const char* cursor = Block();
179 const char* cursor_end = BlockEnd();
180 while (cursor < cursor_end) {
181 nsCSSProperty iProp = PropertyAtCursor(cursor);
182 NS_ABORT_IF_FALSE(!nsCSSProps::IsShorthand(iProp), "out of range");
183 if (nsCachedStyleData::GetBitForSID(nsCSSProps::kSIDTable[iProp]) &
184 aRuleData->mSIDs) {
185 nsCSSValue* target = aRuleData->ValueFor(iProp);
186 if (target->GetUnit() == eCSSUnit_Null) {
187 const nsCSSValue *val = ValueAtCursor(cursor);
188 NS_ABORT_IF_FALSE(val->GetUnit() != eCSSUnit_Null, "oops");
189 if (ShouldStartImageLoads(aRuleData, iProp)) {
190 TryToStartImageLoad(*val, doc, iProp);
192 *target = *val;
193 if (iProp == eCSSProperty_font_family) {
194 // XXX Are there other things like this?
195 aRuleData->mFontData->mFamilyFromHTML = PR_FALSE;
197 if (nsCSSProps::PropHasFlags(iProp,
198 CSS_PROPERTY_IGNORED_WHEN_COLORS_DISABLED) &&
199 ShouldIgnoreColors(aRuleData))
201 if (iProp == eCSSProperty_background_color) {
202 // Force non-'transparent' background
203 // colors to the user's default.
204 if (target->IsNonTransparentColor()) {
205 target->SetColorValue(aRuleData->mPresContext->
206 DefaultBackgroundColor());
208 } else {
209 // Ignore 'color', 'border-*-color', etc.
210 *target = nsCSSValue();
215 cursor += CDBValueStorage_advance;
217 NS_ABORT_IF_FALSE(cursor == cursor_end, "inconsistent data");
220 const nsCSSValue*
221 nsCSSCompressedDataBlock::ValueFor(nsCSSProperty aProperty) const
223 NS_ABORT_IF_FALSE(!nsCSSProps::IsShorthand(aProperty),
224 "Don't call for shorthands");
226 // If we have no data for this struct, then return immediately.
227 // This optimization should make us return most of the time, so we
228 // have to worry much less (although still some) about the speed of
229 // the rest of the function.
230 if (!(nsCachedStyleData::GetBitForSID(nsCSSProps::kSIDTable[aProperty]) &
231 mStyleBits))
232 return nsnull;
234 const char* cursor = Block();
235 const char* cursor_end = BlockEnd();
236 while (cursor < cursor_end) {
237 nsCSSProperty iProp = PropertyAtCursor(cursor);
238 NS_ABORT_IF_FALSE(!nsCSSProps::IsShorthand(iProp), "out of range");
239 if (iProp == aProperty) {
240 return ValueAtCursor(cursor);
242 cursor += CDBValueStorage_advance;
244 NS_ABORT_IF_FALSE(cursor == cursor_end, "inconsistent data");
246 return nsnull;
249 PRBool
250 nsCSSCompressedDataBlock::TryReplaceValue(nsCSSProperty aProperty,
251 nsCSSExpandedDataBlock& aFromBlock,
252 PRBool *aChanged)
254 nsCSSValue* newValue = aFromBlock.PropertyAt(aProperty);
255 NS_ABORT_IF_FALSE(newValue && newValue->GetUnit() != eCSSUnit_Null,
256 "cannot replace with empty value");
258 const nsCSSValue* oldValue = ValueFor(aProperty);
259 if (!oldValue) {
260 *aChanged = PR_FALSE;
261 return PR_FALSE;
264 *aChanged = MoveValue(newValue, const_cast<nsCSSValue*>(oldValue));
265 aFromBlock.ClearPropertyBit(aProperty);
266 return PR_TRUE;
269 nsCSSCompressedDataBlock*
270 nsCSSCompressedDataBlock::Clone() const
272 const char *cursor = Block(), *cursor_end = BlockEnd();
273 char *result_cursor;
275 nsAutoPtr<nsCSSCompressedDataBlock> result
276 (new(cursor_end - cursor) nsCSSCompressedDataBlock());
277 if (!result)
278 return nsnull;
279 result_cursor = result->Block();
281 while (cursor < cursor_end) {
282 nsCSSProperty iProp = PropertyAtCursor(cursor);
283 NS_ABORT_IF_FALSE(!nsCSSProps::IsShorthand(iProp), "out of range");
284 PropertyAtCursor(result_cursor) = iProp;
286 const nsCSSValue* val = ValueAtCursor(cursor);
287 nsCSSValue *result_val = ValueAtCursor(result_cursor);
288 new (result_val) nsCSSValue(*val);
289 cursor += CDBValueStorage_advance;
290 result_cursor += CDBValueStorage_advance;
292 NS_ABORT_IF_FALSE(cursor == cursor_end, "inconsistent data");
294 result->mBlockEnd = result_cursor;
295 result->mStyleBits = mStyleBits;
296 NS_ABORT_IF_FALSE(result->DataSize() == DataSize(), "wrong size");
298 return result.forget();
301 nsCSSCompressedDataBlock::~nsCSSCompressedDataBlock()
303 const char* cursor = Block();
304 const char* cursor_end = BlockEnd();
305 while (cursor < cursor_end) {
306 nsCSSProperty iProp = PropertyAtCursor(cursor);
307 NS_ABORT_IF_FALSE(!nsCSSProps::IsShorthand(iProp), "out of range");
309 const nsCSSValue* val = ValueAtCursor(cursor);
310 NS_ABORT_IF_FALSE(val->GetUnit() != eCSSUnit_Null, "oops");
311 val->~nsCSSValue();
312 cursor += CDBValueStorage_advance;
314 NS_ABORT_IF_FALSE(cursor == cursor_end, "inconsistent data");
317 /* static */ nsCSSCompressedDataBlock*
318 nsCSSCompressedDataBlock::CreateEmptyBlock()
320 nsCSSCompressedDataBlock *result = new(0) nsCSSCompressedDataBlock();
321 result->mBlockEnd = result->Block();
322 return result;
325 /*****************************************************************************/
327 nsCSSExpandedDataBlock::nsCSSExpandedDataBlock()
329 AssertInitialState();
332 nsCSSExpandedDataBlock::~nsCSSExpandedDataBlock()
334 AssertInitialState();
337 const size_t
338 nsCSSExpandedDataBlock::kOffsetTable[] = {
339 #define CSS_PROP(name_, id_, method_, flags_, datastruct_, member_, \
340 kwtable_, stylestruct_, stylestructoffset_, animtype_) \
341 offsetof(nsCSSExpandedDataBlock, m##datastruct_.member_),
342 #include "nsCSSPropList.h"
343 #undef CSS_PROP
346 void
347 nsCSSExpandedDataBlock::DoExpand(nsCSSCompressedDataBlock *aBlock,
348 PRBool aImportant)
351 * Save needless copying and allocation by copying the memory
352 * corresponding to the stored data in the compressed block.
354 const char* cursor = aBlock->Block();
355 const char* cursor_end = aBlock->BlockEnd();
356 while (cursor < cursor_end) {
357 nsCSSProperty iProp = PropertyAtCursor(cursor);
358 NS_ABORT_IF_FALSE(!nsCSSProps::IsShorthand(iProp), "out of range");
359 NS_ABORT_IF_FALSE(!HasPropertyBit(iProp),
360 "compressed block has property multiple times");
361 SetPropertyBit(iProp);
362 if (aImportant)
363 SetImportantBit(iProp);
365 const nsCSSValue* val = ValueAtCursor(cursor);
366 nsCSSValue* dest = PropertyAt(iProp);
367 NS_ABORT_IF_FALSE(val->GetUnit() != eCSSUnit_Null, "oops");
368 NS_ABORT_IF_FALSE(dest->GetUnit() == eCSSUnit_Null,
369 "expanding into non-empty block");
370 #ifdef NS_BUILD_REFCNT_LOGGING
371 dest->~nsCSSValue();
372 #endif
373 memcpy(dest, val, sizeof(nsCSSValue));
374 cursor += CDBValueStorage_advance;
376 NS_ABORT_IF_FALSE(cursor == cursor_end, "inconsistent data");
378 // Don't destroy remnants of what we just copied
379 aBlock->mBlockEnd = aBlock->Block();
380 delete aBlock;
383 void
384 nsCSSExpandedDataBlock::Expand(nsCSSCompressedDataBlock *aNormalBlock,
385 nsCSSCompressedDataBlock *aImportantBlock)
387 NS_ABORT_IF_FALSE(aNormalBlock, "unexpected null block");
388 AssertInitialState();
390 DoExpand(aNormalBlock, PR_FALSE);
391 if (aImportantBlock) {
392 DoExpand(aImportantBlock, PR_TRUE);
396 nsCSSExpandedDataBlock::ComputeSizeResult
397 nsCSSExpandedDataBlock::ComputeSize()
399 ComputeSizeResult result = {0, 0};
400 for (size_t iHigh = 0; iHigh < nsCSSPropertySet::kChunkCount; ++iHigh) {
401 if (!mPropertiesSet.HasPropertyInChunk(iHigh))
402 continue;
403 for (size_t iLow = 0; iLow < nsCSSPropertySet::kBitsInChunk; ++iLow) {
404 if (!mPropertiesSet.HasPropertyAt(iHigh, iLow))
405 continue;
406 nsCSSProperty iProp = nsCSSPropertySet::CSSPropertyAt(iHigh, iLow);
407 NS_ABORT_IF_FALSE(!nsCSSProps::IsShorthand(iProp), "out of range");
408 NS_ABORT_IF_FALSE(PropertyAt(iProp)->GetUnit() != eCSSUnit_Null,
409 "null value while computing size");
410 if (mPropertiesImportant.HasPropertyAt(iHigh, iLow))
411 result.important += CDBValueStorage_advance;
412 else
413 result.normal += CDBValueStorage_advance;
416 return result;
419 void
420 nsCSSExpandedDataBlock::Compress(nsCSSCompressedDataBlock **aNormalBlock,
421 nsCSSCompressedDataBlock **aImportantBlock)
423 nsAutoPtr<nsCSSCompressedDataBlock> result_normal, result_important;
424 char *cursor_normal, *cursor_important;
426 ComputeSizeResult size = ComputeSize();
428 result_normal = new(size.normal) nsCSSCompressedDataBlock();
429 cursor_normal = result_normal->Block();
431 if (size.important != 0) {
432 result_important = new(size.important) nsCSSCompressedDataBlock();
433 cursor_important = result_important->Block();
434 } else {
435 result_important = nsnull;
436 cursor_important = nsnull;
440 * Save needless copying and allocation by copying the memory
441 * corresponding to the stored data in the expanded block, and then
442 * clearing the data in the expanded block.
444 for (size_t iHigh = 0; iHigh < nsCSSPropertySet::kChunkCount; ++iHigh) {
445 if (!mPropertiesSet.HasPropertyInChunk(iHigh))
446 continue;
447 for (size_t iLow = 0; iLow < nsCSSPropertySet::kBitsInChunk; ++iLow) {
448 if (!mPropertiesSet.HasPropertyAt(iHigh, iLow))
449 continue;
450 nsCSSProperty iProp = nsCSSPropertySet::CSSPropertyAt(iHigh, iLow);
451 NS_ABORT_IF_FALSE(!nsCSSProps::IsShorthand(iProp), "out of range");
452 PRBool important =
453 mPropertiesImportant.HasPropertyAt(iHigh, iLow);
454 char *&cursor = important ? cursor_important : cursor_normal;
455 nsCSSCompressedDataBlock *result =
456 important ? result_important : result_normal;
457 nsCSSValue* val = PropertyAt(iProp);
458 NS_ABORT_IF_FALSE(val->GetUnit() != eCSSUnit_Null,
459 "Null value while compressing");
460 CDBValueStorage *storage =
461 reinterpret_cast<CDBValueStorage*>(cursor);
462 storage->property = iProp;
463 memcpy(&storage->value, val, sizeof(nsCSSValue));
464 new (val) nsCSSValue();
465 cursor += CDBValueStorage_advance;
466 result->mStyleBits |=
467 nsCachedStyleData::GetBitForSID(nsCSSProps::kSIDTable[iProp]);
471 result_normal->mBlockEnd = cursor_normal;
472 NS_ABORT_IF_FALSE(result_normal->DataSize() == ptrdiff_t(size.normal),
473 "size miscalculation");
475 if (result_important) {
476 result_important->mBlockEnd = cursor_important;
477 NS_ABORT_IF_FALSE(result_important->DataSize() ==
478 ptrdiff_t(size.important),
479 "size miscalculation");
482 ClearSets();
483 AssertInitialState();
484 *aNormalBlock = result_normal.forget();
485 *aImportantBlock = result_important.forget();
488 void
489 nsCSSExpandedDataBlock::AddLonghandProperty(nsCSSProperty aProperty,
490 const nsCSSValue& aValue)
492 NS_ABORT_IF_FALSE(!nsCSSProps::IsShorthand(aProperty),
493 "property out of range");
494 nsCSSValue& storage = *static_cast<nsCSSValue*>(PropertyAt(aProperty));
495 storage = aValue;
496 SetPropertyBit(aProperty);
499 void
500 nsCSSExpandedDataBlock::Clear()
502 for (size_t iHigh = 0; iHigh < nsCSSPropertySet::kChunkCount; ++iHigh) {
503 if (!mPropertiesSet.HasPropertyInChunk(iHigh))
504 continue;
505 for (size_t iLow = 0; iLow < nsCSSPropertySet::kBitsInChunk; ++iLow) {
506 if (!mPropertiesSet.HasPropertyAt(iHigh, iLow))
507 continue;
508 nsCSSProperty iProp = nsCSSPropertySet::CSSPropertyAt(iHigh, iLow);
509 ClearLonghandProperty(iProp);
513 AssertInitialState();
516 void
517 nsCSSExpandedDataBlock::ClearProperty(nsCSSProperty aPropID)
519 if (nsCSSProps::IsShorthand(aPropID)) {
520 CSSPROPS_FOR_SHORTHAND_SUBPROPERTIES(p, aPropID) {
521 ClearLonghandProperty(*p);
523 } else {
524 ClearLonghandProperty(aPropID);
528 void
529 nsCSSExpandedDataBlock::ClearLonghandProperty(nsCSSProperty aPropID)
531 NS_ABORT_IF_FALSE(!nsCSSProps::IsShorthand(aPropID), "out of range");
533 ClearPropertyBit(aPropID);
534 ClearImportantBit(aPropID);
535 PropertyAt(aPropID)->Reset();
538 PRBool
539 nsCSSExpandedDataBlock::TransferFromBlock(nsCSSExpandedDataBlock& aFromBlock,
540 nsCSSProperty aPropID,
541 PRBool aIsImportant,
542 PRBool aOverrideImportant,
543 PRBool aMustCallValueAppended,
544 css::Declaration* aDeclaration)
546 if (!nsCSSProps::IsShorthand(aPropID)) {
547 return DoTransferFromBlock(aFromBlock, aPropID,
548 aIsImportant, aOverrideImportant,
549 aMustCallValueAppended, aDeclaration);
552 PRBool changed = PR_FALSE;
553 CSSPROPS_FOR_SHORTHAND_SUBPROPERTIES(p, aPropID) {
554 changed |= DoTransferFromBlock(aFromBlock, *p,
555 aIsImportant, aOverrideImportant,
556 aMustCallValueAppended, aDeclaration);
558 return changed;
561 PRBool
562 nsCSSExpandedDataBlock::DoTransferFromBlock(nsCSSExpandedDataBlock& aFromBlock,
563 nsCSSProperty aPropID,
564 PRBool aIsImportant,
565 PRBool aOverrideImportant,
566 PRBool aMustCallValueAppended,
567 css::Declaration* aDeclaration)
569 PRBool changed = PR_FALSE;
570 NS_ABORT_IF_FALSE(aFromBlock.HasPropertyBit(aPropID), "oops");
571 if (aIsImportant) {
572 if (!HasImportantBit(aPropID))
573 changed = PR_TRUE;
574 SetImportantBit(aPropID);
575 } else {
576 if (HasImportantBit(aPropID)) {
577 // When parsing a declaration block, an !important declaration
578 // is not overwritten by an ordinary declaration of the same
579 // property later in the block. However, CSSOM manipulations
580 // come through here too, and in that case we do want to
581 // overwrite the property.
582 if (!aOverrideImportant) {
583 aFromBlock.ClearLonghandProperty(aPropID);
584 return PR_FALSE;
586 changed = PR_TRUE;
587 ClearImportantBit(aPropID);
591 if (aMustCallValueAppended || !HasPropertyBit(aPropID)) {
592 aDeclaration->ValueAppended(aPropID);
595 SetPropertyBit(aPropID);
596 aFromBlock.ClearPropertyBit(aPropID);
599 * Save needless copying and allocation by calling the destructor in
600 * the destination, copying memory directly, and then using placement
601 * new.
603 changed |= MoveValue(aFromBlock.PropertyAt(aPropID), PropertyAt(aPropID));
604 return changed;
607 #ifdef DEBUG
608 void
609 nsCSSExpandedDataBlock::DoAssertInitialState()
611 mPropertiesSet.AssertIsEmpty("not initial state");
612 mPropertiesImportant.AssertIsEmpty("not initial state");
614 for (PRUint32 i = 0; i < eCSSProperty_COUNT_no_shorthands; ++i) {
615 NS_ABORT_IF_FALSE(PropertyAt(nsCSSProperty(i))->GetUnit() ==
616 eCSSUnit_Null, "not initial state");
619 #endif