layout/style/CSSVariableDeclarations.cpp

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

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 /* CSS Custom Property assignments for a Declaration at a given priority */
michael@0 7
michael@0 8 #include "CSSVariableDeclarations.h"
michael@0 9
michael@0 10 #include "CSSVariableResolver.h"
michael@0 11 #include "nsCSSScanner.h"
michael@0 12 #include "nsRuleData.h"
michael@0 13
michael@0 14 // These three special string values are used to represent specified values of
michael@0 15 // 'initial', 'inherit' and 'unset'. (Note that none of these are valid
michael@0 16 // variable values.)
michael@0 17 #define INITIAL_VALUE "!"
michael@0 18 #define INHERIT_VALUE ";"
michael@0 19 #define UNSET_VALUE ")"
michael@0 20
michael@0 21 namespace mozilla {
michael@0 22
michael@0 23 CSSVariableDeclarations::CSSVariableDeclarations()
michael@0 24 {
michael@0 25 MOZ_COUNT_CTOR(CSSVariableDeclarations);
michael@0 26 }
michael@0 27
michael@0 28 CSSVariableDeclarations::CSSVariableDeclarations(const CSSVariableDeclarations& aOther)
michael@0 29 {
michael@0 30 MOZ_COUNT_CTOR(CSSVariableDeclarations);
michael@0 31 CopyVariablesFrom(aOther);
michael@0 32 }
michael@0 33
michael@0 34 #ifdef DEBUG
michael@0 35 CSSVariableDeclarations::~CSSVariableDeclarations()
michael@0 36 {
michael@0 37 MOZ_COUNT_DTOR(CSSVariableDeclarations);
michael@0 38 }
michael@0 39 #endif
michael@0 40
michael@0 41 CSSVariableDeclarations&
michael@0 42 CSSVariableDeclarations::operator=(const CSSVariableDeclarations& aOther)
michael@0 43 {
michael@0 44 if (this == &aOther) {
michael@0 45 return *this;
michael@0 46 }
michael@0 47
michael@0 48 mVariables.Clear();
michael@0 49 CopyVariablesFrom(aOther);
michael@0 50 return *this;
michael@0 51 }
michael@0 52
michael@0 53 /* static */ PLDHashOperator
michael@0 54 CSSVariableDeclarations::EnumerateVariableForCopy(const nsAString& aName,
michael@0 55 nsString aValue,
michael@0 56 void* aData)
michael@0 57 {
michael@0 58 CSSVariableDeclarations* variables = static_cast<CSSVariableDeclarations*>(aData);
michael@0 59 variables->mVariables.Put(aName, aValue);
michael@0 60 return PL_DHASH_NEXT;
michael@0 61 }
michael@0 62
michael@0 63 void
michael@0 64 CSSVariableDeclarations::CopyVariablesFrom(const CSSVariableDeclarations& aOther)
michael@0 65 {
michael@0 66 aOther.mVariables.EnumerateRead(EnumerateVariableForCopy, this);
michael@0 67 }
michael@0 68
michael@0 69 bool
michael@0 70 CSSVariableDeclarations::Has(const nsAString& aName) const
michael@0 71 {
michael@0 72 nsString value;
michael@0 73 return mVariables.Get(aName, &value);
michael@0 74 }
michael@0 75
michael@0 76 bool
michael@0 77 CSSVariableDeclarations::Get(const nsAString& aName,
michael@0 78 Type& aType,
michael@0 79 nsString& aTokenStream) const
michael@0 80 {
michael@0 81 nsString value;
michael@0 82 if (!mVariables.Get(aName, &value)) {
michael@0 83 return false;
michael@0 84 }
michael@0 85 if (value.EqualsLiteral(INITIAL_VALUE)) {
michael@0 86 aType = eInitial;
michael@0 87 aTokenStream.Truncate();
michael@0 88 } else if (value.EqualsLiteral(INHERIT_VALUE)) {
michael@0 89 aType = eInitial;
michael@0 90 aTokenStream.Truncate();
michael@0 91 } else if (value.EqualsLiteral(UNSET_VALUE)) {
michael@0 92 aType = eUnset;
michael@0 93 aTokenStream.Truncate();
michael@0 94 } else {
michael@0 95 aType = eTokenStream;
michael@0 96 aTokenStream = value;
michael@0 97 }
michael@0 98 return true;
michael@0 99 }
michael@0 100
michael@0 101 void
michael@0 102 CSSVariableDeclarations::PutTokenStream(const nsAString& aName,
michael@0 103 const nsString& aTokenStream)
michael@0 104 {
michael@0 105 MOZ_ASSERT(!aTokenStream.EqualsLiteral(INITIAL_VALUE) &&
michael@0 106 !aTokenStream.EqualsLiteral(INHERIT_VALUE) &&
michael@0 107 !aTokenStream.EqualsLiteral(UNSET_VALUE));
michael@0 108 mVariables.Put(aName, aTokenStream);
michael@0 109 }
michael@0 110
michael@0 111 void
michael@0 112 CSSVariableDeclarations::PutInitial(const nsAString& aName)
michael@0 113 {
michael@0 114 mVariables.Put(aName, NS_LITERAL_STRING(INITIAL_VALUE));
michael@0 115 }
michael@0 116
michael@0 117 void
michael@0 118 CSSVariableDeclarations::PutInherit(const nsAString& aName)
michael@0 119 {
michael@0 120 mVariables.Put(aName, NS_LITERAL_STRING(INHERIT_VALUE));
michael@0 121 }
michael@0 122
michael@0 123 void
michael@0 124 CSSVariableDeclarations::PutUnset(const nsAString& aName)
michael@0 125 {
michael@0 126 mVariables.Put(aName, NS_LITERAL_STRING(UNSET_VALUE));
michael@0 127 }
michael@0 128
michael@0 129 void
michael@0 130 CSSVariableDeclarations::Remove(const nsAString& aName)
michael@0 131 {
michael@0 132 mVariables.Remove(aName);
michael@0 133 }
michael@0 134
michael@0 135 /* static */ PLDHashOperator
michael@0 136 CSSVariableDeclarations::EnumerateVariableForMapRuleInfoInto(
michael@0 137 const nsAString& aName,
michael@0 138 nsString aValue,
michael@0 139 void* aData)
michael@0 140 {
michael@0 141 nsDataHashtable<nsStringHashKey, nsString>* variables =
michael@0 142 static_cast<nsDataHashtable<nsStringHashKey, nsString>*>(aData);
michael@0 143 if (!variables->Contains(aName)) {
michael@0 144 variables->Put(aName, aValue);
michael@0 145 }
michael@0 146 return PL_DHASH_NEXT;
michael@0 147 }
michael@0 148
michael@0 149 void
michael@0 150 CSSVariableDeclarations::MapRuleInfoInto(nsRuleData* aRuleData)
michael@0 151 {
michael@0 152 if (!(aRuleData->mSIDs & NS_STYLE_INHERIT_BIT(Variables))) {
michael@0 153 return;
michael@0 154 }
michael@0 155
michael@0 156 if (!aRuleData->mVariables) {
michael@0 157 aRuleData->mVariables = new CSSVariableDeclarations(*this);
michael@0 158 } else {
michael@0 159 mVariables.EnumerateRead(EnumerateVariableForMapRuleInfoInto,
michael@0 160 aRuleData->mVariables.get());
michael@0 161 }
michael@0 162 }
michael@0 163
michael@0 164 /* static */ PLDHashOperator
michael@0 165 CSSVariableDeclarations::EnumerateVariableForAddVariablesToResolver(
michael@0 166 const nsAString& aName,
michael@0 167 nsString aValue,
michael@0 168 void* aData)
michael@0 169 {
michael@0 170 CSSVariableResolver* resolver = static_cast<CSSVariableResolver*>(aData);
michael@0 171 if (aValue.EqualsLiteral(INITIAL_VALUE)) {
michael@0 172 // Values of 'initial' are treated the same as an invalid value in the
michael@0 173 // variable resolver.
michael@0 174 resolver->Put(aName, EmptyString(),
michael@0 175 eCSSTokenSerialization_Nothing,
michael@0 176 eCSSTokenSerialization_Nothing,
michael@0 177 false);
michael@0 178 } else if (aValue.EqualsLiteral(INHERIT_VALUE) ||
michael@0 179 aValue.EqualsLiteral(UNSET_VALUE)) {
michael@0 180 // Values of 'inherit' and 'unset' don't need any handling, since it means
michael@0 181 // we just need to keep whatever value is currently in the resolver.
michael@0 182 // Values of 'inherit' and 'unset' don't need any handling, since it means
michael@0 183 // we just need to keep whatever value is currently in the resolver. This
michael@0 184 // is because the specified variable declarations already have only the
michael@0 185 // winning declaration for the variable and no longer have any of the
michael@0 186 // others.
michael@0 187 } else {
michael@0 188 // At this point, we don't know what token types are at the start and end
michael@0 189 // of the specified variable value. These will be determined later during
michael@0 190 // the resolving process.
michael@0 191 resolver->Put(aName, aValue,
michael@0 192 eCSSTokenSerialization_Nothing,
michael@0 193 eCSSTokenSerialization_Nothing,
michael@0 194 false);
michael@0 195 }
michael@0 196 return PL_DHASH_NEXT;
michael@0 197 }
michael@0 198
michael@0 199 void
michael@0 200 CSSVariableDeclarations::AddVariablesToResolver(
michael@0 201 CSSVariableResolver* aResolver) const
michael@0 202 {
michael@0 203 mVariables.EnumerateRead(EnumerateVariableForAddVariablesToResolver,
michael@0 204 aResolver);
michael@0 205 }
michael@0 206
michael@0 207 static size_t
michael@0 208 SizeOfTableEntry(const nsAString& aKey,
michael@0 209 const nsString& aValue,
michael@0 210 MallocSizeOf aMallocSizeOf,
michael@0 211 void* aUserArg)
michael@0 212 {
michael@0 213 size_t n = 0;
michael@0 214 n += aKey.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
michael@0 215 n += aValue.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
michael@0 216 return n;
michael@0 217 }
michael@0 218
michael@0 219 size_t
michael@0 220 CSSVariableDeclarations::SizeOfIncludingThis(
michael@0 221 mozilla::MallocSizeOf aMallocSizeOf) const
michael@0 222 {
michael@0 223 size_t n = aMallocSizeOf(this);
michael@0 224 n += mVariables.SizeOfExcludingThis(SizeOfTableEntry, aMallocSizeOf);
michael@0 225 return n;
michael@0 226 }
michael@0 227
michael@0 228 } // namespace mozilla

mercurial