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.
|
|
|
|
*/
|
|
|
|
|
2018-08-07 22:07:26 +03:00
|
|
|
#include "AttrArray.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 "nsMappedAttributes.h"
|
|
|
|
#include "nsUnicharUtils.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;
|
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
AttrArray::Impl::~Impl()
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
2018-08-13 16:35:57 +03:00
|
|
|
for (InternalAttr& attr : NonMappedAttrs()) {
|
|
|
|
attr.~InternalAttr();
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
NS_IF_RELEASE(mMappedAttrs);
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2004-01-26 22:22:05 +03:00
|
|
|
const nsAttrValue*
|
2018-08-07 22:07:26 +03:00
|
|
|
AttrArray::GetAttr(nsAtom* aLocalName, int32_t aNamespaceID) const
|
2004-01-26 22:22:05 +03:00
|
|
|
{
|
|
|
|
if (aNamespaceID == kNameSpaceID_None) {
|
|
|
|
// This should be the common case so lets make an optimized loop
|
2018-08-13 16:35:57 +03:00
|
|
|
for (const InternalAttr& attr : NonMappedAttrs()) {
|
|
|
|
if (attr.mName.Equals(aLocalName)) {
|
|
|
|
return &attr.mValue;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mImpl && mImpl->mMappedAttrs) {
|
|
|
|
return mImpl->mMappedAttrs->GetAttr(aLocalName);
|
|
|
|
}
|
2018-08-13 16:35:57 +03:00
|
|
|
} else {
|
|
|
|
for (const InternalAttr& attr : NonMappedAttrs()) {
|
|
|
|
if (attr.mName.Equals(aLocalName, aNamespaceID)) {
|
|
|
|
return &attr.mValue;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
}
|
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*
|
2018-08-07 22:07:26 +03:00
|
|
|
AttrArray::GetAttr(const nsAString& aLocalName) const
|
2013-05-11 02:57:58 +04:00
|
|
|
{
|
2018-08-13 16:35:57 +03:00
|
|
|
for (const InternalAttr& attr : NonMappedAttrs()) {
|
|
|
|
if (attr.mName.Equals(aLocalName)) {
|
|
|
|
return &attr.mValue;
|
2013-05-11 02:57:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mImpl && mImpl->mMappedAttrs) {
|
|
|
|
return mImpl->mMappedAttrs->GetAttr(aLocalName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-07-14 03:29:14 +04:00
|
|
|
const nsAttrValue*
|
2018-08-07 22:07:26 +03:00
|
|
|
AttrArray::GetAttr(const nsAString& aName,
|
|
|
|
nsCaseTreatment aCaseSensitive) const
|
2012-07-14 03:29:14 +04:00
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
for (const InternalAttr& attr : NonMappedAttrs()) {
|
|
|
|
if (attr.mName.QualifiedNameEquals(aName)) {
|
|
|
|
return &attr.mValue;
|
2012-07-14 03:29:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mImpl && mImpl->mMappedAttrs) {
|
2018-08-13 16:35:57 +03:00
|
|
|
return mImpl->mMappedAttrs->GetAttr(aName);
|
2012-07-14 03:29:14 +04:00
|
|
|
}
|
|
|
|
|
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*
|
2018-08-07 22:07:26 +03:00
|
|
|
AttrArray::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(),
|
2018-08-07 22:07:26 +03:00
|
|
|
"out-of-bounds access in AttrArray");
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2016-05-01 15:15:26 +03:00
|
|
|
uint32_t nonmapped = NonMappedAttrCount();
|
|
|
|
if (aPos < nonmapped) {
|
2018-08-13 16:35:57 +03:00
|
|
|
return &mImpl->NonMappedAttrs()[aPos].mValue;
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
2016-05-01 15:15:26 +03:00
|
|
|
return mImpl->mMappedAttrs->AttrAt(aPos - nonmapped);
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
template<typename Name>
|
|
|
|
inline nsresult
|
|
|
|
AttrArray::AddNewAttribute(Name* aName, nsAttrValue& aValue)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mImpl || mImpl->mCapacity >= mImpl->mAttrCount);
|
|
|
|
if (!mImpl || mImpl->mCapacity == mImpl->mAttrCount) {
|
|
|
|
if (!GrowBy(1)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InternalAttr& attr = mImpl->mBuffer[mImpl->mAttrCount++];
|
|
|
|
new (&attr.mName) nsAttrName(aName);
|
|
|
|
new (&attr.mValue) nsAttrValue();
|
|
|
|
attr.mValue.SwapValueWith(aValue);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
nsresult
|
2018-08-07 22:07:26 +03:00
|
|
|
AttrArray::SetAndSwapAttr(nsAtom* aLocalName, nsAttrValue& aValue,
|
|
|
|
bool* aHadValue)
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
2017-05-19 00:09:01 +03:00
|
|
|
*aHadValue = false;
|
2018-08-13 16:35:57 +03:00
|
|
|
|
|
|
|
for (InternalAttr& attr : NonMappedAttrs()) {
|
|
|
|
if (attr.mName.Equals(aLocalName)) {
|
|
|
|
attr.mValue.SwapValueWith(aValue);
|
2017-05-19 00:09:01 +03:00
|
|
|
*aHadValue = true;
|
2004-01-15 20:07:27 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
return AddNewAttribute(aLocalName, aValue);
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2018-08-07 22:07:26 +03:00
|
|
|
AttrArray::SetAndSwapAttr(mozilla::dom::NodeInfo* aName,
|
|
|
|
nsAttrValue& aValue, bool* aHadValue)
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t namespaceID = aName->NamespaceID();
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* localName = aName->NameAtom();
|
2004-01-15 20:07:27 +03:00
|
|
|
if (namespaceID == kNameSpaceID_None) {
|
2017-05-19 00:09:01 +03:00
|
|
|
return SetAndSwapAttr(localName, aValue, aHadValue);
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
2017-05-19 00:09:01 +03:00
|
|
|
*aHadValue = false;
|
2018-08-13 16:35:57 +03:00
|
|
|
for (InternalAttr& attr : NonMappedAttrs()) {
|
|
|
|
if (attr.mName.Equals(localName, namespaceID)) {
|
|
|
|
attr.mName.SetTo(aName);
|
|
|
|
attr.mValue.SwapValueWith(aValue);
|
2017-05-19 00:09:01 +03:00
|
|
|
*aHadValue = true;
|
2004-01-15 20:07:27 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
return AddNewAttribute(aName, aValue);
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
2004-01-26 22:22:05 +03:00
|
|
|
nsresult
|
2018-08-07 22:07:26 +03:00
|
|
|
AttrArray::RemoveAttrAt(uint32_t aPos, nsAttrValue& aValue)
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aPos < AttrCount(), "out-of-bounds");
|
|
|
|
|
2016-05-01 15:15:26 +03:00
|
|
|
uint32_t nonmapped = NonMappedAttrCount();
|
|
|
|
if (aPos < nonmapped) {
|
2018-08-13 16:35:57 +03:00
|
|
|
mImpl->mBuffer[aPos].mValue.SwapValueWith(aValue);
|
|
|
|
mImpl->mBuffer[aPos].~InternalAttr();
|
|
|
|
|
|
|
|
memmove(mImpl->mBuffer + aPos,
|
|
|
|
mImpl->mBuffer + aPos + 1,
|
|
|
|
(mImpl->mAttrCount - aPos - 1) * sizeof(InternalAttr));
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
--mImpl->mAttrCount;
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2016-05-01 15:15:26 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2016-05-01 15:15:26 +03:00
|
|
|
if (MappedAttrCount() == 1) {
|
|
|
|
// We're removing the last mapped attribute. Can't swap in this
|
|
|
|
// case; have to copy.
|
|
|
|
aValue.SetTo(*mImpl->mMappedAttrs->AttrAt(0));
|
|
|
|
NS_RELEASE(mImpl->mMappedAttrs);
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2016-05-01 15:15:26 +03:00
|
|
|
return NS_OK;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
2016-05-01 15:15:26 +03:00
|
|
|
RefPtr<nsMappedAttributes> mapped =
|
|
|
|
GetModifiableMapped(nullptr, nullptr, false);
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2016-05-01 15:15:26 +03:00
|
|
|
mapped->RemoveAttrAt(aPos - nonmapped, aValue);
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2016-05-01 15:15:26 +03:00
|
|
|
return MakeMappedUnique(mapped);
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
2016-10-04 15:20:00 +03:00
|
|
|
mozilla::dom::BorrowedAttrInfo
|
2018-08-07 22:07:26 +03:00
|
|
|
AttrArray::AttrInfoAt(uint32_t aPos) const
|
2016-07-22 06:10:23 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aPos < AttrCount(),
|
2018-08-07 22:07:26 +03:00
|
|
|
"out-of-bounds access in AttrArray");
|
2016-07-22 06:10:23 +03:00
|
|
|
|
|
|
|
uint32_t nonmapped = NonMappedAttrCount();
|
|
|
|
if (aPos < nonmapped) {
|
2018-08-13 16:35:57 +03:00
|
|
|
InternalAttr& attr = mImpl->mBuffer[aPos];
|
|
|
|
return BorrowedAttrInfo(&attr.mName, &attr.mValue);
|
2016-07-22 06:10:23 +03:00
|
|
|
}
|
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
return BorrowedAttrInfo(
|
|
|
|
mImpl->mMappedAttrs->NameAt(aPos - nonmapped),
|
|
|
|
mImpl->mMappedAttrs->AttrAt(aPos - nonmapped));
|
2016-07-22 06:10:23 +03:00
|
|
|
}
|
|
|
|
|
2005-12-30 23:12:35 +03:00
|
|
|
const nsAttrName*
|
2018-08-07 22:07:26 +03:00
|
|
|
AttrArray::AttrNameAt(uint32_t aPos) const
|
2005-12-30 23:12:35 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aPos < AttrCount(),
|
2018-08-07 22:07:26 +03:00
|
|
|
"out-of-bounds access in AttrArray");
|
2005-12-30 23:12:35 +03:00
|
|
|
|
2016-05-01 15:15:26 +03:00
|
|
|
uint32_t nonmapped = NonMappedAttrCount();
|
|
|
|
if (aPos < nonmapped) {
|
2018-08-13 16:35:57 +03:00
|
|
|
return &mImpl->mBuffer[aPos].mName;
|
2005-12-30 23:12:35 +03:00
|
|
|
}
|
|
|
|
|
2016-05-01 15:15:26 +03:00
|
|
|
return mImpl->mMappedAttrs->NameAt(aPos - nonmapped);
|
2005-12-30 23:12:35 +03:00
|
|
|
}
|
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
const nsAttrName*
|
2018-08-07 22:07:26 +03:00
|
|
|
AttrArray::GetSafeAttrNameAt(uint32_t aPos) const
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
2016-05-01 15:15:26 +03:00
|
|
|
uint32_t nonmapped = NonMappedAttrCount();
|
|
|
|
if (aPos < nonmapped) {
|
2018-08-13 16:35:57 +03:00
|
|
|
return &mImpl->mBuffer[aPos].mName;
|
2005-12-29 00:52:39 +03:00
|
|
|
}
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2016-05-01 15:15:26 +03:00
|
|
|
if (aPos >= AttrCount()) {
|
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
|
|
|
|
2016-05-01 15:15:26 +03:00
|
|
|
return mImpl->mMappedAttrs->NameAt(aPos - nonmapped);
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const nsAttrName*
|
2018-08-07 22:07:26 +03:00
|
|
|
AttrArray::GetExistingAttrNameFromQName(const nsAString& aName) const
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
2018-08-13 16:35:57 +03:00
|
|
|
for (const InternalAttr& attr : NonMappedAttrs()) {
|
|
|
|
if (attr.mName.QualifiedNameEquals(aName)) {
|
|
|
|
return &attr.mName;
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2018-08-07 22:07:26 +03:00
|
|
|
AttrArray::IndexOfAttr(nsAtom* aLocalName, int32_t aNamespaceID) const
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
2018-08-13 16:35:57 +03:00
|
|
|
if (!mImpl) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t idx;
|
2018-08-13 16:35:57 +03:00
|
|
|
if (mImpl->mMappedAttrs && aNamespaceID == kNameSpaceID_None) {
|
2012-07-14 03:29:14 +04:00
|
|
|
idx = mImpl->mMappedAttrs->IndexOfAttr(aLocalName);
|
2004-02-20 01:16:16 +03:00
|
|
|
if (idx >= 0) {
|
2016-05-01 15:15:26 +03:00
|
|
|
return NonMappedAttrCount() + idx;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
uint32_t i = 0;
|
2004-01-15 20:07:27 +03:00
|
|
|
if (aNamespaceID == kNameSpaceID_None) {
|
|
|
|
// This should be the common case so lets make an optimized loop
|
2017-06-18 05:39:15 +03:00
|
|
|
// Note that here we don't check for AttrSlotIsTaken() in the loop
|
|
|
|
// condition for the sake of performance because comparing aLocalName
|
|
|
|
// against null would fail in the loop body (since Equals() just compares
|
|
|
|
// the raw pointer value of aLocalName to what AttrSlotIsTaken() would be
|
|
|
|
// checking.
|
2018-08-13 16:35:57 +03:00
|
|
|
for (const InternalAttr& attr : NonMappedAttrs()) {
|
|
|
|
if (attr.mName.Equals(aLocalName)) {
|
2016-05-01 15:15:26 +03:00
|
|
|
return i;
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
2018-08-13 16:35:57 +03:00
|
|
|
++i;
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
2018-08-13 16:35:57 +03:00
|
|
|
} else {
|
|
|
|
for (const InternalAttr& attr : NonMappedAttrs()) {
|
|
|
|
if (attr.mName.Equals(aLocalName, aNamespaceID)) {
|
2016-05-01 15:15:26 +03:00
|
|
|
return i;
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
2018-08-13 16:35:57 +03:00
|
|
|
++i;
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-01-26 22:22:05 +03:00
|
|
|
nsresult
|
2018-08-07 22:07:26 +03:00
|
|
|
AttrArray::SetAndSwapMappedAttr(nsAtom* aLocalName,
|
|
|
|
nsAttrValue& aValue,
|
|
|
|
nsMappedAttributeElement* aContent,
|
|
|
|
nsHTMLStyleSheet* aSheet,
|
|
|
|
bool* aHadValue)
|
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
|
|
|
|
2017-05-19 00:09:01 +03:00
|
|
|
mapped->SetAndSwapAttr(aLocalName, aValue, aHadValue);
|
2004-01-26 22:22:05 +03:00
|
|
|
|
|
|
|
return MakeMappedUnique(mapped);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2018-08-07 22:07:26 +03:00
|
|
|
AttrArray::DoSetMappedAttrStyleSheet(nsHTMLStyleSheet* aSheet)
|
2004-01-26 22:22:05 +03:00
|
|
|
{
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(mImpl && mImpl->mMappedAttrs,
|
2012-03-22 08:10:51 +04:00
|
|
|
"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);
|
|
|
|
}
|
|
|
|
|
2018-07-06 21:43:14 +03:00
|
|
|
nsresult
|
2018-08-07 22:07:26 +03:00
|
|
|
AttrArray::DoUpdateMappedAttrRuleMapper(nsMappedAttributeElement& aElement)
|
2018-07-06 21:43:14 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mImpl && mImpl->mMappedAttrs, "Should have mapped attrs here!");
|
|
|
|
|
|
|
|
// First two args don't matter if the assert holds.
|
|
|
|
RefPtr<nsMappedAttributes> mapped =
|
|
|
|
GetModifiableMapped(nullptr, nullptr, false);
|
|
|
|
|
|
|
|
mapped->SetRuleMapper(aElement.GetAttributeMappingFunction());
|
|
|
|
|
|
|
|
return MakeMappedUnique(mapped);
|
|
|
|
}
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2008-02-03 02:41:24 +03:00
|
|
|
void
|
2018-08-07 22:07:26 +03:00
|
|
|
AttrArray::Compact()
|
2008-02-03 02:41:24 +03:00
|
|
|
{
|
|
|
|
if (!mImpl) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
if (!mImpl->mAttrCount && !mImpl->mMappedAttrs) {
|
|
|
|
mImpl.reset();
|
2004-01-15 20:07:27 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
// Nothing to do.
|
|
|
|
if (mImpl->mAttrCount == mImpl->mCapacity) {
|
|
|
|
return;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
Impl* impl = mImpl.release();
|
|
|
|
impl = static_cast<Impl*>(
|
|
|
|
realloc(impl, Impl::AllocationSizeForAttributes(impl->mAttrCount)));
|
|
|
|
MOZ_ASSERT(impl, "failed to reallocate to a smaller buffer!");
|
|
|
|
impl->mCapacity = impl->mAttrCount;
|
|
|
|
mImpl.reset(impl);
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
2018-08-13 16:35:57 +03:00
|
|
|
AttrArray::DoGetMappedAttrCount() const
|
2004-01-26 22:22:05 +03:00
|
|
|
{
|
2018-08-13 16:35:57 +03:00
|
|
|
MOZ_ASSERT(mImpl && mImpl->mMappedAttrs);
|
|
|
|
return static_cast<uint32_t>(mImpl->mMappedAttrs->Count());
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
2017-03-29 22:10:00 +03:00
|
|
|
nsresult
|
2018-08-07 22:07:26 +03:00
|
|
|
AttrArray::ForceMapped(nsMappedAttributeElement* aContent, nsIDocument* aDocument)
|
2017-03-29 22:10:00 +03:00
|
|
|
{
|
|
|
|
nsHTMLStyleSheet* sheet = aDocument->GetAttributeStyleSheet();
|
|
|
|
RefPtr<nsMappedAttributes> mapped = GetModifiableMapped(aContent, sheet, false, 0);
|
|
|
|
return MakeMappedUnique(mapped);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-08-07 22:07:26 +03:00
|
|
|
AttrArray::ClearMappedServoStyle()
|
|
|
|
{
|
2017-03-29 22:10:00 +03:00
|
|
|
if (mImpl && mImpl->mMappedAttrs) {
|
|
|
|
mImpl->mMappedAttrs->ClearServoStyle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-04 11:44:01 +04:00
|
|
|
nsMappedAttributes*
|
2018-08-07 22:07:26 +03:00
|
|
|
AttrArray::GetModifiableMapped(nsMappedAttributeElement* aContent,
|
|
|
|
nsHTMLStyleSheet* aSheet,
|
|
|
|
bool aWillAddAttr,
|
|
|
|
int32_t aAttrCount)
|
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();
|
2017-03-29 22:10:00 +03:00
|
|
|
return new (aAttrCount) nsMappedAttributes(aSheet, mapRuleFunc);
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2018-08-07 22:07:26 +03:00
|
|
|
AttrArray::MakeMappedUnique(nsMappedAttributes* aAttributes)
|
2004-01-26 22:22:05 +03:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-01-20 02:56:53 +03:00
|
|
|
const nsMappedAttributes*
|
2018-08-07 22:07:26 +03:00
|
|
|
AttrArray::GetMapped() const
|
2017-01-20 02:56:53 +03:00
|
|
|
{
|
|
|
|
return mImpl ? mImpl->mMappedAttrs : nullptr;
|
|
|
|
}
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2018-08-07 22:07:26 +03:00
|
|
|
nsresult
|
2018-08-09 02:58:44 +03:00
|
|
|
AttrArray::EnsureCapacityToClone(const AttrArray& aOther)
|
2017-04-20 22:57:48 +03:00
|
|
|
{
|
2018-08-07 22:07:26 +03:00
|
|
|
MOZ_ASSERT(!mImpl, "AttrArray::EnsureCapacityToClone requires the array be empty when called");
|
2017-04-20 22:57:48 +03:00
|
|
|
|
|
|
|
uint32_t attrCount = aOther.NonMappedAttrCount();
|
2018-08-13 16:35:57 +03:00
|
|
|
if (!attrCount) {
|
2017-04-20 22:57:48 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No need to use a CheckedUint32 because we are cloning. We know that we
|
2018-08-07 22:07:26 +03:00
|
|
|
// have already allocated an AttrArray of this size.
|
2018-08-13 16:35:57 +03:00
|
|
|
mImpl.reset(static_cast<Impl*>(malloc(
|
|
|
|
Impl::AllocationSizeForAttributes(attrCount))));
|
2017-04-20 22:57:48 +03:00
|
|
|
NS_ENSURE_TRUE(mImpl, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
mImpl->mMappedAttrs = nullptr;
|
2018-08-13 16:35:57 +03:00
|
|
|
mImpl->mCapacity = attrCount;
|
|
|
|
mImpl->mAttrCount = 0;
|
2017-04-20 22:57:48 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2018-08-07 22:07:26 +03:00
|
|
|
AttrArray::GrowBy(uint32_t aGrowSize)
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
2018-08-13 16:35:57 +03:00
|
|
|
const uint32_t kLinearThreshold = 16;
|
|
|
|
const uint32_t kLinearGrowSize = 4;
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
CheckedUint32 capacity = mImpl ? mImpl->mCapacity : 0;
|
|
|
|
CheckedUint32 minCapacity = capacity;
|
|
|
|
minCapacity += aGrowSize;
|
|
|
|
if (!minCapacity.isValid()) {
|
2015-07-10 02:18:30 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
if (capacity.value() <= kLinearThreshold) {
|
2004-01-15 20:07:27 +03:00
|
|
|
do {
|
2018-08-13 16:35:57 +03:00
|
|
|
capacity += kLinearGrowSize;
|
|
|
|
if (!capacity.isValid()) {
|
2015-07-10 02:18:30 +03:00
|
|
|
return false;
|
|
|
|
}
|
2018-08-13 16:35:57 +03:00
|
|
|
} while (capacity.value() < minCapacity.value());
|
|
|
|
} else {
|
|
|
|
uint32_t shift = mozilla::CeilingLog2(minCapacity.value());
|
2016-08-16 09:04:59 +03:00
|
|
|
if (shift >= 32) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-13 16:35:57 +03:00
|
|
|
capacity = 1u << shift;
|
|
|
|
}
|
2016-08-16 09:04:59 +03:00
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
CheckedUint32 sizeInBytes = capacity.value();
|
|
|
|
sizeInBytes *= sizeof(InternalAttr);
|
|
|
|
if (!sizeInBytes.isValid()) {
|
|
|
|
return false;
|
2004-01-15 20:07:27 +03:00
|
|
|
}
|
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
sizeInBytes += sizeof(Impl);
|
|
|
|
if (!sizeInBytes.isValid()) {
|
2015-07-10 02:18:30 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
MOZ_ASSERT(sizeInBytes.value() ==
|
|
|
|
Impl::AllocationSizeForAttributes(capacity.value()));
|
|
|
|
|
|
|
|
const bool needToInitialize = !mImpl;
|
|
|
|
Impl* newImpl = static_cast<Impl*>(realloc(mImpl.release(), sizeInBytes.value()));
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(newImpl, false);
|
2008-02-03 02:41:24 +03:00
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
mImpl.reset(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;
|
2018-08-13 16:35:57 +03:00
|
|
|
mImpl->mAttrCount = 0;
|
2015-07-10 02:18:30 +03:00
|
|
|
}
|
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
mImpl->mCapacity = capacity.value();
|
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
|
|
|
|
2012-02-02 01:58:01 +04:00
|
|
|
size_t
|
2018-08-07 22:07:26 +03:00
|
|
|
AttrArray::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.
|
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
n += aMallocSizeOf(mImpl.get());
|
2011-08-11 02:54:19 +04:00
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
for (const InternalAttr& attr : NonMappedAttrs()) {
|
|
|
|
n += attr.mValue.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
|
|
|
}
|