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-26 22:22:05 +03:00
|
|
|
|
2006-03-31 12:00:42 +04:00
|
|
|
/*
|
|
|
|
* A unique per-element set of attributes that is used as an
|
|
|
|
* nsIStyleRule; used to implement presentational attributes.
|
|
|
|
*/
|
|
|
|
|
2004-01-26 22:22:05 +03:00
|
|
|
#include "nsMappedAttributes.h"
|
2004-04-13 01:56:09 +04:00
|
|
|
#include "nsHTMLStyleSheet.h"
|
2018-05-30 19:15:25 +03:00
|
|
|
#include "mozilla/DeclarationBlock.h"
|
2012-03-13 02:53:18 +04:00
|
|
|
#include "mozilla/HashFunctions.h"
|
2018-06-22 19:48:42 +03:00
|
|
|
#include "mozilla/MappedDeclarations.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2012-03-13 02:53:18 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2017-04-07 19:21:48 +03:00
|
|
|
bool nsMappedAttributes::sShuttingDown = false;
|
|
|
|
nsTArray<void*>* nsMappedAttributes::sCachedMappedAttributeAllocations =
|
|
|
|
nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-04-07 19:21:48 +03:00
|
|
|
void nsMappedAttributes::Shutdown() {
|
|
|
|
sShuttingDown = true;
|
|
|
|
if (sCachedMappedAttributeAllocations) {
|
|
|
|
for (uint32_t i = 0; i < sCachedMappedAttributeAllocations->Length(); ++i) {
|
|
|
|
void* cachedValue = (*sCachedMappedAttributeAllocations)[i];
|
|
|
|
::operator delete(cachedValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete sCachedMappedAttributeAllocations;
|
|
|
|
sCachedMappedAttributeAllocations = nullptr;
|
|
|
|
}
|
|
|
|
|
2004-04-13 01:56:09 +04:00
|
|
|
nsMappedAttributes::nsMappedAttributes(nsHTMLStyleSheet* aSheet,
|
2004-01-26 22:22:05 +03:00
|
|
|
nsMapRuleToAttributesFunc aMapRuleFunc)
|
2004-02-10 16:17:48 +03:00
|
|
|
: mAttrCount(0),
|
|
|
|
mSheet(aSheet),
|
2017-01-20 02:56:53 +03:00
|
|
|
mRuleMapper(aMapRuleFunc),
|
|
|
|
mServoStyle(nullptr) {
|
2017-04-07 19:21:48 +03:00
|
|
|
MOZ_ASSERT(mRefCnt == 0); // Ensure caching works as expected.
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsMappedAttributes::nsMappedAttributes(const nsMappedAttributes& aCopy)
|
2004-02-10 16:17:48 +03:00
|
|
|
: mAttrCount(aCopy.mAttrCount),
|
|
|
|
mSheet(aCopy.mSheet),
|
2017-01-20 02:56:53 +03:00
|
|
|
mRuleMapper(aCopy.mRuleMapper),
|
|
|
|
// This is only called by ::Clone, which is used to create independent
|
2018-05-30 19:15:25 +03:00
|
|
|
// nsMappedAttributes objects which should not share a DeclarationBlock
|
2017-01-20 02:56:53 +03:00
|
|
|
mServoStyle(nullptr) {
|
2019-10-21 22:40:44 +03:00
|
|
|
MOZ_ASSERT(mBufferSize >= aCopy.mAttrCount, "can't fit attributes");
|
2017-04-07 19:21:48 +03:00
|
|
|
MOZ_ASSERT(mRefCnt == 0); // Ensure caching works as expected.
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i;
|
2004-01-26 22:22:05 +03:00
|
|
|
for (i = 0; i < mAttrCount; ++i) {
|
2004-01-31 01:08:23 +03:00
|
|
|
new (&Attrs()[i]) InternalAttr(aCopy.Attrs()[i]);
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsMappedAttributes::~nsMappedAttributes() {
|
2004-01-31 01:08:23 +03:00
|
|
|
if (mSheet) {
|
|
|
|
mSheet->DropMappedAttributes(this);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i;
|
2004-01-26 22:22:05 +03:00
|
|
|
for (i = 0; i < mAttrCount; ++i) {
|
2004-01-31 01:08:23 +03:00
|
|
|
Attrs()[i].~InternalAttr();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
nsMappedAttributes* nsMappedAttributes::Clone(bool aWillAddAttr) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t extra = aWillAddAttr ? 1 : 0;
|
2004-01-31 01:08:23 +03:00
|
|
|
|
|
|
|
// This will call the overridden operator new
|
|
|
|
return new (mAttrCount + extra) nsMappedAttributes(*this);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void* nsMappedAttributes::operator new(size_t aSize,
|
2019-08-14 04:32:41 +03:00
|
|
|
uint32_t aAttrCount) noexcept(true) {
|
2017-03-29 22:10:00 +03:00
|
|
|
size_t size = aSize + aAttrCount * sizeof(InternalAttr);
|
2004-01-31 01:08:23 +03:00
|
|
|
|
|
|
|
// aSize will include the mAttrs buffer so subtract that.
|
2017-03-29 22:10:00 +03:00
|
|
|
// We don't want to under-allocate, however, so do not subtract
|
|
|
|
// if we have zero attributes. The zero attribute case only happens
|
|
|
|
// for <body>'s mapped attributes
|
|
|
|
if (aAttrCount != 0) {
|
2019-10-21 17:13:44 +03:00
|
|
|
size -= sizeof(void* [1]);
|
2017-03-29 22:10:00 +03:00
|
|
|
}
|
|
|
|
|
2017-04-07 19:21:48 +03:00
|
|
|
if (sCachedMappedAttributeAllocations) {
|
|
|
|
void* cached = sCachedMappedAttributeAllocations->SafeElementAt(aAttrCount);
|
|
|
|
if (cached) {
|
|
|
|
(*sCachedMappedAttributeAllocations)[aAttrCount] = nullptr;
|
|
|
|
return cached;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 22:10:00 +03:00
|
|
|
void* newAttrs = ::operator new(size);
|
2004-01-31 01:08:23 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2012-08-04 11:44:01 +04:00
|
|
|
static_cast<nsMappedAttributes*>(newAttrs)->mBufferSize = aAttrCount;
|
2004-01-31 01:08:23 +03:00
|
|
|
#endif
|
|
|
|
return newAttrs;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
2017-04-07 19:21:48 +03:00
|
|
|
void nsMappedAttributes::LastRelease() {
|
|
|
|
if (!sShuttingDown) {
|
|
|
|
if (!sCachedMappedAttributeAllocations) {
|
|
|
|
sCachedMappedAttributeAllocations = new nsTArray<void*>();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the cache array is at least mAttrCount + 1 long and
|
|
|
|
// that each item is either null or pointing to a cached item.
|
|
|
|
// The size of the array is capped because mapped attributes are defined
|
|
|
|
// statically in element implementations.
|
|
|
|
sCachedMappedAttributeAllocations->SetCapacity(mAttrCount + 1);
|
|
|
|
for (uint32_t i = sCachedMappedAttributeAllocations->Length();
|
|
|
|
i < (uint32_t(mAttrCount) + 1); ++i) {
|
|
|
|
sCachedMappedAttributeAllocations->AppendElement(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(*sCachedMappedAttributeAllocations)[mAttrCount]) {
|
|
|
|
void* memoryToCache = this;
|
|
|
|
this->~nsMappedAttributes();
|
|
|
|
(*sCachedMappedAttributeAllocations)[mAttrCount] = memoryToCache;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
void nsMappedAttributes::SetAndSwapAttr(nsAtom* aAttrName, nsAttrValue& aValue,
|
2017-05-19 00:09:01 +03:00
|
|
|
bool* aValueWasSet) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aAttrName, "null name");
|
2017-05-19 00:09:01 +03:00
|
|
|
*aValueWasSet = false;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i;
|
2004-01-31 01:08:23 +03:00
|
|
|
for (i = 0; i < mAttrCount && !Attrs()[i].mName.IsSmaller(aAttrName); ++i) {
|
|
|
|
if (Attrs()[i].mName.Equals(aAttrName)) {
|
|
|
|
Attrs()[i].mValue.SwapValueWith(aValue);
|
2017-05-19 00:09:01 +03:00
|
|
|
*aValueWasSet = true;
|
2012-09-06 11:14:49 +04:00
|
|
|
return;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-21 22:40:44 +03:00
|
|
|
MOZ_ASSERT(mBufferSize >= mAttrCount + 1, "can't fit attributes");
|
2004-01-26 22:22:05 +03:00
|
|
|
|
|
|
|
if (mAttrCount != i) {
|
2004-01-31 01:08:23 +03:00
|
|
|
memmove(&Attrs()[i + 1], &Attrs()[i],
|
|
|
|
(mAttrCount - i) * sizeof(InternalAttr));
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
2004-01-31 01:08:23 +03:00
|
|
|
new (&Attrs()[i].mName) nsAttrName(aAttrName);
|
|
|
|
new (&Attrs()[i].mValue) nsAttrValue();
|
|
|
|
Attrs()[i].mValue.SwapValueWith(aValue);
|
2004-01-26 22:22:05 +03:00
|
|
|
++mAttrCount;
|
|
|
|
}
|
|
|
|
|
2018-11-13 15:48:21 +03:00
|
|
|
const nsAttrValue* nsMappedAttributes::GetAttr(const nsAtom* aAttrName) const {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aAttrName, "null name");
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mAttrCount; ++i) {
|
2012-07-14 03:29:14 +04:00
|
|
|
if (Attrs()[i].mName.Equals(aAttrName)) {
|
|
|
|
return &Attrs()[i].mValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-07-14 03:29:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const nsAttrValue* nsMappedAttributes::GetAttr(
|
|
|
|
const nsAString& aAttrName) const {
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mAttrCount; ++i) {
|
2012-07-14 03:29:14 +04:00
|
|
|
if (Attrs()[i].mName.Atom()->Equals(aAttrName)) {
|
|
|
|
return &Attrs()[i].mValue;
|
|
|
|
}
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool nsMappedAttributes::Equals(const nsMappedAttributes* aOther) const {
|
|
|
|
if (this == aOther) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mRuleMapper != aOther->mRuleMapper || mAttrCount != aOther->mAttrCount) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i;
|
2004-01-26 22:22:05 +03:00
|
|
|
for (i = 0; i < mAttrCount; ++i) {
|
2004-01-31 01:08:23 +03:00
|
|
|
if (!Attrs()[i].mName.Equals(aOther->Attrs()[i].mName) ||
|
2004-02-03 01:00:36 +03:00
|
|
|
!Attrs()[i].mValue.Equals(aOther->Attrs()[i].mValue)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PLDHashNumber nsMappedAttributes::HashValue() const {
|
2017-07-04 07:48:22 +03:00
|
|
|
PLDHashNumber hash = HashGeneric(mRuleMapper);
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i;
|
2004-01-26 22:22:05 +03:00
|
|
|
for (i = 0; i < mAttrCount; ++i) {
|
2012-03-13 02:53:18 +04:00
|
|
|
hash = AddToHash(hash, Attrs()[i].mName.HashValue(),
|
|
|
|
Attrs()[i].mValue.HashValue());
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
2012-03-13 02:53:18 +04:00
|
|
|
return hash;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
2004-04-13 01:56:09 +04:00
|
|
|
void nsMappedAttributes::SetStyleSheet(nsHTMLStyleSheet* aSheet) {
|
2004-01-26 22:22:05 +03:00
|
|
|
if (mSheet) {
|
|
|
|
mSheet->DropMappedAttributes(this);
|
|
|
|
}
|
|
|
|
mSheet = aSheet; // not ref counted
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void nsMappedAttributes::RemoveAttrAt(uint32_t aPos, nsAttrValue& aValue) {
|
2006-11-11 03:04:46 +03:00
|
|
|
Attrs()[aPos].mValue.SwapValueWith(aValue);
|
2004-01-31 01:08:23 +03:00
|
|
|
Attrs()[aPos].~InternalAttr();
|
|
|
|
memmove(&Attrs()[aPos], &Attrs()[aPos + 1],
|
2004-01-26 22:22:05 +03:00
|
|
|
(mAttrCount - aPos - 1) * sizeof(InternalAttr));
|
|
|
|
mAttrCount--;
|
|
|
|
}
|
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
const nsAttrName* nsMappedAttributes::GetExistingAttrNameFromQName(
|
|
|
|
const nsAString& aName) const {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i;
|
2004-01-26 22:22:05 +03:00
|
|
|
for (i = 0; i < mAttrCount; ++i) {
|
2004-01-31 01:08:23 +03:00
|
|
|
if (Attrs()[i].mName.IsAtom()) {
|
2010-03-08 18:45:00 +03:00
|
|
|
if (Attrs()[i].mName.Atom()->Equals(aName)) {
|
2004-01-31 01:08:23 +03:00
|
|
|
return &Attrs()[i].mName;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
} else {
|
2004-01-31 01:08:23 +03:00
|
|
|
if (Attrs()[i].mName.NodeInfo()->QualifiedNameEquals(aName)) {
|
|
|
|
return &Attrs()[i].mName;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
2018-11-13 15:48:21 +03:00
|
|
|
int32_t nsMappedAttributes::IndexOfAttr(const nsAtom* aLocalName) const {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i;
|
2012-07-14 03:29:14 +04:00
|
|
|
for (i = 0; i < mAttrCount; ++i) {
|
|
|
|
if (Attrs()[i].mName.Equals(aLocalName)) {
|
|
|
|
return i;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2011-08-11 02:54:19 +04:00
|
|
|
|
2013-06-23 16:03:39 +04:00
|
|
|
size_t nsMappedAttributes::SizeOfIncludingThis(
|
|
|
|
MallocSizeOf aMallocSizeOf) const {
|
2019-10-21 22:40:44 +03:00
|
|
|
MOZ_ASSERT(mBufferSize >= mAttrCount, "can't fit attributes");
|
2011-08-11 02:54:19 +04:00
|
|
|
|
2012-02-02 01:58:01 +04:00
|
|
|
size_t n = aMallocSizeOf(this);
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint16_t i = 0; i < mAttrCount; ++i) {
|
2012-02-02 01:58:01 +04:00
|
|
|
n += Attrs()[i].mValue.SizeOfExcludingThis(aMallocSizeOf);
|
2011-08-11 02:54:19 +04:00
|
|
|
}
|
2012-02-02 01:58:01 +04:00
|
|
|
return n;
|
2011-08-11 02:54:19 +04:00
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
void nsMappedAttributes::LazilyResolveServoDeclaration(dom::Document* aDoc) {
|
2017-01-20 02:56:53 +03:00
|
|
|
MOZ_ASSERT(!mServoStyle,
|
|
|
|
"LazilyResolveServoDeclaration should not be called if "
|
|
|
|
"mServoStyle is already set");
|
2017-02-13 03:02:29 +03:00
|
|
|
if (mRuleMapper) {
|
2018-06-22 19:48:42 +03:00
|
|
|
MappedDeclarations declarations(
|
|
|
|
aDoc, Servo_DeclarationBlock_CreateEmpty().Consume());
|
|
|
|
(*mRuleMapper)(this, declarations);
|
|
|
|
mServoStyle = declarations.TakeDeclarationBlock();
|
2017-01-20 02:56:53 +03:00
|
|
|
}
|
|
|
|
}
|