michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Storage of the children and attributes of a DOM node; storage for michael@0: * the two is unified to minimize footprint. michael@0: */ michael@0: michael@0: #ifndef nsAttrAndChildArray_h___ michael@0: #define nsAttrAndChildArray_h___ michael@0: michael@0: #include "mozilla/Attributes.h" michael@0: #include "mozilla/MemoryReporting.h" michael@0: michael@0: #include "nscore.h" michael@0: #include "nsAttrName.h" michael@0: #include "nsAttrValue.h" michael@0: #include "nsCaseTreatment.h" michael@0: michael@0: class nsINode; michael@0: class nsIContent; michael@0: class nsMappedAttributes; michael@0: class nsHTMLStyleSheet; michael@0: class nsRuleWalker; michael@0: class nsMappedAttributeElement; michael@0: michael@0: #define ATTRCHILD_ARRAY_GROWSIZE 8 michael@0: #define ATTRCHILD_ARRAY_LINEAR_THRESHOLD 32 michael@0: michael@0: #define ATTRCHILD_ARRAY_ATTR_SLOTS_BITS 10 michael@0: michael@0: #define ATTRCHILD_ARRAY_MAX_ATTR_COUNT \ michael@0: ((1 << ATTRCHILD_ARRAY_ATTR_SLOTS_BITS) - 1) michael@0: michael@0: #define ATTRCHILD_ARRAY_MAX_CHILD_COUNT \ michael@0: (~uint32_t(0) >> ATTRCHILD_ARRAY_ATTR_SLOTS_BITS) michael@0: michael@0: #define ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK \ michael@0: ((1 << ATTRCHILD_ARRAY_ATTR_SLOTS_BITS) - 1) michael@0: michael@0: michael@0: #define ATTRSIZE (sizeof(InternalAttr) / sizeof(void*)) michael@0: michael@0: class nsAttrAndChildArray michael@0: { michael@0: public: michael@0: nsAttrAndChildArray(); michael@0: ~nsAttrAndChildArray(); michael@0: michael@0: uint32_t ChildCount() const michael@0: { michael@0: return mImpl ? (mImpl->mAttrAndChildCount >> ATTRCHILD_ARRAY_ATTR_SLOTS_BITS) : 0; michael@0: } michael@0: nsIContent* ChildAt(uint32_t aPos) const michael@0: { michael@0: NS_ASSERTION(aPos < ChildCount(), "out-of-bounds access in nsAttrAndChildArray"); michael@0: return reinterpret_cast(mImpl->mBuffer[AttrSlotsSize() + aPos]); michael@0: } michael@0: nsIContent* GetSafeChildAt(uint32_t aPos) const; michael@0: nsIContent * const * GetChildArray(uint32_t* aChildCount) const; michael@0: nsresult AppendChild(nsIContent* aChild) michael@0: { michael@0: return InsertChildAt(aChild, ChildCount()); michael@0: } michael@0: nsresult InsertChildAt(nsIContent* aChild, uint32_t aPos); michael@0: void RemoveChildAt(uint32_t aPos); michael@0: // Like RemoveChildAt but hands the reference to the child being michael@0: // removed back to the caller instead of just releasing it. michael@0: already_AddRefed TakeChildAt(uint32_t aPos); michael@0: int32_t IndexOfChild(const nsINode* aPossibleChild) const; michael@0: michael@0: bool HasAttrs() const michael@0: { michael@0: return MappedAttrCount() || (AttrSlotCount() && AttrSlotIsTaken(0)); michael@0: } michael@0: michael@0: uint32_t AttrCount() const; michael@0: const nsAttrValue* GetAttr(nsIAtom* aLocalName, michael@0: int32_t aNamespaceID = kNameSpaceID_None) const; michael@0: // As above but using a string attr name and always using michael@0: // kNameSpaceID_None. This is always case-sensitive. michael@0: const nsAttrValue* GetAttr(const nsAString& aName) const; michael@0: // Get an nsAttrValue by qualified name. Can optionally do michael@0: // ASCII-case-insensitive name matching. michael@0: const nsAttrValue* GetAttr(const nsAString& aName, michael@0: nsCaseTreatment aCaseSensitive) const; michael@0: const nsAttrValue* AttrAt(uint32_t aPos) const; michael@0: nsresult SetAndTakeAttr(nsIAtom* aLocalName, nsAttrValue& aValue); michael@0: nsresult SetAndTakeAttr(nsINodeInfo* aName, nsAttrValue& aValue); michael@0: michael@0: // Remove the attr at position aPos. The value of the attr is placed in michael@0: // aValue; any value that was already in aValue is destroyed. michael@0: nsresult RemoveAttrAt(uint32_t aPos, nsAttrValue& aValue); michael@0: michael@0: // Returns attribute name at given position, *not* out-of-bounds safe michael@0: const nsAttrName* AttrNameAt(uint32_t aPos) const; michael@0: michael@0: // Returns attribute name at given position or null if aPos is out-of-bounds michael@0: const nsAttrName* GetSafeAttrNameAt(uint32_t aPos) const; michael@0: michael@0: const nsAttrName* GetExistingAttrNameFromQName(const nsAString& aName) const; michael@0: int32_t IndexOfAttr(nsIAtom* aLocalName, int32_t aNamespaceID = kNameSpaceID_None) const; michael@0: michael@0: nsresult SetAndTakeMappedAttr(nsIAtom* aLocalName, nsAttrValue& aValue, michael@0: nsMappedAttributeElement* aContent, michael@0: nsHTMLStyleSheet* aSheet); michael@0: nsresult SetMappedAttrStyleSheet(nsHTMLStyleSheet* aSheet) { michael@0: if (!mImpl || !mImpl->mMappedAttrs) { michael@0: return NS_OK; michael@0: } michael@0: return DoSetMappedAttrStyleSheet(aSheet); michael@0: } michael@0: void WalkMappedAttributeStyleRules(nsRuleWalker* aRuleWalker); michael@0: michael@0: void Compact(); michael@0: michael@0: bool CanFitMoreAttrs() const michael@0: { michael@0: return AttrSlotCount() < ATTRCHILD_ARRAY_MAX_ATTR_COUNT || michael@0: !AttrSlotIsTaken(ATTRCHILD_ARRAY_MAX_ATTR_COUNT - 1); michael@0: } michael@0: michael@0: size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; michael@0: bool HasMappedAttrs() const michael@0: { michael@0: return MappedAttrCount(); michael@0: } michael@0: michael@0: private: michael@0: nsAttrAndChildArray(const nsAttrAndChildArray& aOther) MOZ_DELETE; michael@0: nsAttrAndChildArray& operator=(const nsAttrAndChildArray& aOther) MOZ_DELETE; michael@0: michael@0: void Clear(); michael@0: michael@0: uint32_t NonMappedAttrCount() const; michael@0: uint32_t MappedAttrCount() const; michael@0: michael@0: // Returns a non-null zero-refcount object. michael@0: nsMappedAttributes* michael@0: GetModifiableMapped(nsMappedAttributeElement* aContent, michael@0: nsHTMLStyleSheet* aSheet, michael@0: bool aWillAddAttr); michael@0: nsresult MakeMappedUnique(nsMappedAttributes* aAttributes); michael@0: michael@0: uint32_t AttrSlotsSize() const michael@0: { michael@0: return AttrSlotCount() * ATTRSIZE; michael@0: } michael@0: michael@0: uint32_t AttrSlotCount() const michael@0: { michael@0: return mImpl ? mImpl->mAttrAndChildCount & ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK : 0; michael@0: } michael@0: michael@0: bool AttrSlotIsTaken(uint32_t aSlot) const michael@0: { michael@0: NS_PRECONDITION(aSlot < AttrSlotCount(), "out-of-bounds"); michael@0: return mImpl->mBuffer[aSlot * ATTRSIZE]; michael@0: } michael@0: michael@0: void SetChildCount(uint32_t aCount) michael@0: { michael@0: mImpl->mAttrAndChildCount = michael@0: (mImpl->mAttrAndChildCount & ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK) | michael@0: (aCount << ATTRCHILD_ARRAY_ATTR_SLOTS_BITS); michael@0: } michael@0: michael@0: void SetAttrSlotCount(uint32_t aCount) michael@0: { michael@0: mImpl->mAttrAndChildCount = michael@0: (mImpl->mAttrAndChildCount & ~ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK) | michael@0: aCount; michael@0: } michael@0: michael@0: void SetAttrSlotAndChildCount(uint32_t aSlotCount, uint32_t aChildCount) michael@0: { michael@0: mImpl->mAttrAndChildCount = aSlotCount | michael@0: (aChildCount << ATTRCHILD_ARRAY_ATTR_SLOTS_BITS); michael@0: } michael@0: michael@0: bool GrowBy(uint32_t aGrowSize); michael@0: bool AddAttrSlot(); michael@0: michael@0: /** michael@0: * Set *aPos to aChild and update sibling pointers as needed. aIndex is the michael@0: * index at which aChild is actually being inserted. aChildCount is the michael@0: * number of kids we had before the insertion. michael@0: */ michael@0: inline void SetChildAtPos(void** aPos, nsIContent* aChild, uint32_t aIndex, michael@0: uint32_t aChildCount); michael@0: michael@0: /** michael@0: * Guts of SetMappedAttrStyleSheet for the rare case when we have mapped attrs michael@0: */ michael@0: nsresult DoSetMappedAttrStyleSheet(nsHTMLStyleSheet* aSheet); michael@0: michael@0: struct InternalAttr michael@0: { michael@0: nsAttrName mName; michael@0: nsAttrValue mValue; michael@0: }; michael@0: michael@0: struct Impl { michael@0: uint32_t mAttrAndChildCount; michael@0: uint32_t mBufferSize; michael@0: nsMappedAttributes* mMappedAttrs; michael@0: void* mBuffer[1]; michael@0: }; michael@0: michael@0: Impl* mImpl; michael@0: }; michael@0: michael@0: #endif