Home | History | Annotate | Download | only in rendering
      1 /**
      2  * Copyright (C) 2004 Allan Sandfeld Jensen (kde (at) 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 "core/HTMLNames.h"
     26 #include "core/dom/Element.h"
     27 #include "core/dom/ElementTraversal.h"
     28 #include "core/html/HTMLOListElement.h"
     29 #include "core/rendering/CounterNode.h"
     30 #include "core/rendering/RenderListItem.h"
     31 #include "core/rendering/RenderListMarker.h"
     32 #include "core/rendering/RenderView.h"
     33 #include "core/rendering/style/RenderStyle.h"
     34 #include "wtf/StdLibExtras.h"
     35 
     36 #ifndef NDEBUG
     37 #include <stdio.h>
     38 #endif
     39 
     40 namespace blink {
     41 
     42 using namespace HTMLNames;
     43 
     44 typedef HashMap<AtomicString, RefPtr<CounterNode> > CounterMap;
     45 typedef HashMap<const RenderObject*, OwnPtr<CounterMap> > CounterMaps;
     46 
     47 static CounterNode* makeCounterNode(RenderObject&, const AtomicString& identifier, bool alwaysCreateCounter);
     48 
     49 static CounterMaps& counterMaps()
     50 {
     51     DEFINE_STATIC_LOCAL(CounterMaps, staticCounterMaps, ());
     52     return staticCounterMaps;
     53 }
     54 
     55 // This function processes the renderer tree in the order of the DOM tree
     56 // including pseudo elements as defined in CSS 2.1.
     57 static RenderObject* previousInPreOrder(const RenderObject& object)
     58 {
     59     Element* self = toElement(object.node());
     60     ASSERT(self);
     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.
     69 static RenderObject* previousSiblingOrParent(const RenderObject& object)
     70 {
     71     Element* self = toElement(object.node());
     72     ASSERT(self);
     73     Element* previous = ElementTraversal::pseudoAwarePreviousSibling(*self);
     74     while (previous && !previous->renderer())
     75         previous = ElementTraversal::pseudoAwarePreviousSibling(*previous);
     76     if (previous)
     77         return previous->renderer();
     78     previous = self->parentElement();
     79     return previous ? previous->renderer() : 0;
     80 }
     81 
     82 static inline Element* parentElement(RenderObject& object)
     83 {
     84     return toElement(object.node())->parentElement();
     85 }
     86 
     87 static inline bool areRenderersElementsSiblings(RenderObject& first, RenderObject& second)
     88 {
     89     return parentElement(first) == parentElement(second);
     90 }
     91 
     92 // This function processes the renderer tree in the order of the DOM tree
     93 // including pseudo elements as defined in CSS 2.1.
     94 static RenderObject* nextInPreOrder(const RenderObject& object, const Element* stayWithin, bool skipDescendants = false)
     95 {
     96     Element* self = toElement(object.node());
     97     ASSERT(self);
     98     Element* next = skipDescendants ? ElementTraversal::nextIncludingPseudoSkippingChildren(*self, stayWithin) : ElementTraversal::nextIncludingPseudo(*self, stayWithin);
     99     while (next && !next->renderer())
    100         next = skipDescendants ? ElementTraversal::nextIncludingPseudoSkippingChildren(*next, stayWithin) : ElementTraversal::nextIncludingPseudo(*next, stayWithin);
    101     return next ? next->renderer() : 0;
    102 }
    103 
    104 static bool planCounter(RenderObject& object, const AtomicString& identifier, bool& isReset, int& value)
    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 (isHTMLOListElement(*e)) {
    151                 value = toHTMLOListElement(e)->start();
    152                 isReset = true;
    153                 return true;
    154             }
    155             if (isHTMLUListElement(*e) || isHTMLMenuElement(*e) || isHTMLDirectoryElement(*e)) {
    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 
    182 static 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 = nullptr;
    192     RefPtr<CounterNode> previousSiblingProtector = nullptr;
    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 = nullptr;
    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 
    295 static CounterNode* makeCounterNode(RenderObject& object, const AtomicString& identifier, bool alwaysCreateCounter)
    296 {
    297     if (object.hasCounterNodeMap()) {
    298         if (CounterMap* nodeMap = counterMaps().get(&object)) {
    299             if (CounterNode* node = nodeMap->get(identifier))
    300                 return node;
    301         }
    302     }
    303 
    304     bool isReset = false;
    305     int value = 0;
    306     if (!planCounter(object, identifier, isReset, value) && !alwaysCreateCounter)
    307         return 0;
    308 
    309     RefPtr<CounterNode> newParent = nullptr;
    310     RefPtr<CounterNode> newPreviousSibling = nullptr;
    311     RefPtr<CounterNode> newNode = CounterNode::create(object, isReset, value);
    312     if (findPlaceForCounter(object, identifier, isReset, newParent, newPreviousSibling))
    313         newParent->insertAfter(newNode.get(), newPreviousSibling.get(), identifier);
    314     CounterMap* nodeMap;
    315     if (object.hasCounterNodeMap())
    316         nodeMap = counterMaps().get(&object);
    317     else {
    318         nodeMap = new CounterMap;
    319         counterMaps().set(&object, adoptPtr(nodeMap));
    320         object.setHasCounterNodeMap(true);
    321     }
    322     nodeMap->set(identifier, newNode);
    323     if (newNode->parent())
    324         return newNode.get();
    325     // Checking if some nodes that were previously counter tree root nodes
    326     // should become children of this node now.
    327     CounterMaps& maps = counterMaps();
    328     Element* stayWithin = parentElement(object);
    329     bool skipDescendants;
    330     for (RenderObject* currentRenderer = nextInPreOrder(object, stayWithin); currentRenderer; currentRenderer = nextInPreOrder(*currentRenderer, stayWithin, skipDescendants)) {
    331         skipDescendants = false;
    332         if (!currentRenderer->hasCounterNodeMap())
    333             continue;
    334         CounterNode* currentCounter = maps.get(currentRenderer)->get(identifier);
    335         if (!currentCounter)
    336             continue;
    337         skipDescendants = true;
    338         if (currentCounter->parent())
    339             continue;
    340         if (stayWithin == parentElement(*currentRenderer) && currentCounter->hasResetType())
    341             break;
    342         newNode->insertAfter(currentCounter, newNode->lastChild(), identifier);
    343     }
    344     return newNode.get();
    345 }
    346 
    347 RenderCounter::RenderCounter(Document* node, const CounterContent& counter)
    348     : RenderText(node, StringImpl::empty())
    349     , m_counter(counter)
    350     , m_counterNode(0)
    351     , m_nextForSameCounter(0)
    352 {
    353     view()->addRenderCounter();
    354 }
    355 
    356 RenderCounter::~RenderCounter()
    357 {
    358 }
    359 
    360 void RenderCounter::destroy()
    361 {
    362     if (m_counterNode) {
    363         m_counterNode->removeRenderer(this);
    364         ASSERT(!m_counterNode);
    365     }
    366     RenderText::destroy();
    367 }
    368 
    369 void RenderCounter::willBeDestroyed()
    370 {
    371     if (view())
    372         view()->removeRenderCounter();
    373     RenderText::willBeDestroyed();
    374 }
    375 
    376 const char* RenderCounter::renderName() const
    377 {
    378     return "RenderCounter";
    379 }
    380 
    381 bool RenderCounter::isCounter() const
    382 {
    383     return true;
    384 }
    385 
    386 PassRefPtr<StringImpl> RenderCounter::originalText() const
    387 {
    388     if (!m_counterNode) {
    389         RenderObject* beforeAfterContainer = parent();
    390         while (true) {
    391             if (!beforeAfterContainer)
    392                 return nullptr;
    393             if (!beforeAfterContainer->isAnonymous() && !beforeAfterContainer->isPseudoElement())
    394                 return nullptr; // RenderCounters are restricted to before and after pseudo elements
    395             PseudoId containerStyle = beforeAfterContainer->style()->styleType();
    396             if ((containerStyle == BEFORE) || (containerStyle == AFTER))
    397                 break;
    398             beforeAfterContainer = beforeAfterContainer->parent();
    399         }
    400         makeCounterNode(*beforeAfterContainer, m_counter.identifier(), true)->addRenderer(const_cast<RenderCounter*>(this));
    401         ASSERT(m_counterNode);
    402     }
    403     CounterNode* child = m_counterNode;
    404     int value = child->actsAsReset() ? child->value() : child->countInParent();
    405 
    406     String text = listMarkerText(m_counter.listStyle(), value);
    407 
    408     if (!m_counter.separator().isNull()) {
    409         if (!child->actsAsReset())
    410             child = child->parent();
    411         while (CounterNode* parent = child->parent()) {
    412             text = listMarkerText(m_counter.listStyle(), child->countInParent())
    413                 + m_counter.separator() + text;
    414             child = parent;
    415         }
    416     }
    417 
    418     return text.impl();
    419 }
    420 
    421 void RenderCounter::updateCounter()
    422 {
    423     setTextInternal(originalText());
    424 }
    425 
    426 void RenderCounter::invalidate()
    427 {
    428     m_counterNode->removeRenderer(this);
    429     ASSERT(!m_counterNode);
    430     if (documentBeingDestroyed())
    431         return;
    432     setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
    433 }
    434 
    435 static void destroyCounterNodeWithoutMapRemoval(const AtomicString& identifier, CounterNode* node)
    436 {
    437     CounterNode* previous;
    438     for (RefPtr<CounterNode> child = node->lastDescendant(); child && child != node; child = previous) {
    439         previous = child->previousInPreOrder();
    440         child->parent()->removeChild(child.get());
    441         ASSERT(counterMaps().get(&child->owner())->get(identifier) == child);
    442         counterMaps().get(&child->owner())->remove(identifier);
    443     }
    444     if (CounterNode* parent = node->parent())
    445         parent->removeChild(node);
    446 }
    447 
    448 void RenderCounter::destroyCounterNodes(RenderObject& owner)
    449 {
    450     CounterMaps& maps = counterMaps();
    451     CounterMaps::iterator mapsIterator = maps.find(&owner);
    452     if (mapsIterator == maps.end())
    453         return;
    454     CounterMap* map = mapsIterator->value.get();
    455     CounterMap::const_iterator end = map->end();
    456     for (CounterMap::const_iterator it = map->begin(); it != end; ++it) {
    457         destroyCounterNodeWithoutMapRemoval(it->key, it->value.get());
    458     }
    459     maps.remove(mapsIterator);
    460     owner.setHasCounterNodeMap(false);
    461 }
    462 
    463 void RenderCounter::destroyCounterNode(RenderObject& owner, const AtomicString& identifier)
    464 {
    465     CounterMap* map = counterMaps().get(&owner);
    466     if (!map)
    467         return;
    468     CounterMap::iterator mapIterator = map->find(identifier);
    469     if (mapIterator == map->end())
    470         return;
    471     destroyCounterNodeWithoutMapRemoval(identifier, mapIterator->value.get());
    472     map->remove(mapIterator);
    473     // We do not delete "map" here even if empty because we expect to reuse
    474     // it soon. In order for a renderer to lose all its counters permanently,
    475     // a style change for the renderer involving removal of all counter
    476     // directives must occur, in which case, RenderCounter::destroyCounterNodes()
    477     // must be called.
    478     // The destruction of the Renderer (possibly caused by the removal of its
    479     // associated DOM node) is the other case that leads to the permanent
    480     // destruction of all counters attached to a Renderer. In this case
    481     // RenderCounter::destroyCounterNodes() must be and is now called, too.
    482     // RenderCounter::destroyCounterNodes() handles destruction of the counter
    483     // map associated with a renderer, so there is no risk in leaking the map.
    484 }
    485 
    486 void RenderCounter::rendererRemovedFromTree(RenderObject* renderer)
    487 {
    488     ASSERT(renderer->view());
    489     if (!renderer->view()->hasRenderCounters())
    490         return;
    491     RenderObject* currentRenderer = renderer->lastLeafChild();
    492     if (!currentRenderer)
    493         currentRenderer = renderer;
    494     while (true) {
    495         destroyCounterNodes(*currentRenderer);
    496         if (currentRenderer == renderer)
    497             break;
    498         currentRenderer = currentRenderer->previousInPreOrder();
    499     }
    500 }
    501 
    502 static void updateCounters(RenderObject& renderer)
    503 {
    504     ASSERT(renderer.style());
    505     const CounterDirectiveMap* directiveMap = renderer.style()->counterDirectives();
    506     if (!directiveMap)
    507         return;
    508     CounterDirectiveMap::const_iterator end = directiveMap->end();
    509     if (!renderer.hasCounterNodeMap()) {
    510         for (CounterDirectiveMap::const_iterator it = directiveMap->begin(); it != end; ++it)
    511             makeCounterNode(renderer, it->key, false);
    512         return;
    513     }
    514     CounterMap* counterMap = counterMaps().get(&renderer);
    515     ASSERT(counterMap);
    516     for (CounterDirectiveMap::const_iterator it = directiveMap->begin(); it != end; ++it) {
    517         RefPtr<CounterNode> node = counterMap->get(it->key);
    518         if (!node) {
    519             makeCounterNode(renderer, it->key, false);
    520             continue;
    521         }
    522         RefPtr<CounterNode> newParent = nullptr;
    523         RefPtr<CounterNode> newPreviousSibling = nullptr;
    524 
    525         findPlaceForCounter(renderer, it->key, node->hasResetType(), newParent, newPreviousSibling);
    526         if (node != counterMap->get(it->key))
    527             continue;
    528         CounterNode* parent = node->parent();
    529         if (newParent == parent && newPreviousSibling == node->previousSibling())
    530             continue;
    531         if (parent)
    532             parent->removeChild(node.get());
    533         if (newParent)
    534             newParent->insertAfter(node.get(), newPreviousSibling.get(), it->key);
    535     }
    536 }
    537 
    538 void RenderCounter::rendererSubtreeAttached(RenderObject* renderer)
    539 {
    540     ASSERT(renderer->view());
    541     if (!renderer->view()->hasRenderCounters())
    542         return;
    543     Node* node = renderer->node();
    544     if (node)
    545         node = node->parentNode();
    546     else
    547         node = renderer->generatingNode();
    548     if (node && node->needsAttach())
    549         return; // No need to update if the parent is not attached yet
    550     for (RenderObject* descendant = renderer; descendant; descendant = descendant->nextInPreOrder(renderer))
    551         updateCounters(*descendant);
    552 }
    553 
    554 void RenderCounter::rendererStyleChanged(RenderObject& renderer, const RenderStyle* oldStyle, const RenderStyle* newStyle)
    555 {
    556     Node* node = renderer.generatingNode();
    557     if (!node || node->needsAttach())
    558         return; // cannot have generated content or if it can have, it will be handled during attaching
    559     const CounterDirectiveMap* oldCounterDirectives = oldStyle ? oldStyle->counterDirectives() : 0;
    560     const CounterDirectiveMap* newCounterDirectives = newStyle ? newStyle->counterDirectives() : 0;
    561     if (oldCounterDirectives) {
    562         if (newCounterDirectives) {
    563             CounterDirectiveMap::const_iterator newMapEnd = newCounterDirectives->end();
    564             CounterDirectiveMap::const_iterator oldMapEnd = oldCounterDirectives->end();
    565             for (CounterDirectiveMap::const_iterator it = newCounterDirectives->begin(); it != newMapEnd; ++it) {
    566                 CounterDirectiveMap::const_iterator oldMapIt = oldCounterDirectives->find(it->key);
    567                 if (oldMapIt != oldMapEnd) {
    568                     if (oldMapIt->value == it->value)
    569                         continue;
    570                     RenderCounter::destroyCounterNode(renderer, it->key);
    571                 }
    572                 // We must create this node here, because the changed node may be a node with no display such as
    573                 // as those created by the increment or reset directives and the re-layout that will happen will
    574                 // not catch the change if the node had no children.
    575                 makeCounterNode(renderer, it->key, false);
    576             }
    577             // Destroying old counters that do not exist in the new counterDirective map.
    578             for (CounterDirectiveMap::const_iterator it = oldCounterDirectives->begin(); it !=oldMapEnd; ++it) {
    579                 if (!newCounterDirectives->contains(it->key))
    580                     RenderCounter::destroyCounterNode(renderer, it->key);
    581             }
    582         } else {
    583             if (renderer.hasCounterNodeMap())
    584                 RenderCounter::destroyCounterNodes(renderer);
    585         }
    586     } else if (newCounterDirectives) {
    587         CounterDirectiveMap::const_iterator newMapEnd = newCounterDirectives->end();
    588         for (CounterDirectiveMap::const_iterator it = newCounterDirectives->begin(); it != newMapEnd; ++it) {
    589             // We must create this node here, because the added node may be a node with no display such as
    590             // as those created by the increment or reset directives and the re-layout that will happen will
    591             // not catch the change if the node had no children.
    592             makeCounterNode(renderer, it->key, false);
    593         }
    594     }
    595 }
    596 
    597 } // namespace blink
    598 
    599 #ifndef NDEBUG
    600 
    601 void showCounterRendererTree(const blink::RenderObject* renderer, const char* counterName)
    602 {
    603     if (!renderer)
    604         return;
    605     const blink::RenderObject* root = renderer;
    606     while (root->parent())
    607         root = root->parent();
    608 
    609     AtomicString identifier(counterName);
    610     for (const blink::RenderObject* current = root; current; current = current->nextInPreOrder()) {
    611         fprintf(stderr, "%c", (current == renderer) ? '*' : ' ');
    612         for (const blink::RenderObject* parent = current; parent && parent != root; parent = parent->parent())
    613             fprintf(stderr, "    ");
    614         fprintf(stderr, "%p N:%p P:%p PS:%p NS:%p C:%p\n",
    615             current, current->node(), current->parent(), current->previousSibling(),
    616             current->nextSibling(), current->hasCounterNodeMap() ?
    617             counterName ? blink::counterMaps().get(current)->get(identifier) : (blink::CounterNode*)1 : (blink::CounterNode*)0);
    618     }
    619     fflush(stderr);
    620 }
    621 
    622 #endif // NDEBUG
    623