59123af4ec53cdc8e4cd768d1e15516f8bc9d650
[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 CounterNode* lastDescendant(CounterNode* node)
57 {
58     CounterNode* last = node->lastChild();
59     if (!last)
60         return 0;
61
62     while (CounterNode* lastChild = last->lastChild())
63         last = lastChild;
64
65     return last;
66 }
67
68 static CounterNode* previousInPreOrder(CounterNode* node)
69 {
70     CounterNode* previous = node->previousSibling();
71     if (!previous)
72         return node->parent();
73
74     while (CounterNode* lastChild = previous->lastChild())
75         previous = lastChild;
76
77     return previous;
78 }
79
80 static bool planCounter(RenderObject* object, const AtomicString& counterName, bool& isReset, int& value)
81 {
82     ASSERT(object);
83
84     // Real text nodes don't have their own style so they can't have counters.
85     // We can't even look at their styles or we'll see extra resets and increments!
86     if (object->isText() && !object->isBR())
87         return false;
88
89     RenderStyle* style = object->style();
90     ASSERT(style);
91
92     if (const CounterDirectiveMap* directivesMap = style->counterDirectives()) {
93         CounterDirectives directives = directivesMap->get(counterName.impl());
94         if (directives.m_reset) {
95             value = directives.m_resetValue;
96             if (directives.m_increment)
97                 value += directives.m_incrementValue;
98             isReset = true;
99             return true;
100         }
101         if (directives.m_increment) {
102             value = directives.m_incrementValue;
103             isReset = false;
104             return true;
105         }
106     }
107
108     if (counterName == "list-item") {
109         if (object->isListItem()) {
110             if (toRenderListItem(object)->hasExplicitValue()) {
111                 value = toRenderListItem(object)->explicitValue();
112                 isReset = true;
113                 return true;
114             }
115             value = 1;
116             isReset = false;
117             return true;
118         }
119         if (Node* e = object->node()) {
120             if (e->hasTagName(olTag)) {
121                 value = static_cast<HTMLOListElement*>(e)->start();
122                 isReset = true;
123                 return true;
124             }
125             if (e->hasTagName(ulTag) || e->hasTagName(menuTag) || e->hasTagName(dirTag)) {
126                 value = 0;
127                 isReset = true;
128                 return true;
129             }
130         }
131     }
132
133     return false;
134 }
135
136 static bool findPlaceForCounter(RenderObject* object, const AtomicString& counterName,
137     bool isReset, CounterNode*& parent, CounterNode*& previousSibling)
138 {
139     // Find the appropriate previous sibling for insertion into the parent node
140     // by searching in render tree order for a child of the counter.
141     parent = 0;
142     previousSibling = 0;
143     RenderObject* resetCandidate = isReset ? object->parent() : previousSiblingOrParent(object);
144     RenderObject* prevCounterCandidate = object;
145     CounterNode* candidateCounter = 0;
146     // When a reset counter is chosen as candidateCounter, we'll
147     // decide the new node should be a child of the reset node or a
148     // sibling or the reset node. This flag controls it.
149     bool createChildForReset = true;
150     while ((prevCounterCandidate = prevCounterCandidate->previousInPreOrder())) {
151         CounterNode* c = makeCounterNode(prevCounterCandidate, counterName, false);
152         if (prevCounterCandidate == resetCandidate) {
153             if (!candidateCounter) {
154                 candidateCounter = c;
155                 createChildForReset = true;
156             }
157             if (candidateCounter) {
158                 if (createChildForReset && candidateCounter->isReset()) {
159                     parent = candidateCounter;
160                     previousSibling = 0;
161                 } else {
162                     parent = candidateCounter->parent();
163                     previousSibling = candidateCounter;
164                 }
165                 return true;
166             }
167             resetCandidate = previousSiblingOrParent(resetCandidate);
168         } else if (c) {
169             if (c->isReset()) {
170                 if (c->parent()) {
171                     // The new node may be the next sibling of this reset node.
172                     createChildForReset = false;
173                     candidateCounter = c;
174                 } else {
175                     createChildForReset = true;
176                     candidateCounter = 0;
177                 }
178             } else if (!candidateCounter) {
179                 createChildForReset = true;
180                 candidateCounter = c;
181             }
182         }
183     }
184
185     return false;
186 }
187
188 static CounterNode* makeCounterNode(RenderObject* object, const AtomicString& counterName, bool alwaysCreateCounter)
189 {
190     ASSERT(object);
191
192     if (object->m_hasCounterNodeMap)
193         if (CounterMap* nodeMap = counterMaps().get(object))
194             if (CounterNode* node = nodeMap->get(counterName.impl()))
195                 return node;
196
197     bool isReset = false;
198     int value = 0;
199     if (!planCounter(object, counterName, isReset, value) && !alwaysCreateCounter)
200         return 0;
201
202     CounterNode* newParent = 0;
203     CounterNode* newPreviousSibling = 0;
204     CounterNode* newNode;
205     if (findPlaceForCounter(object, counterName, isReset, newParent, newPreviousSibling)) {
206         newNode = new CounterNode(object, isReset, value);
207         newParent->insertAfter(newNode, newPreviousSibling);
208     } else {
209         // Make a reset node for counters that aren't inside an existing reset node.
210         newNode = new CounterNode(object, true, value);
211     }
212
213     CounterMap* nodeMap;
214     if (object->m_hasCounterNodeMap)
215         nodeMap = counterMaps().get(object);
216     else {
217         nodeMap = new CounterMap;
218         counterMaps().set(object, nodeMap);
219         object->m_hasCounterNodeMap = true;
220     }
221     nodeMap->set(counterName.impl(), newNode);
222
223     return newNode;
224 }
225
226 RenderCounter::RenderCounter(Document* node, const CounterContent& counter)
227     : RenderText(node, StringImpl::empty())
228     , m_counter(counter)
229     , m_counterNode(0)
230 {
231 }
232
233 const char* RenderCounter::renderName() const
234 {
235     return "RenderCounter";
236 }
237
238 bool RenderCounter::isCounter() const
239 {
240     return true;
241 }
242
243 PassRefPtr<StringImpl> RenderCounter::originalText() const
244 {
245     if (!parent())
246         return 0;
247
248     if (!m_counterNode)
249         m_counterNode = makeCounterNode(parent(), m_counter.identifier(), true);
250
251     CounterNode* child = m_counterNode;
252     int value = child->isReset() ? child->value() : child->countInParent();
253
254     String text = listMarkerText(m_counter.listStyle(), value);
255
256     if (!m_counter.separator().isNull()) {
257         if (!child->isReset())
258             child = child->parent();
259         while (CounterNode* parent = child->parent()) {
260             text = listMarkerText(m_counter.listStyle(), child->countInParent())
261                 + m_counter.separator() + text;
262             child = parent;
263         }
264     }
265
266     return text.impl();
267 }
268
269 void RenderCounter::calcPrefWidths(int lead)
270 {
271     setTextInternal(originalText());
272     RenderText::calcPrefWidths(lead);
273 }
274
275 void RenderCounter::invalidate()
276 {
277     m_counterNode = 0;
278     setNeedsLayoutAndPrefWidthsRecalc();
279 }
280
281 static void destroyCounterNodeChildren(AtomicStringImpl* identifier, CounterNode* node)
282 {
283     CounterNode* previous;
284     for (CounterNode* child = lastDescendant(node); child && child != node; child = previous) {
285         previous = previousInPreOrder(child);
286         child->parent()->removeChild(child);
287         ASSERT(counterMaps().get(child->renderer())->get(identifier) == child);
288         counterMaps().get(child->renderer())->remove(identifier);
289         if (!child->renderer()->documentBeingDestroyed()) {
290             RenderObjectChildList* children = child->renderer()->virtualChildren();
291             if (children)
292                 children->invalidateCounters(child->renderer());
293         }
294         delete child;
295     }
296 }
297
298 void RenderCounter::destroyCounterNodes(RenderObject* object)
299 {
300     CounterMaps& maps = counterMaps();
301     CounterMap* map = maps.get(object);
302     if (!map)
303         return;
304     maps.remove(object);
305
306     CounterMap::const_iterator end = map->end();
307     for (CounterMap::const_iterator it = map->begin(); it != end; ++it) {
308         CounterNode* node = it->second;
309         destroyCounterNodeChildren(it->first.get(), node);
310         if (CounterNode* parent = node->parent())
311             parent->removeChild(node);
312         delete node;
313     }
314
315     delete map;
316 }
317
318 } // namespace WebCore