blob: 09f0175e583fc2e09997d48a008dd055765f79d9 [file] [log] [blame]
bdakin5d6edd42006-10-02 23:15:31 +00001/*
bdakin5d6edd42006-10-02 23:15:31 +00002 * Copyright (C) 2005 Allan Sandfeld Jensen (kde@carewolf.com)
darinb5a0e3d2007-01-10 00:08:18 +00003 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
bdakin5d6edd42006-10-02 23:15:31 +00004 *
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
ddkilzerc8eccec2007-09-26 02:29:57 +000017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
bdakin5d6edd42006-10-02 23:15:31 +000019 *
20 */
21
22#include "config.h"
23#include "CounterNode.h"
24
eric@webkit.orgae29b2f2010-01-16 02:14:25 +000025#include "RenderCounter.h"
bdakin5d6edd42006-10-02 23:15:31 +000026#include "RenderObject.h"
hausmann@webkit.org3de6b942008-02-25 13:16:40 +000027#include <stdio.h>
bdakin5d6edd42006-10-02 23:15:31 +000028
29namespace WebCore {
30
eric@webkit.orgd8ca5e12009-12-21 20:11:47 +000031CounterNode::CounterNode(RenderObject* o, bool hasResetType, int value)
32 : m_hasResetType(hasResetType)
darinec375482007-01-06 01:36:24 +000033 , m_value(value)
34 , m_countInParent(0)
carol.szabo@nokia.com7cf0a552011-03-23 00:04:58 +000035 , m_owner(o)
36 , m_rootRenderer(0)
darinec375482007-01-06 01:36:24 +000037 , m_parent(0)
38 , m_previousSibling(0)
39 , m_nextSibling(0)
40 , m_firstChild(0)
41 , m_lastChild(0)
eric@webkit.org02482712009-11-13 20:21:44 +000042{
43}
44
carol.szabo@nokia.comd7917f72011-03-24 02:25:06 +000045CounterNode::~CounterNode()
46{
cdn@chromium.orgc9e05062011-08-08 20:39:23 +000047 // 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 }
carol.szabo@nokia.comd7917f72011-03-24 02:25:06 +000090 resetRenderers();
91}
92
carol.szabo@nokia.com7cf0a552011-03-23 00:04:58 +000093PassRefPtr<CounterNode> CounterNode::create(RenderObject* owner, bool hasResetType, int value)
jschuh@chromium.orge94df972010-09-30 20:31:45 +000094{
carol.szabo@nokia.com7cf0a552011-03-23 00:04:58 +000095 return adoptRef(new CounterNode(owner, hasResetType, value));
jschuh@chromium.orge94df972010-09-30 20:31:45 +000096}
97
eric@webkit.org02482712009-11-13 20:21:44 +000098CounterNode* CounterNode::nextInPreOrderAfterChildren(const CounterNode* stayWithin) const
99{
100 if (this == stayWithin)
101 return 0;
102
eric@webkit.orgd9edbba2010-01-14 03:30:36 +0000103 const CounterNode* current = this;
104 CounterNode* next;
105 while (!(next = current->m_nextSibling)) {
106 current = current->m_parent;
107 if (!current || current == stayWithin)
eric@webkit.org02482712009-11-13 20:21:44 +0000108 return 0;
eric@webkit.org02482712009-11-13 20:21:44 +0000109 }
eric@webkit.orgd9edbba2010-01-14 03:30:36 +0000110 return next;
eric@webkit.org02482712009-11-13 20:21:44 +0000111}
112
113CounterNode* CounterNode::nextInPreOrder(const CounterNode* stayWithin) const
114{
115 if (CounterNode* next = m_firstChild)
116 return next;
117
118 return nextInPreOrderAfterChildren(stayWithin);
119}
120
121CounterNode* 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
133CounterNode* 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;
bdakin5d6edd42006-10-02 23:15:31 +0000143}
144
darinec375482007-01-06 01:36:24 +0000145int CounterNode::computeCountInParent() const
bdakin5d6edd42006-10-02 23:15:31 +0000146{
eric@webkit.orgd8ca5e12009-12-21 20:11:47 +0000147 int increment = actsAsReset() ? 0 : m_value;
darinec375482007-01-06 01:36:24 +0000148 if (m_previousSibling)
149 return m_previousSibling->m_countInParent + increment;
150 ASSERT(m_parent->m_firstChild == this);
151 return m_parent->m_value + increment;
bdakin5d6edd42006-10-02 23:15:31 +0000152}
153
carol.szabo@nokia.com7cf0a552011-03-23 00:04:58 +0000154void CounterNode::addRenderer(RenderCounter* value)
bdakin5d6edd42006-10-02 23:15:31 +0000155{
carol.szabo@nokia.com7cf0a552011-03-23 00:04:58 +0000156 if (!value) {
157 ASSERT_NOT_REACHED();
eric@webkit.org02482712009-11-13 20:21:44 +0000158 return;
carol.szabo@nokia.com7cf0a552011-03-23 00:04:58 +0000159 }
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 }
eric@webkit.org02482712009-11-13 20:21:44 +0000180}
181
carol.szabo@nokia.com7cf0a552011-03-23 00:04:58 +0000182void CounterNode::removeRenderer(RenderCounter* value)
eric@webkit.org02482712009-11-13 20:21:44 +0000183{
carol.szabo@nokia.com7cf0a552011-03-23 00:04:58 +0000184 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
208void CounterNode::resetRenderers()
209{
carol.szabo@nokia.comd7917f72011-03-24 02:25:06 +0000210 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.
carol.szabo@nokia.com7cf0a552011-03-23 00:04:58 +0000212}
213
214void CounterNode::resetThisAndDescendantsRenderers()
215{
216 CounterNode* node = this;
eric@webkit.org02482712009-11-13 20:21:44 +0000217 do {
carol.szabo@nokia.com7cf0a552011-03-23 00:04:58 +0000218 node->resetRenderers();
eric@webkit.org02482712009-11-13 20:21:44 +0000219 node = node->nextInPreOrder(this);
220 } while (node);
221}
222
carol.szabo@nokia.com7cf0a552011-03-23 00:04:58 +0000223void CounterNode::recount()
eric@webkit.org02482712009-11-13 20:21:44 +0000224{
225 for (CounterNode* node = this; node; node = node->m_nextSibling) {
226 int oldCount = node->m_countInParent;
227 int newCount = node->computeCountInParent();
darinec375482007-01-06 01:36:24 +0000228 if (oldCount == newCount)
229 break;
eric@webkit.org02482712009-11-13 20:21:44 +0000230 node->m_countInParent = newCount;
carol.szabo@nokia.com7cf0a552011-03-23 00:04:58 +0000231 node->resetThisAndDescendantsRenderers();
bdakin5d6edd42006-10-02 23:15:31 +0000232 }
233}
234
eric@webkit.org02482712009-11-13 20:21:44 +0000235void CounterNode::insertAfter(CounterNode* newChild, CounterNode* refChild, const AtomicString& identifier)
bdakin5d6edd42006-10-02 23:15:31 +0000236{
darinec375482007-01-06 01:36:24 +0000237 ASSERT(newChild);
238 ASSERT(!newChild->m_parent);
239 ASSERT(!newChild->m_previousSibling);
240 ASSERT(!newChild->m_nextSibling);
cdn@chromium.org009d1ab2011-09-13 22:49:15 +0000241 // 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;
darinec375482007-01-06 01:36:24 +0000245
eric@webkit.orgae29b2f2010-01-16 02:14:25 +0000246 if (newChild->m_hasResetType) {
247 while (m_lastChild != refChild)
carol.szabo@nokia.com7cf0a552011-03-23 00:04:58 +0000248 RenderCounter::destroyCounterNode(m_lastChild->owner(), identifier);
eric@webkit.orgae29b2f2010-01-16 02:14:25 +0000249 }
250
darinec375482007-01-06 01:36:24 +0000251 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
darinec375482007-01-06 01:36:24 +0000261 newChild->m_parent = this;
262 newChild->m_previousSibling = refChild;
darinec375482007-01-06 01:36:24 +0000263
cdn@chromium.org32296c32012-06-20 05:36:27 +0000264 if (next) {
265 ASSERT(next->m_previousSibling == refChild);
266 next->m_previousSibling = newChild;
eric@webkit.orgae29b2f2010-01-16 02:14:25 +0000267 newChild->m_nextSibling = next;
cdn@chromium.org32296c32012-06-20 05:36:27 +0000268 } else {
269 ASSERT(m_lastChild == refChild);
270 m_lastChild = newChild;
271 }
eric@webkit.orgae29b2f2010-01-16 02:14:25 +0000272
cdn@chromium.org32296c32012-06-20 05:36:27 +0000273 if (!newChild->m_firstChild || newChild->m_hasResetType) {
eric@webkit.orgae29b2f2010-01-16 02:14:25 +0000274 newChild->m_countInParent = newChild->computeCountInParent();
carol.szabo@nokia.com7cf0a552011-03-23 00:04:58 +0000275 newChild->resetThisAndDescendantsRenderers();
eric@webkit.orgae29b2f2010-01-16 02:14:25 +0000276 if (next)
carol.szabo@nokia.com7cf0a552011-03-23 00:04:58 +0000277 next->recount();
eric@webkit.orgae29b2f2010-01-16 02:14:25 +0000278 return;
279 }
280
281 // The code below handles the case when a formerly root increment counter is loosing its root position
282 // and therefore its children become next siblings.
283 CounterNode* last = newChild->m_lastChild;
284 CounterNode* first = newChild->m_firstChild;
285
cdn@chromium.org009d1ab2011-09-13 22:49:15 +0000286 if (first) {
287 ASSERT(last);
288 newChild->m_nextSibling = first;
289 if (m_lastChild == newChild)
290 m_lastChild = last;
291
292 first->m_previousSibling = newChild;
293
294 // The case when the original next sibling of the inserted node becomes a child of
295 // one of the former children of the inserted node is not handled as it is believed
296 // to be impossible since:
297 // 1. if the increment counter node lost it's root position as a result of another
298 // counter node being created, it will be inserted as the last child so next is null.
299 // 2. if the increment counter node lost it's root position as a result of a renderer being
300 // inserted into the document's render tree, all its former children counters are attached
301 // to children of the inserted renderer and hence cannot be in scope for counter nodes
302 // attached to renderers that were already in the document's render tree.
303 last->m_nextSibling = next;
304 if (next) {
305 ASSERT(next->m_previousSibling == newChild);
306 next->m_previousSibling = last;
307 } else
308 m_lastChild = last;
309 for (next = first; ; next = next->m_nextSibling) {
310 next->m_parent = this;
311 if (last == next)
312 break;
313 }
eric@webkit.orgae29b2f2010-01-16 02:14:25 +0000314 }
315 newChild->m_firstChild = 0;
316 newChild->m_lastChild = 0;
317 newChild->m_countInParent = newChild->computeCountInParent();
carol.szabo@nokia.com7cf0a552011-03-23 00:04:58 +0000318 newChild->resetRenderers();
319 first->recount();
bdakin5d6edd42006-10-02 23:15:31 +0000320}
321
carol.szabo@nokia.com7cf0a552011-03-23 00:04:58 +0000322void CounterNode::removeChild(CounterNode* oldChild)
bdakin5d6edd42006-10-02 23:15:31 +0000323{
darinec375482007-01-06 01:36:24 +0000324 ASSERT(oldChild);
325 ASSERT(!oldChild->m_firstChild);
326 ASSERT(!oldChild->m_lastChild);
327
328 CounterNode* next = oldChild->m_nextSibling;
eric@webkit.org02482712009-11-13 20:21:44 +0000329 CounterNode* previous = oldChild->m_previousSibling;
darinec375482007-01-06 01:36:24 +0000330
331 oldChild->m_nextSibling = 0;
332 oldChild->m_previousSibling = 0;
333 oldChild->m_parent = 0;
334
eric@webkit.org02482712009-11-13 20:21:44 +0000335 if (previous)
336 previous->m_nextSibling = next;
bdakin5d6edd42006-10-02 23:15:31 +0000337 else {
darinec375482007-01-06 01:36:24 +0000338 ASSERT(m_firstChild == oldChild);
339 m_firstChild = next;
bdakin5d6edd42006-10-02 23:15:31 +0000340 }
eric@webkit.org02482712009-11-13 20:21:44 +0000341
darinec375482007-01-06 01:36:24 +0000342 if (next)
eric@webkit.org02482712009-11-13 20:21:44 +0000343 next->m_previousSibling = previous;
darinec375482007-01-06 01:36:24 +0000344 else {
345 ASSERT(m_lastChild == oldChild);
eric@webkit.org02482712009-11-13 20:21:44 +0000346 m_lastChild = previous;
bdakin5d6edd42006-10-02 23:15:31 +0000347 }
eric@webkit.org02482712009-11-13 20:21:44 +0000348
darinec375482007-01-06 01:36:24 +0000349 if (next)
carol.szabo@nokia.com7cf0a552011-03-23 00:04:58 +0000350 next->recount();
bdakin5d6edd42006-10-02 23:15:31 +0000351}
352
darinec375482007-01-06 01:36:24 +0000353#ifndef NDEBUG
354
darinec375482007-01-06 01:36:24 +0000355static void showTreeAndMark(const CounterNode* node)
bdakin5d6edd42006-10-02 23:15:31 +0000356{
darinec375482007-01-06 01:36:24 +0000357 const CounterNode* root = node;
358 while (root->parent())
359 root = root->parent();
360
eric@webkit.orgcb918422009-11-13 20:55:51 +0000361 for (const CounterNode* current = root; current; current = current->nextInPreOrder()) {
jschuh@chromium.orgb375c982011-01-12 16:07:53 +0000362 fprintf(stderr, "%c", (current == node) ? '*' : ' ');
eric@webkit.orga0062952009-11-09 19:43:17 +0000363 for (const CounterNode* parent = current; parent && parent != root; parent = parent->parent())
jschuh@chromium.orgb375c982011-01-12 16:07:53 +0000364 fprintf(stderr, " ");
eric@webkit.orga0062952009-11-09 19:43:17 +0000365 fprintf(stderr, "%p %s: %d %d P:%p PS:%p NS:%p R:%p\n",
eric@webkit.orgd8ca5e12009-12-21 20:11:47 +0000366 current, current->actsAsReset() ? "reset____" : "increment", current->value(),
eric@webkit.orga0062952009-11-09 19:43:17 +0000367 current->countInParent(), current->parent(), current->previousSibling(),
carol.szabo@nokia.com7cf0a552011-03-23 00:04:58 +0000368 current->nextSibling(), current->owner());
darinec375482007-01-06 01:36:24 +0000369 }
carol.szabo@nokia.com7cf0a552011-03-23 00:04:58 +0000370 fflush(stderr);
bdakin5d6edd42006-10-02 23:15:31 +0000371}
372
darinec375482007-01-06 01:36:24 +0000373#endif
374
weinig1497a7e2006-10-26 20:23:55 +0000375} // namespace WebCore
darinec375482007-01-06 01:36:24 +0000376
377#ifndef NDEBUG
378
carol.szabo@nokia.com02635bf2011-02-05 00:28:57 +0000379void showCounterTree(const WebCore::CounterNode* counter)
darinec375482007-01-06 01:36:24 +0000380{
381 if (counter)
382 showTreeAndMark(counter);
383}
384
385#endif