This source file includes following definitions.
- counterMaps
- previousInPreOrder
- previousSiblingOrParent
- parentElement
- areRenderersElementsSiblings
- nextInPreOrder
- planCounter
- findPlaceForCounter
- makeCounterNode
- m_nextForSameCounter
- willBeDestroyed
- renderName
- isCounter
- originalText
- updateCounter
- invalidate
- destroyCounterNodeWithoutMapRemoval
- destroyCounterNodes
- destroyCounterNode
- rendererRemovedFromTree
- updateCounters
- rendererSubtreeAttached
- rendererStyleChanged
- showCounterRendererTree
#include "config.h"
#include "core/rendering/RenderCounter.h"
#include "HTMLNames.h"
#include "core/dom/Document.h"
#include "core/dom/Element.h"
#include "core/dom/ElementTraversal.h"
#include "core/html/HTMLOListElement.h"
#include "core/rendering/CounterNode.h"
#include "core/rendering/RenderListItem.h"
#include "core/rendering/RenderListMarker.h"
#include "core/rendering/RenderView.h"
#include "core/rendering/style/RenderStyle.h"
#include "wtf/StdLibExtras.h"
#ifndef NDEBUG
#include <stdio.h>
#endif
namespace WebCore {
using namespace HTMLNames;
typedef HashMap<AtomicString, RefPtr<CounterNode> > CounterMap;
typedef HashMap<const RenderObject*, OwnPtr<CounterMap> > CounterMaps;
static CounterNode* makeCounterNode(RenderObject&, const AtomicString& identifier, bool alwaysCreateCounter);
static CounterMaps& counterMaps()
{
DEFINE_STATIC_LOCAL(CounterMaps, staticCounterMaps, ());
return staticCounterMaps;
}
static RenderObject* previousInPreOrder(const RenderObject& object)
{
Element* self = toElement(object.node());
ASSERT(self);
Element* previous = ElementTraversal::previousIncludingPseudo(*self);
while (previous && !previous->renderer())
previous = ElementTraversal::previousIncludingPseudo(*previous);
return previous ? previous->renderer() : 0;
}
static RenderObject* previousSiblingOrParent(const RenderObject& object)
{
Element* self = toElement(object.node());
ASSERT(self);
Element* previous = ElementTraversal::pseudoAwarePreviousSibling(*self);
while (previous && !previous->renderer())
previous = ElementTraversal::pseudoAwarePreviousSibling(*previous);
if (previous)
return previous->renderer();
previous = self->parentElement();
return previous ? previous->renderer() : 0;
}
static inline Element* parentElement(RenderObject& object)
{
return toElement(object.node())->parentElement();
}
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 = toElement(object.node());
ASSERT(self);
Element* next = skipDescendants ? ElementTraversal::nextIncludingPseudoSkippingChildren(*self, stayWithin) : ElementTraversal::nextIncludingPseudo(*self, stayWithin);
while (next && !next->renderer())
next = skipDescendants ? ElementTraversal::nextIncludingPseudoSkippingChildren(*next, stayWithin) : ElementTraversal::nextIncludingPseudo(*next, stayWithin);
return next ? next->renderer() : 0;
}
static bool planCounter(RenderObject& object, const AtomicString& identifier, bool& isReset, int& value)
{
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;
}
const CounterDirectives directives = style->getCounterDirectives(identifier);
if (directives.isDefined()) {
value = directives.combinedValue();
isReset = directives.isReset();
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 (isHTMLOListElement(*e)) {
value = toHTMLOListElement(e)->start();
isReset = true;
return true;
}
if (isHTMLUListElement(*e) || isHTMLMenuElement(*e) || isHTMLDirectoryElement(*e)) {
value = 0;
isReset = true;
return true;
}
}
}
return false;
}
static bool findPlaceForCounter(RenderObject& counterOwner, const AtomicString& identifier, bool isReset, RefPtr<CounterNode>& parent, RefPtr<CounterNode>& previousSibling)
{
RenderObject* searchEndRenderer = previousSiblingOrParent(counterOwner);
RenderObject* currentRenderer = previousInPreOrder(counterOwner);
previousSibling = nullptr;
RefPtr<CounterNode> previousSiblingProtector = nullptr;
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 = nullptr;
previousSibling = previousSiblingProtector.get();
return true;
}
if (!isReset || !areRenderersElementsSiblings(*currentRenderer, counterOwner)) {
if (currentCounter->parent() != previousSiblingProtector->parent())
return false;
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)
{
if (object.hasCounterNodeMap()) {
if (CounterMap* nodeMap = counterMaps().get(&object)) {
if (CounterNode* node = nodeMap->get(identifier))
return node;
}
}
bool isReset = false;
int value = 0;
if (!planCounter(object, identifier, isReset, value) && !alwaysCreateCounter)
return 0;
RefPtr<CounterNode> newParent = nullptr;
RefPtr<CounterNode> newPreviousSibling = nullptr;
RefPtr<CounterNode> newNode = CounterNode::create(object, isReset, value);
if (findPlaceForCounter(object, identifier, isReset, newParent, newPreviousSibling))
newParent->insertAfter(newNode.get(), newPreviousSibling.get(), identifier);
CounterMap* nodeMap;
if (object.hasCounterNodeMap())
nodeMap = counterMaps().get(&object);
else {
nodeMap = new CounterMap;
counterMaps().set(&object, adoptPtr(nodeMap));
object.setHasCounterNodeMap(true);
}
nodeMap->set(identifier, 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->hasCounterNodeMap())
continue;
CounterNode* currentCounter = maps.get(currentRenderer)->get(identifier);
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)
{
view()->addRenderCounter();
}
RenderCounter::~RenderCounter()
{
if (m_counterNode) {
m_counterNode->removeRenderer(this);
ASSERT(!m_counterNode);
}
}
void RenderCounter::willBeDestroyed()
{
if (view())
view()->removeRenderCounter();
RenderText::willBeDestroyed();
}
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 nullptr;
if (!beforeAfterContainer->isAnonymous() && !beforeAfterContainer->isPseudoElement())
return nullptr;
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::updateCounter()
{
setTextInternal(originalText());
}
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) == child);
counterMaps().get(&child->owner())->remove(identifier);
}
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->value.get();
CounterMap::const_iterator end = map->end();
for (CounterMap::const_iterator it = map->begin(); it != end; ++it) {
destroyCounterNodeWithoutMapRemoval(it->key, it->value.get());
}
maps.remove(mapsIterator);
owner.setHasCounterNodeMap(false);
}
void RenderCounter::destroyCounterNode(RenderObject& owner, const AtomicString& identifier)
{
CounterMap* map = counterMaps().get(&owner);
if (!map)
return;
CounterMap::iterator mapIterator = map->find(identifier);
if (mapIterator == map->end())
return;
destroyCounterNodeWithoutMapRemoval(identifier, mapIterator->value.get());
map->remove(mapIterator);
}
void RenderCounter::rendererRemovedFromTree(RenderObject* renderer)
{
ASSERT(renderer->view());
if (!renderer->view()->hasRenderCounters())
return;
RenderObject* currentRenderer = renderer->lastLeafChild();
if (!currentRenderer)
currentRenderer = renderer;
while (true) {
destroyCounterNodes(*currentRenderer);
if (currentRenderer == renderer)
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.hasCounterNodeMap()) {
for (CounterDirectiveMap::const_iterator it = directiveMap->begin(); it != end; ++it)
makeCounterNode(renderer, it->key, 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->key);
if (!node) {
makeCounterNode(renderer, it->key, false);
continue;
}
RefPtr<CounterNode> newParent = nullptr;
RefPtr<CounterNode> newPreviousSibling = nullptr;
findPlaceForCounter(renderer, it->key, node->hasResetType(), newParent, newPreviousSibling);
if (node != counterMap->get(it->key))
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.get(), it->key);
}
}
void RenderCounter::rendererSubtreeAttached(RenderObject* renderer)
{
ASSERT(renderer->view());
if (!renderer->view()->hasRenderCounters())
return;
Node* node = renderer->node();
if (node)
node = node->parentNode();
else
node = renderer->generatingNode();
if (node && node->needsAttach())
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->needsAttach())
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->key);
if (oldMapIt != oldMapEnd) {
if (oldMapIt->value == it->value)
continue;
RenderCounter::destroyCounterNode(renderer, it->key);
}
makeCounterNode(renderer, it->key, false);
}
for (CounterDirectiveMap::const_iterator it = oldCounterDirectives->begin(); it !=oldMapEnd; ++it) {
if (!newCounterDirectives->contains(it->key))
RenderCounter::destroyCounterNode(renderer, it->key);
}
} else {
if (renderer.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->key, 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->hasCounterNodeMap() ?
counterName ? WebCore::counterMaps().get(current)->get(identifier) : (WebCore::CounterNode*)1 : (WebCore::CounterNode*)0);
}
fflush(stderr);
}
#endif