Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* |
michael@0 | 7 | * A unique per-element set of attributes that is used as an |
michael@0 | 8 | * nsIStyleRule; used to implement presentational attributes. |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #include "nsMappedAttributes.h" |
michael@0 | 12 | #include "nsHTMLStyleSheet.h" |
michael@0 | 13 | #include "nsRuleWalker.h" |
michael@0 | 14 | #include "mozilla/HashFunctions.h" |
michael@0 | 15 | #include "mozilla/MemoryReporting.h" |
michael@0 | 16 | |
michael@0 | 17 | using namespace mozilla; |
michael@0 | 18 | |
michael@0 | 19 | nsMappedAttributes::nsMappedAttributes(nsHTMLStyleSheet* aSheet, |
michael@0 | 20 | nsMapRuleToAttributesFunc aMapRuleFunc) |
michael@0 | 21 | : mAttrCount(0), |
michael@0 | 22 | mSheet(aSheet), |
michael@0 | 23 | mRuleMapper(aMapRuleFunc) |
michael@0 | 24 | { |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | nsMappedAttributes::nsMappedAttributes(const nsMappedAttributes& aCopy) |
michael@0 | 28 | : mAttrCount(aCopy.mAttrCount), |
michael@0 | 29 | mSheet(aCopy.mSheet), |
michael@0 | 30 | mRuleMapper(aCopy.mRuleMapper) |
michael@0 | 31 | { |
michael@0 | 32 | NS_ASSERTION(mBufferSize >= aCopy.mAttrCount, "can't fit attributes"); |
michael@0 | 33 | |
michael@0 | 34 | uint32_t i; |
michael@0 | 35 | for (i = 0; i < mAttrCount; ++i) { |
michael@0 | 36 | new (&Attrs()[i]) InternalAttr(aCopy.Attrs()[i]); |
michael@0 | 37 | } |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | nsMappedAttributes::~nsMappedAttributes() |
michael@0 | 41 | { |
michael@0 | 42 | if (mSheet) { |
michael@0 | 43 | mSheet->DropMappedAttributes(this); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | uint32_t i; |
michael@0 | 47 | for (i = 0; i < mAttrCount; ++i) { |
michael@0 | 48 | Attrs()[i].~InternalAttr(); |
michael@0 | 49 | } |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | |
michael@0 | 53 | nsMappedAttributes* |
michael@0 | 54 | nsMappedAttributes::Clone(bool aWillAddAttr) |
michael@0 | 55 | { |
michael@0 | 56 | uint32_t extra = aWillAddAttr ? 1 : 0; |
michael@0 | 57 | |
michael@0 | 58 | // This will call the overridden operator new |
michael@0 | 59 | return new (mAttrCount + extra) nsMappedAttributes(*this); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | void* nsMappedAttributes::operator new(size_t aSize, uint32_t aAttrCount) CPP_THROW_NEW |
michael@0 | 63 | { |
michael@0 | 64 | NS_ASSERTION(aAttrCount > 0, "zero-attribute nsMappedAttributes requested"); |
michael@0 | 65 | |
michael@0 | 66 | // aSize will include the mAttrs buffer so subtract that. |
michael@0 | 67 | void* newAttrs = ::operator new(aSize - sizeof(void*[1]) + |
michael@0 | 68 | aAttrCount * sizeof(InternalAttr)); |
michael@0 | 69 | |
michael@0 | 70 | #ifdef DEBUG |
michael@0 | 71 | static_cast<nsMappedAttributes*>(newAttrs)->mBufferSize = aAttrCount; |
michael@0 | 72 | #endif |
michael@0 | 73 | |
michael@0 | 74 | return newAttrs; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | NS_IMPL_ISUPPORTS(nsMappedAttributes, |
michael@0 | 78 | nsIStyleRule) |
michael@0 | 79 | |
michael@0 | 80 | void |
michael@0 | 81 | nsMappedAttributes::SetAndTakeAttr(nsIAtom* aAttrName, nsAttrValue& aValue) |
michael@0 | 82 | { |
michael@0 | 83 | NS_PRECONDITION(aAttrName, "null name"); |
michael@0 | 84 | |
michael@0 | 85 | uint32_t i; |
michael@0 | 86 | for (i = 0; i < mAttrCount && !Attrs()[i].mName.IsSmaller(aAttrName); ++i) { |
michael@0 | 87 | if (Attrs()[i].mName.Equals(aAttrName)) { |
michael@0 | 88 | Attrs()[i].mValue.Reset(); |
michael@0 | 89 | Attrs()[i].mValue.SwapValueWith(aValue); |
michael@0 | 90 | return; |
michael@0 | 91 | } |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | NS_ASSERTION(mBufferSize >= mAttrCount + 1, "can't fit attributes"); |
michael@0 | 95 | |
michael@0 | 96 | if (mAttrCount != i) { |
michael@0 | 97 | memmove(&Attrs()[i + 1], &Attrs()[i], (mAttrCount - i) * sizeof(InternalAttr)); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | new (&Attrs()[i].mName) nsAttrName(aAttrName); |
michael@0 | 101 | new (&Attrs()[i].mValue) nsAttrValue(); |
michael@0 | 102 | Attrs()[i].mValue.SwapValueWith(aValue); |
michael@0 | 103 | ++mAttrCount; |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | const nsAttrValue* |
michael@0 | 107 | nsMappedAttributes::GetAttr(nsIAtom* aAttrName) const |
michael@0 | 108 | { |
michael@0 | 109 | NS_PRECONDITION(aAttrName, "null name"); |
michael@0 | 110 | |
michael@0 | 111 | for (uint32_t i = 0; i < mAttrCount; ++i) { |
michael@0 | 112 | if (Attrs()[i].mName.Equals(aAttrName)) { |
michael@0 | 113 | return &Attrs()[i].mValue; |
michael@0 | 114 | } |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | return nullptr; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | const nsAttrValue* |
michael@0 | 121 | nsMappedAttributes::GetAttr(const nsAString& aAttrName) const |
michael@0 | 122 | { |
michael@0 | 123 | for (uint32_t i = 0; i < mAttrCount; ++i) { |
michael@0 | 124 | if (Attrs()[i].mName.Atom()->Equals(aAttrName)) { |
michael@0 | 125 | return &Attrs()[i].mValue; |
michael@0 | 126 | } |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | return nullptr; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | bool |
michael@0 | 133 | nsMappedAttributes::Equals(const nsMappedAttributes* aOther) const |
michael@0 | 134 | { |
michael@0 | 135 | if (this == aOther) { |
michael@0 | 136 | return true; |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | if (mRuleMapper != aOther->mRuleMapper || mAttrCount != aOther->mAttrCount) { |
michael@0 | 140 | return false; |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | uint32_t i; |
michael@0 | 144 | for (i = 0; i < mAttrCount; ++i) { |
michael@0 | 145 | if (!Attrs()[i].mName.Equals(aOther->Attrs()[i].mName) || |
michael@0 | 146 | !Attrs()[i].mValue.Equals(aOther->Attrs()[i].mValue)) { |
michael@0 | 147 | return false; |
michael@0 | 148 | } |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | return true; |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | uint32_t |
michael@0 | 155 | nsMappedAttributes::HashValue() const |
michael@0 | 156 | { |
michael@0 | 157 | uint32_t hash = HashGeneric(mRuleMapper); |
michael@0 | 158 | |
michael@0 | 159 | uint32_t i; |
michael@0 | 160 | for (i = 0; i < mAttrCount; ++i) { |
michael@0 | 161 | hash = AddToHash(hash, |
michael@0 | 162 | Attrs()[i].mName.HashValue(), |
michael@0 | 163 | Attrs()[i].mValue.HashValue()); |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | return hash; |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | void |
michael@0 | 170 | nsMappedAttributes::SetStyleSheet(nsHTMLStyleSheet* aSheet) |
michael@0 | 171 | { |
michael@0 | 172 | if (mSheet) { |
michael@0 | 173 | mSheet->DropMappedAttributes(this); |
michael@0 | 174 | } |
michael@0 | 175 | mSheet = aSheet; // not ref counted |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | /* virtual */ void |
michael@0 | 179 | nsMappedAttributes::MapRuleInfoInto(nsRuleData* aRuleData) |
michael@0 | 180 | { |
michael@0 | 181 | if (mRuleMapper) { |
michael@0 | 182 | (*mRuleMapper)(this, aRuleData); |
michael@0 | 183 | } |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | #ifdef DEBUG |
michael@0 | 187 | /* virtual */ void |
michael@0 | 188 | nsMappedAttributes::List(FILE* out, int32_t aIndent) const |
michael@0 | 189 | { |
michael@0 | 190 | nsAutoString buffer; |
michael@0 | 191 | uint32_t i; |
michael@0 | 192 | |
michael@0 | 193 | for (i = 0; i < mAttrCount; ++i) { |
michael@0 | 194 | int32_t indent; |
michael@0 | 195 | for (indent = aIndent; indent > 0; --indent) |
michael@0 | 196 | fputs(" ", out); |
michael@0 | 197 | |
michael@0 | 198 | Attrs()[i].mName.GetQualifiedName(buffer); |
michael@0 | 199 | fputs(NS_LossyConvertUTF16toASCII(buffer).get(), out); |
michael@0 | 200 | |
michael@0 | 201 | Attrs()[i].mValue.ToString(buffer); |
michael@0 | 202 | fputs(NS_LossyConvertUTF16toASCII(buffer).get(), out); |
michael@0 | 203 | fputs("\n", out); |
michael@0 | 204 | } |
michael@0 | 205 | } |
michael@0 | 206 | #endif |
michael@0 | 207 | |
michael@0 | 208 | void |
michael@0 | 209 | nsMappedAttributes::RemoveAttrAt(uint32_t aPos, nsAttrValue& aValue) |
michael@0 | 210 | { |
michael@0 | 211 | Attrs()[aPos].mValue.SwapValueWith(aValue); |
michael@0 | 212 | Attrs()[aPos].~InternalAttr(); |
michael@0 | 213 | memmove(&Attrs()[aPos], &Attrs()[aPos + 1], |
michael@0 | 214 | (mAttrCount - aPos - 1) * sizeof(InternalAttr)); |
michael@0 | 215 | mAttrCount--; |
michael@0 | 216 | } |
michael@0 | 217 | |
michael@0 | 218 | const nsAttrName* |
michael@0 | 219 | nsMappedAttributes::GetExistingAttrNameFromQName(const nsAString& aName) const |
michael@0 | 220 | { |
michael@0 | 221 | uint32_t i; |
michael@0 | 222 | for (i = 0; i < mAttrCount; ++i) { |
michael@0 | 223 | if (Attrs()[i].mName.IsAtom()) { |
michael@0 | 224 | if (Attrs()[i].mName.Atom()->Equals(aName)) { |
michael@0 | 225 | return &Attrs()[i].mName; |
michael@0 | 226 | } |
michael@0 | 227 | } |
michael@0 | 228 | else { |
michael@0 | 229 | if (Attrs()[i].mName.NodeInfo()->QualifiedNameEquals(aName)) { |
michael@0 | 230 | return &Attrs()[i].mName; |
michael@0 | 231 | } |
michael@0 | 232 | } |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | return nullptr; |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | int32_t |
michael@0 | 239 | nsMappedAttributes::IndexOfAttr(nsIAtom* aLocalName) const |
michael@0 | 240 | { |
michael@0 | 241 | uint32_t i; |
michael@0 | 242 | for (i = 0; i < mAttrCount; ++i) { |
michael@0 | 243 | if (Attrs()[i].mName.Equals(aLocalName)) { |
michael@0 | 244 | return i; |
michael@0 | 245 | } |
michael@0 | 246 | } |
michael@0 | 247 | |
michael@0 | 248 | return -1; |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | size_t |
michael@0 | 252 | nsMappedAttributes::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const |
michael@0 | 253 | { |
michael@0 | 254 | NS_ASSERTION(mAttrCount == mBufferSize, |
michael@0 | 255 | "mBufferSize and mAttrCount are expected to be the same."); |
michael@0 | 256 | |
michael@0 | 257 | size_t n = aMallocSizeOf(this); |
michael@0 | 258 | for (uint16_t i = 0; i < mAttrCount; ++i) { |
michael@0 | 259 | n += Attrs()[i].mValue.SizeOfExcludingThis(aMallocSizeOf); |
michael@0 | 260 | } |
michael@0 | 261 | return n; |
michael@0 | 262 | } |
michael@0 | 263 |