2 * Copyright (C) 2005 Allan Sandfeld Jensen (kde@carewolf.com)
3 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
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.
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.
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.
23 #include "CounterNode.h"
25 #include "RenderCounter.h"
26 #include "RenderObject.h"
31 CounterNode::CounterNode(RenderObject* o, bool hasResetType, int value)
32 : m_hasResetType(hasResetType)
38 , m_previousSibling(0)
45 CounterNode::~CounterNode()
47 // Ideally this would be an assert and this would never be reached. In reality this happens a lot
48 // so we need to handle these cases. The node is still connected to the tree so we need to detach it.
49 if (m_parent || m_previousSibling || m_nextSibling || m_firstChild || m_lastChild) {
50 CounterNode* oldParent = 0;
51 CounterNode* oldPreviousSibling = 0;
52 // Instead of calling removeChild() we do this safely as the tree is likely broken if we get here.
54 if (m_parent->m_firstChild == this)
55 m_parent->m_firstChild = m_nextSibling;
56 if (m_parent->m_lastChild == this)
57 m_parent->m_lastChild = m_previousSibling;
61 if (m_previousSibling) {
62 if (m_previousSibling->m_nextSibling == this)
63 m_previousSibling->m_nextSibling = m_nextSibling;
64 oldPreviousSibling = m_previousSibling;
65 m_previousSibling = 0;
68 if (m_nextSibling->m_previousSibling == this)
69 m_nextSibling->m_previousSibling = oldPreviousSibling;
73 // The node's children are reparented to the old parent.
74 for (CounterNode* child = m_firstChild; child; ) {
75 CounterNode* nextChild = child->m_nextSibling;
76 CounterNode* nextSibling = 0;
77 child->m_parent = oldParent;
78 if (oldPreviousSibling) {
79 nextSibling = oldPreviousSibling->m_nextSibling;
80 child->m_previousSibling = oldPreviousSibling;
81 oldPreviousSibling->m_nextSibling = child;
82 child->m_nextSibling = nextSibling;
83 nextSibling->m_previousSibling = child;
84 oldPreviousSibling = child;
93 PassRefPtr<CounterNode> CounterNode::create(RenderObject* owner, bool hasResetType, int value)
95 return adoptRef(new CounterNode(owner, hasResetType, value));
98 CounterNode* CounterNode::nextInPreOrderAfterChildren(const CounterNode* stayWithin) const
100 if (this == stayWithin)
103 const CounterNode* current = this;
105 while (!(next = current->m_nextSibling)) {
106 current = current->m_parent;
107 if (!current || current == stayWithin)
113 CounterNode* CounterNode::nextInPreOrder(const CounterNode* stayWithin) const
115 if (CounterNode* next = m_firstChild)
118 return nextInPreOrderAfterChildren(stayWithin);
121 CounterNode* CounterNode::lastDescendant() const
123 CounterNode* last = m_lastChild;
127 while (CounterNode* lastChild = last->m_lastChild)
133 CounterNode* CounterNode::previousInPreOrder() const
135 CounterNode* previous = m_previousSibling;
139 while (CounterNode* lastChild = previous->m_lastChild)
140 previous = lastChild;
145 int CounterNode::computeCountInParent() const
147 int increment = actsAsReset() ? 0 : m_value;
148 if (m_previousSibling)
149 return m_previousSibling->m_countInParent + increment;
150 ASSERT(m_parent->m_firstChild == this);
151 return m_parent->m_value + increment;
154 void CounterNode::addRenderer(RenderCounter* value)
157 ASSERT_NOT_REACHED();
160 if (value->m_counterNode) {
161 ASSERT_NOT_REACHED();
162 value->m_counterNode->removeRenderer(value);
164 ASSERT(!value->m_nextForSameCounter);
165 for (RenderCounter* iterator = m_rootRenderer;iterator; iterator = iterator->m_nextForSameCounter) {
166 if (iterator == value) {
167 ASSERT_NOT_REACHED();
171 value->m_nextForSameCounter = m_rootRenderer;
172 m_rootRenderer = value;
173 if (value->m_counterNode != this) {
174 if (value->m_counterNode) {
175 ASSERT_NOT_REACHED();
176 value->m_counterNode->removeRenderer(value);
178 value->m_counterNode = this;
182 void CounterNode::removeRenderer(RenderCounter* value)
185 ASSERT_NOT_REACHED();
188 if (value->m_counterNode && value->m_counterNode != this) {
189 ASSERT_NOT_REACHED();
190 value->m_counterNode->removeRenderer(value);
192 RenderCounter* previous = 0;
193 for (RenderCounter* iterator = m_rootRenderer;iterator; iterator = iterator->m_nextForSameCounter) {
194 if (iterator == value) {
196 previous->m_nextForSameCounter = value->m_nextForSameCounter;
198 m_rootRenderer = value->m_nextForSameCounter;
199 value->m_nextForSameCounter = 0;
200 value->m_counterNode = 0;
205 ASSERT_NOT_REACHED();
208 void CounterNode::resetRenderers()
210 while (m_rootRenderer)
211 m_rootRenderer->invalidate(); // This makes m_rootRenderer point to the next renderer if any since it disconnects the m_rootRenderer from this.
214 void CounterNode::resetThisAndDescendantsRenderers()
216 CounterNode* node = this;
218 node->resetRenderers();
219 node = node->nextInPreOrder(this);
223 void CounterNode::recount()
225 for (CounterNode* node = this; node; node = node->m_nextSibling) {
226 int oldCount = node->m_countInParent;
227 int newCount = node->computeCountInParent();
228 if (oldCount == newCount)
230 node->m_countInParent = newCount;
231 node->resetThisAndDescendantsRenderers();
235 void CounterNode::insertAfter(CounterNode* newChild, CounterNode* refChild, const AtomicString& identifier)
238 ASSERT(!newChild->m_parent);
239 ASSERT(!newChild->m_previousSibling);
240 ASSERT(!newChild->m_nextSibling);
241 // If the refChild is not our child we can not complete the request. This hardens against bugs in RenderCounter.
242 // When renderers are reparented it may request that we insert counter nodes improperly.
243 if (refChild && refChild->m_parent != this)
246 if (newChild->m_hasResetType) {
247 while (m_lastChild != refChild)
248 RenderCounter::destroyCounterNode(m_lastChild->owner(), identifier);
254 next = refChild->m_nextSibling;
255 refChild->m_nextSibling = newChild;
258 m_firstChild = newChild;
261 newChild->m_parent = this;
262 newChild->m_previousSibling = refChild;
264 if (!newChild->m_firstChild || newChild->m_hasResetType) {
265 newChild->m_nextSibling = next;
267 ASSERT(next->m_previousSibling == refChild);
268 next->m_previousSibling = newChild;
270 ASSERT(m_lastChild == refChild);
271 m_lastChild = newChild;
274 newChild->m_countInParent = newChild->computeCountInParent();
275 newChild->resetThisAndDescendantsRenderers();
280 // If the new child is the last in the sibling list we must set the parent's lastChild.
281 if (!newChild->m_nextSibling)
282 m_lastChild = newChild;
284 // The code below handles the case when a formerly root increment counter is loosing its root position
285 // and therefore its children become next siblings.
286 CounterNode* last = newChild->m_lastChild;
287 CounterNode* first = newChild->m_firstChild;
291 newChild->m_nextSibling = first;
292 if (m_lastChild == newChild)
295 first->m_previousSibling = newChild;
297 // The case when the original next sibling of the inserted node becomes a child of
298 // one of the former children of the inserted node is not handled as it is believed
299 // to be impossible since:
300 // 1. if the increment counter node lost it's root position as a result of another
301 // counter node being created, it will be inserted as the last child so next is null.
302 // 2. if the increment counter node lost it's root position as a result of a renderer being
303 // inserted into the document's render tree, all its former children counters are attached
304 // to children of the inserted renderer and hence cannot be in scope for counter nodes
305 // attached to renderers that were already in the document's render tree.
306 last->m_nextSibling = next;
308 ASSERT(next->m_previousSibling == newChild);
309 next->m_previousSibling = last;
312 for (next = first; ; next = next->m_nextSibling) {
313 next->m_parent = this;
318 newChild->m_firstChild = 0;
319 newChild->m_lastChild = 0;
320 newChild->m_countInParent = newChild->computeCountInParent();
321 newChild->resetRenderers();
325 void CounterNode::removeChild(CounterNode* oldChild)
328 ASSERT(!oldChild->m_firstChild);
329 ASSERT(!oldChild->m_lastChild);
331 CounterNode* next = oldChild->m_nextSibling;
332 CounterNode* previous = oldChild->m_previousSibling;
334 oldChild->m_nextSibling = 0;
335 oldChild->m_previousSibling = 0;
336 oldChild->m_parent = 0;
339 previous->m_nextSibling = next;
341 ASSERT(m_firstChild == oldChild);
346 next->m_previousSibling = previous;
348 ASSERT(m_lastChild == oldChild);
349 m_lastChild = previous;
358 static void showTreeAndMark(const CounterNode* node)
360 const CounterNode* root = node;
361 while (root->parent())
362 root = root->parent();
364 for (const CounterNode* current = root; current; current = current->nextInPreOrder()) {
365 fprintf(stderr, "%c", (current == node) ? '*' : ' ');
366 for (const CounterNode* parent = current; parent && parent != root; parent = parent->parent())
367 fprintf(stderr, " ");
368 fprintf(stderr, "%p %s: %d %d P:%p PS:%p NS:%p R:%p\n",
369 current, current->actsAsReset() ? "reset____" : "increment", current->value(),
370 current->countInParent(), current->parent(), current->previousSibling(),
371 current->nextSibling(), current->owner());
378 } // namespace WebCore
382 void showCounterTree(const WebCore::CounterNode* counter)
385 showTreeAndMark(counter);