Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | // vim:cindent:ai:sw=4:ts=4:et: |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | /* implementation of CSS counters (for numbering things) */ |
michael@0 | 8 | |
michael@0 | 9 | #include "nsCounterManager.h" |
michael@0 | 10 | #include "nsBulletFrame.h" // legacy location for list style type to text code |
michael@0 | 11 | #include "nsContentUtils.h" |
michael@0 | 12 | #include "nsTArray.h" |
michael@0 | 13 | #include "mozilla/Likely.h" |
michael@0 | 14 | #include "nsIContent.h" |
michael@0 | 15 | |
michael@0 | 16 | bool |
michael@0 | 17 | nsCounterUseNode::InitTextFrame(nsGenConList* aList, |
michael@0 | 18 | nsIFrame* aPseudoFrame, nsIFrame* aTextFrame) |
michael@0 | 19 | { |
michael@0 | 20 | nsCounterNode::InitTextFrame(aList, aPseudoFrame, aTextFrame); |
michael@0 | 21 | |
michael@0 | 22 | nsCounterList *counterList = static_cast<nsCounterList*>(aList); |
michael@0 | 23 | counterList->Insert(this); |
michael@0 | 24 | bool dirty = counterList->IsDirty(); |
michael@0 | 25 | if (!dirty) { |
michael@0 | 26 | if (counterList->IsLast(this)) { |
michael@0 | 27 | Calc(counterList); |
michael@0 | 28 | nsAutoString contentString; |
michael@0 | 29 | GetText(contentString); |
michael@0 | 30 | aTextFrame->GetContent()->SetText(contentString, false); |
michael@0 | 31 | } else { |
michael@0 | 32 | // In all other cases (list already dirty or node not at the end), |
michael@0 | 33 | // just start with an empty string for now and when we recalculate |
michael@0 | 34 | // the list we'll change the value to the right one. |
michael@0 | 35 | counterList->SetDirty(); |
michael@0 | 36 | return true; |
michael@0 | 37 | } |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | return false; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | // assign the correct |mValueAfter| value to a node that has been inserted |
michael@0 | 44 | // Should be called immediately after calling |Insert|. |
michael@0 | 45 | void nsCounterUseNode::Calc(nsCounterList *aList) |
michael@0 | 46 | { |
michael@0 | 47 | NS_ASSERTION(!aList->IsDirty(), |
michael@0 | 48 | "Why are we calculating with a dirty list?"); |
michael@0 | 49 | mValueAfter = aList->ValueBefore(this); |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | // assign the correct |mValueAfter| value to a node that has been inserted |
michael@0 | 53 | // Should be called immediately after calling |Insert|. |
michael@0 | 54 | void nsCounterChangeNode::Calc(nsCounterList *aList) |
michael@0 | 55 | { |
michael@0 | 56 | NS_ASSERTION(!aList->IsDirty(), |
michael@0 | 57 | "Why are we calculating with a dirty list?"); |
michael@0 | 58 | if (mType == RESET) { |
michael@0 | 59 | mValueAfter = mChangeValue; |
michael@0 | 60 | } else { |
michael@0 | 61 | NS_ASSERTION(mType == INCREMENT, "invalid type"); |
michael@0 | 62 | mValueAfter = nsCounterManager::IncrementCounter(aList->ValueBefore(this), |
michael@0 | 63 | mChangeValue); |
michael@0 | 64 | } |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | // The text that should be displayed for this counter. |
michael@0 | 68 | void |
michael@0 | 69 | nsCounterUseNode::GetText(nsString& aResult) |
michael@0 | 70 | { |
michael@0 | 71 | aResult.Truncate(); |
michael@0 | 72 | |
michael@0 | 73 | nsAutoTArray<nsCounterNode*, 8> stack; |
michael@0 | 74 | stack.AppendElement(static_cast<nsCounterNode*>(this)); |
michael@0 | 75 | |
michael@0 | 76 | if (mAllCounters && mScopeStart) |
michael@0 | 77 | for (nsCounterNode *n = mScopeStart; n->mScopePrev; n = n->mScopeStart) |
michael@0 | 78 | stack.AppendElement(n->mScopePrev); |
michael@0 | 79 | |
michael@0 | 80 | const nsCSSValue& styleItem = mCounterStyle->Item(mAllCounters ? 2 : 1); |
michael@0 | 81 | int32_t style = styleItem.GetIntValue(); |
michael@0 | 82 | const char16_t* separator; |
michael@0 | 83 | if (mAllCounters) |
michael@0 | 84 | separator = mCounterStyle->Item(1).GetStringBufferValue(); |
michael@0 | 85 | |
michael@0 | 86 | for (uint32_t i = stack.Length() - 1;; --i) { |
michael@0 | 87 | nsCounterNode *n = stack[i]; |
michael@0 | 88 | bool isTextRTL; |
michael@0 | 89 | nsBulletFrame::AppendCounterText( |
michael@0 | 90 | style, n->mValueAfter, aResult, isTextRTL); |
michael@0 | 91 | if (i == 0) |
michael@0 | 92 | break; |
michael@0 | 93 | NS_ASSERTION(mAllCounters, "yikes, separator is uninitialized"); |
michael@0 | 94 | aResult.Append(separator); |
michael@0 | 95 | } |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | void |
michael@0 | 99 | nsCounterList::SetScope(nsCounterNode *aNode) |
michael@0 | 100 | { |
michael@0 | 101 | // This function is responsible for setting |mScopeStart| and |
michael@0 | 102 | // |mScopePrev| (whose purpose is described in nsCounterManager.h). |
michael@0 | 103 | // We do this by starting from the node immediately preceding |
michael@0 | 104 | // |aNode| in content tree order, which is reasonably likely to be |
michael@0 | 105 | // the previous element in our scope (or, for a reset, the previous |
michael@0 | 106 | // element in the containing scope, which is what we want). If |
michael@0 | 107 | // we're not in the same scope that it is, then it's too deep in the |
michael@0 | 108 | // frame tree, so we walk up parent scopes until we find something |
michael@0 | 109 | // appropriate. |
michael@0 | 110 | |
michael@0 | 111 | if (aNode == First()) { |
michael@0 | 112 | aNode->mScopeStart = nullptr; |
michael@0 | 113 | aNode->mScopePrev = nullptr; |
michael@0 | 114 | return; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | // Get the content node for aNode's rendering object's *parent*, |
michael@0 | 118 | // since scope includes siblings, so we want a descendant check on |
michael@0 | 119 | // parents. |
michael@0 | 120 | nsIContent *nodeContent = aNode->mPseudoFrame->GetContent()->GetParent(); |
michael@0 | 121 | |
michael@0 | 122 | for (nsCounterNode *prev = Prev(aNode), *start; |
michael@0 | 123 | prev; prev = start->mScopePrev) { |
michael@0 | 124 | // If |prev| starts a scope (because it's a real or implied |
michael@0 | 125 | // reset), we want it as the scope start rather than the start |
michael@0 | 126 | // of its enclosing scope. Otherwise, there's no enclosing |
michael@0 | 127 | // scope, so the next thing in prev's scope shares its scope |
michael@0 | 128 | // start. |
michael@0 | 129 | start = (prev->mType == nsCounterNode::RESET || !prev->mScopeStart) |
michael@0 | 130 | ? prev : prev->mScopeStart; |
michael@0 | 131 | |
michael@0 | 132 | // |startContent| is analogous to |nodeContent| (see above). |
michael@0 | 133 | nsIContent *startContent = start->mPseudoFrame->GetContent()->GetParent(); |
michael@0 | 134 | NS_ASSERTION(nodeContent || !startContent, |
michael@0 | 135 | "null check on startContent should be sufficient to " |
michael@0 | 136 | "null check nodeContent as well, since if nodeContent " |
michael@0 | 137 | "is for the root, startContent (which is before it) " |
michael@0 | 138 | "must be too"); |
michael@0 | 139 | |
michael@0 | 140 | // A reset's outer scope can't be a scope created by a sibling. |
michael@0 | 141 | if (!(aNode->mType == nsCounterNode::RESET && |
michael@0 | 142 | nodeContent == startContent) && |
michael@0 | 143 | // everything is inside the root (except the case above, |
michael@0 | 144 | // a second reset on the root) |
michael@0 | 145 | (!startContent || |
michael@0 | 146 | nsContentUtils::ContentIsDescendantOf(nodeContent, |
michael@0 | 147 | startContent))) { |
michael@0 | 148 | aNode->mScopeStart = start; |
michael@0 | 149 | aNode->mScopePrev = prev; |
michael@0 | 150 | return; |
michael@0 | 151 | } |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | aNode->mScopeStart = nullptr; |
michael@0 | 155 | aNode->mScopePrev = nullptr; |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | void |
michael@0 | 159 | nsCounterList::RecalcAll() |
michael@0 | 160 | { |
michael@0 | 161 | mDirty = false; |
michael@0 | 162 | |
michael@0 | 163 | nsCounterNode *node = First(); |
michael@0 | 164 | if (!node) |
michael@0 | 165 | return; |
michael@0 | 166 | |
michael@0 | 167 | do { |
michael@0 | 168 | SetScope(node); |
michael@0 | 169 | node->Calc(this); |
michael@0 | 170 | |
michael@0 | 171 | if (node->mType == nsCounterNode::USE) { |
michael@0 | 172 | nsCounterUseNode *useNode = node->UseNode(); |
michael@0 | 173 | // Null-check mText, since if the frame constructor isn't |
michael@0 | 174 | // batching, we could end up here while the node is being |
michael@0 | 175 | // constructed. |
michael@0 | 176 | if (useNode->mText) { |
michael@0 | 177 | nsAutoString text; |
michael@0 | 178 | useNode->GetText(text); |
michael@0 | 179 | useNode->mText->SetData(text); |
michael@0 | 180 | } |
michael@0 | 181 | } |
michael@0 | 182 | } while ((node = Next(node)) != First()); |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | nsCounterManager::nsCounterManager() |
michael@0 | 186 | : mNames(16) |
michael@0 | 187 | { |
michael@0 | 188 | } |
michael@0 | 189 | |
michael@0 | 190 | bool |
michael@0 | 191 | nsCounterManager::AddCounterResetsAndIncrements(nsIFrame *aFrame) |
michael@0 | 192 | { |
michael@0 | 193 | const nsStyleContent *styleContent = aFrame->StyleContent(); |
michael@0 | 194 | if (!styleContent->CounterIncrementCount() && |
michael@0 | 195 | !styleContent->CounterResetCount()) |
michael@0 | 196 | return false; |
michael@0 | 197 | |
michael@0 | 198 | // Add in order, resets first, so all the comparisons will be optimized |
michael@0 | 199 | // for addition at the end of the list. |
michael@0 | 200 | int32_t i, i_end; |
michael@0 | 201 | bool dirty = false; |
michael@0 | 202 | for (i = 0, i_end = styleContent->CounterResetCount(); i != i_end; ++i) |
michael@0 | 203 | dirty |= AddResetOrIncrement(aFrame, i, |
michael@0 | 204 | styleContent->GetCounterResetAt(i), |
michael@0 | 205 | nsCounterChangeNode::RESET); |
michael@0 | 206 | for (i = 0, i_end = styleContent->CounterIncrementCount(); i != i_end; ++i) |
michael@0 | 207 | dirty |= AddResetOrIncrement(aFrame, i, |
michael@0 | 208 | styleContent->GetCounterIncrementAt(i), |
michael@0 | 209 | nsCounterChangeNode::INCREMENT); |
michael@0 | 210 | return dirty; |
michael@0 | 211 | } |
michael@0 | 212 | |
michael@0 | 213 | bool |
michael@0 | 214 | nsCounterManager::AddResetOrIncrement(nsIFrame *aFrame, int32_t aIndex, |
michael@0 | 215 | const nsStyleCounterData *aCounterData, |
michael@0 | 216 | nsCounterNode::Type aType) |
michael@0 | 217 | { |
michael@0 | 218 | nsCounterChangeNode *node = |
michael@0 | 219 | new nsCounterChangeNode(aFrame, aType, aCounterData->mValue, aIndex); |
michael@0 | 220 | |
michael@0 | 221 | nsCounterList *counterList = CounterListFor(aCounterData->mCounter); |
michael@0 | 222 | if (!counterList) { |
michael@0 | 223 | NS_NOTREACHED("CounterListFor failed (should only happen on OOM)"); |
michael@0 | 224 | return false; |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | counterList->Insert(node); |
michael@0 | 228 | if (!counterList->IsLast(node)) { |
michael@0 | 229 | // Tell the caller it's responsible for recalculating the entire |
michael@0 | 230 | // list. |
michael@0 | 231 | counterList->SetDirty(); |
michael@0 | 232 | return true; |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | // Don't call Calc() if the list is already dirty -- it'll be recalculated |
michael@0 | 236 | // anyway, and trying to calculate with a dirty list doesn't work. |
michael@0 | 237 | if (MOZ_LIKELY(!counterList->IsDirty())) { |
michael@0 | 238 | node->Calc(counterList); |
michael@0 | 239 | } |
michael@0 | 240 | return false; |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | nsCounterList* |
michael@0 | 244 | nsCounterManager::CounterListFor(const nsSubstring& aCounterName) |
michael@0 | 245 | { |
michael@0 | 246 | // XXX Why doesn't nsTHashtable provide an API that allows us to use |
michael@0 | 247 | // get/put in one hashtable lookup? |
michael@0 | 248 | nsCounterList *counterList; |
michael@0 | 249 | if (!mNames.Get(aCounterName, &counterList)) { |
michael@0 | 250 | counterList = new nsCounterList(); |
michael@0 | 251 | mNames.Put(aCounterName, counterList); |
michael@0 | 252 | } |
michael@0 | 253 | return counterList; |
michael@0 | 254 | } |
michael@0 | 255 | |
michael@0 | 256 | static PLDHashOperator |
michael@0 | 257 | RecalcDirtyLists(const nsAString& aKey, nsCounterList* aList, void* aClosure) |
michael@0 | 258 | { |
michael@0 | 259 | if (aList->IsDirty()) |
michael@0 | 260 | aList->RecalcAll(); |
michael@0 | 261 | return PL_DHASH_NEXT; |
michael@0 | 262 | } |
michael@0 | 263 | |
michael@0 | 264 | void |
michael@0 | 265 | nsCounterManager::RecalcAll() |
michael@0 | 266 | { |
michael@0 | 267 | mNames.EnumerateRead(RecalcDirtyLists, nullptr); |
michael@0 | 268 | } |
michael@0 | 269 | |
michael@0 | 270 | struct DestroyNodesData { |
michael@0 | 271 | DestroyNodesData(nsIFrame *aFrame) |
michael@0 | 272 | : mFrame(aFrame) |
michael@0 | 273 | , mDestroyedAny(false) |
michael@0 | 274 | { |
michael@0 | 275 | } |
michael@0 | 276 | |
michael@0 | 277 | nsIFrame *mFrame; |
michael@0 | 278 | bool mDestroyedAny; |
michael@0 | 279 | }; |
michael@0 | 280 | |
michael@0 | 281 | static PLDHashOperator |
michael@0 | 282 | DestroyNodesInList(const nsAString& aKey, nsCounterList* aList, void* aClosure) |
michael@0 | 283 | { |
michael@0 | 284 | DestroyNodesData *data = static_cast<DestroyNodesData*>(aClosure); |
michael@0 | 285 | if (aList->DestroyNodesFor(data->mFrame)) { |
michael@0 | 286 | data->mDestroyedAny = true; |
michael@0 | 287 | aList->SetDirty(); |
michael@0 | 288 | } |
michael@0 | 289 | return PL_DHASH_NEXT; |
michael@0 | 290 | } |
michael@0 | 291 | |
michael@0 | 292 | bool |
michael@0 | 293 | nsCounterManager::DestroyNodesFor(nsIFrame *aFrame) |
michael@0 | 294 | { |
michael@0 | 295 | DestroyNodesData data(aFrame); |
michael@0 | 296 | mNames.EnumerateRead(DestroyNodesInList, &data); |
michael@0 | 297 | return data.mDestroyedAny; |
michael@0 | 298 | } |
michael@0 | 299 | |
michael@0 | 300 | #ifdef DEBUG |
michael@0 | 301 | static PLDHashOperator |
michael@0 | 302 | DumpList(const nsAString& aKey, nsCounterList* aList, void* aClosure) |
michael@0 | 303 | { |
michael@0 | 304 | printf("Counter named \"%s\":\n", NS_ConvertUTF16toUTF8(aKey).get()); |
michael@0 | 305 | nsCounterNode *node = aList->First(); |
michael@0 | 306 | |
michael@0 | 307 | if (node) { |
michael@0 | 308 | int32_t i = 0; |
michael@0 | 309 | do { |
michael@0 | 310 | const char *types[] = { "RESET", "INCREMENT", "USE" }; |
michael@0 | 311 | printf(" Node #%d @%p frame=%p index=%d type=%s valAfter=%d\n" |
michael@0 | 312 | " scope-start=%p scope-prev=%p", |
michael@0 | 313 | i++, (void*)node, (void*)node->mPseudoFrame, |
michael@0 | 314 | node->mContentIndex, types[node->mType], node->mValueAfter, |
michael@0 | 315 | (void*)node->mScopeStart, (void*)node->mScopePrev); |
michael@0 | 316 | if (node->mType == nsCounterNode::USE) { |
michael@0 | 317 | nsAutoString text; |
michael@0 | 318 | node->UseNode()->GetText(text); |
michael@0 | 319 | printf(" text=%s", NS_ConvertUTF16toUTF8(text).get()); |
michael@0 | 320 | } |
michael@0 | 321 | printf("\n"); |
michael@0 | 322 | } while ((node = aList->Next(node)) != aList->First()); |
michael@0 | 323 | } |
michael@0 | 324 | return PL_DHASH_NEXT; |
michael@0 | 325 | } |
michael@0 | 326 | |
michael@0 | 327 | void |
michael@0 | 328 | nsCounterManager::Dump() |
michael@0 | 329 | { |
michael@0 | 330 | printf("\n\nCounter Manager Lists:\n"); |
michael@0 | 331 | mNames.EnumerateRead(DumpList, nullptr); |
michael@0 | 332 | printf("\n\n"); |
michael@0 | 333 | } |
michael@0 | 334 | #endif |