DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Mercurial (1aeaa33a64f9)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

/* the interface (to internal code) for retrieving computed style data */

#ifndef _nsStyleContext_h_
#define _nsStyleContext_h_

#include "mozilla/Assertions.h"
#include "mozilla/RestyleLogging.h"
#include "mozilla/ServoStyleSet.h"
#include "mozilla/ServoUtils.h"
#include "mozilla/StyleComplexColor.h"
#include "nsCSSAnonBoxes.h"
#ifdef MOZ_OLD_STYLE
#include "nsStyleSet.h"
#endif

class nsAtom;
class nsPresContext;

namespace mozilla {
enum class CSSPseudoElementType : uint8_t;
class GeckoStyleContext;
class ServoStyleContext;
}  // namespace mozilla

extern "C" {
void Servo_StyleContext_AddRef(const mozilla::ServoStyleContext* aContext);
void Servo_StyleContext_Release(const mozilla::ServoStyleContext* aContext);
}

/**
 * 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,
 * ElementRestyler::Restyle 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)
 */

class nsStyleContext {
 public:
#ifdef MOZ_STYLO
  bool IsGecko() const { return !IsServo(); }
  bool IsServo() const { return (mBits & NS_STYLE_CONTEXT_IS_GECKO) == 0; }
#else
  bool IsGecko() const { return true; }
  bool IsServo() const { return false; }
#endif
  MOZ_DECL_STYLO_CONVERT_METHODS(mozilla::GeckoStyleContext,
                                 mozilla::ServoStyleContext);

  // These two methods are for use by ArenaRefPtr.
  static mozilla::ArenaObjectID ArenaObjectID() {
    return mozilla::eArenaObjectID_GeckoStyleContext;
  }
  nsIPresShell* Arena();

  inline void AddRef();
  inline void Release();

#ifdef DEBUG
  void FrameAddRef();
  void FrameRelease();
#endif

  inline nsPresContext* PresContext() const;

  nsAtom* GetPseudo() const { return mPseudoTag; }
  mozilla::CSSPseudoElementType GetPseudoType() const {
    return static_cast<mozilla::CSSPseudoElementType>(
        mBits >> NS_STYLE_CONTEXT_TYPE_SHIFT);
  }

  bool IsInheritingAnonBox() const {
    return GetPseudoType() == mozilla::CSSPseudoElementType::InheritingAnonBox;
  }

  bool IsNonInheritingAnonBox() const {
    return GetPseudoType() ==
           mozilla::CSSPseudoElementType::NonInheritingAnonBox;
  }

  // This function is rather slow; you probably don't want to use it outside
  // asserts unless you have to.  We _could_ add a new CSSPseudoElementType for
  // wrapper anon boxes, but that adds a bunch of complexity everywhere we
  // resolve anonymous box styles...
  bool IsWrapperAnonBox() const {
    return nsCSSAnonBoxes::IsWrapperAnonBox(GetPseudo());
  }

  bool IsAnonBox() const {
    return IsInheritingAnonBox() || IsNonInheritingAnonBox();
  }

  bool IsPseudoElement() const { return mPseudoTag && !IsAnonBox(); }

  // Does this style context or any of its ancestors have text
  // decoration lines?
  // Differs from nsStyleTextReset::HasTextDecorationLines, which tests
  // only the data for a single context.
  bool HasTextDecorationLines() const {
    return !!(mBits & NS_STYLE_HAS_TEXT_DECORATION_LINES);
  }

  // Whether any line break inside should be suppressed? If this returns
  // true, the line should not be broken inside, which means inlines act
  // as if nowrap is set, <br> is suppressed, and blocks are inlinized.
  // This bit is propogated to all children of line partitipants. It is
  // currently used by ruby to make its content frames unbreakable.
  // NOTE: for nsTextFrame, use nsTextFrame::ShouldSuppressLineBreak()
  // instead of this method.
  bool ShouldSuppressLineBreak() const {
    return !!(mBits & NS_STYLE_SUPPRESS_LINEBREAK);
  }

  // Does this style context or any of its ancestors have display:none set?
  bool IsInDisplayNoneSubtree() const {
    return !!(mBits & NS_STYLE_IN_DISPLAY_NONE_SUBTREE);
  }

  // Is this horizontal-in-vertical (tate-chu-yoko) text? This flag is
  // only set on style contexts whose pseudo is nsCSSAnonBoxes::mozText.
  bool IsTextCombined() const { return !!(mBits & NS_STYLE_IS_TEXT_COMBINED); }

  // 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
  // non-null for IsPseudoElement().
  bool HasPseudoElementData() const {
    return !!(mBits & NS_STYLE_HAS_PSEUDO_ELEMENT_DATA);
  }

  bool HasChildThatUsesResetStyle() const {
    return mBits & NS_STYLE_HAS_CHILD_THAT_USES_RESET_STYLE;
  }

  // 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?
  bool RelevantLinkVisited() const {
    return !!(mBits & NS_STYLE_RELEVANT_LINK_VISITED);
  }

  // Is this a style context for a link?
  inline bool IsLinkContext() const;

  // Is this style context the GetStyleIfVisited() for some other style
  // context?
  bool IsStyleIfVisited() const {
    return !!(mBits & NS_STYLE_IS_STYLE_IF_VISITED);
  }

  // Tells this style context that it should return true from
  // IsStyleIfVisited.
  void SetIsStyleIfVisited() { mBits |= NS_STYLE_IS_STYLE_IF_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.
  inline nsStyleContext* GetStyleIfVisited() const;

  // Does any descendant of this style context have any style values
  // that were computed based on this style context's ancestors?
  bool HasChildThatUsesGrandancestorStyle() const {
    return !!(mBits & NS_STYLE_CHILD_USES_GRANDANCESTOR_STYLE);
  }

  // Is this style context shared with a sibling or cousin?
  // (See nsStyleSet::GetContext.)
  bool IsShared() const { return !!(mBits & NS_STYLE_IS_SHARED); }

  /**
   * Returns whether this style context has cached style data for a
   * given style struct and it does NOT own that struct.  This can
   * happen because it was inherited from the parent style context, or
   * because it was stored conditionally on the rule node.
   */
  bool HasCachedDependentStyleData(nsStyleStructID aSID) {
    return mBits & GetBitForSID(aSID);
  }

#ifdef MOZ_OLD_STYLE
  inline nsRuleNode* RuleNode();
#endif
  inline const ServoComputedData* ComputedData();

  void AddStyleBit(const uint64_t& aBit) { mBits |= aBit; }

/**
 * 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* StyleBorder();
 *   const nsStyleColor* StyleColor();
 */
#define STYLE_STRUCT(name_, checkdata_cb_) \
  inline const nsStyle##name_* Style##name_() MOZ_NONNULL_RETURN;
#include "nsStyleStructList.h"
#undef STYLE_STRUCT

    /**
     * Equivalent to StyleFoo(), except that we skip the cache write during the
     * servo traversal. This can cause incorrect behavior if used improperly,
     * since we won't record that layout potentially depends on the values in
     * this style struct. Use with care.
     */

#define STYLE_STRUCT(name_, checkdata_cb_) \
  inline const nsStyle##name_* ThreadsafeStyle##name_();
#include "nsStyleStructList.h"
#undef STYLE_STRUCT

/**
 * PeekStyle* is like Style* 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_) \
  inline const nsStyle##name_* PeekStyle##name_();
#include "nsStyleStructList.h"
#undef STYLE_STRUCT

  /**
   * Compute the style changes needed during restyling when this style
   * context is being replaced by aNewContext.  (This is nonsymmetric since
   * we optimize by skipping comparison for styles that have never been
   * requested.)
   *
   * This method returns a change hint (see nsChangeHint.h).  All change
   * hints apply to the frame and its later continuations or ib-split
   * siblings.  Most (all of those except the "NotHandledForDescendants"
   * hints) also apply to all descendants.
   *
   * aEqualStructs must not be null.  Into it will be stored a bitfield
   * representing which structs were compared to be non-equal.
   *
   * aIgnoreVariables indicates whether to skip comparing the Variables
   * struct.  This must only be true for Servo style contexts.  When
   * true, the Variables bit in aEqualStructs will be set.
   */
  nsChangeHint CalcStyleDifference(nsStyleContext* aNewContext,
                                   uint32_t* aEqualStructs,
                                   uint32_t* aSamePointerStructs,
                                   bool aIgnoreVariables = false);

 public:
  /**
   * Get a color that depends on link-visitedness using this and
   * this->GetStyleIfVisited().
   *
   * @param aField A pointer to a member variable in a style struct.
   *               The member variable and its style struct must have
   *               been listed in nsCSSVisitedDependentPropList.h.
   */
  template <typename T, typename S>
  nscolor GetVisitedDependentColor(T S::*aField);

  /**
   * 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, bool aLinkIsVisited);

  /**
   * Start the background image loads for this style context.
   */
  inline void StartBackgroundImageLoads();

  static bool IsReset(const nsStyleStructID aSID) {
    MOZ_ASSERT(0 <= aSID && aSID < nsStyleStructID_Length,
               "must be an inherited or reset SID");
    return nsStyleStructID_Reset_Start <= aSID;
  }
  static bool IsInherited(const nsStyleStructID aSID) { return !IsReset(aSID); }
  static uint32_t GetBitForSID(const nsStyleStructID aSID) { return 1 << aSID; }

#ifdef DEBUG
  void List(FILE* out, int32_t aIndent, bool aListDescendants = true);
  static const char* StructName(nsStyleStructID aSID);
  static bool LookupStruct(const nsACString& aName, nsStyleStructID& aResult);
#endif

 protected:
  // protected destructor to discourage deletion outside of Release()
  ~nsStyleContext() {}

  // Delegated Helper constructor.
  nsStyleContext(nsAtom* aPseudoTag, mozilla::CSSPseudoElementType aPseudoType);

// Helper functions for GetStyle* and PeekStyle*
#define STYLE_STRUCT_INHERITED(name_, checkdata_cb_) \
  template <bool aComputeData>                       \
  const nsStyle##name_* DoGetStyle##name_();
#define STYLE_STRUCT_RESET(name_, checkdata_cb_) \
  template <bool aComputeData>                   \
  const nsStyle##name_* DoGetStyle##name_();

#include "nsStyleStructList.h"
#undef STYLE_STRUCT_RESET
#undef STYLE_STRUCT_INHERITED

  // If this style context is for a pseudo-element or anonymous box,
  // the relevant atom.
  RefPtr<nsAtom> mPseudoTag;

  // mBits stores a number of things:
  //  - It records (using the style struct bits) which structs are
  //    inherited from the parent context or owned by the rule node (i.e.,
  //    not owned by the style context).
  //  - It also stores the additional bits listed at the top of
  //    nsStyleStruct.h.
  uint64_t mBits;

#ifdef DEBUG
  static bool DependencyAllowed(nsStyleStructID aOuterSID,
                                nsStyleStructID aInnerSID) {
    return !!(sDependencyTable[aOuterSID] & GetBitForSID(aInnerSID));
  }

  static const uint32_t sDependencyTable[];
#endif
};

#ifdef MOZ_OLD_STYLE
already_AddRefed<mozilla::GeckoStyleContext> NS_NewStyleContext(
    mozilla::GeckoStyleContext* aParentContext, nsAtom* aPseudoTag,
    mozilla::CSSPseudoElementType aPseudoType, nsRuleNode* aRuleNode,
    bool aSkipParentDisplayBasedStyleFixup);
#endif

#endif