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