| 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 | #pragma once |
| 23 | |
| 24 | #include <wtf/Forward.h> |
| 25 | #include <wtf/RefCounted.h> |
| 26 | |
| 27 | // This implements a counter tree that is used for finding parents in counters() lookup, |
| 28 | // and for propagating count changes when nodes are added or removed. |
| 29 | |
| 30 | // Parents represent unique counters and their scope, which are created either explicitly |
| 31 | // by "counter-reset" style rules or implicitly by referring to a counter that is not in scope. |
| 32 | // Such nodes are tagged as "reset" nodes, although they are not all due to "counter-reset". |
| 33 | |
| 34 | // Not that render tree children are often counter tree siblings due to counter scoping rules. |
| 35 | |
| 36 | namespace WebCore { |
| 37 | |
| 38 | class RenderCounter; |
| 39 | class RenderElement; |
| 40 | |
| 41 | class CounterNode : public RefCounted<CounterNode> { |
| 42 | public: |
| 43 | static Ref<CounterNode> create(RenderElement&, bool isReset, int value); |
| 44 | ~CounterNode(); |
| 45 | bool actsAsReset() const { return m_hasResetType || !m_parent; } |
| 46 | bool hasResetType() const { return m_hasResetType; } |
| 47 | int value() const { return m_value; } |
| 48 | int countInParent() const { return m_countInParent; } |
| 49 | RenderElement& owner() const { return m_owner; } |
| 50 | void addRenderer(RenderCounter&); |
| 51 | void removeRenderer(RenderCounter&); |
| 52 | |
| 53 | // Invalidates the text in the renderers of this counter, if any. |
| 54 | void resetRenderers(); |
| 55 | |
| 56 | CounterNode* parent() const { return m_parent; } |
| 57 | CounterNode* previousSibling() const { return m_previousSibling; } |
| 58 | CounterNode* nextSibling() const { return m_nextSibling; } |
| 59 | CounterNode* firstChild() const { return m_firstChild; } |
| 60 | CounterNode* lastChild() const { return m_lastChild; } |
| 61 | CounterNode* lastDescendant() const; |
| 62 | CounterNode* previousInPreOrder() const; |
| 63 | CounterNode* nextInPreOrder(const CounterNode* stayWithin = nullptr) const; |
| 64 | CounterNode* nextInPreOrderAfterChildren(const CounterNode* stayWithin = nullptr) const; |
| 65 | |
| 66 | void insertAfter(CounterNode& newChild, CounterNode* beforeChild, const AtomicString& identifier); |
| 67 | // identifier must match the identifier of this counter. |
| 68 | void removeChild(CounterNode&); |
| 69 | |
| 70 | private: |
| 71 | CounterNode(RenderElement&, bool isReset, int value); |
| 72 | int computeCountInParent() const; |
| 73 | // Invalidates the text in the renderer of this counter, if any, |
| 74 | // and in the renderers of all descendants of this counter, if any. |
| 75 | void resetThisAndDescendantsRenderers(); |
| 76 | void recount(); |
| 77 | |
| 78 | bool m_hasResetType; |
| 79 | int m_value; |
| 80 | int m_countInParent; |
| 81 | RenderElement& m_owner; |
| 82 | RenderCounter* m_rootRenderer { nullptr }; |
| 83 | |
| 84 | CounterNode* m_parent { nullptr }; |
| 85 | CounterNode* m_previousSibling { nullptr }; |
| 86 | CounterNode* m_nextSibling { nullptr }; |
| 87 | CounterNode* m_firstChild { nullptr }; |
| 88 | CounterNode* m_lastChild { nullptr }; |
| 89 | }; |
| 90 | |
| 91 | } // namespace WebCore |
| 92 | |
| 93 | #if ENABLE(TREE_DEBUGGING) |
| 94 | // Outside the WebCore namespace for ease of invocation from the debugger. |
| 95 | void showCounterTree(const WebCore::CounterNode*); |
| 96 | #endif |
| 97 | |