michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: // vim:cindent:ai:sw=4:ts=4:et: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* implementation of CSS counters (for numbering things) */ michael@0: michael@0: #include "nsCounterManager.h" michael@0: #include "nsBulletFrame.h" // legacy location for list style type to text code michael@0: #include "nsContentUtils.h" michael@0: #include "nsTArray.h" michael@0: #include "mozilla/Likely.h" michael@0: #include "nsIContent.h" michael@0: michael@0: bool michael@0: nsCounterUseNode::InitTextFrame(nsGenConList* aList, michael@0: nsIFrame* aPseudoFrame, nsIFrame* aTextFrame) michael@0: { michael@0: nsCounterNode::InitTextFrame(aList, aPseudoFrame, aTextFrame); michael@0: michael@0: nsCounterList *counterList = static_cast(aList); michael@0: counterList->Insert(this); michael@0: bool dirty = counterList->IsDirty(); michael@0: if (!dirty) { michael@0: if (counterList->IsLast(this)) { michael@0: Calc(counterList); michael@0: nsAutoString contentString; michael@0: GetText(contentString); michael@0: aTextFrame->GetContent()->SetText(contentString, false); michael@0: } else { michael@0: // In all other cases (list already dirty or node not at the end), michael@0: // just start with an empty string for now and when we recalculate michael@0: // the list we'll change the value to the right one. michael@0: counterList->SetDirty(); michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: // assign the correct |mValueAfter| value to a node that has been inserted michael@0: // Should be called immediately after calling |Insert|. michael@0: void nsCounterUseNode::Calc(nsCounterList *aList) michael@0: { michael@0: NS_ASSERTION(!aList->IsDirty(), michael@0: "Why are we calculating with a dirty list?"); michael@0: mValueAfter = aList->ValueBefore(this); michael@0: } michael@0: michael@0: // assign the correct |mValueAfter| value to a node that has been inserted michael@0: // Should be called immediately after calling |Insert|. michael@0: void nsCounterChangeNode::Calc(nsCounterList *aList) michael@0: { michael@0: NS_ASSERTION(!aList->IsDirty(), michael@0: "Why are we calculating with a dirty list?"); michael@0: if (mType == RESET) { michael@0: mValueAfter = mChangeValue; michael@0: } else { michael@0: NS_ASSERTION(mType == INCREMENT, "invalid type"); michael@0: mValueAfter = nsCounterManager::IncrementCounter(aList->ValueBefore(this), michael@0: mChangeValue); michael@0: } michael@0: } michael@0: michael@0: // The text that should be displayed for this counter. michael@0: void michael@0: nsCounterUseNode::GetText(nsString& aResult) michael@0: { michael@0: aResult.Truncate(); michael@0: michael@0: nsAutoTArray stack; michael@0: stack.AppendElement(static_cast(this)); michael@0: michael@0: if (mAllCounters && mScopeStart) michael@0: for (nsCounterNode *n = mScopeStart; n->mScopePrev; n = n->mScopeStart) michael@0: stack.AppendElement(n->mScopePrev); michael@0: michael@0: const nsCSSValue& styleItem = mCounterStyle->Item(mAllCounters ? 2 : 1); michael@0: int32_t style = styleItem.GetIntValue(); michael@0: const char16_t* separator; michael@0: if (mAllCounters) michael@0: separator = mCounterStyle->Item(1).GetStringBufferValue(); michael@0: michael@0: for (uint32_t i = stack.Length() - 1;; --i) { michael@0: nsCounterNode *n = stack[i]; michael@0: bool isTextRTL; michael@0: nsBulletFrame::AppendCounterText( michael@0: style, n->mValueAfter, aResult, isTextRTL); michael@0: if (i == 0) michael@0: break; michael@0: NS_ASSERTION(mAllCounters, "yikes, separator is uninitialized"); michael@0: aResult.Append(separator); michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsCounterList::SetScope(nsCounterNode *aNode) michael@0: { michael@0: // This function is responsible for setting |mScopeStart| and michael@0: // |mScopePrev| (whose purpose is described in nsCounterManager.h). michael@0: // We do this by starting from the node immediately preceding michael@0: // |aNode| in content tree order, which is reasonably likely to be michael@0: // the previous element in our scope (or, for a reset, the previous michael@0: // element in the containing scope, which is what we want). If michael@0: // we're not in the same scope that it is, then it's too deep in the michael@0: // frame tree, so we walk up parent scopes until we find something michael@0: // appropriate. michael@0: michael@0: if (aNode == First()) { michael@0: aNode->mScopeStart = nullptr; michael@0: aNode->mScopePrev = nullptr; michael@0: return; michael@0: } michael@0: michael@0: // Get the content node for aNode's rendering object's *parent*, michael@0: // since scope includes siblings, so we want a descendant check on michael@0: // parents. michael@0: nsIContent *nodeContent = aNode->mPseudoFrame->GetContent()->GetParent(); michael@0: michael@0: for (nsCounterNode *prev = Prev(aNode), *start; michael@0: prev; prev = start->mScopePrev) { michael@0: // If |prev| starts a scope (because it's a real or implied michael@0: // reset), we want it as the scope start rather than the start michael@0: // of its enclosing scope. Otherwise, there's no enclosing michael@0: // scope, so the next thing in prev's scope shares its scope michael@0: // start. michael@0: start = (prev->mType == nsCounterNode::RESET || !prev->mScopeStart) michael@0: ? prev : prev->mScopeStart; michael@0: michael@0: // |startContent| is analogous to |nodeContent| (see above). michael@0: nsIContent *startContent = start->mPseudoFrame->GetContent()->GetParent(); michael@0: NS_ASSERTION(nodeContent || !startContent, michael@0: "null check on startContent should be sufficient to " michael@0: "null check nodeContent as well, since if nodeContent " michael@0: "is for the root, startContent (which is before it) " michael@0: "must be too"); michael@0: michael@0: // A reset's outer scope can't be a scope created by a sibling. michael@0: if (!(aNode->mType == nsCounterNode::RESET && michael@0: nodeContent == startContent) && michael@0: // everything is inside the root (except the case above, michael@0: // a second reset on the root) michael@0: (!startContent || michael@0: nsContentUtils::ContentIsDescendantOf(nodeContent, michael@0: startContent))) { michael@0: aNode->mScopeStart = start; michael@0: aNode->mScopePrev = prev; michael@0: return; michael@0: } michael@0: } michael@0: michael@0: aNode->mScopeStart = nullptr; michael@0: aNode->mScopePrev = nullptr; michael@0: } michael@0: michael@0: void michael@0: nsCounterList::RecalcAll() michael@0: { michael@0: mDirty = false; michael@0: michael@0: nsCounterNode *node = First(); michael@0: if (!node) michael@0: return; michael@0: michael@0: do { michael@0: SetScope(node); michael@0: node->Calc(this); michael@0: michael@0: if (node->mType == nsCounterNode::USE) { michael@0: nsCounterUseNode *useNode = node->UseNode(); michael@0: // Null-check mText, since if the frame constructor isn't michael@0: // batching, we could end up here while the node is being michael@0: // constructed. michael@0: if (useNode->mText) { michael@0: nsAutoString text; michael@0: useNode->GetText(text); michael@0: useNode->mText->SetData(text); michael@0: } michael@0: } michael@0: } while ((node = Next(node)) != First()); michael@0: } michael@0: michael@0: nsCounterManager::nsCounterManager() michael@0: : mNames(16) michael@0: { michael@0: } michael@0: michael@0: bool michael@0: nsCounterManager::AddCounterResetsAndIncrements(nsIFrame *aFrame) michael@0: { michael@0: const nsStyleContent *styleContent = aFrame->StyleContent(); michael@0: if (!styleContent->CounterIncrementCount() && michael@0: !styleContent->CounterResetCount()) michael@0: return false; michael@0: michael@0: // Add in order, resets first, so all the comparisons will be optimized michael@0: // for addition at the end of the list. michael@0: int32_t i, i_end; michael@0: bool dirty = false; michael@0: for (i = 0, i_end = styleContent->CounterResetCount(); i != i_end; ++i) michael@0: dirty |= AddResetOrIncrement(aFrame, i, michael@0: styleContent->GetCounterResetAt(i), michael@0: nsCounterChangeNode::RESET); michael@0: for (i = 0, i_end = styleContent->CounterIncrementCount(); i != i_end; ++i) michael@0: dirty |= AddResetOrIncrement(aFrame, i, michael@0: styleContent->GetCounterIncrementAt(i), michael@0: nsCounterChangeNode::INCREMENT); michael@0: return dirty; michael@0: } michael@0: michael@0: bool michael@0: nsCounterManager::AddResetOrIncrement(nsIFrame *aFrame, int32_t aIndex, michael@0: const nsStyleCounterData *aCounterData, michael@0: nsCounterNode::Type aType) michael@0: { michael@0: nsCounterChangeNode *node = michael@0: new nsCounterChangeNode(aFrame, aType, aCounterData->mValue, aIndex); michael@0: michael@0: nsCounterList *counterList = CounterListFor(aCounterData->mCounter); michael@0: if (!counterList) { michael@0: NS_NOTREACHED("CounterListFor failed (should only happen on OOM)"); michael@0: return false; michael@0: } michael@0: michael@0: counterList->Insert(node); michael@0: if (!counterList->IsLast(node)) { michael@0: // Tell the caller it's responsible for recalculating the entire michael@0: // list. michael@0: counterList->SetDirty(); michael@0: return true; michael@0: } michael@0: michael@0: // Don't call Calc() if the list is already dirty -- it'll be recalculated michael@0: // anyway, and trying to calculate with a dirty list doesn't work. michael@0: if (MOZ_LIKELY(!counterList->IsDirty())) { michael@0: node->Calc(counterList); michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: nsCounterList* michael@0: nsCounterManager::CounterListFor(const nsSubstring& aCounterName) michael@0: { michael@0: // XXX Why doesn't nsTHashtable provide an API that allows us to use michael@0: // get/put in one hashtable lookup? michael@0: nsCounterList *counterList; michael@0: if (!mNames.Get(aCounterName, &counterList)) { michael@0: counterList = new nsCounterList(); michael@0: mNames.Put(aCounterName, counterList); michael@0: } michael@0: return counterList; michael@0: } michael@0: michael@0: static PLDHashOperator michael@0: RecalcDirtyLists(const nsAString& aKey, nsCounterList* aList, void* aClosure) michael@0: { michael@0: if (aList->IsDirty()) michael@0: aList->RecalcAll(); michael@0: return PL_DHASH_NEXT; michael@0: } michael@0: michael@0: void michael@0: nsCounterManager::RecalcAll() michael@0: { michael@0: mNames.EnumerateRead(RecalcDirtyLists, nullptr); michael@0: } michael@0: michael@0: struct DestroyNodesData { michael@0: DestroyNodesData(nsIFrame *aFrame) michael@0: : mFrame(aFrame) michael@0: , mDestroyedAny(false) michael@0: { michael@0: } michael@0: michael@0: nsIFrame *mFrame; michael@0: bool mDestroyedAny; michael@0: }; michael@0: michael@0: static PLDHashOperator michael@0: DestroyNodesInList(const nsAString& aKey, nsCounterList* aList, void* aClosure) michael@0: { michael@0: DestroyNodesData *data = static_cast(aClosure); michael@0: if (aList->DestroyNodesFor(data->mFrame)) { michael@0: data->mDestroyedAny = true; michael@0: aList->SetDirty(); michael@0: } michael@0: return PL_DHASH_NEXT; michael@0: } michael@0: michael@0: bool michael@0: nsCounterManager::DestroyNodesFor(nsIFrame *aFrame) michael@0: { michael@0: DestroyNodesData data(aFrame); michael@0: mNames.EnumerateRead(DestroyNodesInList, &data); michael@0: return data.mDestroyedAny; michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: static PLDHashOperator michael@0: DumpList(const nsAString& aKey, nsCounterList* aList, void* aClosure) michael@0: { michael@0: printf("Counter named \"%s\":\n", NS_ConvertUTF16toUTF8(aKey).get()); michael@0: nsCounterNode *node = aList->First(); michael@0: michael@0: if (node) { michael@0: int32_t i = 0; michael@0: do { michael@0: const char *types[] = { "RESET", "INCREMENT", "USE" }; michael@0: printf(" Node #%d @%p frame=%p index=%d type=%s valAfter=%d\n" michael@0: " scope-start=%p scope-prev=%p", michael@0: i++, (void*)node, (void*)node->mPseudoFrame, michael@0: node->mContentIndex, types[node->mType], node->mValueAfter, michael@0: (void*)node->mScopeStart, (void*)node->mScopePrev); michael@0: if (node->mType == nsCounterNode::USE) { michael@0: nsAutoString text; michael@0: node->UseNode()->GetText(text); michael@0: printf(" text=%s", NS_ConvertUTF16toUTF8(text).get()); michael@0: } michael@0: printf("\n"); michael@0: } while ((node = aList->Next(node)) != aList->First()); michael@0: } michael@0: return PL_DHASH_NEXT; michael@0: } michael@0: michael@0: void michael@0: nsCounterManager::Dump() michael@0: { michael@0: printf("\n\nCounter Manager Lists:\n"); michael@0: mNames.EnumerateRead(DumpList, nullptr); michael@0: printf("\n\n"); michael@0: } michael@0: #endif