2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A unique per-element set of attributes that is used as an
|
|
|
|
* nsIStyleRule; used to implement presentational attributes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsMappedAttributes.h"
|
|
|
|
#include "nsHTMLStyleSheet.h"
|
|
|
|
#include "nsRuleWalker.h"
|
|
|
|
#include "prmem.h"
|
2012-03-12 15:53:18 -07:00
|
|
|
#include "mozilla/HashFunctions.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsMappedAttributes::nsMappedAttributes(nsHTMLStyleSheet* aSheet,
|
|
|
|
nsMapRuleToAttributesFunc aMapRuleFunc)
|
|
|
|
: mAttrCount(0),
|
|
|
|
mSheet(aSheet),
|
|
|
|
mRuleMapper(aMapRuleFunc)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsMappedAttributes::nsMappedAttributes(const nsMappedAttributes& aCopy)
|
|
|
|
: mAttrCount(aCopy.mAttrCount),
|
|
|
|
mSheet(aCopy.mSheet),
|
|
|
|
mRuleMapper(aCopy.mRuleMapper)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mBufferSize >= aCopy.mAttrCount, "can't fit attributes");
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = 0; i < mAttrCount; ++i) {
|
|
|
|
new (&Attrs()[i]) InternalAttr(aCopy.Attrs()[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsMappedAttributes::~nsMappedAttributes()
|
|
|
|
{
|
|
|
|
if (mSheet) {
|
|
|
|
mSheet->DropMappedAttributes(this);
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = 0; i < mAttrCount; ++i) {
|
|
|
|
Attrs()[i].~InternalAttr();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsMappedAttributes*
|
2011-09-28 23:19:26 -07:00
|
|
|
nsMappedAttributes::Clone(bool aWillAddAttr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t extra = aWillAddAttr ? 1 : 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// This will call the overridden operator new
|
|
|
|
return new (mAttrCount + extra) nsMappedAttributes(*this);
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
void* nsMappedAttributes::operator new(size_t aSize, uint32_t aAttrCount) CPP_THROW_NEW
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aAttrCount > 0, "zero-attribute nsMappedAttributes requested");
|
|
|
|
|
|
|
|
// aSize will include the mAttrs buffer so subtract that.
|
|
|
|
void* newAttrs = ::operator new(aSize - sizeof(void*[1]) +
|
|
|
|
aAttrCount * sizeof(InternalAttr));
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2012-08-04 00:44:01 -07:00
|
|
|
static_cast<nsMappedAttributes*>(newAttrs)->mBufferSize = aAttrCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return newAttrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsMappedAttributes,
|
|
|
|
nsIStyleRule)
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsMappedAttributes::SetAndTakeAttr(nsIAtom* aAttrName, nsAttrValue& aValue)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aAttrName, "null name");
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = 0; i < mAttrCount && !Attrs()[i].mName.IsSmaller(aAttrName); ++i) {
|
|
|
|
if (Attrs()[i].mName.Equals(aAttrName)) {
|
|
|
|
Attrs()[i].mValue.Reset();
|
|
|
|
Attrs()[i].mValue.SwapValueWith(aValue);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(mBufferSize >= mAttrCount + 1, "can't fit attributes");
|
|
|
|
|
|
|
|
if (mAttrCount != i) {
|
|
|
|
memmove(&Attrs()[i + 1], &Attrs()[i], (mAttrCount - i) * sizeof(InternalAttr));
|
|
|
|
}
|
|
|
|
|
|
|
|
new (&Attrs()[i].mName) nsAttrName(aAttrName);
|
|
|
|
new (&Attrs()[i].mValue) nsAttrValue();
|
|
|
|
Attrs()[i].mValue.SwapValueWith(aValue);
|
|
|
|
++mAttrCount;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsAttrValue*
|
|
|
|
nsMappedAttributes::GetAttr(nsIAtom* aAttrName) const
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aAttrName, "null name");
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mAttrCount; ++i) {
|
2012-07-13 16:29:14 -07:00
|
|
|
if (Attrs()[i].mName.Equals(aAttrName)) {
|
|
|
|
return &Attrs()[i].mValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-07-13 16:29:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const nsAttrValue*
|
|
|
|
nsMappedAttributes::GetAttr(const nsAString& aAttrName) const
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mAttrCount; ++i) {
|
2012-07-13 16:29:14 -07:00
|
|
|
if (Attrs()[i].mName.Atom()->Equals(aAttrName)) {
|
|
|
|
return &Attrs()[i].mValue;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsMappedAttributes::Equals(const nsMappedAttributes* aOther) const
|
|
|
|
{
|
|
|
|
if (this == aOther) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mRuleMapper != aOther->mRuleMapper || mAttrCount != aOther->mAttrCount) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = 0; i < mAttrCount; ++i) {
|
|
|
|
if (!Attrs()[i].mName.Equals(aOther->Attrs()[i].mName) ||
|
|
|
|
!Attrs()[i].mValue.Equals(aOther->Attrs()[i].mValue)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t
|
2007-03-22 10:30:00 -07:00
|
|
|
nsMappedAttributes::HashValue() const
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t hash = HashGeneric(mRuleMapper);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = 0; i < mAttrCount; ++i) {
|
2012-03-12 15:53:18 -07:00
|
|
|
hash = AddToHash(hash,
|
|
|
|
Attrs()[i].mName.HashValue(),
|
|
|
|
Attrs()[i].mValue.HashValue());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-03-12 15:53:18 -07:00
|
|
|
return hash;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsMappedAttributes::SetStyleSheet(nsHTMLStyleSheet* aSheet)
|
|
|
|
{
|
|
|
|
if (mSheet) {
|
|
|
|
mSheet->DropMappedAttributes(this);
|
|
|
|
}
|
|
|
|
mSheet = aSheet; // not ref counted
|
|
|
|
}
|
|
|
|
|
2010-05-19 19:28:00 -07:00
|
|
|
/* virtual */ void
|
2007-03-22 10:30:00 -07:00
|
|
|
nsMappedAttributes::MapRuleInfoInto(nsRuleData* aRuleData)
|
|
|
|
{
|
|
|
|
if (mRuleMapper) {
|
|
|
|
(*mRuleMapper)(this, aRuleData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2010-05-19 19:28:00 -07:00
|
|
|
/* virtual */ void
|
2012-08-22 08:56:38 -07:00
|
|
|
nsMappedAttributes::List(FILE* out, int32_t aIndent) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsAutoString buffer;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
for (i = 0; i < mAttrCount; ++i) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t indent;
|
2007-03-22 10:30:00 -07:00
|
|
|
for (indent = aIndent; indent > 0; --indent)
|
|
|
|
fputs(" ", out);
|
|
|
|
|
2011-05-05 09:26:33 -07:00
|
|
|
Attrs()[i].mName.GetQualifiedName(buffer);
|
2007-03-22 10:30:00 -07:00
|
|
|
fputs(NS_LossyConvertUTF16toASCII(buffer).get(), out);
|
|
|
|
|
|
|
|
Attrs()[i].mValue.ToString(buffer);
|
|
|
|
fputs(NS_LossyConvertUTF16toASCII(buffer).get(), out);
|
|
|
|
fputs("\n", out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
2012-08-22 08:56:38 -07:00
|
|
|
nsMappedAttributes::RemoveAttrAt(uint32_t aPos, nsAttrValue& aValue)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
Attrs()[aPos].mValue.SwapValueWith(aValue);
|
|
|
|
Attrs()[aPos].~InternalAttr();
|
|
|
|
memmove(&Attrs()[aPos], &Attrs()[aPos + 1],
|
|
|
|
(mAttrCount - aPos - 1) * sizeof(InternalAttr));
|
|
|
|
mAttrCount--;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsAttrName*
|
2010-03-08 07:45:00 -08:00
|
|
|
nsMappedAttributes::GetExistingAttrNameFromQName(const nsAString& aName) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = 0; i < mAttrCount; ++i) {
|
|
|
|
if (Attrs()[i].mName.IsAtom()) {
|
2010-03-08 07:45:00 -08:00
|
|
|
if (Attrs()[i].mName.Atom()->Equals(aName)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return &Attrs()[i].mName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (Attrs()[i].mName.NodeInfo()->QualifiedNameEquals(aName)) {
|
|
|
|
return &Attrs()[i].mName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t
|
2012-07-13 16:29:14 -07:00
|
|
|
nsMappedAttributes::IndexOfAttr(nsIAtom* aLocalName) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i;
|
2012-07-13 16:29:14 -07:00
|
|
|
for (i = 0; i < mAttrCount; ++i) {
|
|
|
|
if (Attrs()[i].mName.Equals(aLocalName)) {
|
|
|
|
return i;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2011-08-10 15:54:19 -07:00
|
|
|
|
2012-02-01 13:58:01 -08:00
|
|
|
size_t
|
|
|
|
nsMappedAttributes::SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const
|
2011-08-10 15:54:19 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mAttrCount == mBufferSize,
|
|
|
|
"mBufferSize and mAttrCount are expected to be the same.");
|
|
|
|
|
2012-02-01 13:58:01 -08:00
|
|
|
size_t n = aMallocSizeOf(this);
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint16_t i = 0; i < mAttrCount; ++i) {
|
2012-02-01 13:58:01 -08:00
|
|
|
n += Attrs()[i].mValue.SizeOfExcludingThis(aMallocSizeOf);
|
2011-08-10 15:54:19 -07:00
|
|
|
}
|
2012-02-01 13:58:01 -08:00
|
|
|
return n;
|
2011-08-10 15:54:19 -07:00
|
|
|
}
|
|
|
|
|