static CounterNode* previousInPreOrder(CounterNode* node) { CounterNode* previous = node->previousSibling(); if (!previous) return node->parent(); while (CounterNode* lastChild = previous->lastChild()) previous = lastChild; return previous; }
static CounterNode* lastDescendant(CounterNode* node) { CounterNode* last = node->lastChild(); if (!last) return 0; while (CounterNode* lastChild = last->lastChild()) last = lastChild; return last; }
static CounterNode* makeCounterNode(RenderObject* object, const AtomicString& identifier, bool alwaysCreateCounter) { ASSERT(object); if (object->m_hasCounterNodeMap) if (CounterMap* nodeMap = counterMaps().get(object)) if (CounterNode* node = nodeMap->get(identifier.impl())) return node; bool isReset = false; int value = 0; if (!planCounter(object, identifier, isReset, value) && !alwaysCreateCounter) return 0; CounterNode* newParent = 0; CounterNode* newPreviousSibling = 0; CounterNode* newNode = new CounterNode(object, isReset, value); if (findPlaceForCounter(object, identifier, isReset, newParent, newPreviousSibling)) newParent->insertAfter(newNode, newPreviousSibling, identifier); CounterMap* nodeMap; if (object->m_hasCounterNodeMap) nodeMap = counterMaps().get(object); else { nodeMap = new CounterMap; counterMaps().set(object, nodeMap); object->m_hasCounterNodeMap = true; } nodeMap->set(identifier.impl(), newNode); if (newNode->parent() || !object->nextInPreOrder(object->parent())) return newNode; // Checking if some nodes that were previously counter tree root nodes // should become children of this node now. CounterMaps& maps = counterMaps(); RenderObject* stayWithin = object->parent(); for (RenderObject* currentRenderer = object->nextInPreOrder(stayWithin); currentRenderer; currentRenderer = currentRenderer->nextInPreOrder(stayWithin)) { if (!currentRenderer->m_hasCounterNodeMap) continue; CounterNode* currentCounter = maps.get(currentRenderer)->get(identifier.impl()); if (!currentCounter) continue; if (currentCounter->parent()) { ASSERT(newNode->firstChild()); if (currentRenderer->lastChild()) currentRenderer = currentRenderer->lastChild(); continue; } if (stayWithin != currentRenderer->parent() || !currentCounter->hasResetType()) newNode->insertAfter(currentCounter, newNode->lastChild(), identifier); if (currentRenderer->lastChild()) currentRenderer = currentRenderer->lastChild(); } return newNode; }