#include "config.h"
#include "RenderCounter.h"
#include "CounterNode.h"
#include "Document.h"
#include "Element.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>, RefPtr<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 RenderObject* rendererOfAfterPseudoElement(RenderObject* renderer)
{
RenderObject* lastContinuation = renderer;
while (RenderObject* continuation = lastContinuation->virtualContinuation())
lastContinuation = continuation;
return lastContinuation->afterPseudoElementRenderer();
}
static RenderObject* previousInPreOrder(const RenderObject* object)
{
Element* parent;
Element* sibling;
switch (object->style()->styleType()) {
case NOPSEUDO:
ASSERT(!object->isAnonymous());
parent = toElement(object->node());
sibling = parent->previousElementSibling();
parent = parent->parentElement();
break;
case BEFORE:
return object->generatingNode()->renderer(); case AFTER:
parent = toElement(object->generatingNode());
sibling = parent->lastElementChild();
break;
default:
ASSERT_NOT_REACHED();
return 0;
}
while (sibling) {
if (RenderObject* renderer = sibling->renderer()) {
if (RenderObject* after = rendererOfAfterPseudoElement(renderer))
return after;
parent = sibling;
sibling = sibling->lastElementChild();
if (!sibling) {
if (RenderObject* before = renderer->beforePseudoElementRenderer())
return before;
return renderer;
}
} else
sibling = sibling->previousElementSibling();
}
if (!parent)
return 0;
RenderObject* renderer = parent->renderer(); if (RenderObject* before = renderer->beforePseudoElementRenderer())
return before;
return renderer;
}
static RenderObject* previousSiblingOrParent(const RenderObject* object)
{
Element* parent;
Element* sibling;
switch (object->style()->styleType()) {
case NOPSEUDO:
ASSERT(!object->isAnonymous());
parent = toElement(object->node());
sibling = parent->previousElementSibling();
parent = parent->parentElement();
break;
case BEFORE:
return object->generatingNode()->renderer(); case AFTER:
parent = toElement(object->generatingNode());
sibling = parent->lastElementChild();
break;
default:
ASSERT_NOT_REACHED();
return 0;
}
while (sibling) {
if (RenderObject* renderer = sibling->renderer()) return renderer;
sibling = sibling->previousElementSibling();
}
if (parent) {
RenderObject* renderer = parent->renderer();
if (RenderObject* before = renderer->virtualChildren()->beforePseudoElementRenderer(renderer))
return before;
return renderer;
}
return 0;
}
static Element* parentElement(RenderObject* object)
{
switch (object->style()->styleType()) {
case NOPSEUDO:
ASSERT(!object->isAnonymous());
return toElement(object->node())->parentElement();
case BEFORE:
case AFTER:
return toElement(object->generatingNode());
default:
ASSERT_NOT_REACHED();
return 0;
}
}
static inline bool areRenderersElementsSiblings(RenderObject* first, RenderObject* second)
{
return parentElement(first) == parentElement(second);
}
static RenderObject* nextInPreOrder(const RenderObject* object, const Element* stayWithin, bool skipDescendants = false)
{
Element* self;
Element* child;
RenderObject* result;
self = toElement(object->generatingNode());
if (skipDescendants)
goto nextsibling;
switch (object->style()->styleType()) {
case NOPSEUDO:
ASSERT(!object->isAnonymous());
result = object->beforePseudoElementRenderer();
if (result)
return result;
break;
case BEFORE:
break;
case AFTER:
goto nextsibling;
default:
ASSERT_NOT_REACHED();
return 0;
}
child = self->firstElementChild();
while (true) {
while (child) {
result = child->renderer();
if (result)
return result;
child = child->nextElementSibling();
}
result = rendererOfAfterPseudoElement(self->renderer());
if (result)
return result;
nextsibling:
if (self == stayWithin)
return 0;
child = self->nextElementSibling();
self = self->parentElement();
if (!self) {
ASSERT(!child); return 0; }
}
}
static bool planCounter(RenderObject* object, const AtomicString& identifier, bool& isReset, int& value)
{
ASSERT(object);
if (object->isText() && !object->isBR())
return false;
Node* generatingNode = object->generatingNode();
if (!generatingNode)
return false;
RenderStyle* style = object->style();
ASSERT(style);
switch (style->styleType()) {
case NOPSEUDO:
if (generatingNode->renderer() != object)
return false;
break;
case BEFORE:
case AFTER:
break;
default:
return false; }
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 = previousInPreOrder(counterOwner);
previousSibling = 0;
RefPtr<CounterNode> previousSiblingProtector = 0;
while (currentRenderer) {
CounterNode* currentCounter = makeCounterNode(currentRenderer, identifier, false);
if (searchEndRenderer == currentRenderer) {
if (currentCounter) {
if (previousSiblingProtector) { if (currentCounter->actsAsReset()) {
if (isReset && areRenderersElementsSiblings(currentRenderer, counterOwner)) {
parent = currentCounter->parent();
previousSibling = parent ? currentCounter : 0;
return parent;
}
parent = currentCounter;
if (previousSiblingProtector->parent() != currentCounter)
previousSiblingProtector = 0;
previousSibling = previousSiblingProtector.get();
return true;
}
if (!isReset || !areRenderersElementsSiblings(currentRenderer, counterOwner)) {
ASSERT(currentCounter->parent() == previousSiblingProtector->parent());
parent = currentCounter->parent();
previousSibling = previousSiblingProtector.get();
return true;
}
} else {
if (currentCounter->actsAsReset()) {
if (isReset && areRenderersElementsSiblings(currentRenderer, counterOwner)) {
parent = currentCounter->parent();
previousSibling = currentCounter;
return parent;
}
parent = currentCounter;
previousSibling = previousSiblingProtector.get();
return true;
}
if (!isReset || !areRenderersElementsSiblings(currentRenderer, counterOwner)) {
parent = currentCounter->parent();
previousSibling = currentCounter;
return true;
}
previousSiblingProtector = currentCounter;
}
}
searchEndRenderer = previousSiblingOrParent(currentRenderer);
} else {
if (currentCounter) {
if (previousSiblingProtector) {
if (currentCounter->actsAsReset()) {
previousSiblingProtector = currentCounter;
currentRenderer = parentElement(currentRenderer)->renderer();
continue;
}
} else
previousSiblingProtector = currentCounter;
currentRenderer = previousSiblingOrParent(currentRenderer);
continue;
}
}
if (previousSiblingProtector)
currentRenderer = previousSiblingOrParent(currentRenderer);
else
currentRenderer = previousInPreOrder(currentRenderer);
}
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()).get())
return node;
}
}
bool isReset = false;
int value = 0;
if (!planCounter(object, identifier, isReset, value) && !alwaysCreateCounter)
return 0;
CounterNode* newParent = 0;
CounterNode* newPreviousSibling = 0;
RefPtr<CounterNode> newNode = CounterNode::create(object, isReset, value);
if (findPlaceForCounter(object, identifier, isReset, newParent, newPreviousSibling))
newParent->insertAfter(newNode.get(), 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())
return newNode.get();
CounterMaps& maps = counterMaps();
Element* stayWithin = parentElement(object);
bool skipDescendants;
for (RenderObject* currentRenderer = nextInPreOrder(object, stayWithin); currentRenderer; currentRenderer = nextInPreOrder(currentRenderer, stayWithin, skipDescendants)) {
skipDescendants = false;
if (!currentRenderer->m_hasCounterNodeMap)
continue;
CounterNode* currentCounter = maps.get(currentRenderer)->get(identifier.impl()).get();
if (!currentCounter)
continue;
skipDescendants = true;
if (currentCounter->parent())
continue;
if (stayWithin == parentElement(currentRenderer) && currentCounter->hasResetType())
break;
newNode->insertAfter(currentCounter, newNode->lastChild(), identifier);
}
return newNode.get();
}
RenderCounter::RenderCounter(Document* node, const CounterContent& counter)
: RenderText(node, StringImpl::empty())
, m_counter(counter)
, m_counterNode(0)
, m_nextForSameCounter(0)
{
}
RenderCounter::~RenderCounter()
{
if (m_counterNode) {
m_counterNode->removeRenderer(this);
ASSERT(!m_counterNode);
}
}
const char* RenderCounter::renderName() const
{
return "RenderCounter";
}
bool RenderCounter::isCounter() const
{
return true;
}
PassRefPtr<StringImpl> RenderCounter::originalText() const
{
if (!m_counterNode) {
RenderObject* beforeAfterContainer = parent();
while (true) {
if (!beforeAfterContainer)
return 0;
if (!beforeAfterContainer->isAnonymous())
return 0; PseudoId containerStyle = beforeAfterContainer->style()->styleType();
if ((containerStyle == BEFORE) || (containerStyle == AFTER))
break;
beforeAfterContainer = beforeAfterContainer->parent();
}
makeCounterNode(beforeAfterContainer, m_counter.identifier(), true)->addRenderer(const_cast<RenderCounter*>(this));
ASSERT(m_counterNode);
}
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::computePreferredLogicalWidths(float lead)
{
setTextInternal(originalText());
RenderText::computePreferredLogicalWidths(lead);
}
void RenderCounter::invalidate()
{
m_counterNode->removeRenderer(this);
ASSERT(!m_counterNode);
if (documentBeingDestroyed())
return;
setNeedsLayoutAndPrefWidthsRecalc();
}
static void destroyCounterNodeWithoutMapRemoval(const AtomicString& identifier, CounterNode* node)
{
CounterNode* previous;
for (RefPtr<CounterNode> child = node->lastDescendant(); child && child != node; child = previous) {
previous = child->previousInPreOrder();
child->parent()->removeChild(child.get());
ASSERT(counterMaps().get(child->owner())->get(identifier.impl()) == child);
counterMaps().get(child->owner())->remove(identifier.impl());
}
if (CounterNode* parent = node->parent())
parent->removeChild(node);
}
void RenderCounter::destroyCounterNodes(RenderObject* owner)
{
CounterMaps& maps = counterMaps();
CounterMaps::iterator mapsIterator = maps.find(owner);
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.get());
}
maps.remove(mapsIterator);
delete map;
owner->m_hasCounterNodeMap = false;
}
void RenderCounter::destroyCounterNode(RenderObject* owner, const AtomicString& identifier)
{
CounterMap* map = counterMaps().get(owner);
if (!map)
return;
CounterMap::iterator mapIterator = map->find(identifier.impl());
if (mapIterator == map->end())
return;
destroyCounterNodeWithoutMapRemoval(identifier, mapIterator->second.get());
map->remove(mapIterator);
}
void RenderCounter::rendererRemovedFromTree(RenderObject* removedRenderer)
{
RenderObject* currentRenderer = removedRenderer->lastLeafChild();
if (!currentRenderer)
currentRenderer = removedRenderer;
while (true) {
destroyCounterNodes(currentRenderer);
if (currentRenderer == removedRenderer)
break;
currentRenderer = currentRenderer->previousInPreOrder();
}
}
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) {
RefPtr<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);
if (node != counterMap->get(it->first.get()))
continue;
CounterNode* parent = node->parent();
if (newParent == parent && newPreviousSibling == node->previousSibling())
continue;
if (parent)
parent->removeChild(node.get());
if (newParent)
newParent->insertAfter(node.get(), newPreviousSibling, it->first.get());
}
}
void RenderCounter::rendererSubtreeAttached(RenderObject* renderer)
{
Node* node = renderer->node();
if (node)
node = node->parentNode();
else
node = renderer->generatingNode();
if (node && !node->attached())
return; for (RenderObject* descendant = renderer; descendant; descendant = descendant->nextInPreOrder(renderer))
updateCounters(descendant);
}
void RenderCounter::rendererStyleChanged(RenderObject* renderer, const RenderStyle* oldStyle, const RenderStyle* newStyle)
{
Node* node = renderer->generatingNode();
if (!node || !node->attached())
return; 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);
}
}
}
}
#ifndef NDEBUG
void showCounterRendererTree(const WebCore::RenderObject* renderer, const char* counterName)
{
if (!renderer)
return;
const WebCore::RenderObject* root = renderer;
while (root->parent())
root = root->parent();
AtomicString identifier(counterName);
for (const WebCore::RenderObject* current = root; current; current = current->nextInPreOrder()) {
fprintf(stderr, "%c", (current == renderer) ? '*' : ' ');
for (const WebCore::RenderObject* parent = current; parent && parent != root; parent = parent->parent())
fprintf(stderr, " ");
fprintf(stderr, "%p N:%p P:%p PS:%p NS:%p C:%p\n",
current, current->node(), current->parent(), current->previousSibling(),
current->nextSibling(), current->m_hasCounterNodeMap?
counterName ? WebCore::counterMaps().get(current)->get(identifier.impl()).get() : (WebCore::CounterNode*)1 : (WebCore::CounterNode*)0);
}
fflush(stderr);
}
#endif // NDEBUG