2015-05-03 19:32:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00: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/. */
|
2004-01-15 17:07:27 +00:00
|
|
|
|
2006-03-30 08:03:04 +00:00
|
|
|
/*
|
|
|
|
* Storage of the children and attributes of a DOM node; storage for
|
|
|
|
* the two is unified to minimize footprint.
|
|
|
|
*/
|
|
|
|
|
2004-01-15 17:07:27 +00:00
|
|
|
#ifndef nsAttrAndChildArray_h___
|
|
|
|
#define nsAttrAndChildArray_h___
|
|
|
|
|
2011-12-16 19:42:07 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-06-23 12:03:39 +00:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2016-07-22 21:11:41 +00:00
|
|
|
#include "mozilla/dom/BorrowedAttrInfo.h"
|
2011-12-16 19:42:07 +00:00
|
|
|
|
2004-01-15 17:07:27 +00:00
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsAttrName.h"
|
|
|
|
#include "nsAttrValue.h"
|
2012-07-13 23:29:14 +00:00
|
|
|
#include "nsCaseTreatment.h"
|
2004-01-15 17:07:27 +00:00
|
|
|
|
2006-05-16 14:51:52 +00:00
|
|
|
class nsINode;
|
2004-01-15 17:07:27 +00:00
|
|
|
class nsIContent;
|
2004-01-26 19:22:05 +00:00
|
|
|
class nsMappedAttributes;
|
2004-04-12 21:56:09 +00:00
|
|
|
class nsHTMLStyleSheet;
|
2004-01-26 19:22:05 +00:00
|
|
|
class nsRuleWalker;
|
2008-01-09 09:38:28 +00:00
|
|
|
class nsMappedAttributeElement;
|
2004-01-15 17:07:27 +00: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 15:56:38 +00:00
|
|
|
(~uint32_t(0) >> ATTRCHILD_ARRAY_ATTR_SLOTS_BITS)
|
2004-01-15 17:07:27 +00:00
|
|
|
|
|
|
|
#define ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK \
|
|
|
|
((1 << ATTRCHILD_ARRAY_ATTR_SLOTS_BITS) - 1)
|
|
|
|
|
|
|
|
|
|
|
|
#define ATTRSIZE (sizeof(InternalAttr) / sizeof(void*))
|
|
|
|
|
|
|
|
class nsAttrAndChildArray
|
|
|
|
{
|
2016-07-22 21:11:41 +00:00
|
|
|
typedef mozilla::dom::BorrowedAttrInfo BorrowedAttrInfo;
|
2004-01-15 17:07:27 +00:00
|
|
|
public:
|
2008-02-02 23:41:24 +00:00
|
|
|
nsAttrAndChildArray();
|
2004-01-15 17:07:27 +00:00
|
|
|
~nsAttrAndChildArray();
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t ChildCount() const
|
2004-01-15 17:07:27 +00:00
|
|
|
{
|
|
|
|
return mImpl ? (mImpl->mAttrAndChildCount >> ATTRCHILD_ARRAY_ATTR_SLOTS_BITS) : 0;
|
|
|
|
}
|
2012-08-22 15:56:38 +00:00
|
|
|
nsIContent* ChildAt(uint32_t aPos) const
|
2004-01-15 17:07:27 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aPos < ChildCount(), "out-of-bounds access in nsAttrAndChildArray");
|
2007-07-08 07:08:04 +00:00
|
|
|
return reinterpret_cast<nsIContent*>(mImpl->mBuffer[AttrSlotsSize() + aPos]);
|
2004-01-15 17:07:27 +00:00
|
|
|
}
|
2012-08-22 15:56:38 +00:00
|
|
|
nsIContent* GetSafeChildAt(uint32_t aPos) const;
|
|
|
|
nsIContent * const * GetChildArray(uint32_t* aChildCount) const;
|
2004-01-15 17:07:27 +00:00
|
|
|
nsresult AppendChild(nsIContent* aChild)
|
|
|
|
{
|
|
|
|
return InsertChildAt(aChild, ChildCount());
|
|
|
|
}
|
2012-08-22 15:56:38 +00:00
|
|
|
nsresult InsertChildAt(nsIContent* aChild, uint32_t aPos);
|
|
|
|
void RemoveChildAt(uint32_t aPos);
|
2011-11-03 03:36:16 +00:00
|
|
|
// Like RemoveChildAt but hands the reference to the child being
|
|
|
|
// removed back to the caller instead of just releasing it.
|
2012-08-22 15:56:38 +00:00
|
|
|
already_AddRefed<nsIContent> TakeChildAt(uint32_t aPos);
|
2012-10-06 07:19:46 +00:00
|
|
|
int32_t IndexOfChild(const nsINode* aPossibleChild) const;
|
2004-01-15 17:07:27 +00:00
|
|
|
|
2013-11-05 11:53:57 +00:00
|
|
|
bool HasAttrs() const
|
|
|
|
{
|
|
|
|
return MappedAttrCount() || (AttrSlotCount() && AttrSlotIsTaken(0));
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t AttrCount() const;
|
2013-05-10 22:57:58 +00: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 23:29:14 +00: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 15:56:38 +00:00
|
|
|
const nsAttrValue* AttrAt(uint32_t aPos) const;
|
2015-07-25 05:57:13 +00:00
|
|
|
// SetAndSwapAttr swaps the current attribute value with aValue.
|
2015-07-22 02:09:41 +00:00
|
|
|
nsresult SetAndSwapAttr(nsIAtom* aLocalName, nsAttrValue& aValue);
|
|
|
|
nsresult SetAndSwapAttr(mozilla::dom::NodeInfo* aName, nsAttrValue& aValue);
|
2006-11-11 00:04:46 +00: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 15:56:38 +00:00
|
|
|
nsresult RemoveAttrAt(uint32_t aPos, nsAttrValue& aValue);
|
2005-12-30 20:12:35 +00:00
|
|
|
|
|
|
|
// Returns attribute name at given position, *not* out-of-bounds safe
|
2012-08-22 15:56:38 +00:00
|
|
|
const nsAttrName* AttrNameAt(uint32_t aPos) const;
|
2005-12-30 20:12:35 +00:00
|
|
|
|
2016-07-22 03:10:23 +00:00
|
|
|
// Returns the attribute info at a given position, *not* out-of-bounds safe
|
2016-07-22 21:11:41 +00:00
|
|
|
BorrowedAttrInfo AttrInfoAt(uint32_t aPos) const;
|
2016-07-22 03:10:23 +00:00
|
|
|
|
2005-12-30 20:25:54 +00:00
|
|
|
// Returns attribute name at given position or null if aPos is out-of-bounds
|
2012-08-22 15:56:38 +00:00
|
|
|
const nsAttrName* GetSafeAttrNameAt(uint32_t aPos) const;
|
2005-12-30 20:12:35 +00:00
|
|
|
|
2010-03-08 15:45:00 +00:00
|
|
|
const nsAttrName* GetExistingAttrNameFromQName(const nsAString& aName) const;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t IndexOfAttr(nsIAtom* aLocalName, int32_t aNamespaceID = kNameSpaceID_None) const;
|
2004-01-15 17:07:27 +00:00
|
|
|
|
2004-01-26 19:22:05 +00:00
|
|
|
nsresult SetAndTakeMappedAttr(nsIAtom* aLocalName, nsAttrValue& aValue,
|
2008-01-09 09:38:28 +00:00
|
|
|
nsMappedAttributeElement* aContent,
|
2005-01-12 19:45:38 +00:00
|
|
|
nsHTMLStyleSheet* aSheet);
|
2012-03-22 04:10:51 +00:00
|
|
|
nsresult SetMappedAttrStyleSheet(nsHTMLStyleSheet* aSheet) {
|
|
|
|
if (!mImpl || !mImpl->mMappedAttrs) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return DoSetMappedAttrStyleSheet(aSheet);
|
|
|
|
}
|
2004-01-26 19:22:05 +00:00
|
|
|
void WalkMappedAttributeStyleRules(nsRuleWalker* aRuleWalker);
|
|
|
|
|
2008-02-02 23:41:24 +00:00
|
|
|
void Compact();
|
|
|
|
|
2010-12-14 17:16:24 +00:00
|
|
|
bool CanFitMoreAttrs() const
|
|
|
|
{
|
|
|
|
return AttrSlotCount() < ATTRCHILD_ARRAY_MAX_ATTR_COUNT ||
|
|
|
|
!AttrSlotIsTaken(ATTRCHILD_ARRAY_MAX_ATTR_COUNT - 1);
|
|
|
|
}
|
|
|
|
|
2013-06-23 12:03:39 +00:00
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
2012-02-15 23:40:45 +00:00
|
|
|
bool HasMappedAttrs() const
|
|
|
|
{
|
|
|
|
return MappedAttrCount();
|
|
|
|
}
|
2011-07-12 14:56:01 +00:00
|
|
|
|
2004-01-15 17:07:27 +00:00
|
|
|
private:
|
2015-01-06 23:35:02 +00:00
|
|
|
nsAttrAndChildArray(const nsAttrAndChildArray& aOther) = delete;
|
|
|
|
nsAttrAndChildArray& operator=(const nsAttrAndChildArray& aOther) = delete;
|
2004-01-15 17:07:27 +00:00
|
|
|
|
2004-08-10 16:16:58 +00:00
|
|
|
void Clear();
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t NonMappedAttrCount() const;
|
|
|
|
uint32_t MappedAttrCount() const;
|
2004-01-26 19:22:05 +00:00
|
|
|
|
2012-08-04 07:44:01 +00:00
|
|
|
// Returns a non-null zero-refcount object.
|
|
|
|
nsMappedAttributes*
|
|
|
|
GetModifiableMapped(nsMappedAttributeElement* aContent,
|
|
|
|
nsHTMLStyleSheet* aSheet,
|
|
|
|
bool aWillAddAttr);
|
2004-01-26 19:22:05 +00:00
|
|
|
nsresult MakeMappedUnique(nsMappedAttributes* aAttributes);
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t AttrSlotsSize() const
|
2004-01-15 17:07:27 +00:00
|
|
|
{
|
|
|
|
return AttrSlotCount() * ATTRSIZE;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t AttrSlotCount() const
|
2004-01-15 17:07:27 +00:00
|
|
|
{
|
|
|
|
return mImpl ? mImpl->mAttrAndChildCount & ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK : 0;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
bool AttrSlotIsTaken(uint32_t aSlot) const
|
2010-12-14 17:16:24 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aSlot < AttrSlotCount(), "out-of-bounds");
|
|
|
|
return mImpl->mBuffer[aSlot * ATTRSIZE];
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
void SetChildCount(uint32_t aCount)
|
2004-01-15 17:07:27 +00:00
|
|
|
{
|
2016-07-22 03:10:23 +00:00
|
|
|
mImpl->mAttrAndChildCount =
|
2004-01-15 17:07:27 +00:00
|
|
|
(mImpl->mAttrAndChildCount & ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK) |
|
|
|
|
(aCount << ATTRCHILD_ARRAY_ATTR_SLOTS_BITS);
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
void SetAttrSlotCount(uint32_t aCount)
|
2004-01-15 17:07:27 +00:00
|
|
|
{
|
|
|
|
mImpl->mAttrAndChildCount =
|
|
|
|
(mImpl->mAttrAndChildCount & ~ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK) |
|
|
|
|
aCount;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
void SetAttrSlotAndChildCount(uint32_t aSlotCount, uint32_t aChildCount)
|
2004-01-15 17:07:27 +00:00
|
|
|
{
|
|
|
|
mImpl->mAttrAndChildCount = aSlotCount |
|
|
|
|
(aChildCount << ATTRCHILD_ARRAY_ATTR_SLOTS_BITS);
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
bool GrowBy(uint32_t aGrowSize);
|
2011-09-29 06:19:26 +00:00
|
|
|
bool AddAttrSlot();
|
2004-01-15 17:07:27 +00:00
|
|
|
|
2010-05-11 01:12:33 +00: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 15:56:38 +00:00
|
|
|
inline void SetChildAtPos(void** aPos, nsIContent* aChild, uint32_t aIndex,
|
|
|
|
uint32_t aChildCount);
|
2010-05-11 01:12:33 +00:00
|
|
|
|
2012-03-22 04:10:51 +00:00
|
|
|
/**
|
|
|
|
* Guts of SetMappedAttrStyleSheet for the rare case when we have mapped attrs
|
|
|
|
*/
|
|
|
|
nsresult DoSetMappedAttrStyleSheet(nsHTMLStyleSheet* aSheet);
|
|
|
|
|
2004-01-15 17:07:27 +00:00
|
|
|
struct InternalAttr
|
|
|
|
{
|
|
|
|
nsAttrName mName;
|
|
|
|
nsAttrValue mValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Impl {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mAttrAndChildCount;
|
|
|
|
uint32_t mBufferSize;
|
2004-01-26 19:22:05 +00:00
|
|
|
nsMappedAttributes* mMappedAttrs;
|
2004-01-15 17:07:27 +00:00
|
|
|
void* mBuffer[1];
|
|
|
|
};
|
|
|
|
|
2008-02-02 23:41:24 +00:00
|
|
|
Impl* mImpl;
|
2004-01-15 17:07:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|