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"
|
2004-01-26 22:22:05 +03:00
|
|
|
#include "nsRuleWalker.h"
|
2012-03-13 02:53:18 +04:00
|
|
|
#include "mozilla/HashFunctions.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
|
|
|
|
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),
|
2004-01-31 01:08:23 +03:00
|
|
|
mRuleMapper(aMapRuleFunc)
|
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),
|
2004-01-31 01:08:23 +03:00
|
|
|
mRuleMapper(aCopy.mRuleMapper)
|
2004-01-26 22:22:05 +03:00
|
|
|
{
|
2004-01-31 01:08:23 +03:00
|
|
|
NS_ASSERTION(mBufferSize >= aCopy.mAttrCount, "can't fit attributes");
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsMappedAttributes*
|
2011-09-29 10:19:26 +04:00
|
|
|
nsMappedAttributes::Clone(bool aWillAddAttr)
|
2004-01-31 01:08:23 +03:00
|
|
|
{
|
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, uint32_t aAttrCount) CPP_THROW_NEW
|
2004-01-31 01:08:23 +03: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]) +
|
2004-01-31 01:26:24 +03:00
|
|
|
aAttrCount * sizeof(InternalAttr));
|
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
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2004-01-31 01:08:23 +03:00
|
|
|
return newAttrs;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsMappedAttributes,
|
|
|
|
nsIStyleRule)
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2012-09-06 11:14:49 +04:00
|
|
|
void
|
2004-01-26 22:22:05 +03:00
|
|
|
nsMappedAttributes::SetAndTakeAttr(nsIAtom* aAttrName, nsAttrValue& aValue)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aAttrName, "null name");
|
|
|
|
|
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.Reset();
|
|
|
|
Attrs()[i].mValue.SwapValueWith(aValue);
|
2012-09-06 11:14:49 +04:00
|
|
|
return;
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-31 01:08:23 +03:00
|
|
|
NS_ASSERTION(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsAttrValue*
|
|
|
|
nsMappedAttributes::GetAttr(nsIAtom* aAttrName) const
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aAttrName, "null name");
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2004-01-26 22:22:05 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
2004-01-26 22:22:05 +03:00
|
|
|
nsMappedAttributes::HashValue() const
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t 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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-04-13 01:56:09 +04:00
|
|
|
nsMappedAttributes::SetStyleSheet(nsHTMLStyleSheet* aSheet)
|
2004-01-26 22:22:05 +03:00
|
|
|
{
|
|
|
|
if (mSheet) {
|
|
|
|
mSheet->DropMappedAttributes(this);
|
|
|
|
}
|
|
|
|
mSheet = aSheet; // not ref counted
|
|
|
|
}
|
|
|
|
|
2010-05-20 06:28:00 +04:00
|
|
|
/* virtual */ void
|
2004-01-26 22:22:05 +03:00
|
|
|
nsMappedAttributes::MapRuleInfoInto(nsRuleData* aRuleData)
|
|
|
|
{
|
|
|
|
if (mRuleMapper) {
|
|
|
|
(*mRuleMapper)(this, aRuleData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-17 07:09:55 +03:00
|
|
|
/* virtual */ bool
|
|
|
|
nsMappedAttributes::MightMapInheritedStyleData()
|
|
|
|
{
|
|
|
|
// Just assume that we do, rather than adding checks to all of the different
|
|
|
|
// kinds of attribute mapping functions we have.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-01-26 22:22:05 +03:00
|
|
|
#ifdef DEBUG
|
2010-05-20 06:28:00 +04:00
|
|
|
/* virtual */ void
|
2012-08-22 19:56:38 +04:00
|
|
|
nsMappedAttributes::List(FILE* out, int32_t aIndent) const
|
2004-01-26 22:22:05 +03:00
|
|
|
{
|
2014-11-27 09:29:44 +03:00
|
|
|
nsAutoCString str;
|
|
|
|
nsAutoString tmp;
|
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-08-22 19:56:38 +04:00
|
|
|
int32_t indent;
|
2014-11-27 09:29:44 +03:00
|
|
|
for (indent = aIndent; indent > 0; --indent) {
|
2014-11-27 09:29:44 +03:00
|
|
|
str.AppendLiteral(" ");
|
2014-11-27 09:29:44 +03:00
|
|
|
}
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2014-11-27 09:29:44 +03:00
|
|
|
Attrs()[i].mName.GetQualifiedName(tmp);
|
|
|
|
LossyAppendUTF16toASCII(tmp, str);
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2014-11-27 09:29:44 +03:00
|
|
|
Attrs()[i].mValue.ToString(tmp);
|
|
|
|
LossyAppendUTF16toASCII(tmp, str);
|
|
|
|
str.Append('\n');
|
|
|
|
fprintf_stderr(out, "%s", str.get());
|
2004-01-26 22:22:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
nsMappedAttributes::RemoveAttrAt(uint32_t aPos, nsAttrValue& aValue)
|
2004-01-26 22:22:05 +03:00
|
|
|
{
|
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--;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsAttrName*
|
2010-03-08 18:45:00 +03:00
|
|
|
nsMappedAttributes::GetExistingAttrNameFromQName(const nsAString& aName) const
|
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.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
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2012-07-14 03:29:14 +04:00
|
|
|
nsMappedAttributes::IndexOfAttr(nsIAtom* aLocalName) const
|
2004-01-26 22:22:05 +03:00
|
|
|
{
|
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
|
|
|
|
2012-02-02 01:58:01 +04:00
|
|
|
size_t
|
2013-06-23 16:03:39 +04:00
|
|
|
nsMappedAttributes::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
2011-08-11 02:54:19 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mAttrCount == mBufferSize,
|
|
|
|
"mBufferSize and mAttrCount are expected to be the same.");
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|