Bug 867089 - Validate the playbackRate before using it. r=ehsan
[gecko.git] / layout / style / nsHTMLStyleSheet.cpp
blobde610795b56af7ce8c47c448aee5a270893e3c3e
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 * This Original Code has been modified by IBM Corporation. Modifications made by IBM
8 * described herein are Copyright (c) International Business Machines Corporation, 2000.
9 * Modifications to Mozilla code or documentation identified per MPL Section 3.3
11 * Date Modified by Description of modification
12 * 04/20/2000 IBM Corp. OS/2 VisualAge build.
16 * style sheet and style rule processor representing data from presentational
17 * HTML attributes
20 #include "nsHTMLStyleSheet.h"
21 #include "nsMappedAttributes.h"
22 #include "nsGkAtoms.h"
23 #include "nsPresContext.h"
24 #include "nsEventStates.h"
25 #include "nsIDocument.h"
26 #include "nsIPresShell.h"
27 #include "nsStyleConsts.h"
28 #include "nsRuleWalker.h"
29 #include "nsRuleData.h"
30 #include "nsError.h"
31 #include "nsRuleProcessorData.h"
32 #include "nsCSSRuleProcessor.h"
33 #include "mozilla/dom/Element.h"
34 #include "nsCSSFrameConstructor.h"
36 using namespace mozilla::dom;
38 NS_IMPL_ISUPPORTS1(nsHTMLStyleSheet::HTMLColorRule, nsIStyleRule)
40 /* virtual */ void
41 nsHTMLStyleSheet::HTMLColorRule::MapRuleInfoInto(nsRuleData* aRuleData)
43 if (aRuleData->mSIDs & NS_STYLE_INHERIT_BIT(Color)) {
44 nsCSSValue* color = aRuleData->ValueForColor();
45 if (color->GetUnit() == eCSSUnit_Null &&
46 aRuleData->mPresContext->UseDocumentColors())
47 color->SetColorValue(mColor);
51 #ifdef DEBUG
52 /* virtual */ void
53 nsHTMLStyleSheet::HTMLColorRule::List(FILE* out, int32_t aIndent) const
55 for (int32_t index = aIndent; --index >= 0; ) fputs(" ", out);
56 fputs("[html color rule] {}\n", out);
58 #endif
61 NS_IMPL_ISUPPORTS1(nsHTMLStyleSheet::GenericTableRule, nsIStyleRule)
63 #ifdef DEBUG
64 /* virtual */ void
65 nsHTMLStyleSheet::GenericTableRule::List(FILE* out, int32_t aIndent) const
67 for (int32_t index = aIndent; --index >= 0; ) fputs(" ", out);
68 fputs("[generic table rule] {}\n", out);
70 #endif
72 /* virtual */ void
73 nsHTMLStyleSheet::TableTHRule::MapRuleInfoInto(nsRuleData* aRuleData)
75 if (aRuleData->mSIDs & NS_STYLE_INHERIT_BIT(Text)) {
76 nsCSSValue* textAlign = aRuleData->ValueForTextAlign();
77 if (textAlign->GetUnit() == eCSSUnit_Null) {
78 textAlign->SetIntValue(NS_STYLE_TEXT_ALIGN_MOZ_CENTER_OR_INHERIT,
79 eCSSUnit_Enumerated);
84 /* virtual */ void
85 nsHTMLStyleSheet::TableQuirkColorRule::MapRuleInfoInto(nsRuleData* aRuleData)
87 if (aRuleData->mSIDs & NS_STYLE_INHERIT_BIT(Color)) {
88 nsCSSValue* color = aRuleData->ValueForColor();
89 // We do not check UseDocumentColors() here, because we want to
90 // use the body color no matter what.
91 if (color->GetUnit() == eCSSUnit_Null)
92 color->SetIntValue(NS_STYLE_COLOR_INHERIT_FROM_BODY,
93 eCSSUnit_Enumerated);
97 // -----------------------------------------------------------
99 struct MappedAttrTableEntry : public PLDHashEntryHdr {
100 nsMappedAttributes *mAttributes;
103 static PLDHashNumber
104 MappedAttrTable_HashKey(PLDHashTable *table, const void *key)
106 nsMappedAttributes *attributes =
107 static_cast<nsMappedAttributes*>(const_cast<void*>(key));
109 return attributes->HashValue();
112 static void
113 MappedAttrTable_ClearEntry(PLDHashTable *table, PLDHashEntryHdr *hdr)
115 MappedAttrTableEntry *entry = static_cast<MappedAttrTableEntry*>(hdr);
117 entry->mAttributes->DropStyleSheetReference();
118 memset(entry, 0, sizeof(MappedAttrTableEntry));
121 static bool
122 MappedAttrTable_MatchEntry(PLDHashTable *table, const PLDHashEntryHdr *hdr,
123 const void *key)
125 nsMappedAttributes *attributes =
126 static_cast<nsMappedAttributes*>(const_cast<void*>(key));
127 const MappedAttrTableEntry *entry =
128 static_cast<const MappedAttrTableEntry*>(hdr);
130 return attributes->Equals(entry->mAttributes);
133 static PLDHashTableOps MappedAttrTable_Ops = {
134 PL_DHashAllocTable,
135 PL_DHashFreeTable,
136 MappedAttrTable_HashKey,
137 MappedAttrTable_MatchEntry,
138 PL_DHashMoveEntryStub,
139 MappedAttrTable_ClearEntry,
140 PL_DHashFinalizeStub,
141 NULL
144 // -----------------------------------------------------------
146 nsHTMLStyleSheet::nsHTMLStyleSheet(nsIURI* aURL, nsIDocument* aDocument)
147 : mURL(aURL)
148 , mDocument(aDocument)
149 , mTableQuirkColorRule(new TableQuirkColorRule())
150 , mTableTHRule(new TableTHRule())
152 MOZ_ASSERT(aURL);
153 MOZ_ASSERT(aDocument);
154 mMappedAttrTable.ops = nullptr;
157 nsHTMLStyleSheet::~nsHTMLStyleSheet()
159 if (mMappedAttrTable.ops)
160 PL_DHashTableFinish(&mMappedAttrTable);
163 NS_IMPL_ISUPPORTS2(nsHTMLStyleSheet, nsIStyleSheet, nsIStyleRuleProcessor)
165 /* virtual */ void
166 nsHTMLStyleSheet::RulesMatching(ElementRuleProcessorData* aData)
168 nsRuleWalker *ruleWalker = aData->mRuleWalker;
169 if (aData->mElement->IsHTML() && !ruleWalker->AuthorStyleDisabled()) {
170 nsIAtom* tag = aData->mElement->Tag();
172 // if we have anchor colors, check if this is an anchor with an href
173 if (tag == nsGkAtoms::a) {
174 if (mLinkRule || mVisitedRule || mActiveRule) {
175 nsEventStates state = nsCSSRuleProcessor::GetContentStateForVisitedHandling(
176 aData->mElement,
177 aData->mTreeMatchContext,
178 aData->mTreeMatchContext.VisitedHandling(),
179 // If the node being matched is a link,
180 // it's the relevant link.
181 nsCSSRuleProcessor::IsLink(aData->mElement));
182 if (mLinkRule && state.HasState(NS_EVENT_STATE_UNVISITED)) {
183 ruleWalker->Forward(mLinkRule);
184 aData->mTreeMatchContext.SetHaveRelevantLink();
186 else if (mVisitedRule && state.HasState(NS_EVENT_STATE_VISITED)) {
187 ruleWalker->Forward(mVisitedRule);
188 aData->mTreeMatchContext.SetHaveRelevantLink();
191 // No need to add to the active rule if it's not a link
192 if (mActiveRule && nsCSSRuleProcessor::IsLink(aData->mElement) &&
193 state.HasState(NS_EVENT_STATE_ACTIVE)) {
194 ruleWalker->Forward(mActiveRule);
196 } // end link/visited/active rules
197 } // end A tag
198 // add the rule to handle text-align for a <th>
199 else if (tag == nsGkAtoms::th) {
200 ruleWalker->Forward(mTableTHRule);
202 else if (tag == nsGkAtoms::table) {
203 if (aData->mTreeMatchContext.mCompatMode == eCompatibility_NavQuirks) {
204 ruleWalker->Forward(mTableQuirkColorRule);
207 } // end html element
209 // just get the style rules from the content. For SVG we do this even if
210 // author style is disabled, because SVG presentational hints aren't
211 // considered style.
212 if (!ruleWalker->AuthorStyleDisabled() || aData->mElement->IsSVG()) {
213 aData->mElement->WalkContentStyleRules(ruleWalker);
217 // Test if style is dependent on content state
218 /* virtual */ nsRestyleHint
219 nsHTMLStyleSheet::HasStateDependentStyle(StateRuleProcessorData* aData)
221 if (aData->mElement->IsHTML(nsGkAtoms::a) &&
222 nsCSSRuleProcessor::IsLink(aData->mElement) &&
223 ((mActiveRule && aData->mStateMask.HasState(NS_EVENT_STATE_ACTIVE)) ||
224 (mLinkRule && aData->mStateMask.HasState(NS_EVENT_STATE_VISITED)) ||
225 (mVisitedRule && aData->mStateMask.HasState(NS_EVENT_STATE_VISITED)))) {
226 return eRestyle_Self;
229 return nsRestyleHint(0);
232 /* virtual */ bool
233 nsHTMLStyleSheet::HasDocumentStateDependentStyle(StateRuleProcessorData* aData)
235 return false;
238 /* virtual */ nsRestyleHint
239 nsHTMLStyleSheet::HasAttributeDependentStyle(AttributeRuleProcessorData* aData)
241 // Do nothing on before-change checks
242 if (!aData->mAttrHasChanged) {
243 return nsRestyleHint(0);
246 // Note: no need to worry about whether some states changed with this
247 // attribute here, because we handle that under HasStateDependentStyle() as
248 // needed.
250 // Result is true for |href| changes on HTML links if we have link rules.
251 Element *element = aData->mElement;
252 if (aData->mAttribute == nsGkAtoms::href &&
253 (mLinkRule || mVisitedRule || mActiveRule) &&
254 element->IsHTML(nsGkAtoms::a)) {
255 return eRestyle_Self;
258 // Don't worry about the mDocumentColorRule since it only applies
259 // to descendants of body, when we're already reresolving.
261 // Handle the content style rules.
262 if (element->IsAttributeMapped(aData->mAttribute)) {
263 // cellpadding on tables is special and requires reresolving all
264 // the cells in the table
265 if (aData->mAttribute == nsGkAtoms::cellpadding &&
266 element->IsHTML(nsGkAtoms::table)) {
267 return eRestyle_Subtree;
269 return eRestyle_Self;
272 return nsRestyleHint(0);
275 /* virtual */ bool
276 nsHTMLStyleSheet::MediumFeaturesChanged(nsPresContext* aPresContext)
278 return false;
281 /* virtual */ size_t
282 nsHTMLStyleSheet::SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf) const
284 return 0; // nsHTMLStyleSheets are charged to the DOM, not layout
287 /* virtual */ size_t
288 nsHTMLStyleSheet::SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const
290 return 0; // nsHTMLStyleSheets are charged to the DOM, not layout
293 /* virtual */ void
294 nsHTMLStyleSheet::RulesMatching(PseudoElementRuleProcessorData* aData)
298 /* virtual */ void
299 nsHTMLStyleSheet::RulesMatching(AnonBoxRuleProcessorData* aData)
303 #ifdef MOZ_XUL
304 /* virtual */ void
305 nsHTMLStyleSheet::RulesMatching(XULTreeRuleProcessorData* aData)
308 #endif
310 // nsIStyleSheet api
311 /* virtual */ nsIURI*
312 nsHTMLStyleSheet::GetSheetURI() const
314 return mURL;
317 /* virtual */ nsIURI*
318 nsHTMLStyleSheet::GetBaseURI() const
320 return mURL;
323 /* virtual */ void
324 nsHTMLStyleSheet::GetTitle(nsString& aTitle) const
326 aTitle.Truncate();
329 /* virtual */ void
330 nsHTMLStyleSheet::GetType(nsString& aType) const
332 aType.AssignLiteral("text/html");
335 /* virtual */ bool
336 nsHTMLStyleSheet::HasRules() const
338 return true; // We have rules at all reasonable times
341 /* virtual */ bool
342 nsHTMLStyleSheet::IsApplicable() const
344 return true;
347 /* virtual */ void
348 nsHTMLStyleSheet::SetEnabled(bool aEnabled)
349 { // these can't be disabled
352 /* virtual */ bool
353 nsHTMLStyleSheet::IsComplete() const
355 return true;
358 /* virtual */ void
359 nsHTMLStyleSheet::SetComplete()
363 /* virtual */ nsIStyleSheet*
364 nsHTMLStyleSheet::GetParentSheet() const
366 return nullptr;
369 /* virtual */ nsIDocument*
370 nsHTMLStyleSheet::GetOwningDocument() const
372 return mDocument;
375 /* virtual */ void
376 nsHTMLStyleSheet::SetOwningDocument(nsIDocument* aDocument)
378 mDocument = aDocument; // not refcounted
381 void
382 nsHTMLStyleSheet::Reset(nsIURI* aURL)
384 mURL = aURL;
386 mLinkRule = nullptr;
387 mVisitedRule = nullptr;
388 mActiveRule = nullptr;
390 if (mMappedAttrTable.ops) {
391 PL_DHashTableFinish(&mMappedAttrTable);
392 mMappedAttrTable.ops = nullptr;
396 nsresult
397 nsHTMLStyleSheet::ImplLinkColorSetter(nsRefPtr<HTMLColorRule>& aRule, nscolor aColor)
399 if (aRule && aRule->mColor == aColor) {
400 return NS_OK;
403 aRule = new HTMLColorRule();
404 if (!aRule)
405 return NS_ERROR_OUT_OF_MEMORY;
407 aRule->mColor = aColor;
408 // Now make sure we restyle any links that might need it. This
409 // shouldn't happen often, so just rebuilding everything is ok.
410 if (mDocument && mDocument->GetShell()) {
411 Element* root = mDocument->GetRootElement();
412 if (root) {
413 mDocument->GetShell()->FrameConstructor()->
414 PostRestyleEvent(root, eRestyle_Subtree, NS_STYLE_HINT_NONE);
417 return NS_OK;
420 nsresult
421 nsHTMLStyleSheet::SetLinkColor(nscolor aColor)
423 return ImplLinkColorSetter(mLinkRule, aColor);
427 nsresult
428 nsHTMLStyleSheet::SetActiveLinkColor(nscolor aColor)
430 return ImplLinkColorSetter(mActiveRule, aColor);
433 nsresult
434 nsHTMLStyleSheet::SetVisitedLinkColor(nscolor aColor)
436 return ImplLinkColorSetter(mVisitedRule, aColor);
439 already_AddRefed<nsMappedAttributes>
440 nsHTMLStyleSheet::UniqueMappedAttributes(nsMappedAttributes* aMapped)
442 if (!mMappedAttrTable.ops) {
443 bool res = PL_DHashTableInit(&mMappedAttrTable, &MappedAttrTable_Ops,
444 nullptr, sizeof(MappedAttrTableEntry), 16);
445 if (!res) {
446 mMappedAttrTable.ops = nullptr;
447 return nullptr;
450 MappedAttrTableEntry *entry = static_cast<MappedAttrTableEntry*>
451 (PL_DHashTableOperate(&mMappedAttrTable, aMapped, PL_DHASH_ADD));
452 if (!entry)
453 return nullptr;
454 if (!entry->mAttributes) {
455 // We added a new entry to the hashtable, so we have a new unique set.
456 entry->mAttributes = aMapped;
458 nsRefPtr<nsMappedAttributes> ret = entry->mAttributes;
459 return ret.forget();
462 void
463 nsHTMLStyleSheet::DropMappedAttributes(nsMappedAttributes* aMapped)
465 NS_ENSURE_TRUE_VOID(aMapped);
467 NS_ASSERTION(mMappedAttrTable.ops, "table uninitialized");
468 #ifdef DEBUG
469 uint32_t entryCount = mMappedAttrTable.entryCount - 1;
470 #endif
472 PL_DHashTableOperate(&mMappedAttrTable, aMapped, PL_DHASH_REMOVE);
474 NS_ASSERTION(entryCount == mMappedAttrTable.entryCount, "not removed");
477 #ifdef DEBUG
478 /* virtual */ void
479 nsHTMLStyleSheet::List(FILE* out, int32_t aIndent) const
481 // Indent
482 for (int32_t index = aIndent; --index >= 0; ) fputs(" ", out);
484 fputs("HTML Style Sheet: ", out);
485 nsAutoCString urlSpec;
486 mURL->GetSpec(urlSpec);
487 if (!urlSpec.IsEmpty()) {
488 fputs(urlSpec.get(), out);
490 fputs("\n", out);
492 #endif
494 static size_t
495 SizeOfAttributesEntryExcludingThis(PLDHashEntryHdr* aEntry,
496 nsMallocSizeOfFun aMallocSizeOf,
497 void* aArg)
499 NS_PRECONDITION(aEntry, "The entry should not be null!");
501 MappedAttrTableEntry* entry = static_cast<MappedAttrTableEntry*>(aEntry);
502 NS_ASSERTION(entry->mAttributes, "entry->mAttributes should not be null!");
503 return entry->mAttributes->SizeOfIncludingThis(aMallocSizeOf);
506 size_t
507 nsHTMLStyleSheet::DOMSizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const
509 size_t n = aMallocSizeOf(this);
511 if (mMappedAttrTable.ops) {
512 n += PL_DHashTableSizeOfExcludingThis(&mMappedAttrTable,
513 SizeOfAttributesEntryExcludingThis,
514 aMallocSizeOf);
517 // Measurement of the following members may be added later if DMD finds it is
518 // worthwhile:
519 // - mURL
520 // - mLinkRule
521 // - mVisitedRule
522 // - mActiveRule
523 // - mTableQuirkColorRule
524 // - mTableTHRule
526 // The following members are not measured:
527 // - mDocument, because it's non-owning
529 return n;