#include "config.h"
#include "RenderCounter.h"
#include "CounterNode.h"
#include "Document.h"
#include "Element.h"
#include "ElementTraversal.h"
#include "HTMLNames.h"
#include "HTMLOListElement.h"
#include "PseudoElement.h"
#include "RenderListItem.h"
#include "RenderListMarker.h"
#include "RenderStyle.h"
#include "RenderView.h"
#include <wtf/StdLibExtras.h>
#if ENABLE(TREE_DEBUGGING)
#include <stdio.h>
#endif
namespace WebCore {
using namespace HTMLNames;
typedef HashMap<AtomicString, RefPtr<CounterNode>> CounterMap;
typedef HashMap<const RenderElement*, std::unique_ptr<CounterMap>> CounterMaps;
static CounterNode* makeCounterNode(RenderElement&, const AtomicString& identifier, bool alwaysCreateCounter);
static CounterMaps& counterMaps()
{
DEPRECATED_DEFINE_STATIC_LOCAL(CounterMaps, staticCounterMaps, ());
return staticCounterMaps;
}
static RenderElement* previousInPreOrder(const RenderElement& renderer)
{
ASSERT(renderer.element());
Element* previous = ElementTraversal::previousIncludingPseudo(*renderer.element());
while (previous && !previous->renderer())
previous = ElementTraversal::previousIncludingPseudo(*previous);
return previous ? previous->renderer() : 0;
}
static inline Element* parentOrPseudoHostElement(const RenderElement& renderer)
{
if (renderer.isPseudoElement())
return renderer.generatingElement();
return renderer.element() ? renderer.element()->parentElement() : nullptr;
}
static RenderElement* previousSiblingOrParent(const RenderElement& renderer)
{
ASSERT(renderer.element());
Element* previous = ElementTraversal::pseudoAwarePreviousSibling(*renderer.element());
while (previous && !previous->renderer())
previous = ElementTraversal::pseudoAwarePreviousSibling(*previous);
if (previous)
return previous->renderer();
previous = parentOrPseudoHostElement(renderer);
return previous ? previous->renderer() : nullptr;
}
static inline bool areRenderersElementsSiblings(const RenderElement& first, const RenderElement& second)
{
return parentOrPseudoHostElement(first) == parentOrPseudoHostElement(second);
}
static RenderElement* nextInPreOrder(const RenderElement& renderer, const Element* stayWithin, bool skipDescendants = false)
{
ASSERT(renderer.element());
Element& self = *renderer.element();
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() : nullptr;
}
static bool planCounter(RenderElement& renderer, const AtomicString& identifier, bool& isReset, int& value)
{
Element* generatingElement = renderer.generatingElement();
if (!generatingElement)
return false;
const RenderStyle& style = renderer.style();
switch (style.styleType()) {
case NOPSEUDO:
if (generatingElement->renderer() != &renderer)
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 (is<RenderListItem>(renderer)) {
if (downcast<RenderListItem>(renderer).hasExplicitValue()) {
value = downcast<RenderListItem>(renderer).explicitValue();
isReset = true;
return true;
}
value = 1;
isReset = false;
return true;
}
if (Element* element = renderer.element()) {
if (is<HTMLOListElement>(*element)) {
value = downcast<HTMLOListElement>(*element).start();
isReset = true;
return true;
}
if (element->hasTagName(ulTag) || element->hasTagName(menuTag) || element->hasTagName(dirTag)) {
value = 0;
isReset = true;
return true;
}
}
}
return false;
}
static bool findPlaceForCounter(RenderElement& counterOwner, const AtomicString& identifier, bool isReset, RefPtr<CounterNode>& parent, RefPtr<CounterNode>& previousSibling)
{
RenderElement* searchEndRenderer = previousSiblingOrParent(counterOwner);
RenderElement* currentRenderer = previousInPreOrder(counterOwner);
previousSibling = nullptr;
RefPtr<CounterNode> previousSiblingProtector;
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 : nullptr;
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 = parentOrPseudoHostElement(*currentRenderer)->renderer();
continue;
}
} else
previousSiblingProtector = currentCounter;
currentRenderer = previousSiblingOrParent(*currentRenderer);
continue;
}
}
if (previousSiblingProtector)
currentRenderer = previousSiblingOrParent(*currentRenderer);
else
currentRenderer = previousInPreOrder(*currentRenderer);
}
return false;
}
static CounterNode* makeCounterNode(RenderElement& renderer, const AtomicString& identifier, bool alwaysCreateCounter)
{
if (renderer.hasCounterNodeMap()) {
if (CounterMap* nodeMap = counterMaps().get(&renderer)) {
if (CounterNode* node = nodeMap->get(identifier))
return node;
}
}
bool isReset = false;
int value = 0;
if (!planCounter(renderer, identifier, isReset, value) && !alwaysCreateCounter)
return nullptr;
RefPtr<CounterNode> newParent = 0;
RefPtr<CounterNode> newPreviousSibling = 0;
RefPtr<CounterNode> newNode = CounterNode::create(renderer, isReset, value);
if (findPlaceForCounter(renderer, identifier, isReset, newParent, newPreviousSibling))
newParent->insertAfter(newNode.get(), newPreviousSibling.get(), identifier);
CounterMap* nodeMap;
if (renderer.hasCounterNodeMap())
nodeMap = counterMaps().get(&renderer);
else {
nodeMap = new CounterMap;
counterMaps().set(&renderer, std::unique_ptr<CounterMap>(nodeMap));
renderer.setHasCounterNodeMap(true);
}
nodeMap->set(identifier, newNode);
if (newNode->parent())
return newNode.get();
CounterMaps& maps = counterMaps();
Element* stayWithin = parentOrPseudoHostElement(renderer);
bool skipDescendants;
for (RenderElement* currentRenderer = nextInPreOrder(renderer, 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 == parentOrPseudoHostElement(*currentRenderer) && currentCounter->hasResetType())
break;
newNode->insertAfter(currentCounter, newNode->lastChild(), identifier);
}
return newNode.get();
}
RenderCounter::RenderCounter(Document& document, const CounterContent& counter)
: RenderText(document, emptyString())
, m_counter(counter)
, m_counterNode(nullptr)
, m_nextForSameCounter(0)
{
view().addRenderCounter();
}
RenderCounter::~RenderCounter()
{
view().removeRenderCounter();
if (m_counterNode) {
m_counterNode->removeRenderer(this);
ASSERT(!m_counterNode);
}
}
const char* RenderCounter::renderName() const
{
return "RenderCounter";
}
bool RenderCounter::isCounter() const
{
return true;
}
String RenderCounter::originalText() const
{
if (!m_counterNode) {
RenderElement* beforeAfterContainer = parent();
while (true) {
if (!beforeAfterContainer)
return String();
if (!beforeAfterContainer->isAnonymous() && !beforeAfterContainer->isPseudoElement())
return String(); 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;
}
void RenderCounter::updateCounter()
{
computePreferredLogicalWidths(0);
}
void RenderCounter::computePreferredLogicalWidths(float lead)
{
#ifndef NDEBUG
SetLayoutNeededForbiddenScope layoutForbiddenScope(this, false);
#endif
setRenderedText(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) == child);
counterMaps().get(&child->owner())->remove(identifier);
}
if (CounterNode* parent = node->parent())
parent->removeChild(node);
}
void RenderCounter::destroyCounterNodes(RenderElement& 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(RenderElement& 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(RenderElement& renderer)
{
if (!renderer.view().hasRenderCounters())
return;
RenderObject* currentRenderer = renderer.lastLeafChild();
if (!currentRenderer)
currentRenderer = &renderer;
while (true) {
if (is<RenderElement>(*currentRenderer))
destroyCounterNodes(downcast<RenderElement>(*currentRenderer));
if (currentRenderer == &renderer)
break;
currentRenderer = currentRenderer->previousInPreOrder();
}
}
static void updateCounters(RenderElement& renderer)
{
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 = 0;
RefPtr<CounterNode> newPreviousSibling = 0;
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(RenderElement& renderer)
{
if (!renderer.view().hasRenderCounters())
return;
Element* element = renderer.element();
if (element && !element->isPseudoElement())
element = element->parentElement();
else
element = renderer.generatingElement();
if (element && !element->renderer())
return; for (RenderObject* descendant = &renderer; descendant; descendant = descendant->nextInPreOrder(&renderer)) {
if (is<RenderElement>(*descendant))
updateCounters(downcast<RenderElement>(*descendant));
}
}
void RenderCounter::rendererStyleChanged(RenderElement& renderer, const RenderStyle* oldStyle, const RenderStyle* newStyle)
{
Element* element = renderer.generatingElement();
if (!element || !element->renderer())
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);
}
}
}
}
#if ENABLE(TREE_DEBUGGING)
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()) {
if (!is<WebCore::RenderElement>(*current))
continue;
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(), downcast<WebCore::RenderElement>(*current).hasCounterNodeMap() ?
counterName ? WebCore::counterMaps().get(downcast<WebCore::RenderElement>(current))->get(identifier) : (WebCore::CounterNode*)1 : (WebCore::CounterNode*)0);
}
fflush(stderr);
}
#endif // NDEBUG