initial import
[vuplus_webkit] / Source / WebCore / rendering / CounterNode.cpp
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 #include "config.h"
23 #include "CounterNode.h"
24
25 #include "RenderCounter.h"
26 #include "RenderObject.h"
27 #include <stdio.h>
28
29 namespace WebCore {
30
31 CounterNode::CounterNode(RenderObject* o, bool hasResetType, int value)
32     : m_hasResetType(hasResetType)
33     , m_value(value)
34     , m_countInParent(0)
35     , m_owner(o)
36     , m_rootRenderer(0)
37     , m_parent(0)
38     , m_previousSibling(0)
39     , m_nextSibling(0)
40     , m_firstChild(0)
41     , m_lastChild(0)
42 {
43 }
44
45 CounterNode::~CounterNode()
46 {
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.
53         if (m_parent) {
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;
58             oldParent = m_parent;
59             m_parent = 0;
60         }
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;
66         }
67         if (m_nextSibling) {
68             if (m_nextSibling->m_previousSibling == this)
69                 m_nextSibling->m_previousSibling = oldPreviousSibling;
70             m_nextSibling = 0;
71         }
72         if (m_firstChild) {
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;
85                 }
86                 child = nextChild;
87             }
88         }
89     }
90     resetRenderers();
91 }
92
93 PassRefPtr<CounterNode> CounterNode::create(RenderObject* owner, bool hasResetType, int value)
94 {
95     return adoptRef(new CounterNode(owner, hasResetType, value));
96 }
97
98 CounterNode* CounterNode::nextInPreOrderAfterChildren(const CounterNode* stayWithin) const
99 {
100     if (this == stayWithin)
101         return 0;
102
103     const CounterNode* current = this;
104     CounterNode* next;
105     while (!(next = current->m_nextSibling)) {
106         current = current->m_parent;
107         if (!current || current == stayWithin)
108             return 0;
109     }
110     return next;
111 }
112
113 CounterNode* CounterNode::nextInPreOrder(const CounterNode* stayWithin) const
114 {
115     if (CounterNode* next = m_firstChild)
116         return next;
117
118     return nextInPreOrderAfterChildren(stayWithin);
119 }
120
121 CounterNode* CounterNode::lastDescendant() const
122 {
123     CounterNode* last = m_lastChild;
124     if (!last)
125         return 0;
126
127     while (CounterNode* lastChild = last->m_lastChild)
128         last = lastChild;
129
130     return last;
131 }
132
133 CounterNode* CounterNode::previousInPreOrder() const
134 {
135     CounterNode* previous = m_previousSibling;
136     if (!previous)
137         return m_parent;
138
139     while (CounterNode* lastChild = previous->m_lastChild)
140         previous = lastChild;
141
142     return previous;
143 }
144
145 int CounterNode::computeCountInParent() const
146 {
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;
152 }
153
154 void CounterNode::addRenderer(RenderCounter* value)
155 {
156     if (!value) {
157         ASSERT_NOT_REACHED();
158         return;
159     }
160     if (value->m_counterNode) {
161         ASSERT_NOT_REACHED();
162         value->m_counterNode->removeRenderer(value);
163     }
164     ASSERT(!value->m_nextForSameCounter);
165     for (RenderCounter* iterator = m_rootRenderer;iterator; iterator = iterator->m_nextForSameCounter) {
166         if (iterator == value) {
167             ASSERT_NOT_REACHED();
168             return;
169         }
170     }
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);
177         }
178         value->m_counterNode = this;
179     }
180 }
181
182 void CounterNode::removeRenderer(RenderCounter* value)
183 {
184     if (!value) {
185         ASSERT_NOT_REACHED();
186         return;
187     }
188     if (value->m_counterNode && value->m_counterNode != this) {
189         ASSERT_NOT_REACHED();
190         value->m_counterNode->removeRenderer(value);
191     }
192     RenderCounter* previous = 0;
193     for (RenderCounter* iterator = m_rootRenderer;iterator; iterator = iterator->m_nextForSameCounter) {
194         if (iterator == value) {
195             if (previous)
196                 previous->m_nextForSameCounter = value->m_nextForSameCounter;
197             else
198                 m_rootRenderer = value->m_nextForSameCounter;
199             value->m_nextForSameCounter = 0;
200             value->m_counterNode = 0;
201             return;
202         }
203         previous = iterator;
204     }
205     ASSERT_NOT_REACHED();
206 }
207
208 void CounterNode::resetRenderers()
209 {
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.
212 }
213
214 void CounterNode::resetThisAndDescendantsRenderers()
215 {
216     CounterNode* node = this;
217     do {
218         node->resetRenderers();
219         node = node->nextInPreOrder(this);
220     } while (node);
221 }
222
223 void CounterNode::recount()
224 {
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)
229             break;
230         node->m_countInParent = newCount;
231         node->resetThisAndDescendantsRenderers();
232     }
233 }
234
235 void CounterNode::insertAfter(CounterNode* newChild, CounterNode* refChild, const AtomicString& identifier)
236 {
237     ASSERT(newChild);
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)
244         return;
245
246     if (newChild->m_hasResetType) {
247         while (m_lastChild != refChild)
248             RenderCounter::destroyCounterNode(m_lastChild->owner(), identifier);
249     }
250
251     CounterNode* next;
252
253     if (refChild) {
254         next = refChild->m_nextSibling;
255         refChild->m_nextSibling = newChild;
256     } else {
257         next = m_firstChild;
258         m_firstChild = newChild;
259     }
260
261     newChild->m_parent = this;
262     newChild->m_previousSibling = refChild;
263
264     if (!newChild->m_firstChild || newChild->m_hasResetType) {
265         newChild->m_nextSibling = next;
266         if (next) {
267             ASSERT(next->m_previousSibling == refChild);
268             next->m_previousSibling = newChild;
269         } else {
270             ASSERT(m_lastChild == refChild);
271             m_lastChild = newChild;
272         }
273
274         newChild->m_countInParent = newChild->computeCountInParent();
275         newChild->resetThisAndDescendantsRenderers();
276         if (next)
277             next->recount();
278         return;
279     }
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;
283
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;
288
289     if (first) {
290         ASSERT(last);
291         newChild->m_nextSibling = first;
292         if (m_lastChild == newChild)
293             m_lastChild = last;
294
295         first->m_previousSibling = newChild;
296     
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;
307         if (next) {
308             ASSERT(next->m_previousSibling == newChild);
309             next->m_previousSibling = last;
310         } else
311             m_lastChild = last;
312         for (next = first; ; next = next->m_nextSibling) {
313             next->m_parent = this;
314             if (last == next)
315                 break;
316         }
317     }
318     newChild->m_firstChild = 0;
319     newChild->m_lastChild = 0;
320     newChild->m_countInParent = newChild->computeCountInParent();
321     newChild->resetRenderers();
322     first->recount();
323 }
324
325 void CounterNode::removeChild(CounterNode* oldChild)
326 {
327     ASSERT(oldChild);
328     ASSERT(!oldChild->m_firstChild);
329     ASSERT(!oldChild->m_lastChild);
330
331     CounterNode* next = oldChild->m_nextSibling;
332     CounterNode* previous = oldChild->m_previousSibling;
333
334     oldChild->m_nextSibling = 0;
335     oldChild->m_previousSibling = 0;
336     oldChild->m_parent = 0;
337
338     if (previous) 
339         previous->m_nextSibling = next;
340     else {
341         ASSERT(m_firstChild == oldChild);
342         m_firstChild = next;
343     }
344
345     if (next)
346         next->m_previousSibling = previous;
347     else {
348         ASSERT(m_lastChild == oldChild);
349         m_lastChild = previous;
350     }
351
352     if (next)
353         next->recount();
354 }
355
356 #ifndef NDEBUG
357
358 static void showTreeAndMark(const CounterNode* node)
359 {
360     const CounterNode* root = node;
361     while (root->parent())
362         root = root->parent();
363
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());
372     }
373     fflush(stderr);
374 }
375
376 #endif
377
378 } // namespace WebCore
379
380 #ifndef NDEBUG
381
382 void showCounterTree(const WebCore::CounterNode* counter)
383 {
384     if (counter)
385         showTreeAndMark(counter);
386 }
387
388 #endif