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
|
|
|
#include "nsAttrAndChildArray.h"
|
2013-06-30 20:26:39 +04:00
|
|
|
|
2015-09-01 16:42:26 +03:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2013-11-13 04:22:38 +04:00
|
|
|
#include "mozilla/MathAlgorithms.h"
|
2013-06-30 20:26:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
|
|
|
|
2008-01-09 12:38:28 +03:00
|
|
|
#include "nsMappedAttributeElement.h"
|
2004-01-15 20:07:27 +03:00
|
|
|
#include "nsString.h"
|
2004-04-13 01:56:09 +04:00
|
|
|
#include "nsHTMLStyleSheet.h"
|
2004-01-26 22:22:05 +03:00
|
|
|
#include "nsRuleWalker.h"
|
|
|
|
#include "nsMappedAttributes.h"
|
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2011-08-11 17:29:50 +04:00
|
|
|
#include "nsContentUtils.h" // nsAutoScriptBlocker
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2015-09-01 16:42:26 +03:00
|
|
|
using mozilla::CheckedUint32;
|
|
|
|
|
2006-03-01 01:16:06 +03:00
|
|
|
/*
|
2006-03-01 01:17:23 +03:00
|
|
|
CACHE_POINTER_SHIFT indicates how many steps to downshift the |this| pointer.
|
|
|
|
It should be small enough to not cause collisions between adjecent arrays, and
|
|
|
|
large enough to make sure that all indexes are used. The size below is based
|
|
|
|
on the size of the smallest possible element (currently 24[*] bytes) which is
|
|
|
|
the smallest distance between two nsAttrAndChildArray. 24/(2^_5_) is 0.75.
|
|
|
|
This means that two adjacent nsAttrAndChildArrays will overlap one in 4 times.
|
|
|
|
However not all elements will have enough children to get cached. And any
|
|
|
|
allocator that doesn't return addresses aligned to 64 bytes will ensure that
|
|
|
|
any index will get used.
|
2006-03-01 01:16:06 +03:00
|
|
|
|
2012-11-15 02:10:08 +04:00
|
|
|
[*] sizeof(Element) + 4 bytes for nsIDOMElement vtable pointer.
|
2006-03-01 01:16:06 +03:00
|
|
|
*/
|
|
|
|
|
2006-03-01 01:17:23 +03:00
|
|
|
#define CACHE_POINTER_SHIFT 5
|
|
|
|
#define CACHE_NUM_SLOTS 128
|
|
|
|
#define CACHE_CHILD_LIMIT 10
|
2006-03-01 01:16:06 +03:00
|
|
|
|
2006-03-01 01:17:23 +03:00
|
|
|
#define CACHE_GET_INDEX(_array) \
|
|
|
|
((NS_PTR_TO_INT32(_array) >> CACHE_POINTER_SHIFT) & \
|
|
|
|
(CACHE_NUM_SLOTS - 1))
|
2005-03-11 12:40:03 +03:00
|
|
|
|
|
|
|
struct IndexCacheSlot
|
|
|
|
{
|
|
|
|
const nsAttrAndChildArray* array;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t index;
|
2005-03-11 12:40:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// This is inited to all zeroes since it's static. Though even if it wasn't
|
|
|
|
// the worst thing that'd happen is a small inefficency if you'd get a false
|
|
|
|
// positive cachehit.
|
2006-03-01 01:17:23 +03:00
|
|
|
static IndexCacheSlot indexCache[CACHE_NUM_SLOTS];
|
2005-03-11 12:40:03 +03:00
|
|
|
|
|
|
|
static
|
2006-03-01 01:16:06 +03:00
|
|
|
inline
|
2005-03-11 12:40:03 +03:00
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
AddIndexToCache(const nsAttrAndChildArray* aArray, int32_t aIndex)
|
2005-03-11 12:40:03 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t ix = CACHE_GET_INDEX(aArray);
|
2006-03-01 01:16:06 +03:00
|
|
|
indexCache[ix].array = aArray;
|
|
|
|
indexCache[ix].index = aIndex;
|
2005-03-11 12:40:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2006-03-01 01:16:06 +03:00
|
|
|
inline
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2005-03-11 12:40:03 +03:00
|
|
|
GetIndexFromCache(const nsAttrAndChildArray* aArray)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t ix = CACHE_GET_INDEX(aArray);
|
2006-03-01 01:16:06 +03:00
|
|
|
return indexCache[ix].array == aArray ? indexCache[ix].index : -1;
|
2005-03-11 12:40:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-17 00:04:12 +03:00
|
|
|
/**
|
|
|
|
* Due to a compiler bug in VisualAge C++ for AIX, we need to return the
|
|
|
|
* address of the first index into mBuffer here, instead of simply returning
|
|
|
|
* mBuffer itself.
|
|
|
|
*
|
|
|
|
* See Bug 231104 for more information.
|
|
|
|
*/
|
2004-01-15 20:07:27 +03:00
|
|
|
#define ATTRS(_impl) \
|
2007-07-08 11:08:04 +04:00
|
|
|
reinterpret_cast<InternalAttr*>(&((_impl)->mBuffer[0]))
|
2004-01-15 20:07:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
#define NS_IMPL_EXTRA_SIZE \
|
|
|
|
((sizeof(Impl) - sizeof(mImpl->mBuffer)) / sizeof(void*))
|
|
|
|
|
2008-02-03 02:41:24 +03:00
|
|
|
nsAttrAndChildArray::nsAttrAndChildArray()
|
2012-07-30 18:20:58 +04:00
|
|
|
: mImpl(nullptr)
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAttrAndChildArray::~nsAttrAndChildArray()
|
|
|
|
{
|
2008-02-03 02:41:24 +03:00
|
|
|
if (!mImpl) {
|
|
|
|
return;
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
2008-02-03 02:41:24 +03:00
|
|
|
Clear();
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2015-02-19 07:51:06 +03:00
|
|
|
free(mImpl);
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIContent*
|
2012-08-22 19:56:38 +04:00
|
|
|
nsAttrAndChildArray::GetSafeChildAt(uint32_t aPos) const
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
|
|
|
if (aPos < ChildCount()) {
|
|
|
|
return ChildAt(aPos);
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
2008-07-12 00:42:19 +04:00
|
|
|
nsIContent * const *
|
2012-08-22 19:56:38 +04:00
|
|
|
nsAttrAndChildArray::GetChildArray(uint32_t* aChildCount) const
|
2008-07-12 00:42:19 +04:00
|
|
|
{
|
2008-12-03 17:02:03 +03:00
|
|
|
*aChildCount = ChildCount();
|
|
|
|
|
|
|
|
if (!*aChildCount) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2008-07-12 00:42:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return reinterpret_cast<nsIContent**>(mImpl->mBuffer + AttrSlotsSize());
|
|
|
|
}
|
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsAttrAndChildArray::InsertChildAt(nsIContent* aChild, uint32_t aPos)
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aChild, "nullchild");
|
|
|
|
NS_ASSERTION(aPos <= ChildCount(), "out-of-bounds");
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t offset = AttrSlotsSize();
|
|
|
|
uint32_t childCount = ChildCount();
|
2004-01-15 20:07:27 +03:00
|
|
|
|
|
|
|
NS_ENSURE_TRUE(childCount < ATTRCHILD_ARRAY_MAX_CHILD_COUNT,
|
|
|
|
NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
// First try to fit new child in existing childlist
|
|
|
|
if (mImpl && offset + childCount < mImpl->mBufferSize) {
|
|
|
|
void** pos = mImpl->mBuffer + offset + aPos;
|
|
|
|
if (childCount != aPos) {
|
|
|
|
memmove(pos + 1, pos, (childCount - aPos) * sizeof(nsIContent*));
|
|
|
|
}
|
2010-05-11 05:12:33 +04:00
|
|
|
SetChildAtPos(pos, aChild, aPos, childCount);
|
2004-01-15 20:07:27 +03:00
|
|
|
|
|
|
|
SetChildCount(childCount + 1);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to fit new child in existing buffer by compressing attrslots
|
|
|
|
if (offset && !mImpl->mBuffer[offset - ATTRSIZE]) {
|
|
|
|
// Compress away all empty slots while we're at it. This might not be the
|
|
|
|
// optimal thing to do.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t attrCount = NonMappedAttrCount();
|
2004-01-15 20:07:27 +03:00
|
|
|
void** newStart = mImpl->mBuffer + attrCount * ATTRSIZE;
|
|
|
|
void** oldStart = mImpl->mBuffer + offset;
|
|
|
|
memmove(newStart, oldStart, aPos * sizeof(nsIContent*));
|
2004-01-16 02:10:45 +03:00
|
|
|
memmove(&newStart[aPos + 1], &oldStart[aPos],
|
2004-01-15 20:07:27 +03:00
|
|
|
(childCount - aPos) * sizeof(nsIContent*));
|
2010-05-11 05:12:33 +04:00
|
|
|
SetChildAtPos(newStart + aPos, aChild, aPos, childCount);
|
2004-01-15 20:07:27 +03:00
|
|
|
|
|
|
|
SetAttrSlotAndChildCount(attrCount, childCount + 1);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can't fit in current buffer, Realloc time!
|
|
|
|
if (!GrowBy(1)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
void** pos = mImpl->mBuffer + offset + aPos;
|
|
|
|
if (childCount != aPos) {
|
|
|
|
memmove(pos + 1, pos, (childCount - aPos) * sizeof(nsIContent*));
|
|
|
|
}
|
2010-05-11 05:12:33 +04:00
|
|
|
SetChildAtPos(pos, aChild, aPos, childCount);
|
2004-01-15 20:07:27 +03:00
|
|
|
|
|
|
|
SetChildCount(childCount + 1);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
nsAttrAndChildArray::RemoveChildAt(uint32_t aPos)
|
2011-11-03 07:36:16 +04:00
|
|
|
{
|
|
|
|
// Just store the return value of TakeChildAt in an nsCOMPtr to
|
|
|
|
// trigger a release.
|
|
|
|
nsCOMPtr<nsIContent> child = TakeChildAt(aPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIContent>
|
2012-08-22 19:56:38 +04:00
|
|
|
nsAttrAndChildArray::TakeChildAt(uint32_t aPos)
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aPos < ChildCount(), "out-of-bounds");
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t childCount = ChildCount();
|
2004-01-15 20:07:27 +03:00
|
|
|
void** pos = mImpl->mBuffer + AttrSlotsSize() + aPos;
|
2007-07-08 11:08:04 +04:00
|
|
|
nsIContent* child = static_cast<nsIContent*>(*pos);
|
2010-05-11 05:12:33 +04:00
|
|
|
if (child->mPreviousSibling) {
|
|
|
|
child->mPreviousSibling->mNextSibling = child->mNextSibling;
|
|
|
|
}
|
|
|
|
if (child->mNextSibling) {
|
|
|
|
child->mNextSibling->mPreviousSibling = child->mPreviousSibling;
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
child->mPreviousSibling = child->mNextSibling = nullptr;
|
2010-05-11 05:12:33 +04:00
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
memmove(pos, pos + 1, (childCount - aPos - 1) * sizeof(nsIContent*));
|
|
|
|
SetChildCount(childCount - 1);
|
2011-11-03 07:36:16 +04:00
|
|
|
|
2013-04-22 15:15:59 +04:00
|
|
|
return dont_AddRef(child);
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2012-10-06 11:19:46 +04:00
|
|
|
nsAttrAndChildArray::IndexOfChild(const nsINode* aPossibleChild) const
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
|
|
|
if (!mImpl) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
void** children = mImpl->mBuffer + AttrSlotsSize();
|
2005-03-11 12:40:03 +03:00
|
|
|
// Use signed here since we compare count to cursor which has to be signed
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t i, count = ChildCount();
|
2005-03-11 12:40:03 +03:00
|
|
|
|
2006-03-01 01:17:23 +03:00
|
|
|
if (count >= CACHE_CHILD_LIMIT) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t cursor = GetIndexFromCache(this);
|
2005-03-11 12:40:03 +03:00
|
|
|
// Need to compare to count here since we may have removed children since
|
|
|
|
// the index was added to the cache.
|
|
|
|
// We're also relying on that GetIndexFromCache returns -1 if no cached
|
|
|
|
// index was found.
|
|
|
|
if (cursor >= count) {
|
|
|
|
cursor = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seek outward from the last found index. |inc| will change sign every
|
|
|
|
// run through the loop. |sign| just exists to make sure the absolute
|
|
|
|
// value of |inc| increases each time through.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t inc = 1, sign = 1;
|
2005-03-11 12:40:03 +03:00
|
|
|
while (cursor >= 0 && cursor < count) {
|
|
|
|
if (children[cursor] == aPossibleChild) {
|
|
|
|
AddIndexToCache(this, cursor);
|
|
|
|
|
|
|
|
return cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor += inc;
|
|
|
|
inc = -inc - sign;
|
|
|
|
sign = -sign;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We ran into one 'edge'. Add inc to cursor once more to get back to
|
|
|
|
// the 'side' where we still need to search, then step in the |sign|
|
|
|
|
// direction.
|
|
|
|
cursor += inc;
|
|
|
|
|
|
|
|
if (sign > 0) {
|
|
|
|
for (; cursor < count; ++cursor) {
|
|
|
|
if (children[cursor] == aPossibleChild) {
|
|
|
|
AddIndexToCache(this, cursor);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
return static_cast<int32_t>(cursor);
|
2005-03-11 12:40:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (; cursor >= 0; --cursor) {
|
|
|
|
if (children[cursor] == aPossibleChild) {
|
|
|
|
AddIndexToCache(this, cursor);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
return static_cast<int32_t>(cursor);
|
2005-03-11 12:40:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The child wasn't even in the remaining children
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
if (children[i] == aPossibleChild) {
|
2012-08-22 19:56:38 +04:00
|
|
|
return static_cast<int32_t>(i);
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
2004-01-15 20:07:27 +03:00
|
|
|
nsAttrAndChildArray::AttrCount() const
|
|
|
|
{
|
2004-01-26 22:22:05 +03:00
|
|
|
return NonMappedAttrCount() + MappedAttrCount();
|
|
|
|
}
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2004-01-26 22:22:05 +03:00
|
|
|
const nsAttrValue*
|
2012-08-22 19:56:38 +04:00
|
|
|
nsAttrAndChildArray::GetAttr(nsIAtom* aLocalName, int32_t aNamespaceID) const
|
2004-01-26 22:22:05 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i, slotCount = AttrSlotCount();
|
2004-01-26 22:22:05 +03:00
|
|
|
if (aNamespaceID == kNameSpaceID_None) {
|
|
|
|
// This should be the common case so lets make an optimized loop
|
2010-12-14 20:16:24 +03:00
|
|
|
for (i = 0; i < slotCount && AttrSlotIsTaken(i); ++i) {
|
2004-01-26 22:22:05 +03:00
|
|
|
if (ATTRS(mImpl)[i].mName.Equals(aLocalName)) {
|
|
|
|
return &ATTRS(mImpl)[i].mValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mImpl && mImpl->mMappedAttrs) {
|
|
|
|
return mImpl->mMappedAttrs->GetAttr(aLocalName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2010-12-14 20:16:24 +03:00
|
|
|
for (i = 0; i < slotCount && AttrSlotIsTaken(i); ++i) {
|
2004-01-26 22:22:05 +03:00
|
|
|
if (ATTRS(mImpl)[i].mName.Equals(aLocalName, aNamespaceID)) {
|
|
|
|
return &ATTRS(mImpl)[i].mValue;
|
|
|
|
}
|
|
|
|
}
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
2013-05-11 02:57:58 +04:00
|
|
|
const nsAttrValue*
|
|
|
|
nsAttrAndChildArray::GetAttr(const nsAString& aLocalName) const
|
|
|
|
{
|
|
|
|
uint32_t i, slotCount = AttrSlotCount();
|
|
|
|
for (i = 0; i < slotCount && AttrSlotIsTaken(i); ++i) {
|
|
|
|
if (ATTRS(mImpl)[i].mName.Equals(aLocalName)) {
|
|
|
|
return &ATTRS(mImpl)[i].mValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mImpl && mImpl->mMappedAttrs) {
|
|
|
|
return mImpl->mMappedAttrs->GetAttr(aLocalName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-07-14 03:29:14 +04:00
|
|
|
const nsAttrValue*
|
|
|
|
nsAttrAndChildArray::GetAttr(const nsAString& aName,
|
|
|
|
nsCaseTreatment aCaseSensitive) const
|
|
|
|
{
|
|
|
|
// Check whether someone is being silly and passing non-lowercase
|
|
|
|
// attr names.
|
|
|
|
if (aCaseSensitive == eIgnoreCase &&
|
|
|
|
nsContentUtils::StringContainsASCIIUpper(aName)) {
|
|
|
|
// Try again with a lowercased name, but make sure we can't reenter this
|
|
|
|
// block by passing eCaseSensitive for aCaseSensitive.
|
|
|
|
nsAutoString lowercase;
|
|
|
|
nsContentUtils::ASCIIToLower(aName, lowercase);
|
|
|
|
return GetAttr(lowercase, eCaseMatters);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i, slotCount = AttrSlotCount();
|
2012-07-14 03:29:14 +04:00
|
|
|
for (i = 0; i < slotCount && AttrSlotIsTaken(i); ++i) {
|
|
|
|
if (ATTRS(mImpl)[i].mName.QualifiedNameEquals(aName)) {
|
|
|
|
return &ATTRS(mImpl)[i].mValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mImpl && mImpl->mMappedAttrs) {
|
|
|
|
const nsAttrValue* val =
|
|
|
|
mImpl->mMappedAttrs->GetAttr(aName);
|
|
|
|
if (val) {
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-07-14 03:29:14 +04:00
|
|
|
}
|
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
const nsAttrValue*
|
2012-08-22 19:56:38 +04:00
|
|
|
nsAttrAndChildArray::AttrAt(uint32_t aPos) const
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
2004-01-26 22:22:05 +03:00
|
|
|
NS_ASSERTION(aPos < AttrCount(),
|
|
|
|
"out-of-bounds access in nsAttrAndChildArray");
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mapped = MappedAttrCount();
|
2004-01-26 22:22:05 +03:00
|
|
|
if (aPos < mapped) {
|
|
|
|
return mImpl->mMappedAttrs->AttrAt(aPos);
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
2004-01-26 22:22:05 +03:00
|
|
|
return &ATTRS(mImpl)[aPos - mapped].mValue;
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-07-22 05:09:41 +03:00
|
|
|
nsAttrAndChildArray::SetAndSwapAttr(nsIAtom* aLocalName, nsAttrValue& aValue)
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i, slotCount = AttrSlotCount();
|
2010-12-14 20:16:24 +03:00
|
|
|
for (i = 0; i < slotCount && AttrSlotIsTaken(i); ++i) {
|
2004-01-15 20:07:27 +03:00
|
|
|
if (ATTRS(mImpl)[i].mName.Equals(aLocalName)) {
|
2004-01-26 22:22:05 +03:00
|
|
|
ATTRS(mImpl)[i].mValue.SwapValueWith(aValue);
|
2004-01-15 20:07:27 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-26 22:22:05 +03:00
|
|
|
NS_ENSURE_TRUE(i < ATTRCHILD_ARRAY_MAX_ATTR_COUNT,
|
2004-01-15 20:07:27 +03:00
|
|
|
NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
if (i == slotCount && !AddAttrSlot()) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
new (&ATTRS(mImpl)[i].mName) nsAttrName(aLocalName);
|
2004-01-26 22:22:05 +03:00
|
|
|
new (&ATTRS(mImpl)[i].mValue) nsAttrValue();
|
|
|
|
ATTRS(mImpl)[i].mValue.SwapValueWith(aValue);
|
2004-01-15 20:07:27 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-07-22 05:09:41 +03:00
|
|
|
nsAttrAndChildArray::SetAndSwapAttr(mozilla::dom::NodeInfo* aName, nsAttrValue& aValue)
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t namespaceID = aName->NamespaceID();
|
2004-01-15 20:07:27 +03:00
|
|
|
nsIAtom* localName = aName->NameAtom();
|
|
|
|
if (namespaceID == kNameSpaceID_None) {
|
2015-07-22 05:09:41 +03:00
|
|
|
return SetAndSwapAttr(localName, aValue);
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i, slotCount = AttrSlotCount();
|
2010-12-14 20:16:24 +03:00
|
|
|
for (i = 0; i < slotCount && AttrSlotIsTaken(i); ++i) {
|
2004-01-15 20:07:27 +03:00
|
|
|
if (ATTRS(mImpl)[i].mName.Equals(localName, namespaceID)) {
|
|
|
|
ATTRS(mImpl)[i].mName.SetTo(aName);
|
2004-01-26 22:22:05 +03:00
|
|
|
ATTRS(mImpl)[i].mValue.Reset();
|
|
|
|
ATTRS(mImpl)[i].mValue.SwapValueWith(aValue);
|
2004-01-15 20:07:27 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-26 22:22:05 +03:00
|
|
|
NS_ENSURE_TRUE(i < ATTRCHILD_ARRAY_MAX_ATTR_COUNT,
|
2004-01-15 20:07:27 +03:00
|
|
|
NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
if (i == slotCount && !AddAttrSlot()) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
new (&ATTRS(mImpl)[i].mName) nsAttrName(aName);
|
2004-01-26 22:22:05 +03:00
|
|
|
new (&ATTRS(mImpl)[i].mValue) nsAttrValue();
|
|
|
|
ATTRS(mImpl)[i].mValue.SwapValueWith(aValue);
|
2004-01-15 20:07:27 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-26 22:22:05 +03:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsAttrAndChildArray::RemoveAttrAt(uint32_t aPos, nsAttrValue& aValue)
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aPos < AttrCount(), "out-of-bounds");
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mapped = MappedAttrCount();
|
2004-01-26 22:22:05 +03:00
|
|
|
if (aPos < mapped) {
|
|
|
|
if (mapped == 1) {
|
2006-11-11 03:04:46 +03:00
|
|
|
// We're removing the last mapped attribute. Can't swap in this
|
|
|
|
// case; have to copy.
|
|
|
|
aValue.SetTo(*mImpl->mMappedAttrs->AttrAt(0));
|
2004-01-26 22:22:05 +03:00
|
|
|
NS_RELEASE(mImpl->mMappedAttrs);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsMappedAttributes> mapped =
|
2012-08-04 11:44:01 +04:00
|
|
|
GetModifiableMapped(nullptr, nullptr, false);
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2006-11-11 03:04:46 +03:00
|
|
|
mapped->RemoveAttrAt(aPos, aValue);
|
2004-01-26 22:22:05 +03:00
|
|
|
|
|
|
|
return MakeMappedUnique(mapped);
|
|
|
|
}
|
|
|
|
|
|
|
|
aPos -= mapped;
|
2006-11-11 03:04:46 +03:00
|
|
|
ATTRS(mImpl)[aPos].mValue.SwapValueWith(aValue);
|
2004-01-15 20:07:27 +03:00
|
|
|
ATTRS(mImpl)[aPos].~InternalAttr();
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t slotCount = AttrSlotCount();
|
2004-01-15 20:07:27 +03:00
|
|
|
memmove(&ATTRS(mImpl)[aPos],
|
|
|
|
&ATTRS(mImpl)[aPos + 1],
|
|
|
|
(slotCount - aPos - 1) * sizeof(InternalAttr));
|
2012-07-20 15:16:17 +04:00
|
|
|
memset(&ATTRS(mImpl)[slotCount - 1], 0, sizeof(InternalAttr));
|
2004-01-26 22:22:05 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
2005-12-30 23:12:35 +03:00
|
|
|
const nsAttrName*
|
2012-08-22 19:56:38 +04:00
|
|
|
nsAttrAndChildArray::AttrNameAt(uint32_t aPos) const
|
2005-12-30 23:12:35 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aPos < AttrCount(),
|
|
|
|
"out-of-bounds access in nsAttrAndChildArray");
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mapped = MappedAttrCount();
|
2005-12-30 23:12:35 +03:00
|
|
|
if (aPos < mapped) {
|
|
|
|
return mImpl->mMappedAttrs->NameAt(aPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ATTRS(mImpl)[aPos - mapped].mName;
|
|
|
|
}
|
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
const nsAttrName*
|
2012-08-22 19:56:38 +04:00
|
|
|
nsAttrAndChildArray::GetSafeAttrNameAt(uint32_t aPos) const
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mapped = MappedAttrCount();
|
2004-01-26 22:22:05 +03:00
|
|
|
if (aPos < mapped) {
|
|
|
|
return mImpl->mMappedAttrs->NameAt(aPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
aPos -= mapped;
|
2005-12-29 02:33:14 +03:00
|
|
|
if (aPos >= AttrSlotCount()) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2005-12-29 00:52:39 +03:00
|
|
|
}
|
2004-01-15 20:07:27 +03:00
|
|
|
|
|
|
|
void** pos = mImpl->mBuffer + aPos * ATTRSIZE;
|
2005-12-29 00:52:39 +03:00
|
|
|
if (!*pos) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2005-12-29 00:52:39 +03:00
|
|
|
}
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2007-07-08 11:08:04 +04:00
|
|
|
return &reinterpret_cast<InternalAttr*>(pos)->mName;
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const nsAttrName*
|
2010-03-08 18:45:00 +03:00
|
|
|
nsAttrAndChildArray::GetExistingAttrNameFromQName(const nsAString& aName) const
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i, slotCount = AttrSlotCount();
|
2010-12-14 20:16:24 +03:00
|
|
|
for (i = 0; i < slotCount && AttrSlotIsTaken(i); ++i) {
|
2004-02-10 12:08:06 +03:00
|
|
|
if (ATTRS(mImpl)[i].mName.QualifiedNameEquals(aName)) {
|
2004-01-15 20:07:27 +03:00
|
|
|
return &ATTRS(mImpl)[i].mName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-26 22:22:05 +03:00
|
|
|
if (mImpl && mImpl->mMappedAttrs) {
|
2004-02-10 12:08:06 +03:00
|
|
|
return mImpl->mMappedAttrs->GetExistingAttrNameFromQName(aName);
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
|
|
|
nsAttrAndChildArray::IndexOfAttr(nsIAtom* aLocalName, int32_t aNamespaceID) const
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t idx;
|
2012-07-14 03:29:14 +04:00
|
|
|
if (mImpl && mImpl->mMappedAttrs && aNamespaceID == kNameSpaceID_None) {
|
|
|
|
idx = mImpl->mMappedAttrs->IndexOfAttr(aLocalName);
|
2004-02-20 01:16:16 +03:00
|
|
|
if (idx >= 0) {
|
|
|
|
return idx;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i;
|
|
|
|
uint32_t mapped = MappedAttrCount();
|
|
|
|
uint32_t slotCount = AttrSlotCount();
|
2004-01-15 20:07:27 +03:00
|
|
|
if (aNamespaceID == kNameSpaceID_None) {
|
|
|
|
// This should be the common case so lets make an optimized loop
|
2010-12-14 20:16:24 +03:00
|
|
|
for (i = 0; i < slotCount && AttrSlotIsTaken(i); ++i) {
|
2004-01-15 20:07:27 +03:00
|
|
|
if (ATTRS(mImpl)[i].mName.Equals(aLocalName)) {
|
2004-01-26 22:22:05 +03:00
|
|
|
return i + mapped;
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2010-12-14 20:16:24 +03:00
|
|
|
for (i = 0; i < slotCount && AttrSlotIsTaken(i); ++i) {
|
2004-01-15 20:07:27 +03:00
|
|
|
if (ATTRS(mImpl)[i].mName.Equals(aLocalName, aNamespaceID)) {
|
2004-01-26 22:22:05 +03:00
|
|
|
return i + mapped;
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-01-26 22:22:05 +03:00
|
|
|
nsresult
|
|
|
|
nsAttrAndChildArray::SetAndTakeMappedAttr(nsIAtom* aLocalName,
|
|
|
|
nsAttrValue& aValue,
|
2008-01-09 12:38:28 +03:00
|
|
|
nsMappedAttributeElement* aContent,
|
2004-04-13 01:56:09 +04:00
|
|
|
nsHTMLStyleSheet* aSheet)
|
2004-01-26 22:22:05 +03:00
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool willAdd = true;
|
2011-08-24 21:27:52 +04:00
|
|
|
if (mImpl && mImpl->mMappedAttrs) {
|
2012-08-04 11:44:01 +04:00
|
|
|
willAdd = !mImpl->mMappedAttrs->GetAttr(aLocalName);
|
2011-08-24 21:27:52 +04:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsMappedAttributes> mapped =
|
2012-08-04 11:44:01 +04:00
|
|
|
GetModifiableMapped(aContent, aSheet, willAdd);
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2012-09-06 11:14:49 +04:00
|
|
|
mapped->SetAndTakeAttr(aLocalName, aValue);
|
2004-01-26 22:22:05 +03:00
|
|
|
|
|
|
|
return MakeMappedUnique(mapped);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-03-22 08:10:51 +04:00
|
|
|
nsAttrAndChildArray::DoSetMappedAttrStyleSheet(nsHTMLStyleSheet* aSheet)
|
2004-01-26 22:22:05 +03:00
|
|
|
{
|
2012-03-22 08:10:51 +04:00
|
|
|
NS_PRECONDITION(mImpl && mImpl->mMappedAttrs,
|
|
|
|
"Should have mapped attrs here!");
|
|
|
|
if (aSheet == mImpl->mMappedAttrs->GetStyleSheet()) {
|
2004-01-26 22:22:05 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsMappedAttributes> mapped =
|
2012-08-04 11:44:01 +04:00
|
|
|
GetModifiableMapped(nullptr, nullptr, false);
|
2004-01-26 22:22:05 +03:00
|
|
|
|
|
|
|
mapped->SetStyleSheet(aSheet);
|
|
|
|
|
|
|
|
return MakeMappedUnique(mapped);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsAttrAndChildArray::WalkMappedAttributeStyleRules(nsRuleWalker* aRuleWalker)
|
|
|
|
{
|
2010-04-07 02:32:51 +04:00
|
|
|
if (mImpl && mImpl->mMappedAttrs) {
|
2004-01-26 22:22:05 +03:00
|
|
|
aRuleWalker->Forward(mImpl->mMappedAttrs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-03 02:41:24 +03:00
|
|
|
void
|
|
|
|
nsAttrAndChildArray::Compact()
|
|
|
|
{
|
|
|
|
if (!mImpl) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First compress away empty attrslots
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t slotCount = AttrSlotCount();
|
|
|
|
uint32_t attrCount = NonMappedAttrCount();
|
|
|
|
uint32_t childCount = ChildCount();
|
2008-02-03 02:41:24 +03:00
|
|
|
|
|
|
|
if (attrCount < slotCount) {
|
|
|
|
memmove(mImpl->mBuffer + attrCount * ATTRSIZE,
|
|
|
|
mImpl->mBuffer + slotCount * ATTRSIZE,
|
|
|
|
childCount * sizeof(nsIContent*));
|
|
|
|
SetAttrSlotCount(attrCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then resize or free buffer
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t newSize = attrCount * ATTRSIZE + childCount;
|
2008-02-03 02:41:24 +03:00
|
|
|
if (!newSize && !mImpl->mMappedAttrs) {
|
2015-02-19 07:51:06 +03:00
|
|
|
free(mImpl);
|
2012-07-30 18:20:58 +04:00
|
|
|
mImpl = nullptr;
|
2008-02-03 02:41:24 +03:00
|
|
|
}
|
|
|
|
else if (newSize < mImpl->mBufferSize) {
|
2015-02-19 07:51:06 +03:00
|
|
|
mImpl = static_cast<Impl*>(realloc(mImpl, (newSize + NS_IMPL_EXTRA_SIZE) * sizeof(nsIContent*)));
|
2008-02-03 02:41:24 +03:00
|
|
|
NS_ASSERTION(mImpl, "failed to reallocate to smaller buffer");
|
|
|
|
|
|
|
|
mImpl->mBufferSize = newSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
void
|
|
|
|
nsAttrAndChildArray::Clear()
|
|
|
|
{
|
|
|
|
if (!mImpl) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-01-26 22:22:05 +03:00
|
|
|
if (mImpl->mMappedAttrs) {
|
|
|
|
NS_RELEASE(mImpl->mMappedAttrs);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i, slotCount = AttrSlotCount();
|
2010-12-14 20:16:24 +03:00
|
|
|
for (i = 0; i < slotCount && AttrSlotIsTaken(i); ++i) {
|
2004-01-15 20:07:27 +03:00
|
|
|
ATTRS(mImpl)[i].~InternalAttr();
|
|
|
|
}
|
|
|
|
|
2008-04-11 21:29:06 +04:00
|
|
|
nsAutoScriptBlocker scriptBlocker;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t end = slotCount * ATTRSIZE + ChildCount();
|
2004-01-15 20:07:27 +03:00
|
|
|
for (i = slotCount * ATTRSIZE; i < end; ++i) {
|
2007-07-08 11:08:04 +04:00
|
|
|
nsIContent* child = static_cast<nsIContent*>(mImpl->mBuffer[i]);
|
2011-10-17 18:59:28 +04:00
|
|
|
// making this false so tree teardown doesn't end up being
|
2005-04-06 03:54:35 +04:00
|
|
|
// O(N*D) (number of nodes times average depth of tree).
|
2011-10-17 18:59:28 +04:00
|
|
|
child->UnbindFromTree(false); // XXX is it better to let the owner do this?
|
2012-02-20 16:54:45 +04:00
|
|
|
// Make sure to unlink our kids from each other, since someone
|
|
|
|
// else could stil be holding references to some of them.
|
2010-05-12 07:34:49 +04:00
|
|
|
|
|
|
|
// XXXbz We probably can't push this assignment down into the |aNullParent|
|
|
|
|
// case of UnbindFromTree because we still need the assignment in
|
|
|
|
// RemoveChildAt. In particular, ContentRemoved fires between
|
|
|
|
// RemoveChildAt and UnbindFromTree, and in ContentRemoved the sibling
|
|
|
|
// chain needs to be correct. Though maybe we could set the prev and next
|
|
|
|
// to point to each other but keep the kid being removed pointing to them
|
|
|
|
// through ContentRemoved so consumers can find where it used to be in the
|
|
|
|
// list?
|
2012-07-30 18:20:58 +04:00
|
|
|
child->mPreviousSibling = child->mNextSibling = nullptr;
|
2004-01-15 20:07:27 +03:00
|
|
|
NS_RELEASE(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
SetAttrSlotAndChildCount(0, 0);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
2004-01-26 22:22:05 +03:00
|
|
|
nsAttrAndChildArray::NonMappedAttrCount() const
|
|
|
|
{
|
|
|
|
if (!mImpl) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count = AttrSlotCount();
|
2004-01-26 22:22:05 +03:00
|
|
|
while (count > 0 && !mImpl->mBuffer[(count - 1) * ATTRSIZE]) {
|
|
|
|
--count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
2004-01-26 22:22:05 +03:00
|
|
|
nsAttrAndChildArray::MappedAttrCount() const
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
return mImpl && mImpl->mMappedAttrs ? (uint32_t)mImpl->mMappedAttrs->Count() : 0;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
2012-08-04 11:44:01 +04:00
|
|
|
nsMappedAttributes*
|
2008-01-09 12:38:28 +03:00
|
|
|
nsAttrAndChildArray::GetModifiableMapped(nsMappedAttributeElement* aContent,
|
2004-04-13 01:56:09 +04:00
|
|
|
nsHTMLStyleSheet* aSheet,
|
2012-08-04 11:44:01 +04:00
|
|
|
bool aWillAddAttr)
|
2004-01-26 22:22:05 +03:00
|
|
|
{
|
|
|
|
if (mImpl && mImpl->mMappedAttrs) {
|
2012-08-04 11:44:01 +04:00
|
|
|
return mImpl->mMappedAttrs->Clone(aWillAddAttr);
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
2012-08-04 11:44:01 +04:00
|
|
|
MOZ_ASSERT(aContent, "Trying to create modifiable without content");
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2005-01-12 22:45:38 +03:00
|
|
|
nsMapRuleToAttributesFunc mapRuleFunc =
|
|
|
|
aContent->GetAttributeMappingFunction();
|
2012-08-04 11:44:01 +04:00
|
|
|
return new nsMappedAttributes(aSheet, mapRuleFunc);
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsAttrAndChildArray::MakeMappedUnique(nsMappedAttributes* aAttributes)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aAttributes, "missing attributes");
|
|
|
|
|
|
|
|
if (!mImpl && !GrowBy(1)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aAttributes->GetStyleSheet()) {
|
|
|
|
// This doesn't currently happen, but it could if we do loading right
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsMappedAttributes> mapped(aAttributes);
|
2004-01-26 22:22:05 +03:00
|
|
|
mapped.swap(mImpl->mMappedAttrs);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsMappedAttributes> mapped =
|
2004-04-13 01:56:09 +04:00
|
|
|
aAttributes->GetStyleSheet()->UniqueMappedAttributes(aAttributes);
|
|
|
|
NS_ENSURE_TRUE(mapped, NS_ERROR_OUT_OF_MEMORY);
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2004-01-31 01:08:23 +03:00
|
|
|
if (mapped != aAttributes) {
|
|
|
|
// Reset the stylesheet of aAttributes so that it doesn't spend time
|
|
|
|
// trying to remove itself from the hash. There is no risk that aAttributes
|
|
|
|
// is in the hash since it will always have come from GetModifiableMapped,
|
|
|
|
// which never returns maps that are in the hash (such hashes are by
|
|
|
|
// nature not modifiable).
|
|
|
|
aAttributes->DropStyleSheetReference();
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
mapped.swap(mImpl->mMappedAttrs);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2012-08-22 19:56:38 +04:00
|
|
|
nsAttrAndChildArray::GrowBy(uint32_t aGrowSize)
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
2015-07-10 02:18:30 +03:00
|
|
|
CheckedUint32 size = 0;
|
|
|
|
if (mImpl) {
|
|
|
|
size += mImpl->mBufferSize;
|
|
|
|
size += NS_IMPL_EXTRA_SIZE;
|
|
|
|
if (!size.isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2015-07-10 02:18:30 +03:00
|
|
|
CheckedUint32 minSize = size.value();
|
|
|
|
minSize += aGrowSize;
|
|
|
|
if (!minSize.isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (minSize.value() <= ATTRCHILD_ARRAY_LINEAR_THRESHOLD) {
|
2004-01-15 20:07:27 +03:00
|
|
|
do {
|
|
|
|
size += ATTRCHILD_ARRAY_GROWSIZE;
|
2015-07-10 02:18:30 +03:00
|
|
|
if (!size.isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} while (size.value() < minSize.value());
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
else {
|
2015-07-10 02:18:30 +03:00
|
|
|
size = 1u << mozilla::CeilingLog2(minSize.value());
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool needToInitialize = !mImpl;
|
2015-07-10 02:18:30 +03:00
|
|
|
CheckedUint32 neededSize = size;
|
|
|
|
neededSize *= sizeof(void*);
|
|
|
|
if (!neededSize.isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Impl* newImpl = static_cast<Impl*>(realloc(mImpl, neededSize.value()));
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(newImpl, false);
|
2008-02-03 02:41:24 +03:00
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
mImpl = newImpl;
|
2008-02-03 02:41:24 +03:00
|
|
|
|
|
|
|
// Set initial counts if we didn't have a buffer before
|
2009-07-23 02:31:01 +04:00
|
|
|
if (needToInitialize) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mImpl->mMappedAttrs = nullptr;
|
2004-01-15 20:07:27 +03:00
|
|
|
SetAttrSlotAndChildCount(0, 0);
|
|
|
|
}
|
|
|
|
|
2015-07-10 02:18:30 +03:00
|
|
|
mImpl->mBufferSize = size.value() - NS_IMPL_EXTRA_SIZE;
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2004-01-15 20:07:27 +03:00
|
|
|
nsAttrAndChildArray::AddAttrSlot()
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t slotCount = AttrSlotCount();
|
|
|
|
uint32_t childCount = ChildCount();
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2015-07-10 02:18:30 +03:00
|
|
|
CheckedUint32 size = slotCount;
|
|
|
|
size += 1;
|
|
|
|
size *= ATTRSIZE;
|
|
|
|
size += childCount;
|
|
|
|
if (!size.isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
// Grow buffer if needed
|
2015-07-10 02:18:30 +03:00
|
|
|
if (!(mImpl && mImpl->mBufferSize >= size.value()) &&
|
2004-01-15 20:07:27 +03:00
|
|
|
!GrowBy(ATTRSIZE)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
2015-07-10 02:18:30 +03:00
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
void** offset = mImpl->mBuffer + slotCount * ATTRSIZE;
|
2004-02-10 01:48:53 +03:00
|
|
|
|
|
|
|
if (childCount > 0) {
|
|
|
|
memmove(&ATTRS(mImpl)[slotCount + 1], &ATTRS(mImpl)[slotCount],
|
|
|
|
childCount * sizeof(nsIContent*));
|
|
|
|
}
|
2004-01-15 20:07:27 +03:00
|
|
|
|
|
|
|
SetAttrSlotCount(slotCount + 1);
|
2012-07-30 18:20:58 +04:00
|
|
|
offset[0] = nullptr;
|
|
|
|
offset[1] = nullptr;
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
2010-05-11 05:12:33 +04:00
|
|
|
|
|
|
|
inline void
|
|
|
|
nsAttrAndChildArray::SetChildAtPos(void** aPos, nsIContent* aChild,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aIndex, uint32_t aChildCount)
|
2010-05-11 05:12:33 +04:00
|
|
|
{
|
2012-02-20 16:54:45 +04:00
|
|
|
NS_PRECONDITION(!aChild->GetNextSibling(), "aChild with next sibling?");
|
|
|
|
NS_PRECONDITION(!aChild->GetPreviousSibling(), "aChild with prev sibling?");
|
2010-05-11 05:12:33 +04:00
|
|
|
|
|
|
|
*aPos = aChild;
|
|
|
|
NS_ADDREF(aChild);
|
|
|
|
if (aIndex != 0) {
|
|
|
|
nsIContent* previous = static_cast<nsIContent*>(*(aPos - 1));
|
|
|
|
aChild->mPreviousSibling = previous;
|
|
|
|
previous->mNextSibling = aChild;
|
|
|
|
}
|
|
|
|
if (aIndex != aChildCount) {
|
|
|
|
nsIContent* next = static_cast<nsIContent*>(*(aPos + 1));
|
|
|
|
aChild->mNextSibling = next;
|
|
|
|
next->mPreviousSibling = aChild;
|
|
|
|
}
|
|
|
|
}
|
2011-07-12 18:56:01 +04:00
|
|
|
|
2012-02-02 01:58:01 +04:00
|
|
|
size_t
|
2013-06-23 16:03:39 +04:00
|
|
|
nsAttrAndChildArray::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
|
2011-07-12 18:56:01 +04:00
|
|
|
{
|
2012-02-02 01:58:01 +04:00
|
|
|
size_t n = 0;
|
2011-07-12 18:56:01 +04:00
|
|
|
if (mImpl) {
|
|
|
|
// Don't add the size taken by *mMappedAttrs because it's shared.
|
|
|
|
|
2012-02-02 01:58:01 +04:00
|
|
|
n += aMallocSizeOf(mImpl);
|
2011-08-11 02:54:19 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t slotCount = AttrSlotCount();
|
|
|
|
for (uint32_t i = 0; i < slotCount && AttrSlotIsTaken(i); ++i) {
|
2011-08-11 02:54:19 +04:00
|
|
|
nsAttrValue* value = &ATTRS(mImpl)[i].mValue;
|
2012-02-02 01:58:01 +04:00
|
|
|
n += value->SizeOfExcludingThis(aMallocSizeOf);
|
2011-08-11 02:54:19 +04:00
|
|
|
}
|
2011-07-12 18:56:01 +04:00
|
|
|
}
|
|
|
|
|
2012-02-02 01:58:01 +04:00
|
|
|
return n;
|
2011-07-12 18:56:01 +04:00
|
|
|
}
|
|
|
|
|