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
/* -*- 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/. */

/*
 * A unique per-element set of attributes that is used as an
 * nsIStyleRule; used to implement presentational attributes.
 */

#ifndef nsMappedAttributes_h___
#define nsMappedAttributes_h___

#include "nsAttrAndChildArray.h"
#include "nsMappedAttributeElement.h"
#ifdef MOZ_OLD_STYLE
#include "nsIStyleRule.h"
#endif
#include "mozilla/Attributes.h"
#include "mozilla/ServoBindingTypes.h"
#include "mozilla/MemoryReporting.h"

class nsAtom;
class nsHTMLStyleSheet;

class nsMappedAttributes final
#ifdef MOZ_OLD_STYLE
    : public nsIStyleRule
#endif
{
 public:
  nsMappedAttributes(nsHTMLStyleSheet* aSheet,
                     nsMapRuleToAttributesFunc aMapRuleFunc);

  // Do not return null.
  void* operator new(size_t size, uint32_t aAttrCount = 1) CPP_THROW_NEW;
  nsMappedAttributes* Clone(bool aWillAddAttr);

#ifdef MOZ_OLD_STYLE
  NS_DECL_ISUPPORTS
#else
  NS_INLINE_DECL_REFCOUNTING_WITH_DESTROY(nsMappedAttributes, LastRelease())
#endif

  void SetAndSwapAttr(nsAtom* aAttrName, nsAttrValue& aValue,
                      bool* aValueWasSet);
  const nsAttrValue* GetAttr(nsAtom* aAttrName) const;
  const nsAttrValue* GetAttr(const nsAString& aAttrName) const;

  uint32_t Count() const { return mAttrCount; }

  bool Equals(const nsMappedAttributes* aAttributes) const;
  PLDHashNumber HashValue() const;

  void DropStyleSheetReference() { mSheet = nullptr; }
  void SetStyleSheet(nsHTMLStyleSheet* aSheet);
  nsHTMLStyleSheet* GetStyleSheet() { return mSheet; }

  const nsAttrName* NameAt(uint32_t aPos) const {
    NS_ASSERTION(aPos < mAttrCount, "out-of-bounds");
    return &Attrs()[aPos].mName;
  }
  const nsAttrValue* AttrAt(uint32_t aPos) const {
    NS_ASSERTION(aPos < mAttrCount, "out-of-bounds");
    return &Attrs()[aPos].mValue;
  }
  // Remove the attr at position aPos.  The value of the attr is placed in
  // aValue; any value that was already in aValue is destroyed.
  void RemoveAttrAt(uint32_t aPos, nsAttrValue& aValue);
  const nsAttrName* GetExistingAttrNameFromQName(const nsAString& aName) const;
  int32_t IndexOfAttr(nsAtom* aLocalName) const;

  // Apply the contained mapper to the contained set of servo rules,
  // unless the servo rules have already been initialized.
  void LazilyResolveServoDeclaration(nsIDocument* aDocument);

  // Obtain the contained servo declaration block
  // May return null if called before the inner block
  // has been (lazily) resolved
  const RefPtr<RawServoDeclarationBlock>& GetServoStyle() const {
    return mServoStyle;
  }

  void ClearServoStyle() {
    MOZ_ASSERT(NS_IsMainThread());
    mServoStyle = nullptr;
  }

#ifdef MOZ_OLD_STYLE
  // nsIStyleRule
  virtual void MapRuleInfoInto(nsRuleData* aRuleData) override;
  virtual bool MightMapInheritedStyleData() override;
  virtual bool GetDiscretelyAnimatedCSSValue(nsCSSPropertyID aProperty,
                                             nsCSSValue* aValue) override;
#ifdef DEBUG
  virtual void List(FILE* out = stdout, int32_t aIndent = 0) const override;
#endif
#endif

  size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;

  static void Shutdown();

 private:
  void LastRelease();

  nsMappedAttributes(const nsMappedAttributes& aCopy);
  ~nsMappedAttributes();

  struct InternalAttr {
    nsAttrName mName;
    nsAttrValue mValue;
  };

  /**
   * Due to a compiler bug in VisualAge C++ for AIX, we need to return the
   * address of the first index into mAttrs here, instead of simply
   * returning mAttrs itself.
   *
   * See Bug 231104 for more information.
   */
  const InternalAttr* Attrs() const {
    return reinterpret_cast<const InternalAttr*>(&(mAttrs[0]));
  }
  InternalAttr* Attrs() {
    return reinterpret_cast<InternalAttr*>(&(mAttrs[0]));
  }

  uint16_t mAttrCount;
#ifdef DEBUG
  uint16_t mBufferSize;
#endif
  nsHTMLStyleSheet* mSheet;  // weak
  nsMapRuleToAttributesFunc mRuleMapper;
  RefPtr<RawServoDeclarationBlock> mServoStyle;
  void* mAttrs[1];

  static bool sShuttingDown;

  // We're caching some memory to avoid trashing the allocator.
  // The memory stored at index N can hold N attribute values.
  static nsTArray<void*>* sCachedMappedAttributeAllocations;
};

#endif /* nsMappedAttributes_h___ */