gecko/layout/style/nsHTMLCSSStyleSheet.cpp
L. David Baron c7d7287021 Bug 1221436 patch 1 - Have a pointer back from the css::Declaration for style attributes to the nsHTMLCSSStyleSheet. r=heycam
This is needed for patch 2, since we need a replacement for the
mechanism to get from rules to the style attribute sheet
(nsHTMLCSSStyleSheet) that works instead for declarations rather than
rules.

This pointer will be merged with the owning rule pointer in patch 4, but
we can't do that until after patch 2.
2015-11-09 15:57:16 +08:00

222 lines
6.5 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* style sheet and style rule processor representing style attributes
*/
#include "nsHTMLCSSStyleSheet.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/css/StyleRule.h"
#include "nsIStyleRuleProcessor.h"
#include "nsPresContext.h"
#include "nsRuleWalker.h"
#include "nsRuleProcessorData.h"
#include "mozilla/dom/Element.h"
#include "nsAttrValue.h"
#include "nsAttrValueInlines.h"
#include "RestyleManager.h"
using namespace mozilla;
using namespace mozilla::dom;
namespace {
PLDHashOperator
ClearAttrCache(const nsAString& aKey, MiscContainer*& aValue, void*)
{
// Ideally we'd just call MiscContainer::Evict, but we can't do that since
// we're iterating the hashtable.
MOZ_ASSERT(aValue->mType == nsAttrValue::eCSSStyleRule);
css::StyleRule* styleRule = aValue->mValue.mCSSStyleRule;
styleRule->SetHTMLCSSStyleSheet(nullptr);
css::Declaration* declaration = styleRule->GetDeclaration();
if (declaration) {
declaration->SetHTMLCSSStyleSheet(nullptr);
}
aValue->mValue.mCached = 0;
return PL_DHASH_REMOVE;
}
} // namespace
nsHTMLCSSStyleSheet::nsHTMLCSSStyleSheet()
{
}
nsHTMLCSSStyleSheet::~nsHTMLCSSStyleSheet()
{
// We may go away before all of our cached style attributes do,
// so clean up any that are left.
mCachedStyleAttrs.Enumerate(ClearAttrCache, nullptr);
}
NS_IMPL_ISUPPORTS(nsHTMLCSSStyleSheet, nsIStyleRuleProcessor)
/* virtual */ void
nsHTMLCSSStyleSheet::RulesMatching(ElementRuleProcessorData* aData)
{
ElementRulesMatching(aData->mPresContext, aData->mElement,
aData->mRuleWalker);
}
void
nsHTMLCSSStyleSheet::ElementRulesMatching(nsPresContext* aPresContext,
Element* aElement,
nsRuleWalker* aRuleWalker)
{
// just get the one and only style rule from the content's STYLE attribute
css::StyleRule* rule = aElement->GetInlineStyleRule();
if (rule) {
css::Declaration* declaration = rule->GetDeclaration();
declaration->SetImmutable();
aRuleWalker->Forward(declaration);
}
rule = aElement->GetSMILOverrideStyleRule();
if (rule) {
RestyleManager* restyleManager = aPresContext->RestyleManager();
if (!restyleManager->SkipAnimationRules()) {
// Animation restyle (or non-restyle traversal of rules)
// Now we can walk SMIL overrride style, without triggering transitions.
css::Declaration* declaration = rule->GetDeclaration();
declaration->SetImmutable();
aRuleWalker->Forward(declaration);
}
}
}
void
nsHTMLCSSStyleSheet::PseudoElementRulesMatching(Element* aPseudoElement,
nsCSSPseudoElements::Type
aPseudoType,
nsRuleWalker* aRuleWalker)
{
MOZ_ASSERT(nsCSSPseudoElements::
PseudoElementSupportsStyleAttribute(aPseudoType));
MOZ_ASSERT(aPseudoElement);
// just get the one and only style rule from the content's STYLE attribute
css::StyleRule* rule = aPseudoElement->GetInlineStyleRule();
if (rule) {
css::Declaration* declaration = rule->GetDeclaration();
declaration->SetImmutable();
aRuleWalker->Forward(declaration);
}
}
/* virtual */ void
nsHTMLCSSStyleSheet::RulesMatching(PseudoElementRuleProcessorData* aData)
{
if (nsCSSPseudoElements::PseudoElementSupportsStyleAttribute(aData->mPseudoType) &&
aData->mPseudoElement) {
PseudoElementRulesMatching(aData->mPseudoElement, aData->mPseudoType,
aData->mRuleWalker);
}
}
/* virtual */ void
nsHTMLCSSStyleSheet::RulesMatching(AnonBoxRuleProcessorData* aData)
{
}
#ifdef MOZ_XUL
/* virtual */ void
nsHTMLCSSStyleSheet::RulesMatching(XULTreeRuleProcessorData* aData)
{
}
#endif
// Test if style is dependent on content state
/* virtual */ nsRestyleHint
nsHTMLCSSStyleSheet::HasStateDependentStyle(StateRuleProcessorData* aData)
{
return nsRestyleHint(0);
}
/* virtual */ nsRestyleHint
nsHTMLCSSStyleSheet::HasStateDependentStyle(PseudoElementStateRuleProcessorData* aData)
{
return nsRestyleHint(0);
}
/* virtual */ bool
nsHTMLCSSStyleSheet::HasDocumentStateDependentStyle(StateRuleProcessorData* aData)
{
return false;
}
// Test if style is dependent on attribute
/* virtual */ nsRestyleHint
nsHTMLCSSStyleSheet::HasAttributeDependentStyle(
AttributeRuleProcessorData* aData,
RestyleHintData& aRestyleHintDataResult)
{
// Perhaps should check that it's XUL, SVG, (or HTML) namespace, but
// it doesn't really matter.
if (aData->mAttrHasChanged && aData->mAttribute == nsGkAtoms::style) {
return eRestyle_StyleAttribute;
}
return nsRestyleHint(0);
}
/* virtual */ bool
nsHTMLCSSStyleSheet::MediumFeaturesChanged(nsPresContext* aPresContext)
{
return false;
}
/* virtual */ size_t
nsHTMLCSSStyleSheet::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
{
// The size of mCachedStyleAttrs's mTable member (a PLDHashTable) is
// significant in itself, but more significant is the size of the nsString
// members of the nsStringHashKeys.
size_t n = 0;
n += mCachedStyleAttrs.ShallowSizeOfExcludingThis(aMallocSizeOf);
for (auto iter = mCachedStyleAttrs.ConstIter(); !iter.Done(); iter.Next()) {
// We don't own the MiscContainers (the hash table values) so we don't
// count them. We do care about the size of the nsString members in the
// keys though.
n += iter.Key().SizeOfExcludingThisIfUnshared(aMallocSizeOf);
}
return n;
}
/* virtual */ size_t
nsHTMLCSSStyleSheet::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
{
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
}
void
nsHTMLCSSStyleSheet::CacheStyleAttr(const nsAString& aSerialized,
MiscContainer* aValue)
{
mCachedStyleAttrs.Put(aSerialized, aValue);
}
void
nsHTMLCSSStyleSheet::EvictStyleAttr(const nsAString& aSerialized,
MiscContainer* aValue)
{
#ifdef DEBUG
{
NS_ASSERTION(aValue == mCachedStyleAttrs.Get(aSerialized),
"Cached value does not match?!");
}
#endif
mCachedStyleAttrs.Remove(aSerialized);
}
MiscContainer*
nsHTMLCSSStyleSheet::LookupStyleAttr(const nsAString& aSerialized)
{
return mCachedStyleAttrs.Get(aSerialized);
}