1/**
2 * Copyright (C) 2004 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 "core/rendering/RenderCounter.h"
24
25#include "HTMLNames.h"
26#include "core/dom/Document.h"
27#include "core/dom/Element.h"
28#include "core/dom/NodeTraversal.h"
29#include "core/html/HTMLOListElement.h"
30#include "core/rendering/CounterNode.h"
31#include "core/rendering/RenderListItem.h"
32#include "core/rendering/RenderListMarker.h"
33#include "core/rendering/RenderView.h"
34#include "core/rendering/style/RenderStyle.h"
35#include "wtf/StdLibExtras.h"
36
37#ifndef NDEBUG
38#include <stdio.h>
39#endif
40
41namespace WebCore {
42
43using namespace HTMLNames;
44
45typedef HashMap<AtomicString, RefPtr<CounterNode> > CounterMap;
46typedef HashMap<const RenderObject*, OwnPtr<CounterMap> > CounterMaps;
47
48static CounterNode* makeCounterNode(RenderObject*, const AtomicString& identifier, bool alwaysCreateCounter);
49
50static CounterMaps& counterMaps()
51{
52    DEFINE_STATIC_LOCAL(CounterMaps, staticCounterMaps, ());
53    return staticCounterMaps;
54}
55
56// This function processes the renderer tree in the order of the DOM tree
57// including pseudo elements as defined in CSS 2.1.
58static RenderObject* previousInPreOrder(const RenderObject* object)
59{
60    Element* self = toElement(object->node());
61    Element* previous = ElementTraversal::previousIncludingPseudo(self);
62    while (previous && !previous->renderer())
63        previous = ElementTraversal::previousIncludingPseudo(previous);
64    return previous ? previous->renderer() : 0;
65}
66
67// This function processes the renderer tree in the order of the DOM tree
68// including pseudo elements as defined in CSS 2.1.
69static RenderObject* previousSiblingOrParent(const RenderObject* object)
70{
71    Element* self = toElement(object->node());
72    Element* previous = ElementTraversal::pseudoAwarePreviousSibling(self);
73    while (previous && !previous->renderer())
74        previous = ElementTraversal::pseudoAwarePreviousSibling(previous);
75    if (previous)
76        return previous->renderer();
77    previous = self->parentElement();
78    return previous ? previous->renderer() : 0;
79}
80
81static inline Element* parentElement(RenderObject* object)
82{
83    return toElement(object->node())->parentElement();
84}
85
86static inline bool areRenderersElementsSiblings(RenderObject* first, RenderObject* second)
87{
88    return parentElement(first) == parentElement(second);
89}
90
91// This function processes the renderer tree in the order of the DOM tree
92// including pseudo elements as defined in CSS 2.1.
93static RenderObject* nextInPreOrder(const RenderObject* object, const Element* stayWithin, bool skipDescendants = false)
94{
95    Element* self = toElement(object->node());
96    Element* next = skipDescendants ? ElementTraversal::nextIncludingPseudoSkippingChildren(self, stayWithin) : ElementTraversal::nextIncludingPseudo(self, stayWithin);
97    while (next && !next->renderer())
98        next = skipDescendants ? ElementTraversal::nextIncludingPseudoSkippingChildren(next, stayWithin) : ElementTraversal::nextIncludingPseudo(next, stayWithin);
99    return next ? next->renderer() : 0;
100}
101
102static bool planCounter(RenderObject* object, const AtomicString& identifier, bool& isReset, int& value)
103{
104    ASSERT(object);
105
106    // Real text nodes don't have their own style so they can't have counters.
107    // We can't even look at their styles or we'll see extra resets and increments!
108    if (object->isText() && !object->isBR())
109        return false;
110    Node* generatingNode = object->generatingNode();
111    // We must have a generating node or else we cannot have a counter.
112    if (!generatingNode)
113        return false;
114    RenderStyle* style = object->style();
115    ASSERT(style);
116
117    switch (style->styleType()) {
118    case NOPSEUDO:
119        // Sometimes nodes have more then one renderer. Only the first one gets the counter
120        // LayoutTests/http/tests/css/counter-crash.html
121        if (generatingNode->renderer() != object)
122            return false;
123        break;
124    case BEFORE:
125    case AFTER:
126        break;
127    default:
128        return false; // Counters are forbidden from all other pseudo elements.
129    }
130
131    const CounterDirectives directives = style->getCounterDirectives(identifier);
132    if (directives.isDefined()) {
133        value = directives.combinedValue();
134        isReset = directives.isReset();
135        return true;
136    }
137
138    if (identifier == "list-item") {
139        if (object->isListItem()) {
140            if (toRenderListItem(object)->hasExplicitValue()) {
141                value = toRenderListItem(object)->explicitValue();
142                isReset = true;
143                return true;
144            }
145            value = 1;
146            isReset = false;
147            return true;
148        }
149        if (Node* e = object->node()) {
150            if (e->hasTagName(olTag)) {
151                value = static_cast<HTMLOListElement*>(e)->start();
152                isReset = true;
153                return true;
154            }
155            if (e->hasTagName(ulTag) || e->hasTagName(menuTag) || e->hasTagName(dirTag)) {
156                value = 0;
157                isReset = true;
158                return true;
159            }
160        }
161    }
162
163    return false;
164}
165
166// - Finds the insertion point for the counter described by counterOwner, isReset and
167// identifier in the CounterNode tree for identifier and sets parent and
168// previousSibling accordingly.
169// - The function returns true if the counter whose insertion point is searched is NOT
170// the root of the tree.
171// - The root of the tree is a counter reference that is not in the scope of any other
172// counter with the same identifier.
173// - All the counter references with the same identifier as this one that are in
174// children or subsequent siblings of the renderer that owns the root of the tree
175// form the rest of of the nodes of the tree.
176// - The root of the tree is always a reset type reference.
177// - A subtree rooted at any reset node in the tree is equivalent to all counter
178// references that are in the scope of the counter or nested counter defined by that
179// reset node.
180// - Non-reset CounterNodes cannot have descendants.
181
182static bool findPlaceForCounter(RenderObject* counterOwner, const AtomicString& identifier, bool isReset, RefPtr<CounterNode>& parent, RefPtr<CounterNode>& previousSibling)
183{
184    // We cannot stop searching for counters with the same identifier before we also
185    // check this renderer, because it may affect the positioning in the tree of our counter.
186    RenderObject* searchEndRenderer = previousSiblingOrParent(counterOwner);
187    // We check renderers in preOrder from the renderer that our counter is attached to
188    // towards the begining of the document for counters with the same identifier as the one
189    // we are trying to find a place for. This is the next renderer to be checked.
190    RenderObject* currentRenderer = previousInPreOrder(counterOwner);
191    previousSibling = 0;
192    RefPtr<CounterNode> previousSiblingProtector = 0;
193
194    while (currentRenderer) {
195        CounterNode* currentCounter = makeCounterNode(currentRenderer, identifier, false);
196        if (searchEndRenderer == currentRenderer) {
197            // We may be at the end of our search.
198            if (currentCounter) {
199                // We have a suitable counter on the EndSearchRenderer.
200                if (previousSiblingProtector) { // But we already found another counter that we come after.
201                    if (currentCounter->actsAsReset()) {
202                        // We found a reset counter that is on a renderer that is a sibling of ours or a parent.
203                        if (isReset && areRenderersElementsSiblings(currentRenderer, counterOwner)) {
204                            // We are also a reset counter and the previous reset was on a sibling renderer
205                            // hence we are the next sibling of that counter if that reset is not a root or
206                            // we are a root node if that reset is a root.
207                            parent = currentCounter->parent();
208                            previousSibling = parent ? currentCounter : 0;
209                            return parent;
210                        }
211                        // We are not a reset node or the previous reset must be on an ancestor of our owner renderer
212                        // hence we must be a child of that reset counter.
213                        parent = currentCounter;
214                        // In some cases renders can be reparented (ex. nodes inside a table but not in a column or row).
215                        // In these cases the identified previousSibling will be invalid as its parent is different from
216                        // our identified parent.
217                        if (previousSiblingProtector->parent() != currentCounter)
218                            previousSiblingProtector = 0;
219
220                        previousSibling = previousSiblingProtector.get();
221                        return true;
222                    }
223                    // CurrentCounter, the counter at the EndSearchRenderer, is not reset.
224                    if (!isReset || !areRenderersElementsSiblings(currentRenderer, counterOwner)) {
225                        // If the node we are placing is not reset or we have found a counter that is attached
226                        // to an ancestor of the placed counter's owner renderer we know we are a sibling of that node.
227                        if (currentCounter->parent() != previousSiblingProtector->parent())
228                            return false;
229
230                        parent = currentCounter->parent();
231                        previousSibling = previousSiblingProtector.get();
232                        return true;
233                    }
234                } else {
235                    // We are at the potential end of the search, but we had no previous sibling candidate
236                    // In this case we follow pretty much the same logic as above but no ASSERTs about
237                    // previousSibling, and when we are a sibling of the end counter we must set previousSibling
238                    // to currentCounter.
239                    if (currentCounter->actsAsReset()) {
240                        if (isReset && areRenderersElementsSiblings(currentRenderer, counterOwner)) {
241                            parent = currentCounter->parent();
242                            previousSibling = currentCounter;
243                            return parent;
244                        }
245                        parent = currentCounter;
246                        previousSibling = previousSiblingProtector.get();
247                        return true;
248                    }
249                    if (!isReset || !areRenderersElementsSiblings(currentRenderer, counterOwner)) {
250                        parent = currentCounter->parent();
251                        previousSibling = currentCounter;
252                        return true;
253                    }
254                    previousSiblingProtector = currentCounter;
255                }
256            }
257            // We come here if the previous sibling or parent of our owner renderer had no
258            // good counter, or we are a reset node and the counter on the previous sibling
259            // of our owner renderer was not a reset counter.
260            // Set a new goal for the end of the search.
261            searchEndRenderer = previousSiblingOrParent(currentRenderer);
262        } else {
263            // We are searching descendants of a previous sibling of the renderer that the
264            // counter being placed is attached to.
265            if (currentCounter) {
266                // We found a suitable counter.
267                if (previousSiblingProtector) {
268                    // Since we had a suitable previous counter before, we should only consider this one as our
269                    // previousSibling if it is a reset counter and hence the current previousSibling is its child.
270                    if (currentCounter->actsAsReset()) {
271                        previousSiblingProtector = currentCounter;
272                        // We are no longer interested in previous siblings of the currentRenderer or their children
273                        // as counters they may have attached cannot be the previous sibling of the counter we are placing.
274                        currentRenderer = parentElement(currentRenderer)->renderer();
275                        continue;
276                    }
277                } else
278                    previousSiblingProtector = currentCounter;
279                currentRenderer = previousSiblingOrParent(currentRenderer);
280                continue;
281            }
282        }
283        // This function is designed so that the same test is not done twice in an iteration, except for this one
284        // which may be done twice in some cases. Rearranging the decision points though, to accommodate this
285        // performance improvement would create more code duplication than is worthwhile in my oppinion and may further
286        // impede the readability of this already complex algorithm.
287        if (previousSiblingProtector)
288            currentRenderer = previousSiblingOrParent(currentRenderer);
289        else
290            currentRenderer = previousInPreOrder(currentRenderer);
291    }
292    return false;
293}
294
295static CounterNode* makeCounterNode(RenderObject* object, const AtomicString& identifier, bool alwaysCreateCounter)
296{
297    ASSERT(object);
298
299    if (object->hasCounterNodeMap()) {
300        if (CounterMap* nodeMap = counterMaps().get(object)) {
301            if (CounterNode* node = nodeMap->get(identifier))
302                return node;
303        }
304    }
305
306    bool isReset = false;
307    int value = 0;
308    if (!planCounter(object, identifier, isReset, value) && !alwaysCreateCounter)
309        return 0;
310
311    RefPtr<CounterNode> newParent = 0;
312    RefPtr<CounterNode> newPreviousSibling = 0;
313    RefPtr<CounterNode> newNode = CounterNode::create(object, isReset, value);
314    if (findPlaceForCounter(object, identifier, isReset, newParent, newPreviousSibling))
315        newParent->insertAfter(newNode.get(), newPreviousSibling.get(), identifier);
316    CounterMap* nodeMap;
317    if (object->hasCounterNodeMap())
318        nodeMap = counterMaps().get(object);
319    else {
320        nodeMap = new CounterMap;
321        counterMaps().set(object, adoptPtr(nodeMap));
322        object->setHasCounterNodeMap(true);
323    }
324    nodeMap->set(identifier, newNode);
325    if (newNode->parent())
326        return newNode.get();
327    // Checking if some nodes that were previously counter tree root nodes
328    // should become children of this node now.
329    CounterMaps& maps = counterMaps();
330    Element* stayWithin = parentElement(object);
331    bool skipDescendants;
332    for (RenderObject* currentRenderer = nextInPreOrder(object, stayWithin); currentRenderer; currentRenderer = nextInPreOrder(currentRenderer, stayWithin, skipDescendants)) {
333        skipDescendants = false;
334        if (!currentRenderer->hasCounterNodeMap())
335            continue;
336        CounterNode* currentCounter = maps.get(currentRenderer)->get(identifier);
337        if (!currentCounter)
338            continue;
339        skipDescendants = true;
340        if (currentCounter->parent())
341            continue;
342        if (stayWithin == parentElement(currentRenderer) && currentCounter->hasResetType())
343            break;
344        newNode->insertAfter(currentCounter, newNode->lastChild(), identifier);
345    }
346    return newNode.get();
347}
348
349RenderCounter::RenderCounter(Document* node, const CounterContent& counter)
350    : RenderText(node, StringImpl::empty())
351    , m_counter(counter)
352    , m_counterNode(0)
353    , m_nextForSameCounter(0)
354{
355    view()->addRenderCounter();
356}
357
358RenderCounter::~RenderCounter()
359{
360    if (m_counterNode) {
361        m_counterNode->removeRenderer(this);
362        ASSERT(!m_counterNode);
363    }
364}
365
366void RenderCounter::willBeDestroyed()
367{
368    if (view())
369        view()->removeRenderCounter();
370    RenderText::willBeDestroyed();
371}
372
373const char* RenderCounter::renderName() const
374{
375    return "RenderCounter";
376}
377
378bool RenderCounter::isCounter() const
379{
380    return true;
381}
382
383PassRefPtr<StringImpl> RenderCounter::originalText() const
384{
385    if (!m_counterNode) {
386        RenderObject* beforeAfterContainer = parent();
387        while (true) {
388            if (!beforeAfterContainer)
389                return 0;
390            if (!beforeAfterContainer->isAnonymous() && !beforeAfterContainer->isPseudoElement())
391                return 0; // RenderCounters are restricted to before and after pseudo elements
392            PseudoId containerStyle = beforeAfterContainer->style()->styleType();
393            if ((containerStyle == BEFORE) || (containerStyle == AFTER))
394                break;
395            beforeAfterContainer = beforeAfterContainer->parent();
396        }
397        makeCounterNode(beforeAfterContainer, m_counter.identifier(), true)->addRenderer(const_cast<RenderCounter*>(this));
398        ASSERT(m_counterNode);
399    }
400    CounterNode* child = m_counterNode;
401    int value = child->actsAsReset() ? child->value() : child->countInParent();
402
403    String text = listMarkerText(m_counter.listStyle(), value);
404
405    if (!m_counter.separator().isNull()) {
406        if (!child->actsAsReset())
407            child = child->parent();
408        while (CounterNode* parent = child->parent()) {
409            text = listMarkerText(m_counter.listStyle(), child->countInParent())
410                + m_counter.separator() + text;
411            child = parent;
412        }
413    }
414
415    return text.impl();
416}
417
418void RenderCounter::updateCounter()
419{
420    computePreferredLogicalWidths(0);
421}
422
423void RenderCounter::computePreferredLogicalWidths(float lead)
424{
425#ifndef NDEBUG
426    // FIXME: We shouldn't be modifying the tree in computePreferredLogicalWidths.
427    // Instead, we should properly hook the appropriate changes in the DOM and modify
428    // the render tree then. When that's done, we also won't need to override
429    // computePreferredLogicalWidths at all.
430    // https://bugs.webkit.org/show_bug.cgi?id=104829
431    SetLayoutNeededForbiddenScope layoutForbiddenScope(this, false);
432#endif
433
434    setTextInternal(originalText());
435
436    RenderText::computePreferredLogicalWidths(lead);
437}
438
439void RenderCounter::invalidate()
440{
441    m_counterNode->removeRenderer(this);
442    ASSERT(!m_counterNode);
443    if (documentBeingDestroyed())
444        return;
445    setNeedsLayoutAndPrefWidthsRecalc();
446}
447
448static void destroyCounterNodeWithoutMapRemoval(const AtomicString& identifier, CounterNode* node)
449{
450    CounterNode* previous;
451    for (RefPtr<CounterNode> child = node->lastDescendant(); child && child != node; child = previous) {
452        previous = child->previousInPreOrder();
453        child->parent()->removeChild(child.get());
454        ASSERT(counterMaps().get(child->owner())->get(identifier) == child);
455        counterMaps().get(child->owner())->remove(identifier);
456    }
457    if (CounterNode* parent = node->parent())
458        parent->removeChild(node);
459}
460
461void RenderCounter::destroyCounterNodes(RenderObject* owner)
462{
463    CounterMaps& maps = counterMaps();
464    CounterMaps::iterator mapsIterator = maps.find(owner);
465    if (mapsIterator == maps.end())
466        return;
467    CounterMap* map = mapsIterator->value.get();
468    CounterMap::const_iterator end = map->end();
469    for (CounterMap::const_iterator it = map->begin(); it != end; ++it) {
470        destroyCounterNodeWithoutMapRemoval(it->key, it->value.get());
471    }
472    maps.remove(mapsIterator);
473    owner->setHasCounterNodeMap(false);
474}
475
476void RenderCounter::destroyCounterNode(RenderObject* owner, const AtomicString& identifier)
477{
478    CounterMap* map = counterMaps().get(owner);
479    if (!map)
480        return;
481    CounterMap::iterator mapIterator = map->find(identifier);
482    if (mapIterator == map->end())
483        return;
484    destroyCounterNodeWithoutMapRemoval(identifier, mapIterator->value.get());
485    map->remove(mapIterator);
486    // We do not delete "map" here even if empty because we expect to reuse
487    // it soon. In order for a renderer to lose all its counters permanently,
488    // a style change for the renderer involving removal of all counter
489    // directives must occur, in which case, RenderCounter::destroyCounterNodes()
490    // must be called.
491    // The destruction of the Renderer (possibly caused by the removal of its
492    // associated DOM node) is the other case that leads to the permanent
493    // destruction of all counters attached to a Renderer. In this case
494    // RenderCounter::destroyCounterNodes() must be and is now called, too.
495    // RenderCounter::destroyCounterNodes() handles destruction of the counter
496    // map associated with a renderer, so there is no risk in leaking the map.
497}
498
499void RenderCounter::rendererRemovedFromTree(RenderObject* renderer)
500{
501    ASSERT(renderer->view());
502    if (!renderer->view()->hasRenderCounters())
503        return;
504    RenderObject* currentRenderer = renderer->lastLeafChild();
505    if (!currentRenderer)
506        currentRenderer = renderer;
507    while (true) {
508        destroyCounterNodes(currentRenderer);
509        if (currentRenderer == renderer)
510            break;
511        currentRenderer = currentRenderer->previousInPreOrder();
512    }
513}
514
515static void updateCounters(RenderObject* renderer)
516{
517    ASSERT(renderer->style());
518    const CounterDirectiveMap* directiveMap = renderer->style()->counterDirectives();
519    if (!directiveMap)
520        return;
521    CounterDirectiveMap::const_iterator end = directiveMap->end();
522    if (!renderer->hasCounterNodeMap()) {
523        for (CounterDirectiveMap::const_iterator it = directiveMap->begin(); it != end; ++it)
524            makeCounterNode(renderer, it->key, false);
525        return;
526    }
527    CounterMap* counterMap = counterMaps().get(renderer);
528    ASSERT(counterMap);
529    for (CounterDirectiveMap::const_iterator it = directiveMap->begin(); it != end; ++it) {
530        RefPtr<CounterNode> node = counterMap->get(it->key);
531        if (!node) {
532            makeCounterNode(renderer, it->key, false);
533            continue;
534        }
535        RefPtr<CounterNode> newParent = 0;
536        RefPtr<CounterNode> newPreviousSibling = 0;
537
538        findPlaceForCounter(renderer, it->key, node->hasResetType(), newParent, newPreviousSibling);
539        if (node != counterMap->get(it->key))
540            continue;
541        CounterNode* parent = node->parent();
542        if (newParent == parent && newPreviousSibling == node->previousSibling())
543            continue;
544        if (parent)
545            parent->removeChild(node.get());
546        if (newParent)
547            newParent->insertAfter(node.get(), newPreviousSibling.get(), it->key);
548    }
549}
550
551void RenderCounter::rendererSubtreeAttached(RenderObject* renderer)
552{
553    ASSERT(renderer->view());
554    if (!renderer->view()->hasRenderCounters())
555        return;
556    Node* node = renderer->node();
557    if (node)
558        node = node->parentNode();
559    else
560        node = renderer->generatingNode();
561    if (node && !node->attached())
562        return; // No need to update if the parent is not attached yet
563    for (RenderObject* descendant = renderer; descendant; descendant = descendant->nextInPreOrder(renderer))
564        updateCounters(descendant);
565}
566
567void RenderCounter::rendererStyleChanged(RenderObject* renderer, const RenderStyle* oldStyle, const RenderStyle* newStyle)
568{
569    Node* node = renderer->generatingNode();
570    if (!node || !node->attached())
571        return; // cannot have generated content or if it can have, it will be handled during attaching
572    const CounterDirectiveMap* newCounterDirectives;
573    const CounterDirectiveMap* oldCounterDirectives;
574    if (oldStyle && (oldCounterDirectives = oldStyle->counterDirectives())) {
575        if (newStyle && (newCounterDirectives = newStyle->counterDirectives())) {
576            CounterDirectiveMap::const_iterator newMapEnd = newCounterDirectives->end();
577            CounterDirectiveMap::const_iterator oldMapEnd = oldCounterDirectives->end();
578            for (CounterDirectiveMap::const_iterator it = newCounterDirectives->begin(); it != newMapEnd; ++it) {
579                CounterDirectiveMap::const_iterator oldMapIt = oldCounterDirectives->find(it->key);
580                if (oldMapIt != oldMapEnd) {
581                    if (oldMapIt->value == it->value)
582                        continue;
583                    RenderCounter::destroyCounterNode(renderer, it->key);
584                }
585                // We must create this node here, because the changed node may be a node with no display such as
586                // as those created by the increment or reset directives and the re-layout that will happen will
587                // not catch the change if the node had no children.
588                makeCounterNode(renderer, it->key, false);
589            }
590            // Destroying old counters that do not exist in the new counterDirective map.
591            for (CounterDirectiveMap::const_iterator it = oldCounterDirectives->begin(); it !=oldMapEnd; ++it) {
592                if (!newCounterDirectives->contains(it->key))
593                    RenderCounter::destroyCounterNode(renderer, it->key);
594            }
595        } else {
596            if (renderer->hasCounterNodeMap())
597                RenderCounter::destroyCounterNodes(renderer);
598        }
599    } else if (newStyle && (newCounterDirectives = newStyle->counterDirectives())) {
600        CounterDirectiveMap::const_iterator newMapEnd = newCounterDirectives->end();
601        for (CounterDirectiveMap::const_iterator it = newCounterDirectives->begin(); it != newMapEnd; ++it) {
602            // We must create this node here, because the added node may be a node with no display such as
603            // as those created by the increment or reset directives and the re-layout that will happen will
604            // not catch the change if the node had no children.
605            makeCounterNode(renderer, it->key, false);
606        }
607    }
608}
609
610} // namespace WebCore
611
612#ifndef NDEBUG
613
614void showCounterRendererTree(const WebCore::RenderObject* renderer, const char* counterName)
615{
616    if (!renderer)
617        return;
618    const WebCore::RenderObject* root = renderer;
619    while (root->parent())
620        root = root->parent();
621
622    AtomicString identifier(counterName);
623    for (const WebCore::RenderObject* current = root; current; current = current->nextInPreOrder()) {
624        fprintf(stderr, "%c", (current == renderer) ? '*' : ' ');
625        for (const WebCore::RenderObject* parent = current; parent && parent != root; parent = parent->parent())
626            fprintf(stderr, "    ");
627        fprintf(stderr, "%p N:%p P:%p PS:%p NS:%p C:%p\n",
628            current, current->node(), current->parent(), current->previousSibling(),
629            current->nextSibling(), current->hasCounterNodeMap() ?
630            counterName ? WebCore::counterMaps().get(current)->get(identifier) : (WebCore::CounterNode*)1 : (WebCore::CounterNode*)0);
631    }
632    fflush(stderr);
633}
634
635#endif // NDEBUG
636