6a07616220ae5e7ec5f4b8296f3daccf394307b7
[WebKit-https.git] / WebCore / rendering / RenderCounter.cpp
1 /**
2  * Copyright (C) 2004 Allan Sandfeld Jensen (kde@carewolf.com)
3  * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  */
21
22 #include "config.h"
23 #include "RenderCounter.h"
24
25 #include "CounterNode.h"
26 #include "Document.h"
27 #include "HTMLNames.h"
28 #include "HTMLOListElement.h"
29 #include "RenderListItem.h"
30 #include "RenderListMarker.h"
31 #include "RenderStyle.h"
32 #include <wtf/StdLibExtras.h>
33
34 namespace WebCore {
35
36 using namespace HTMLNames;
37
38 typedef HashMap<RefPtr<AtomicStringImpl>, CounterNode*> CounterMap;
39 typedef HashMap<const RenderObject*, CounterMap*> CounterMaps;
40
41 static CounterNode* makeCounterNode(RenderObject*, const AtomicString& counterName, bool alwaysCreateCounter);
42
43 static CounterMaps& counterMaps()
44 {
45     DEFINE_STATIC_LOCAL(CounterMaps, staticCounterMaps, ());
46     return staticCounterMaps;
47 }
48
49 static inline RenderObject* previousSiblingOrParent(RenderObject* object)
50 {
51     if (RenderObject* sibling = object->previousSibling())
52         return sibling;
53     return object->parent();
54 }
55
56 static bool planCounter(RenderObject* object, const AtomicString& counterName, bool& isReset, int& value)
57 {
58     ASSERT(object);
59
60     // Real text nodes don't have their own style so they can't have counters.
61     // We can't even look at their styles or we'll see extra resets and increments!
62     if (object->isText() && !object->isBR())
63         return false;
64
65     RenderStyle* style = object->style();
66     ASSERT(style);
67
68     if (const CounterDirectiveMap* directivesMap = style->counterDirectives()) {
69         CounterDirectives directives = directivesMap->get(counterName.impl());
70         if (directives.m_reset) {
71             value = directives.m_resetValue;
72             if (directives.m_increment)
73                 value += directives.m_incrementValue;
74             isReset = true;
75             return true;
76         }
77         if (directives.m_increment) {
78             value = directives.m_incrementValue;
79             isReset = false;
80             return true;
81         }
82     }
83
84     if (counterName == "list-item") {
85         if (object->isListItem()) {
86             if (toRenderListItem(object)->hasExplicitValue()) {
87                 value = toRenderListItem(object)->explicitValue();
88                 isReset = true;
89                 return true;
90             }
91             value = 1;
92             isReset = false;
93             return true;
94         }
95         if (Node* e = object->node()) {
96             if (e->hasTagName(olTag)) {
97                 value = static_cast<HTMLOListElement*>(e)->start();
98                 isReset = true;
99                 return true;
100             }
101             if (e->hasTagName(ulTag) || e->hasTagName(menuTag) || e->hasTagName(dirTag)) {
102                 value = 0;
103                 isReset = true;
104                 return true;
105             }
106         }
107     }
108
109     return false;
110 }
111
112 static bool findPlaceForCounter(RenderObject* object, const AtomicString& counterName,
113     bool isReset, CounterNode*& parent, CounterNode*& previousSibling)
114 {
115     // Find the appropriate previous sibling for insertion into the parent node
116     // by searching in render tree order for a child of the counter.
117     parent = 0;
118     previousSibling = 0;
119     RenderObject* resetCandidate = isReset ? object->parent() : previousSiblingOrParent(object);
120     RenderObject* prevCounterCandidate = object;
121     CounterNode* candidateCounter = 0;
122     // When a reset counter is chosen as candidateCounter, we'll
123     // decide the new node should be a child of the reset node or a
124     // sibling or the reset node. This flag controls it.
125     bool createChildForReset = true;
126     while ((prevCounterCandidate = prevCounterCandidate->previousInPreOrder())) {
127         CounterNode* c = makeCounterNode(prevCounterCandidate, counterName, false);
128         if (prevCounterCandidate == resetCandidate) {
129             if (!candidateCounter) {
130                 candidateCounter = c;
131                 createChildForReset = true;
132             }
133             if (candidateCounter) {
134                 if (createChildForReset && candidateCounter->isReset()) {
135                     parent = candidateCounter;
136                     previousSibling = 0;
137                 } else {
138                     parent = candidateCounter->parent();
139                     previousSibling = candidateCounter;
140                 }
141                 return true;
142             }
143             resetCandidate = previousSiblingOrParent(resetCandidate);
144         } else if (c) {
145             if (c->isReset()) {
146                 if (c->parent()) {
147                     // The new node may be the next sibling of this reset node.
148                     createChildForReset = false;
149                     candidateCounter = c;
150                 } else {
151                     createChildForReset = true;
152                     candidateCounter = 0;
153                 }
154             } else if (!candidateCounter) {
155                 createChildForReset = true;
156                 candidateCounter = c;
157             }
158         }
159     }
160
161     return false;
162 }
163
164 static CounterNode* makeCounterNode(RenderObject* object, const AtomicString& counterName, bool alwaysCreateCounter)
165 {
166     ASSERT(object);
167
168     if (object->m_hasCounterNodeMap)
169         if (CounterMap* nodeMap = counterMaps().get(object))
170             if (CounterNode* node = nodeMap->get(counterName.impl()))
171                 return node;
172
173     bool isReset = false;
174     int value = 0;
175     if (!planCounter(object, counterName, isReset, value) && !alwaysCreateCounter)
176         return 0;
177
178     CounterNode* newParent = 0;
179     CounterNode* newPreviousSibling = 0;
180     CounterNode* newNode;
181     if (findPlaceForCounter(object, counterName, isReset, newParent, newPreviousSibling)) {
182         newNode = new CounterNode(object, isReset, value);
183         newParent->insertAfter(newNode, newPreviousSibling, counterName);
184     } else {
185         // Make a reset node for counters that aren't inside an existing reset node.
186         newNode = new CounterNode(object, true, value);
187     }
188
189     CounterMap* nodeMap;
190     if (object->m_hasCounterNodeMap)
191         nodeMap = counterMaps().get(object);
192     else {
193         nodeMap = new CounterMap;
194         counterMaps().set(object, nodeMap);
195         object->m_hasCounterNodeMap = true;
196     }
197     nodeMap->set(counterName.impl(), newNode);
198
199     return newNode;
200 }
201
202 RenderCounter::RenderCounter(Document* node, const CounterContent& counter)
203     : RenderText(node, StringImpl::empty())
204     , m_counter(counter)
205     , m_counterNode(0)
206 {
207 }
208
209 const char* RenderCounter::renderName() const
210 {
211     return "RenderCounter";
212 }
213
214 bool RenderCounter::isCounter() const
215 {
216     return true;
217 }
218
219 PassRefPtr<StringImpl> RenderCounter::originalText() const
220 {
221     if (!parent())
222         return 0;
223
224     if (!m_counterNode)
225         m_counterNode = makeCounterNode(parent(), m_counter.identifier(), true);
226
227     CounterNode* child = m_counterNode;
228     int value = child->isReset() ? child->value() : child->countInParent();
229
230     String text = listMarkerText(m_counter.listStyle(), value);
231
232     if (!m_counter.separator().isNull()) {
233         if (!child->isReset())
234             child = child->parent();
235         while (CounterNode* parent = child->parent()) {
236             text = listMarkerText(m_counter.listStyle(), child->countInParent())
237                 + m_counter.separator() + text;
238             child = parent;
239         }
240     }
241
242     return text.impl();
243 }
244
245 void RenderCounter::calcPrefWidths(int lead)
246 {
247     setTextInternal(originalText());
248     RenderText::calcPrefWidths(lead);
249 }
250
251 void RenderCounter::invalidate(const AtomicString& identifier)
252 {
253     if (m_counter.identifier() != identifier)
254         return;
255     m_counterNode = 0;
256     setNeedsLayoutAndPrefWidthsRecalc();
257 }
258
259 static void destroyCounterNodeChildren(const AtomicString& identifier, CounterNode* node)
260 {
261     CounterNode* previous;
262     for (CounterNode* child = node->lastDescendant(); child && child != node; child = previous) {
263         previous = child->previousInPreOrder();
264         child->parent()->removeChild(child, identifier);
265         ASSERT(counterMaps().get(child->renderer())->get(identifier.impl()) == child);
266         counterMaps().get(child->renderer())->remove(identifier.impl());
267         if (!child->renderer()->documentBeingDestroyed()) {
268             RenderObjectChildList* children = child->renderer()->virtualChildren();
269             if (children)
270                 children->invalidateCounters(child->renderer(), identifier);
271         }
272         delete child;
273     }
274 }
275
276 void RenderCounter::destroyCounterNodes(RenderObject* object)
277 {
278     CounterMaps& maps = counterMaps();
279     CounterMap* map = maps.get(object);
280     if (!map)
281         return;
282     maps.remove(object);
283
284     CounterMap::const_iterator end = map->end();
285     for (CounterMap::const_iterator it = map->begin(); it != end; ++it) {
286         CounterNode* node = it->second;
287         AtomicString identifier(it->first.get());
288         destroyCounterNodeChildren(identifier, node);
289         if (CounterNode* parent = node->parent())
290             parent->removeChild(node, identifier);
291         delete node;
292     }
293
294     delete map;
295 }
296
297 } // namespace WebCore