#include "config.h"
#include "RenderCounter.h"
#include "CounterNode.h"
#include "Document.h"
#include "HTMLNames.h"
#include "HTMLOListElement.h"
#include "RenderListItem.h"
#include "RenderListMarker.h"
#include "RenderStyle.h"
#include <wtf/StdLibExtras.h>
namespace WebCore {
using namespace HTMLNames;
typedef HashMap<RefPtr<AtomicStringImpl>, CounterNode*> CounterMap;
typedef HashMap<const RenderObject*, CounterMap*> CounterMaps;
static CounterNode* makeCounterNode(RenderObject*, const AtomicString& identifier, bool alwaysCreateCounter);
static CounterMaps& counterMaps()
{
DEFINE_STATIC_LOCAL(CounterMaps, staticCounterMaps, ());
return staticCounterMaps;
}
static inline RenderObject* previousSiblingOrParent(RenderObject* object)
{
if (RenderObject* sibling = object->previousSibling())
return sibling;
return object->parent();
}
static bool planCounter(RenderObject* object, const AtomicString& identifier, bool& isReset, int& value)
{
ASSERT(object);
if (object->isText() && !object->isBR())
return false;
RenderStyle* style = object->style();
ASSERT(style);
if (const CounterDirectiveMap* directivesMap = style->counterDirectives()) {
CounterDirectives directives = directivesMap->get(identifier.impl());
if (directives.m_reset) {
value = directives.m_resetValue;
if (directives.m_increment)
value += directives.m_incrementValue;
isReset = true;
return true;
}
if (directives.m_increment) {
value = directives.m_incrementValue;
isReset = false;
return true;
}
}
if (identifier == "list-item") {
if (object->isListItem()) {
if (toRenderListItem(object)->hasExplicitValue()) {
value = toRenderListItem(object)->explicitValue();
isReset = true;
return true;
}
value = 1;
isReset = false;
return true;
}
if (Node* e = object->node()) {
if (e->hasTagName(olTag)) {
value = static_cast<HTMLOListElement*>(e)->start();
isReset = true;
return true;
}
if (e->hasTagName(ulTag) || e->hasTagName(menuTag) || e->hasTagName(dirTag)) {
value = 0;
isReset = true;
return true;
}
}
}
return false;
}
static bool findPlaceForCounter(RenderObject* counterOwner, const AtomicString& identifier, bool isReset, CounterNode*& parent, CounterNode*& previousSibling)
{
RenderObject* searchEndRenderer = previousSiblingOrParent(counterOwner);
RenderObject* currentRenderer = counterOwner->previousInPreOrder();
previousSibling = 0;
while (currentRenderer) {
CounterNode* currentCounter = makeCounterNode(currentRenderer, identifier, false);
if (searchEndRenderer == currentRenderer) {
if (currentCounter) {
if (previousSibling) { if (currentCounter->actsAsReset()) {
if (isReset && currentRenderer->parent() == counterOwner->parent()) {
parent = currentCounter->parent();
previousSibling = parent ? currentCounter : 0;
return parent;
}
parent = currentCounter;
ASSERT(previousSibling->parent() == currentCounter);
return true;
}
if (!isReset || currentRenderer->parent() != counterOwner->parent()) {
ASSERT(currentCounter->parent() == previousSibling->parent());
parent = currentCounter->parent();
return true;
}
} else {
if (currentCounter->actsAsReset()) {
if (isReset && currentRenderer->parent() == counterOwner->parent()) {
parent = currentCounter->parent();
previousSibling = currentCounter;
return parent;
}
parent = currentCounter;
return true;
}
if (!isReset || currentRenderer->parent() != counterOwner->parent()) {
parent = currentCounter->parent();
previousSibling = currentCounter;
return true;
}
previousSibling = currentCounter;
}
}
searchEndRenderer = previousSiblingOrParent(currentRenderer);
} else {
if (currentCounter) {
if (previousSibling) {
if (currentCounter->actsAsReset()) {
previousSibling = currentCounter;
currentRenderer = currentRenderer->parent();
continue;
}
} else
previousSibling = currentCounter;
currentRenderer = previousSiblingOrParent(currentRenderer);
continue;
}
}
if (previousSibling)
currentRenderer = previousSiblingOrParent(currentRenderer);
else
currentRenderer = currentRenderer->previousInPreOrder();
}
return false;
}
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;
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;
}
RenderCounter::RenderCounter(Document* node, const CounterContent& counter)
: RenderText(node, StringImpl::empty())
, m_counter(counter)
, m_counterNode(0)
{
}
const char* RenderCounter::renderName() const
{
return "RenderCounter";
}
bool RenderCounter::isCounter() const
{
return true;
}
PassRefPtr<StringImpl> RenderCounter::originalText() const
{
if (!parent())
return 0;
if (!m_counterNode)
m_counterNode = makeCounterNode(parent(), m_counter.identifier(), true);
CounterNode* child = m_counterNode;
int value = child->actsAsReset() ? child->value() : child->countInParent();
String text = listMarkerText(m_counter.listStyle(), value);
if (!m_counter.separator().isNull()) {
if (!child->actsAsReset())
child = child->parent();
while (CounterNode* parent = child->parent()) {
text = listMarkerText(m_counter.listStyle(), child->countInParent())
+ m_counter.separator() + text;
child = parent;
}
}
return text.impl();
}
void RenderCounter::calcPrefWidths(int lead)
{
setTextInternal(originalText());
RenderText::calcPrefWidths(lead);
}
void RenderCounter::invalidate(const AtomicString& identifier)
{
if (m_counter.identifier() != identifier)
return;
m_counterNode = 0;
setNeedsLayoutAndPrefWidthsRecalc();
}
static void destroyCounterNodeWithoutMapRemoval(const AtomicString& identifier, CounterNode* node)
{
CounterNode* previous;
for (CounterNode* child = node->lastDescendant(); child && child != node; child = previous) {
previous = child->previousInPreOrder();
child->parent()->removeChild(child, identifier);
ASSERT(counterMaps().get(child->renderer())->get(identifier.impl()) == child);
counterMaps().get(child->renderer())->remove(identifier.impl());
if (!child->renderer()->documentBeingDestroyed()) {
RenderObjectChildList* children = child->renderer()->virtualChildren();
if (children)
children->invalidateCounters(child->renderer(), identifier);
}
delete child;
}
RenderObject* renderer = node->renderer();
if (!renderer->documentBeingDestroyed()) {
if (RenderObjectChildList* children = renderer->virtualChildren())
children->invalidateCounters(renderer, identifier);
}
if (CounterNode* parent = node->parent())
parent->removeChild(node, identifier);
delete node;
}
void RenderCounter::destroyCounterNodes(RenderObject* renderer)
{
CounterMaps& maps = counterMaps();
CounterMaps::iterator mapsIterator = maps.find(renderer);
if (mapsIterator == maps.end())
return;
CounterMap* map = mapsIterator->second;
CounterMap::const_iterator end = map->end();
for (CounterMap::const_iterator it = map->begin(); it != end; ++it) {
AtomicString identifier(it->first.get());
destroyCounterNodeWithoutMapRemoval(identifier, it->second);
}
maps.remove(mapsIterator);
delete map;
renderer->m_hasCounterNodeMap = false;
}
void RenderCounter::destroyCounterNode(RenderObject* renderer, const AtomicString& identifier)
{
CounterMap* map = counterMaps().get(renderer);
if (!map)
return;
CounterMap::iterator mapIterator = map->find(identifier.impl());
if (mapIterator == map->end())
return;
destroyCounterNodeWithoutMapRemoval(identifier, mapIterator->second);
map->remove(mapIterator);
}
static void updateCounters(RenderObject* renderer)
{
ASSERT(renderer->style());
const CounterDirectiveMap* directiveMap = renderer->style()->counterDirectives();
if (!directiveMap)
return;
CounterDirectiveMap::const_iterator end = directiveMap->end();
if (!renderer->m_hasCounterNodeMap) {
for (CounterDirectiveMap::const_iterator it = directiveMap->begin(); it != end; ++it)
makeCounterNode(renderer, AtomicString(it->first.get()), false);
return;
}
CounterMap* counterMap = counterMaps().get(renderer);
ASSERT(counterMap);
for (CounterDirectiveMap::const_iterator it = directiveMap->begin(); it != end; ++it) {
CounterNode* node = counterMap->get(it->first.get());
if (!node) {
makeCounterNode(renderer, AtomicString(it->first.get()), false);
continue;
}
CounterNode* newParent = 0;
CounterNode* newPreviousSibling;
findPlaceForCounter(renderer, AtomicString(it->first.get()), node->hasResetType(), newParent, newPreviousSibling);
CounterNode* parent = node->parent();
if (newParent == parent && newPreviousSibling == node->previousSibling())
continue;
if (parent)
parent->removeChild(node, it->first.get());
if (newParent)
newParent->insertAfter(node, newPreviousSibling, it->first.get());
}
}
void RenderCounter::rendererSubtreeAttached(RenderObject* renderer)
{
for (RenderObject* descendant = renderer; descendant; descendant = descendant->nextInPreOrder(renderer))
updateCounters(descendant);
}
void RenderCounter::rendererStyleChanged(RenderObject* renderer, const RenderStyle* oldStyle, const RenderStyle* newStyle)
{
const CounterDirectiveMap* newCounterDirectives;
const CounterDirectiveMap* oldCounterDirectives;
if (oldStyle && (oldCounterDirectives = oldStyle->counterDirectives())) {
if (newStyle && (newCounterDirectives = newStyle->counterDirectives())) {
CounterDirectiveMap::const_iterator newMapEnd = newCounterDirectives->end();
CounterDirectiveMap::const_iterator oldMapEnd = oldCounterDirectives->end();
for (CounterDirectiveMap::const_iterator it = newCounterDirectives->begin(); it != newMapEnd; ++it) {
CounterDirectiveMap::const_iterator oldMapIt = oldCounterDirectives->find(it->first);
if (oldMapIt != oldMapEnd) {
if (oldMapIt->second == it->second)
continue;
RenderCounter::destroyCounterNode(renderer, it->first.get());
}
makeCounterNode(renderer, it->first.get(), false);
}
for (CounterDirectiveMap::const_iterator it = oldCounterDirectives->begin(); it !=oldMapEnd; ++it) {
if (!newCounterDirectives->contains(it->first))
RenderCounter::destroyCounterNode(renderer, it->first.get());
}
} else {
if (renderer->m_hasCounterNodeMap)
RenderCounter::destroyCounterNodes(renderer);
}
} else if (newStyle && (newCounterDirectives = newStyle->counterDirectives())) {
CounterDirectiveMap::const_iterator newMapEnd = newCounterDirectives->end();
for (CounterDirectiveMap::const_iterator it = newCounterDirectives->begin(); it != newMapEnd; ++it) {
makeCounterNode(renderer, it->first.get(), false);
}
}
}
}