2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* David Hyatt <hyatt@netscape.com>
|
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
/* the interface (to internal code) for retrieving computed style data */
|
|
|
|
|
|
|
|
#ifndef _nsStyleContext_h_
|
|
|
|
#define _nsStyleContext_h_
|
|
|
|
|
|
|
|
#include "nsRuleNode.h"
|
|
|
|
#include "nsIAtom.h"
|
2009-12-10 23:37:40 -08:00
|
|
|
#include "nsCSSPseudoElements.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
class nsPresContext;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An nsStyleContext represents the computed style data for an element.
|
|
|
|
* The computed style data are stored in a set of structs (see
|
|
|
|
* nsStyleStruct.h) that are cached either on the style context or in
|
|
|
|
* the rule tree (see nsRuleNode.h for a description of this caching and
|
|
|
|
* how the cached structs are shared).
|
|
|
|
*
|
|
|
|
* Since the data in |nsIStyleRule|s and |nsRuleNode|s are immutable
|
|
|
|
* (with a few exceptions, like system color changes), the data in an
|
|
|
|
* nsStyleContext are also immutable (with the additional exception of
|
|
|
|
* GetUniqueStyleData). When style data change,
|
|
|
|
* nsFrameManager::ReResolveStyleContext creates a new style context.
|
|
|
|
*
|
|
|
|
* Style contexts are reference counted. References are generally held
|
|
|
|
* by:
|
|
|
|
* 1. the |nsIFrame|s that are using the style context and
|
|
|
|
* 2. any *child* style contexts (this might be the reverse of
|
|
|
|
* expectation, but it makes sense in this case)
|
|
|
|
* Style contexts participate in the mark phase of rule node garbage
|
|
|
|
* collection.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class nsStyleContext
|
|
|
|
{
|
|
|
|
public:
|
2009-12-10 23:37:40 -08:00
|
|
|
nsStyleContext(nsStyleContext* aParent, nsIAtom* aPseudoTag,
|
|
|
|
nsCSSPseudoElements::Type aPseudoType,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsRuleNode* aRuleNode, nsPresContext* aPresContext) NS_HIDDEN;
|
|
|
|
~nsStyleContext() NS_HIDDEN;
|
|
|
|
|
|
|
|
NS_HIDDEN_(void*) operator new(size_t sz, nsPresContext* aPresContext) CPP_THROW_NEW;
|
|
|
|
NS_HIDDEN_(void) Destroy();
|
|
|
|
|
|
|
|
nsrefcnt AddRef() {
|
2008-12-27 17:58:14 -08:00
|
|
|
if (mRefCnt == PR_UINT32_MAX) {
|
|
|
|
NS_WARNING("refcount overflow, leaking object");
|
|
|
|
return mRefCnt;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
++mRefCnt;
|
|
|
|
NS_LOG_ADDREF(this, mRefCnt, "nsStyleContext", sizeof(nsStyleContext));
|
|
|
|
return mRefCnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsrefcnt Release() {
|
2008-12-27 17:58:14 -08:00
|
|
|
if (mRefCnt == PR_UINT32_MAX) {
|
|
|
|
NS_WARNING("refcount overflow, leaking object");
|
|
|
|
return mRefCnt;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
--mRefCnt;
|
|
|
|
NS_LOG_RELEASE(this, mRefCnt, "nsStyleContext");
|
|
|
|
if (mRefCnt == 0) {
|
|
|
|
Destroy();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return mRefCnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPresContext* PresContext() const { return mRuleNode->GetPresContext(); }
|
|
|
|
|
|
|
|
nsStyleContext* GetParent() const { return mParent; }
|
|
|
|
|
2009-10-29 14:17:56 -07:00
|
|
|
nsIAtom* GetPseudo() const { return mPseudoTag; }
|
2009-12-10 23:37:40 -08:00
|
|
|
nsCSSPseudoElements::Type GetPseudoType() const {
|
|
|
|
return static_cast<nsCSSPseudoElements::Type>(mBits >>
|
|
|
|
NS_STYLE_CONTEXT_TYPE_SHIFT);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-02 18:58:26 -07:00
|
|
|
// Find, if it already exists *and is easily findable* (i.e., near the
|
|
|
|
// start of the child list), a style context whose:
|
|
|
|
// * GetPseudo() matches aPseudoTag
|
|
|
|
// * GetRuleNode() matches aRules
|
|
|
|
// * !GetStyleIfVisited() == !aRulesIfVisited, and, if they're
|
|
|
|
// non-null, GetStyleIfVisited()->GetRuleNode() == aRulesIfVisited
|
|
|
|
// * RelevantLinkVisited() == aRelevantLinkVisited
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_HIDDEN_(already_AddRefed<nsStyleContext>)
|
2010-04-02 18:58:26 -07:00
|
|
|
FindChildWithRules(const nsIAtom* aPseudoTag, nsRuleNode* aRules,
|
|
|
|
nsRuleNode* aRulesIfVisited,
|
|
|
|
PRBool aRelevantLinkVisited);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-08-10 15:52:29 -07:00
|
|
|
// Does this style context or any of its ancestors have text
|
|
|
|
// decorations?
|
|
|
|
PRBool HasTextDecorations() const
|
|
|
|
{ return !!(mBits & NS_STYLE_HAS_TEXT_DECORATIONS); }
|
|
|
|
|
|
|
|
// Does this style context represent the style for a pseudo-element or
|
|
|
|
// inherit data from such a style context? Whether this returns true
|
|
|
|
// is equivalent to whether it or any of its ancestors returns
|
2009-10-29 14:17:56 -07:00
|
|
|
// non-null for GetPseudo.
|
2009-08-10 15:52:29 -07:00
|
|
|
PRBool HasPseudoElementData() const
|
|
|
|
{ return !!(mBits & NS_STYLE_HAS_PSEUDO_ELEMENT_DATA); }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-02 18:58:25 -07:00
|
|
|
// Is the only link whose visitedness is allowed to influence the
|
|
|
|
// style of the node this style context is for (which is that element
|
|
|
|
// or its nearest ancestor that is a link) visited?
|
|
|
|
PRBool RelevantLinkVisited() const
|
|
|
|
{ return !!(mBits & NS_STYLE_RELEVANT_LINK_VISITED); }
|
|
|
|
|
|
|
|
// Return the style context whose style data should be used for the R,
|
|
|
|
// G, and B components of color, background-color, and border-*-color
|
|
|
|
// if RelevantLinkIsVisited().
|
|
|
|
//
|
|
|
|
// GetPseudo() and GetPseudoType() on this style context return the
|
|
|
|
// same as on |this|, and its depth in the tree (number of GetParent()
|
|
|
|
// calls until null is returned) is the same as |this|, since its
|
|
|
|
// parent is either |this|'s parent or |this|'s parent's
|
|
|
|
// style-if-visited.
|
|
|
|
//
|
|
|
|
// Structs on this context should never be examined without also
|
|
|
|
// examining the corresponding struct on |this|. Doing so will likely
|
|
|
|
// both (1) lead to a privacy leak and (2) lead to dynamic change bugs
|
|
|
|
// related to the Peek code in nsStyleContext::CalcStyleDifference.
|
|
|
|
nsStyleContext* GetStyleIfVisited()
|
|
|
|
{ return mStyleIfVisited; }
|
|
|
|
|
|
|
|
// To be called only from nsStyleSet.
|
|
|
|
void SetStyleIfVisited(already_AddRefed<nsStyleContext> aStyleIfVisited)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mStyleIfVisited, "should only be set once");
|
|
|
|
mStyleIfVisited = aStyleIfVisited;
|
|
|
|
|
|
|
|
NS_ASSERTION(GetStyleIfVisited()->GetPseudo() == GetPseudo(),
|
|
|
|
"pseudo tag mismatch");
|
|
|
|
if (GetParent() && GetParent()->GetStyleIfVisited()) {
|
|
|
|
NS_ASSERTION(GetStyleIfVisited()->GetParent() ==
|
|
|
|
GetParent()->GetStyleIfVisited() ||
|
|
|
|
GetStyleIfVisited()->GetParent() == GetParent(),
|
|
|
|
"parent mismatch");
|
|
|
|
} else {
|
|
|
|
NS_ASSERTION(GetStyleIfVisited()->GetParent() == GetParent(),
|
|
|
|
"parent mismatch");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-02 15:27:21 -08:00
|
|
|
// Tell this style context to cache aStruct as the struct for aSID
|
2008-01-10 12:56:49 -08:00
|
|
|
NS_HIDDEN_(void) SetStyle(nsStyleStructID aSID, void* aStruct);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-02 15:27:21 -08:00
|
|
|
// Setters for inherit structs only, since rulenode only sets those eagerly.
|
|
|
|
#define STYLE_STRUCT_INHERITED(name_, checkdata_cb_, ctor_args_) \
|
|
|
|
void SetStyle##name_ (nsStyle##name_ * aStruct) { \
|
|
|
|
NS_ASSERTION(!mCachedInheritedData.m##name_##Data || \
|
|
|
|
(mBits & \
|
|
|
|
nsCachedStyleData::GetBitForSID(eStyleStruct_##name_)), \
|
|
|
|
"Going to leak styledata"); \
|
|
|
|
mCachedInheritedData.m##name_##Data = aStruct; \
|
|
|
|
}
|
|
|
|
#define STYLE_STRUCT_RESET(name_, checkdata_cb_, ctor_args_) /* nothing */
|
|
|
|
#include "nsStyleStructList.h"
|
|
|
|
#undef STYLE_STRUCT_RESET
|
|
|
|
#undef STYLE_STRUCT_INHERITED
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsRuleNode* GetRuleNode() { return mRuleNode; }
|
|
|
|
void AddStyleBit(const PRUint32& aBit) { mBits |= aBit; }
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mark this style context's rule node (and its ancestors) to prevent
|
|
|
|
* it from being garbage collected.
|
|
|
|
*/
|
|
|
|
NS_HIDDEN_(void) Mark();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the style data for a style struct. This is the most important
|
|
|
|
* member function of nsIStyleContext. It fills in a const pointer
|
|
|
|
* to a style data struct that is appropriate for the style context's
|
|
|
|
* frame. This struct may be shared with other contexts (either in
|
|
|
|
* the rule tree or the style context tree), so it should not be
|
|
|
|
* modified.
|
|
|
|
*
|
|
|
|
* This function will NOT return null (even when out of memory) when
|
|
|
|
* given a valid style struct ID, so the result does not need to be
|
|
|
|
* null-checked.
|
|
|
|
*
|
|
|
|
* The typesafe functions below are preferred to the use of this
|
2010-03-02 15:27:21 -08:00
|
|
|
* function, bothe because they're easier to read and because they're
|
|
|
|
* faster.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2008-01-10 12:56:49 -08:00
|
|
|
NS_HIDDEN_(const void*) NS_FASTCALL GetStyleData(nsStyleStructID aSID);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Define typesafe getter functions for each style struct by
|
|
|
|
* preprocessing the list of style structs. These functions are the
|
|
|
|
* preferred way to get style data. The macro creates functions like:
|
|
|
|
* const nsStyleBorder* GetStyleBorder();
|
|
|
|
* const nsStyleColor* GetStyleColor();
|
|
|
|
*/
|
2010-03-02 15:27:21 -08:00
|
|
|
#define STYLE_STRUCT(name_, checkdata_cb_, ctor_args_) \
|
|
|
|
const nsStyle##name_ * GetStyle##name_() { \
|
|
|
|
return DoGetStyle##name_(PR_TRUE); \
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsStyleStructList.h"
|
|
|
|
#undef STYLE_STRUCT
|
|
|
|
|
2010-03-02 15:27:21 -08:00
|
|
|
/**
|
|
|
|
* PeekStyle* is like GetStyle* but doesn't trigger style
|
|
|
|
* computation if the data is not cached on either the style context
|
|
|
|
* or the rule node.
|
|
|
|
*
|
|
|
|
* Perhaps this shouldn't be a public nsStyleContext API.
|
|
|
|
*/
|
|
|
|
#define STYLE_STRUCT(name_, checkdata_cb_, ctor_args_) \
|
|
|
|
const nsStyle##name_ * PeekStyle##name_() { \
|
|
|
|
return DoGetStyle##name_(PR_FALSE); \
|
|
|
|
}
|
|
|
|
#include "nsStyleStructList.h"
|
|
|
|
#undef STYLE_STRUCT
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-01-10 12:56:49 -08:00
|
|
|
NS_HIDDEN_(void*) GetUniqueStyleData(const nsStyleStructID& aSID);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_HIDDEN_(nsChangeHint) CalcStyleDifference(nsStyleContext* aOther);
|
|
|
|
|
2010-04-02 18:58:25 -07:00
|
|
|
/**
|
|
|
|
* Get a color that depends on link-visitedness using this and
|
|
|
|
* this->GetStyleIfVisited().
|
|
|
|
*
|
|
|
|
* aProperty must be a color-valued property that nsStyleAnimation
|
|
|
|
* knows how to extract. It must also be a property that we know to
|
|
|
|
* do change handling for in nsStyleContext::CalcDifference.
|
2010-04-02 18:58:26 -07:00
|
|
|
*
|
|
|
|
* Note that if aProperty is eCSSProperty_border_*_color, this
|
|
|
|
* function handles -moz-use-text-color.
|
2010-04-02 18:58:25 -07:00
|
|
|
*/
|
|
|
|
NS_HIDDEN_(nscolor) GetVisitedDependentColor(nsCSSProperty aProperty);
|
|
|
|
|
2010-04-02 18:58:26 -07:00
|
|
|
/**
|
|
|
|
* aColors should be a two element array of nscolor in which the first
|
|
|
|
* color is the unvisited color and the second is the visited color.
|
|
|
|
*
|
|
|
|
* Combine the R, G, and B components of whichever of aColors should
|
|
|
|
* be used based on aLinkIsVisited with the A component of aColors[0].
|
|
|
|
*/
|
|
|
|
static nscolor CombineVisitedColors(nscolor *aColors,
|
|
|
|
PRBool aLinkIsVisited);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
NS_HIDDEN_(void) List(FILE* out, PRInt32 aIndent);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
protected:
|
|
|
|
NS_HIDDEN_(void) AddChild(nsStyleContext* aChild);
|
|
|
|
NS_HIDDEN_(void) RemoveChild(nsStyleContext* aChild);
|
|
|
|
|
|
|
|
NS_HIDDEN_(void) ApplyStyleFixups(nsPresContext* aPresContext);
|
|
|
|
|
2010-03-02 15:27:21 -08:00
|
|
|
// Helper function that GetStyleData and GetUniqueStyleData use. Only
|
|
|
|
// returns the structs we cache ourselves; never consults the ruletree.
|
|
|
|
inline const void* GetCachedStyleData(nsStyleStructID aSID);
|
|
|
|
|
|
|
|
// Helper functions for GetStyle* and PeekStyle*
|
|
|
|
#define STYLE_STRUCT_INHERITED(name_, checkdata_cb_, ctor_args_) \
|
|
|
|
const nsStyle##name_ * DoGetStyle##name_(PRBool aComputeData) { \
|
|
|
|
const nsStyle##name_ * cachedData = \
|
|
|
|
mCachedInheritedData.m##name_##Data; \
|
|
|
|
if (cachedData) /* Have it cached already, yay */ \
|
|
|
|
return cachedData; \
|
|
|
|
/* Have the rulenode deal */ \
|
|
|
|
return mRuleNode->GetStyle##name_(this, aComputeData); \
|
|
|
|
}
|
|
|
|
#define STYLE_STRUCT_RESET(name_, checkdata_cb_, ctor_args_) \
|
|
|
|
const nsStyle##name_ * DoGetStyle##name_(PRBool aComputeData) { \
|
|
|
|
const nsStyle##name_ * cachedData = \
|
|
|
|
mCachedResetData ? mCachedResetData->m##name_##Data : nsnull; \
|
|
|
|
if (cachedData) /* Have it cached already, yay */ \
|
|
|
|
return cachedData; \
|
|
|
|
/* Have the rulenode deal */ \
|
|
|
|
return mRuleNode->GetStyle##name_(this, aComputeData); \
|
|
|
|
}
|
|
|
|
#include "nsStyleStructList.h"
|
|
|
|
#undef STYLE_STRUCT_RESET
|
|
|
|
#undef STYLE_STRUCT_INHERITED
|
|
|
|
|
2010-04-02 18:58:25 -07:00
|
|
|
nsStyleContext* const mParent; // STRONG
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Children are kept in two circularly-linked lists. The list anchor
|
|
|
|
// is not part of the list (null for empty), and we point to the first
|
|
|
|
// child.
|
|
|
|
// mEmptyChild for children whose rule node is the root rule node, and
|
|
|
|
// mChild for other children. The order of children is not
|
|
|
|
// meaningful.
|
|
|
|
nsStyleContext* mChild;
|
|
|
|
nsStyleContext* mEmptyChild;
|
|
|
|
nsStyleContext* mPrevSibling;
|
|
|
|
nsStyleContext* mNextSibling;
|
|
|
|
|
2010-04-02 18:58:25 -07:00
|
|
|
// Style to be used instead for the R, G, and B components of color,
|
|
|
|
// background-color, and border-*-color if the nearest ancestor link
|
|
|
|
// element is visited (see RelevantLinkVisited()).
|
|
|
|
nsRefPtr<nsStyleContext> mStyleIfVisited;
|
|
|
|
|
2009-10-29 14:17:56 -07:00
|
|
|
// If this style context is for a pseudo-element or anonymous box,
|
|
|
|
// the relevant atom.
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIAtom> mPseudoTag;
|
|
|
|
|
|
|
|
// The rule node is the node in the lexicographic tree of rule nodes
|
|
|
|
// (the "rule tree") that indicates which style rules are used to
|
|
|
|
// compute the style data, and in what cascading order. The least
|
|
|
|
// specific rule matched is the one whose rule node is a child of the
|
|
|
|
// root of the rule tree, and the most specific rule matched is the
|
|
|
|
// |mRule| member of |mRuleNode|.
|
|
|
|
nsRuleNode* const mRuleNode;
|
|
|
|
|
2010-03-02 15:27:21 -08:00
|
|
|
// mCachedInheritedData and mCachedResetData point to both structs that
|
|
|
|
// are owned by this style context and structs that are owned by one of
|
|
|
|
// this style context's ancestors (which are indirectly owned since this
|
|
|
|
// style context owns a reference to its parent). If the bit in |mBits|
|
|
|
|
// is set for a struct, that means that the pointer for that struct is
|
2010-03-02 15:27:21 -08:00
|
|
|
// owned by an ancestor or by mRuleNode rather than by this style context.
|
2010-03-02 15:27:21 -08:00
|
|
|
// Since style contexts typically have some inherited data but only sometimes
|
|
|
|
// have reset data, we always allocate the mCachedInheritedData, but only
|
|
|
|
// sometimes allocate the mCachedResetData.
|
|
|
|
nsResetStyleData* mCachedResetData; // Cached reset style data.
|
|
|
|
nsInheritedStyleData mCachedInheritedData; // Cached inherited style data
|
2007-03-22 10:30:00 -07:00
|
|
|
PRUint32 mBits; // Which structs are inherited from the
|
2010-03-02 15:27:21 -08:00
|
|
|
// parent context or owned by mRuleNode.
|
2007-03-22 10:30:00 -07:00
|
|
|
PRUint32 mRefCnt;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_HIDDEN_(already_AddRefed<nsStyleContext>)
|
|
|
|
NS_NewStyleContext(nsStyleContext* aParentContext,
|
|
|
|
nsIAtom* aPseudoTag,
|
2009-12-10 23:37:40 -08:00
|
|
|
nsCSSPseudoElements::Type aPseudoType,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsRuleNode* aRuleNode,
|
|
|
|
nsPresContext* aPresContext);
|
|
|
|
#endif
|