1/*
2 * Copyright (C) 2005 Allan Sandfeld Jensen (kde@carewolf.com)
3 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20*/
21
22#ifndef CounterNode_h
23#define CounterNode_h
24
25#include "wtf/Forward.h"
26#include "wtf/Noncopyable.h"
27#include "wtf/RefCounted.h"
28
29// This implements a counter tree that is used for finding parents in counters() lookup,
30// and for propagating count changes when nodes are added or removed.
31
32// Parents represent unique counters and their scope, which are created either explicitly
33// by "counter-reset" style rules or implicitly by referring to a counter that is not in scope.
34// Such nodes are tagged as "reset" nodes, although they are not all due to "counter-reset".
35
36// Not that render tree children are often counter tree siblings due to counter scoping rules.
37
38namespace blink {
39
40class RenderObject;
41class RenderCounter;
42
43class CounterNode : public RefCounted<CounterNode> {
44public:
45    static PassRefPtr<CounterNode> create(RenderObject&, bool isReset, int value);
46    ~CounterNode();
47    bool actsAsReset() const { return m_hasResetType || !m_parent; }
48    bool hasResetType() const { return m_hasResetType; }
49    int value() const { return m_value; }
50    int countInParent() const { return m_countInParent; }
51    RenderObject& owner() const { return m_owner; }
52    void addRenderer(RenderCounter*);
53    void removeRenderer(RenderCounter*);
54
55    // Invalidates the text in the renderers of this counter, if any.
56    void resetRenderers();
57
58    CounterNode* parent() const { return m_parent; }
59    CounterNode* previousSibling() const { return m_previousSibling; }
60    CounterNode* nextSibling() const { return m_nextSibling; }
61    CounterNode* firstChild() const { return m_firstChild; }
62    CounterNode* lastChild() const { return m_lastChild; }
63    CounterNode* lastDescendant() const;
64    CounterNode* previousInPreOrder() const;
65    CounterNode* nextInPreOrder(const CounterNode* stayWithin = 0) const;
66    CounterNode* nextInPreOrderAfterChildren(const CounterNode* stayWithin = 0) const;
67
68    void insertAfter(CounterNode* newChild, CounterNode* beforeChild, const AtomicString& identifier);
69
70    // identifier must match the identifier of this counter.
71    void removeChild(CounterNode*);
72
73private:
74    CounterNode(RenderObject&, bool isReset, int value);
75    int computeCountInParent() const;
76    // Invalidates the text in the renderer of this counter, if any,
77    // and in the renderers of all descendants of this counter, if any.
78    void resetThisAndDescendantsRenderers();
79    void recount();
80
81    bool m_hasResetType;
82    int m_value;
83    int m_countInParent;
84    RenderObject& m_owner;
85    RenderCounter* m_rootRenderer;
86
87    CounterNode* m_parent;
88    CounterNode* m_previousSibling;
89    CounterNode* m_nextSibling;
90    CounterNode* m_firstChild;
91    CounterNode* m_lastChild;
92};
93
94} // namespace blink
95
96#ifndef NDEBUG
97// Outside the WebCore namespace for ease of invocation from gdb.
98void showCounterTree(const blink::CounterNode*);
99#endif
100
101#endif // CounterNode_h
102