2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Storage of the children and attributes of a DOM node; storage for
|
|
|
|
* the two is unified to minimize footprint.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef nsAttrAndChildArray_h___
|
|
|
|
#define nsAttrAndChildArray_h___
|
|
|
|
|
2011-12-16 11:42:07 -08:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-06-23 05:03:39 -07:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2011-12-16 11:42:07 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsAttrName.h"
|
|
|
|
#include "nsAttrValue.h"
|
2012-07-13 16:29:14 -07:00
|
|
|
#include "nsCaseTreatment.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
class nsINode;
|
|
|
|
class nsIContent;
|
|
|
|
class nsMappedAttributes;
|
|
|
|
class nsHTMLStyleSheet;
|
|
|
|
class nsRuleWalker;
|
2008-01-09 01:38:28 -08:00
|
|
|
class nsMappedAttributeElement;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#define ATTRCHILD_ARRAY_GROWSIZE 8
|
|
|
|
#define ATTRCHILD_ARRAY_LINEAR_THRESHOLD 32
|
|
|
|
|
|
|
|
#define ATTRCHILD_ARRAY_ATTR_SLOTS_BITS 10
|
|
|
|
|
|
|
|
#define ATTRCHILD_ARRAY_MAX_ATTR_COUNT \
|
|
|
|
((1 << ATTRCHILD_ARRAY_ATTR_SLOTS_BITS) - 1)
|
|
|
|
|
|
|
|
#define ATTRCHILD_ARRAY_MAX_CHILD_COUNT \
|
2012-08-22 08:56:38 -07:00
|
|
|
(~uint32_t(0) >> ATTRCHILD_ARRAY_ATTR_SLOTS_BITS)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#define ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK \
|
|
|
|
((1 << ATTRCHILD_ARRAY_ATTR_SLOTS_BITS) - 1)
|
|
|
|
|
|
|
|
|
|
|
|
#define ATTRSIZE (sizeof(InternalAttr) / sizeof(void*))
|
|
|
|
|
|
|
|
class nsAttrAndChildArray
|
|
|
|
{
|
|
|
|
public:
|
2008-02-02 15:41:24 -08:00
|
|
|
nsAttrAndChildArray();
|
2007-03-22 10:30:00 -07:00
|
|
|
~nsAttrAndChildArray();
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t ChildCount() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return mImpl ? (mImpl->mAttrAndChildCount >> ATTRCHILD_ARRAY_ATTR_SLOTS_BITS) : 0;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
nsIContent* ChildAt(uint32_t aPos) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aPos < ChildCount(), "out-of-bounds access in nsAttrAndChildArray");
|
2007-07-08 00:08:04 -07:00
|
|
|
return reinterpret_cast<nsIContent*>(mImpl->mBuffer[AttrSlotsSize() + aPos]);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
nsIContent* GetSafeChildAt(uint32_t aPos) const;
|
|
|
|
nsIContent * const * GetChildArray(uint32_t* aChildCount) const;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult AppendChild(nsIContent* aChild)
|
|
|
|
{
|
|
|
|
return InsertChildAt(aChild, ChildCount());
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult InsertChildAt(nsIContent* aChild, uint32_t aPos);
|
|
|
|
void RemoveChildAt(uint32_t aPos);
|
2011-11-02 20:36:16 -07:00
|
|
|
// Like RemoveChildAt but hands the reference to the child being
|
|
|
|
// removed back to the caller instead of just releasing it.
|
2012-08-22 08:56:38 -07:00
|
|
|
already_AddRefed<nsIContent> TakeChildAt(uint32_t aPos);
|
2012-10-06 00:19:46 -07:00
|
|
|
int32_t IndexOfChild(const nsINode* aPossibleChild) const;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-11-05 03:53:57 -08:00
|
|
|
bool HasAttrs() const
|
|
|
|
{
|
|
|
|
return MappedAttrCount() || (AttrSlotCount() && AttrSlotIsTaken(0));
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t AttrCount() const;
|
2013-05-10 15:57:58 -07:00
|
|
|
const nsAttrValue* GetAttr(nsIAtom* aLocalName,
|
|
|
|
int32_t aNamespaceID = kNameSpaceID_None) const;
|
|
|
|
// As above but using a string attr name and always using
|
|
|
|
// kNameSpaceID_None. This is always case-sensitive.
|
|
|
|
const nsAttrValue* GetAttr(const nsAString& aName) const;
|
2012-07-13 16:29:14 -07:00
|
|
|
// Get an nsAttrValue by qualified name. Can optionally do
|
|
|
|
// ASCII-case-insensitive name matching.
|
|
|
|
const nsAttrValue* GetAttr(const nsAString& aName,
|
|
|
|
nsCaseTreatment aCaseSensitive) const;
|
2012-08-22 08:56:38 -07:00
|
|
|
const nsAttrValue* AttrAt(uint32_t aPos) const;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult SetAndTakeAttr(nsIAtom* aLocalName, nsAttrValue& aValue);
|
2014-06-19 19:01:40 -07:00
|
|
|
nsresult SetAndTakeAttr(mozilla::dom::NodeInfo* aName, nsAttrValue& aValue);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Remove the attr at position aPos. The value of the attr is placed in
|
|
|
|
// aValue; any value that was already in aValue is destroyed.
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult RemoveAttrAt(uint32_t aPos, nsAttrValue& aValue);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Returns attribute name at given position, *not* out-of-bounds safe
|
2012-08-22 08:56:38 -07:00
|
|
|
const nsAttrName* AttrNameAt(uint32_t aPos) const;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Returns attribute name at given position or null if aPos is out-of-bounds
|
2012-08-22 08:56:38 -07:00
|
|
|
const nsAttrName* GetSafeAttrNameAt(uint32_t aPos) const;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
const nsAttrName* GetExistingAttrNameFromQName(const nsAString& aName) const;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t IndexOfAttr(nsIAtom* aLocalName, int32_t aNamespaceID = kNameSpaceID_None) const;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult SetAndTakeMappedAttr(nsIAtom* aLocalName, nsAttrValue& aValue,
|
2008-01-09 01:38:28 -08:00
|
|
|
nsMappedAttributeElement* aContent,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsHTMLStyleSheet* aSheet);
|
2012-03-21 21:10:51 -07:00
|
|
|
nsresult SetMappedAttrStyleSheet(nsHTMLStyleSheet* aSheet) {
|
|
|
|
if (!mImpl || !mImpl->mMappedAttrs) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return DoSetMappedAttrStyleSheet(aSheet);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
void WalkMappedAttributeStyleRules(nsRuleWalker* aRuleWalker);
|
|
|
|
|
2008-02-02 15:41:24 -08:00
|
|
|
void Compact();
|
|
|
|
|
2010-12-14 09:16:24 -08:00
|
|
|
bool CanFitMoreAttrs() const
|
|
|
|
{
|
|
|
|
return AttrSlotCount() < ATTRCHILD_ARRAY_MAX_ATTR_COUNT ||
|
|
|
|
!AttrSlotIsTaken(ATTRCHILD_ARRAY_MAX_ATTR_COUNT - 1);
|
|
|
|
}
|
|
|
|
|
2013-06-23 05:03:39 -07:00
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
2012-02-15 15:40:45 -08:00
|
|
|
bool HasMappedAttrs() const
|
|
|
|
{
|
|
|
|
return MappedAttrCount();
|
|
|
|
}
|
2011-07-12 07:56:01 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
private:
|
2011-12-16 11:42:07 -08:00
|
|
|
nsAttrAndChildArray(const nsAttrAndChildArray& aOther) MOZ_DELETE;
|
|
|
|
nsAttrAndChildArray& operator=(const nsAttrAndChildArray& aOther) MOZ_DELETE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void Clear();
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t NonMappedAttrCount() const;
|
|
|
|
uint32_t MappedAttrCount() const;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-04 00:44:01 -07:00
|
|
|
// Returns a non-null zero-refcount object.
|
|
|
|
nsMappedAttributes*
|
|
|
|
GetModifiableMapped(nsMappedAttributeElement* aContent,
|
|
|
|
nsHTMLStyleSheet* aSheet,
|
|
|
|
bool aWillAddAttr);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult MakeMappedUnique(nsMappedAttributes* aAttributes);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t AttrSlotsSize() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return AttrSlotCount() * ATTRSIZE;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t AttrSlotCount() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return mImpl ? mImpl->mAttrAndChildCount & ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK : 0;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
bool AttrSlotIsTaken(uint32_t aSlot) const
|
2010-12-14 09:16:24 -08:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aSlot < AttrSlotCount(), "out-of-bounds");
|
|
|
|
return mImpl->mBuffer[aSlot * ATTRSIZE];
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
void SetChildCount(uint32_t aCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mImpl->mAttrAndChildCount =
|
|
|
|
(mImpl->mAttrAndChildCount & ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK) |
|
|
|
|
(aCount << ATTRCHILD_ARRAY_ATTR_SLOTS_BITS);
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
void SetAttrSlotCount(uint32_t aCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mImpl->mAttrAndChildCount =
|
|
|
|
(mImpl->mAttrAndChildCount & ~ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK) |
|
|
|
|
aCount;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
void SetAttrSlotAndChildCount(uint32_t aSlotCount, uint32_t aChildCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mImpl->mAttrAndChildCount = aSlotCount |
|
|
|
|
(aChildCount << ATTRCHILD_ARRAY_ATTR_SLOTS_BITS);
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
bool GrowBy(uint32_t aGrowSize);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool AddAttrSlot();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-10 18:12:33 -07:00
|
|
|
/**
|
|
|
|
* Set *aPos to aChild and update sibling pointers as needed. aIndex is the
|
|
|
|
* index at which aChild is actually being inserted. aChildCount is the
|
|
|
|
* number of kids we had before the insertion.
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
inline void SetChildAtPos(void** aPos, nsIContent* aChild, uint32_t aIndex,
|
|
|
|
uint32_t aChildCount);
|
2010-05-10 18:12:33 -07:00
|
|
|
|
2012-03-21 21:10:51 -07:00
|
|
|
/**
|
|
|
|
* Guts of SetMappedAttrStyleSheet for the rare case when we have mapped attrs
|
|
|
|
*/
|
|
|
|
nsresult DoSetMappedAttrStyleSheet(nsHTMLStyleSheet* aSheet);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
struct InternalAttr
|
|
|
|
{
|
|
|
|
nsAttrName mName;
|
|
|
|
nsAttrValue mValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Impl {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mAttrAndChildCount;
|
|
|
|
uint32_t mBufferSize;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsMappedAttributes* mMappedAttrs;
|
|
|
|
void* mBuffer[1];
|
|
|
|
};
|
|
|
|
|
2008-02-02 15:41:24 -08:00
|
|
|
Impl* mImpl;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|