2015-05-03 22:32:37 +03: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 15:12:37 +04: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 20:07:27 +03:00
|
|
|
|
2006-03-30 12:03:04 +04:00
|
|
|
/*
|
|
|
|
* Storage of the children and attributes of a DOM node; storage for
|
|
|
|
* the two is unified to minimize footprint.
|
|
|
|
*/
|
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
#ifndef nsAttrAndChildArray_h___
|
|
|
|
#define nsAttrAndChildArray_h___
|
|
|
|
|
2011-12-16 23:42:07 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2016-07-23 00:11:41 +03:00
|
|
|
#include "mozilla/dom/BorrowedAttrInfo.h"
|
2011-12-16 23:42:07 +04:00
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsAttrName.h"
|
|
|
|
#include "nsAttrValue.h"
|
2012-07-14 03:29:14 +04:00
|
|
|
#include "nsCaseTreatment.h"
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2006-05-16 18:51:52 +04:00
|
|
|
class nsINode;
|
2004-01-15 20:07:27 +03:00
|
|
|
class nsIContent;
|
2004-01-26 22:22:05 +03:00
|
|
|
class nsMappedAttributes;
|
2004-04-13 01:56:09 +04:00
|
|
|
class nsHTMLStyleSheet;
|
2004-01-26 22:22:05 +03:00
|
|
|
class nsRuleWalker;
|
2008-01-09 12:38:28 +03:00
|
|
|
class nsMappedAttributeElement;
|
2004-01-15 20:07:27 +03: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 19:56:38 +04:00
|
|
|
(~uint32_t(0) >> ATTRCHILD_ARRAY_ATTR_SLOTS_BITS)
|
2004-01-15 20:07:27 +03: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-23 00:11:41 +03:00
|
|
|
typedef mozilla::dom::BorrowedAttrInfo BorrowedAttrInfo;
|
2004-01-15 20:07:27 +03:00
|
|
|
public:
|
2008-02-03 02:41:24 +03:00
|
|
|
nsAttrAndChildArray();
|
2004-01-15 20:07:27 +03:00
|
|
|
~nsAttrAndChildArray();
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t ChildCount() const
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
|
|
|
return mImpl ? (mImpl->mAttrAndChildCount >> ATTRCHILD_ARRAY_ATTR_SLOTS_BITS) : 0;
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
nsIContent* ChildAt(uint32_t aPos) const
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aPos < ChildCount(), "out-of-bounds access in nsAttrAndChildArray");
|
2007-07-08 11:08:04 +04:00
|
|
|
return reinterpret_cast<nsIContent*>(mImpl->mBuffer[AttrSlotsSize() + aPos]);
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
nsIContent* GetSafeChildAt(uint32_t aPos) const;
|
|
|
|
nsIContent * const * GetChildArray(uint32_t* aChildCount) const;
|
|
|
|
nsresult InsertChildAt(nsIContent* aChild, uint32_t aPos);
|
|
|
|
void RemoveChildAt(uint32_t aPos);
|
2011-11-03 07:36:16 +04:00
|
|
|
// Like RemoveChildAt but hands the reference to the child being
|
|
|
|
// removed back to the caller instead of just releasing it.
|
2012-08-22 19:56:38 +04:00
|
|
|
already_AddRefed<nsIContent> TakeChildAt(uint32_t aPos);
|
2012-10-06 11:19:46 +04:00
|
|
|
int32_t IndexOfChild(const nsINode* aPossibleChild) const;
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2013-11-05 15:53:57 +04:00
|
|
|
bool HasAttrs() const
|
|
|
|
{
|
|
|
|
return MappedAttrCount() || (AttrSlotCount() && AttrSlotIsTaken(0));
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t AttrCount() const;
|
2017-10-03 01:05:19 +03:00
|
|
|
const nsAttrValue* GetAttr(nsAtom* aLocalName,
|
2013-05-11 02:57:58 +04:00
|
|
|
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-14 03:29:14 +04: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 19:56:38 +04:00
|
|
|
const nsAttrValue* AttrAt(uint32_t aPos) const;
|
2015-07-25 08:57:13 +03:00
|
|
|
// SetAndSwapAttr swaps the current attribute value with aValue.
|
2017-05-19 00:09:01 +03:00
|
|
|
// If the attribute was unset, an empty value will be swapped into aValue
|
|
|
|
// and aHadValue will be set to false. Otherwise, aHadValue will be set to
|
|
|
|
// true.
|
2017-10-03 01:05:19 +03:00
|
|
|
nsresult SetAndSwapAttr(nsAtom* aLocalName, nsAttrValue& aValue,
|
2017-05-19 00:09:01 +03:00
|
|
|
bool* aHadValue);
|
|
|
|
nsresult SetAndSwapAttr(mozilla::dom::NodeInfo* aName, nsAttrValue& aValue,
|
|
|
|
bool* aHadValue);
|
2006-11-11 03:04:46 +03: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 19:56:38 +04:00
|
|
|
nsresult RemoveAttrAt(uint32_t aPos, nsAttrValue& aValue);
|
2005-12-30 23:12:35 +03:00
|
|
|
|
|
|
|
// Returns attribute name at given position, *not* out-of-bounds safe
|
2012-08-22 19:56:38 +04:00
|
|
|
const nsAttrName* AttrNameAt(uint32_t aPos) const;
|
2005-12-30 23:12:35 +03:00
|
|
|
|
2016-07-22 06:10:23 +03:00
|
|
|
// Returns the attribute info at a given position, *not* out-of-bounds safe
|
2016-07-23 00:11:41 +03:00
|
|
|
BorrowedAttrInfo AttrInfoAt(uint32_t aPos) const;
|
2016-07-22 06:10:23 +03:00
|
|
|
|
2005-12-30 23:25:54 +03:00
|
|
|
// Returns attribute name at given position or null if aPos is out-of-bounds
|
2012-08-22 19:56:38 +04:00
|
|
|
const nsAttrName* GetSafeAttrNameAt(uint32_t aPos) const;
|
2005-12-30 23:12:35 +03:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
const nsAttrName* GetExistingAttrNameFromQName(const nsAString& aName) const;
|
2017-10-03 01:05:19 +03:00
|
|
|
int32_t IndexOfAttr(nsAtom* aLocalName, int32_t aNamespaceID = kNameSpaceID_None) const;
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2017-05-19 00:09:01 +03:00
|
|
|
// SetAndSwapMappedAttr swaps the current attribute value with aValue.
|
|
|
|
// If the attribute was unset, an empty value will be swapped into aValue
|
|
|
|
// and aHadValue will be set to false. Otherwise, aHadValue will be set to
|
|
|
|
// true.
|
2017-10-03 01:05:19 +03:00
|
|
|
nsresult SetAndSwapMappedAttr(nsAtom* aLocalName, nsAttrValue& aValue,
|
2008-01-09 12:38:28 +03:00
|
|
|
nsMappedAttributeElement* aContent,
|
2017-05-19 00:09:01 +03:00
|
|
|
nsHTMLStyleSheet* aSheet,
|
|
|
|
bool* aHadValue);
|
2012-03-22 08:10:51 +04:00
|
|
|
nsresult SetMappedAttrStyleSheet(nsHTMLStyleSheet* aSheet) {
|
|
|
|
if (!mImpl || !mImpl->mMappedAttrs) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return DoSetMappedAttrStyleSheet(aSheet);
|
|
|
|
}
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2008-02-03 02:41:24 +03:00
|
|
|
void Compact();
|
|
|
|
|
2010-12-14 20:16:24 +03:00
|
|
|
bool CanFitMoreAttrs() const
|
|
|
|
{
|
|
|
|
return AttrSlotCount() < ATTRCHILD_ARRAY_MAX_ATTR_COUNT ||
|
|
|
|
!AttrSlotIsTaken(ATTRCHILD_ARRAY_MAX_ATTR_COUNT - 1);
|
|
|
|
}
|
|
|
|
|
2013-06-23 16:03:39 +04:00
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
2012-02-16 03:40:45 +04:00
|
|
|
bool HasMappedAttrs() const
|
|
|
|
{
|
|
|
|
return MappedAttrCount();
|
|
|
|
}
|
2017-01-20 02:56:53 +03:00
|
|
|
const nsMappedAttributes* GetMapped() const;
|
2011-07-12 18:56:01 +04:00
|
|
|
|
2017-03-29 22:10:00 +03:00
|
|
|
// Force this to have mapped attributes, even if those attributes are empty.
|
|
|
|
nsresult ForceMapped(nsMappedAttributeElement* aContent, nsIDocument* aDocument);
|
|
|
|
|
|
|
|
// Clear the servo declaration block on the mapped attributes, if any
|
|
|
|
// Will assert off main thread
|
|
|
|
void ClearMappedServoStyle();
|
|
|
|
|
2017-04-20 22:57:48 +03:00
|
|
|
// Increases capacity (if necessary) to have enough space to accomodate the
|
|
|
|
// unmapped attributes and children of |aOther|. If |aAllocateChildren| is not
|
|
|
|
// true, only enough space for unmapped attributes will be reserved.
|
|
|
|
// It is REQUIRED that this function be called ONLY when the array is empty.
|
|
|
|
nsresult EnsureCapacityToClone(const nsAttrAndChildArray& aOther,
|
|
|
|
bool aAllocateChildren);
|
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
private:
|
2015-01-07 02:35:02 +03:00
|
|
|
nsAttrAndChildArray(const nsAttrAndChildArray& aOther) = delete;
|
|
|
|
nsAttrAndChildArray& operator=(const nsAttrAndChildArray& aOther) = delete;
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2004-08-10 20:16:58 +04:00
|
|
|
void Clear();
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t NonMappedAttrCount() const;
|
|
|
|
uint32_t MappedAttrCount() const;
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2012-08-04 11:44:01 +04:00
|
|
|
// Returns a non-null zero-refcount object.
|
|
|
|
nsMappedAttributes*
|
|
|
|
GetModifiableMapped(nsMappedAttributeElement* aContent,
|
|
|
|
nsHTMLStyleSheet* aSheet,
|
2017-03-29 22:10:00 +03:00
|
|
|
bool aWillAddAttr,
|
|
|
|
int32_t aAttrCount = 1);
|
2004-01-26 22:22:05 +03:00
|
|
|
nsresult MakeMappedUnique(nsMappedAttributes* aAttributes);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t AttrSlotsSize() const
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
|
|
|
return AttrSlotCount() * ATTRSIZE;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t AttrSlotCount() const
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
|
|
|
return mImpl ? mImpl->mAttrAndChildCount & ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK : 0;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
bool AttrSlotIsTaken(uint32_t aSlot) const
|
2010-12-14 20:16:24 +03:00
|
|
|
{
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aSlot < AttrSlotCount(), "out-of-bounds");
|
2010-12-14 20:16:24 +03:00
|
|
|
return mImpl->mBuffer[aSlot * ATTRSIZE];
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void SetChildCount(uint32_t aCount)
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
2016-07-22 06:10:23 +03:00
|
|
|
mImpl->mAttrAndChildCount =
|
2004-01-15 20:07:27 +03:00
|
|
|
(mImpl->mAttrAndChildCount & ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK) |
|
|
|
|
(aCount << ATTRCHILD_ARRAY_ATTR_SLOTS_BITS);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void SetAttrSlotCount(uint32_t aCount)
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
|
|
|
mImpl->mAttrAndChildCount =
|
|
|
|
(mImpl->mAttrAndChildCount & ~ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK) |
|
|
|
|
aCount;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void SetAttrSlotAndChildCount(uint32_t aSlotCount, uint32_t aChildCount)
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
|
|
|
mImpl->mAttrAndChildCount = aSlotCount |
|
|
|
|
(aChildCount << ATTRCHILD_ARRAY_ATTR_SLOTS_BITS);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
bool GrowBy(uint32_t aGrowSize);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool AddAttrSlot();
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2010-05-11 05:12:33 +04: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 19:56:38 +04:00
|
|
|
inline void SetChildAtPos(void** aPos, nsIContent* aChild, uint32_t aIndex,
|
|
|
|
uint32_t aChildCount);
|
2010-05-11 05:12:33 +04:00
|
|
|
|
2012-03-22 08:10:51 +04:00
|
|
|
/**
|
|
|
|
* Guts of SetMappedAttrStyleSheet for the rare case when we have mapped attrs
|
|
|
|
*/
|
|
|
|
nsresult DoSetMappedAttrStyleSheet(nsHTMLStyleSheet* aSheet);
|
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
struct InternalAttr
|
|
|
|
{
|
|
|
|
nsAttrName mName;
|
|
|
|
nsAttrValue mValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Impl {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mAttrAndChildCount;
|
|
|
|
uint32_t mBufferSize;
|
2004-01-26 22:22:05 +03:00
|
|
|
nsMappedAttributes* mMappedAttrs;
|
2004-01-15 20:07:27 +03:00
|
|
|
void* mBuffer[1];
|
|
|
|
};
|
|
|
|
|
2008-02-03 02:41:24 +03:00
|
|
|
Impl* mImpl;
|
2004-01-15 20:07:27 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|