~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/rendering/RenderCounter.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "Element.h"
 
28
#include "HTMLNames.h"
 
29
#include "HTMLOListElement.h"
 
30
#include "RenderListItem.h"
 
31
#include "RenderListMarker.h"
 
32
#include "RenderStyle.h"
 
33
#include "RenderView.h"
 
34
#include <wtf/StdLibExtras.h>
 
35
 
 
36
#ifndef NDEBUG
 
37
#include <stdio.h>
 
38
#endif
 
39
 
 
40
namespace WebCore {
 
41
 
 
42
using namespace HTMLNames;
 
43
 
 
44
typedef HashMap<AtomicString, RefPtr<CounterNode> > CounterMap;
 
45
typedef HashMap<const RenderObject*, OwnPtr<CounterMap> > CounterMaps;
 
46
 
 
47
static CounterNode* makeCounterNode(RenderObject*, const AtomicString& identifier, bool alwaysCreateCounter);
 
48
 
 
49
static CounterMaps& counterMaps()
 
50
{
 
51
    DEFINE_STATIC_LOCAL(CounterMaps, staticCounterMaps, ());
 
52
    return staticCounterMaps;
 
53
}
 
54
 
 
55
static RenderObject* rendererOfAfterPseudoElement(RenderObject* renderer)
 
56
{
 
57
    RenderObject* lastContinuation = renderer;
 
58
    while (RenderObject* continuation = lastContinuation->virtualContinuation())
 
59
        lastContinuation = continuation;
 
60
    return lastContinuation->afterPseudoElementRenderer();
 
61
}
 
62
 
 
63
// This function processes the renderer tree in the order of the DOM tree
 
64
// including pseudo elements as defined in CSS 2.1.
 
65
// Anonymous renderers are skipped except for those representing pseudo elements.
 
66
static RenderObject* previousInPreOrder(const RenderObject* object)
 
67
{
 
68
    Element* parent;
 
69
    Element* sibling;
 
70
    switch (object->style()->styleType()) {
 
71
    case NOPSEUDO:
 
72
        ASSERT(!object->isAnonymous());
 
73
        parent = toElement(object->node());
 
74
        sibling = parent->previousElementSibling();
 
75
        parent = parent->parentElement();
 
76
        break;
 
77
    case BEFORE:
 
78
        return object->generatingNode()->renderer(); // It is always the generating node's renderer
 
79
    case AFTER:
 
80
        parent = toElement(object->generatingNode());
 
81
        sibling = parent->lastElementChild();
 
82
        break;
 
83
    default:
 
84
        ASSERT_NOT_REACHED();
 
85
        return 0;
 
86
    }
 
87
    while (sibling) {
 
88
        if (RenderObject* renderer = sibling->renderer()) {
 
89
            if (RenderObject* after = rendererOfAfterPseudoElement(renderer))
 
90
                return after;
 
91
            parent = sibling;
 
92
            sibling = sibling->lastElementChild();
 
93
            if (!sibling) {
 
94
                if (RenderObject* before = renderer->beforePseudoElementRenderer())
 
95
                    return before;
 
96
                return renderer;
 
97
            }
 
98
        } else
 
99
            sibling = sibling->previousElementSibling();
 
100
    }
 
101
    if (!parent)
 
102
        return 0;
 
103
    RenderObject* renderer = parent->renderer(); // Should never be null
 
104
    if (RenderObject* before = renderer->beforePseudoElementRenderer())
 
105
        return before;
 
106
    return renderer;
 
107
}
 
108
 
 
109
// This function processes the renderer tree in the order of the DOM tree
 
110
// including pseudo elements as defined in CSS 2.1.
 
111
// Anonymous renderers are skipped except for those representing pseudo elements.
 
112
static RenderObject* previousSiblingOrParent(const RenderObject* object)
 
113
{
 
114
    Element* parent;
 
115
    Element* sibling;
 
116
    switch (object->style()->styleType()) {
 
117
    case NOPSEUDO:
 
118
        ASSERT(!object->isAnonymous());
 
119
        parent = toElement(object->node());
 
120
        sibling = parent->previousElementSibling();
 
121
        parent = parent->parentElement();
 
122
        break;
 
123
    case BEFORE:
 
124
        return object->generatingNode()->renderer(); // It is always the generating node's renderer
 
125
    case AFTER:
 
126
        parent = toElement(object->generatingNode());
 
127
        sibling = parent->lastElementChild();
 
128
        break;
 
129
    default:
 
130
        ASSERT_NOT_REACHED();
 
131
        return 0;
 
132
    }
 
133
    while (sibling) {
 
134
        if (RenderObject* renderer = sibling->renderer()) // This skips invisible nodes
 
135
            return renderer;
 
136
        sibling = sibling->previousElementSibling();
 
137
    }
 
138
    if (parent) {
 
139
        RenderObject* renderer = parent->renderer();
 
140
        if (RenderObject* before = renderer->virtualChildren()->beforePseudoElementRenderer(renderer))
 
141
            return before;
 
142
        return renderer;
 
143
    }
 
144
    return 0;
 
145
}
 
146
 
 
147
static Element* parentElement(RenderObject* object)
 
148
{
 
149
    switch (object->style()->styleType()) {
 
150
    case NOPSEUDO:
 
151
        ASSERT(!object->isAnonymous());
 
152
        return toElement(object->node())->parentElement();
 
153
    case BEFORE:
 
154
    case AFTER:
 
155
        return toElement(object->generatingNode());
 
156
    default:
 
157
        ASSERT_NOT_REACHED();
 
158
        return 0;
 
159
    }
 
160
}
 
161
 
 
162
static inline bool areRenderersElementsSiblings(RenderObject* first, RenderObject* second)
 
163
{
 
164
    return parentElement(first) == parentElement(second);
 
165
}
 
166
 
 
167
// This function processes the renderer tree in the order of the DOM tree
 
168
// including pseudo elements as defined in CSS 2.1.
 
169
// Anonymous renderers are skipped except for those representing pseudo elements.
 
170
static RenderObject* nextInPreOrder(const RenderObject* object, const Element* stayWithin, bool skipDescendants = false)
 
171
{
 
172
    Element* self;
 
173
    Element* child;
 
174
    RenderObject* result;
 
175
    self = toElement(object->generatingNode());
 
176
    if (skipDescendants)
 
177
        goto nextsibling;
 
178
    switch (object->style()->styleType()) {
 
179
    case NOPSEUDO:
 
180
        ASSERT(!object->isAnonymous());
 
181
        result = object->beforePseudoElementRenderer();
 
182
        if (result)
 
183
            return result;
 
184
        break;
 
185
    case BEFORE:
 
186
        break;
 
187
    case AFTER:
 
188
        goto nextsibling;
 
189
    default:
 
190
        ASSERT_NOT_REACHED();
 
191
        return 0;
 
192
    }
 
193
    child = self->firstElementChild();
 
194
    while (true) {
 
195
        while (child) {
 
196
            result = child->renderer();
 
197
            if (result)
 
198
                return result;
 
199
            child = child->nextElementSibling();
 
200
        }
 
201
        result = rendererOfAfterPseudoElement(self->renderer());
 
202
        if (result)
 
203
            return result;
 
204
nextsibling:
 
205
        if (self == stayWithin)
 
206
            return 0;
 
207
        child = self->nextElementSibling();
 
208
        self = self->parentElement();
 
209
        if (!self) {
 
210
            ASSERT(!child); // We can only reach this if we are searching beyond the root element
 
211
            return 0; //  which cannot have siblings
 
212
        }
 
213
    }
 
214
}
 
215
 
 
216
static bool planCounter(RenderObject* object, const AtomicString& identifier, bool& isReset, int& value)
 
217
{
 
218
    ASSERT(object);
 
219
 
 
220
    // Real text nodes don't have their own style so they can't have counters.
 
221
    // We can't even look at their styles or we'll see extra resets and increments!
 
222
    if (object->isText() && !object->isBR())
 
223
        return false;
 
224
    Node* generatingNode = object->generatingNode();
 
225
    // We must have a generating node or else we cannot have a counter.
 
226
    if (!generatingNode)
 
227
        return false;
 
228
    RenderStyle* style = object->style();
 
229
    ASSERT(style);
 
230
 
 
231
    switch (style->styleType()) {
 
232
    case NOPSEUDO:
 
233
        // Sometimes nodes have more then one renderer. Only the first one gets the counter
 
234
        // LayoutTests/http/tests/css/counter-crash.html
 
235
        if (generatingNode->renderer() != object)
 
236
            return false;
 
237
        break;
 
238
    case BEFORE:
 
239
    case AFTER:
 
240
        break;
 
241
    default:
 
242
        return false; // Counters are forbidden from all other pseudo elements.
 
243
    }
 
244
 
 
245
    const CounterDirectives directives = style->getCounterDirectives(identifier);
 
246
    if (directives.isDefined()) {
 
247
        value = directives.combinedValue();
 
248
        isReset = directives.isReset();
 
249
        return true;
 
250
    }
 
251
 
 
252
    if (identifier == "list-item") {
 
253
        if (object->isListItem()) {
 
254
            if (toRenderListItem(object)->hasExplicitValue()) {
 
255
                value = toRenderListItem(object)->explicitValue();
 
256
                isReset = true;
 
257
                return true;
 
258
            }
 
259
            value = 1;
 
260
            isReset = false;
 
261
            return true;
 
262
        }
 
263
        if (Node* e = object->node()) {
 
264
            if (e->hasTagName(olTag)) {
 
265
                value = static_cast<HTMLOListElement*>(e)->start();
 
266
                isReset = true;
 
267
                return true;
 
268
            }
 
269
            if (e->hasTagName(ulTag) || e->hasTagName(menuTag) || e->hasTagName(dirTag)) {
 
270
                value = 0;
 
271
                isReset = true;
 
272
                return true;
 
273
            }
 
274
        }
 
275
    }
 
276
 
 
277
    return false;
 
278
}
 
279
 
 
280
// - Finds the insertion point for the counter described by counterOwner, isReset and 
 
281
// identifier in the CounterNode tree for identifier and sets parent and
 
282
// previousSibling accordingly.
 
283
// - The function returns true if the counter whose insertion point is searched is NOT
 
284
// the root of the tree.
 
285
// - The root of the tree is a counter reference that is not in the scope of any other
 
286
// counter with the same identifier.
 
287
// - All the counter references with the same identifier as this one that are in
 
288
// children or subsequent siblings of the renderer that owns the root of the tree
 
289
// form the rest of of the nodes of the tree.
 
290
// - The root of the tree is always a reset type reference.
 
291
// - A subtree rooted at any reset node in the tree is equivalent to all counter 
 
292
// references that are in the scope of the counter or nested counter defined by that
 
293
// reset node.
 
294
// - Non-reset CounterNodes cannot have descendants.
 
295
 
 
296
static bool findPlaceForCounter(RenderObject* counterOwner, const AtomicString& identifier, bool isReset, RefPtr<CounterNode>& parent, RefPtr<CounterNode>& previousSibling)
 
297
{
 
298
    // We cannot stop searching for counters with the same identifier before we also
 
299
    // check this renderer, because it may affect the positioning in the tree of our counter.
 
300
    RenderObject* searchEndRenderer = previousSiblingOrParent(counterOwner);
 
301
    // We check renderers in preOrder from the renderer that our counter is attached to
 
302
    // towards the begining of the document for counters with the same identifier as the one
 
303
    // we are trying to find a place for. This is the next renderer to be checked.
 
304
    RenderObject* currentRenderer = previousInPreOrder(counterOwner);
 
305
    previousSibling = 0;
 
306
    RefPtr<CounterNode> previousSiblingProtector = 0;
 
307
 
 
308
    while (currentRenderer) {
 
309
        CounterNode* currentCounter = makeCounterNode(currentRenderer, identifier, false);
 
310
        if (searchEndRenderer == currentRenderer) {
 
311
            // We may be at the end of our search.
 
312
            if (currentCounter) {
 
313
                // We have a suitable counter on the EndSearchRenderer.
 
314
                if (previousSiblingProtector) { // But we already found another counter that we come after.
 
315
                    if (currentCounter->actsAsReset()) {
 
316
                        // We found a reset counter that is on a renderer that is a sibling of ours or a parent.
 
317
                        if (isReset && areRenderersElementsSiblings(currentRenderer, counterOwner)) {
 
318
                            // We are also a reset counter and the previous reset was on a sibling renderer
 
319
                            // hence we are the next sibling of that counter if that reset is not a root or
 
320
                            // we are a root node if that reset is a root.
 
321
                            parent = currentCounter->parent();
 
322
                            previousSibling = parent ? currentCounter : 0;
 
323
                            return parent;
 
324
                        }
 
325
                        // We are not a reset node or the previous reset must be on an ancestor of our owner renderer
 
326
                        // hence we must be a child of that reset counter.
 
327
                        parent = currentCounter;
 
328
                        // In some cases renders can be reparented (ex. nodes inside a table but not in a column or row).
 
329
                        // In these cases the identified previousSibling will be invalid as its parent is different from
 
330
                        // our identified parent.
 
331
                        if (previousSiblingProtector->parent() != currentCounter)
 
332
                            previousSiblingProtector = 0;
 
333
 
 
334
                        previousSibling = previousSiblingProtector.get();
 
335
                        return true;
 
336
                    }
 
337
                    // CurrentCounter, the counter at the EndSearchRenderer, is not reset.
 
338
                    if (!isReset || !areRenderersElementsSiblings(currentRenderer, counterOwner)) {
 
339
                        // If the node we are placing is not reset or we have found a counter that is attached
 
340
                        // to an ancestor of the placed counter's owner renderer we know we are a sibling of that node.
 
341
                        if (currentCounter->parent() != previousSiblingProtector->parent())
 
342
                            return false;
 
343
 
 
344
                        parent = currentCounter->parent();
 
345
                        previousSibling = previousSiblingProtector.get();
 
346
                        return true;
 
347
                    }
 
348
                } else { 
 
349
                    // We are at the potential end of the search, but we had no previous sibling candidate
 
350
                    // In this case we follow pretty much the same logic as above but no ASSERTs about 
 
351
                    // previousSibling, and when we are a sibling of the end counter we must set previousSibling
 
352
                    // to currentCounter.
 
353
                    if (currentCounter->actsAsReset()) {
 
354
                        if (isReset && areRenderersElementsSiblings(currentRenderer, counterOwner)) {
 
355
                            parent = currentCounter->parent();
 
356
                            previousSibling = currentCounter;
 
357
                            return parent;
 
358
                        }
 
359
                        parent = currentCounter;
 
360
                        previousSibling = previousSiblingProtector.get();
 
361
                        return true;
 
362
                    }
 
363
                    if (!isReset || !areRenderersElementsSiblings(currentRenderer, counterOwner)) {
 
364
                        parent = currentCounter->parent();
 
365
                        previousSibling = currentCounter;
 
366
                        return true;
 
367
                    }
 
368
                    previousSiblingProtector = currentCounter;
 
369
                }
 
370
            }
 
371
            // We come here if the previous sibling or parent of our owner renderer had no
 
372
            // good counter, or we are a reset node and the counter on the previous sibling
 
373
            // of our owner renderer was not a reset counter.
 
374
            // Set a new goal for the end of the search.
 
375
            searchEndRenderer = previousSiblingOrParent(currentRenderer);
 
376
        } else {
 
377
            // We are searching descendants of a previous sibling of the renderer that the
 
378
            // counter being placed is attached to.
 
379
            if (currentCounter) {
 
380
                // We found a suitable counter.
 
381
                if (previousSiblingProtector) {
 
382
                    // Since we had a suitable previous counter before, we should only consider this one as our 
 
383
                    // previousSibling if it is a reset counter and hence the current previousSibling is its child.
 
384
                    if (currentCounter->actsAsReset()) {
 
385
                        previousSiblingProtector = currentCounter;
 
386
                        // We are no longer interested in previous siblings of the currentRenderer or their children
 
387
                        // as counters they may have attached cannot be the previous sibling of the counter we are placing.
 
388
                        currentRenderer = parentElement(currentRenderer)->renderer();
 
389
                        continue;
 
390
                    }
 
391
                } else
 
392
                    previousSiblingProtector = currentCounter;
 
393
                currentRenderer = previousSiblingOrParent(currentRenderer);
 
394
                continue;
 
395
            }
 
396
        }
 
397
        // This function is designed so that the same test is not done twice in an iteration, except for this one
 
398
        // which may be done twice in some cases. Rearranging the decision points though, to accommodate this 
 
399
        // performance improvement would create more code duplication than is worthwhile in my oppinion and may further
 
400
        // impede the readability of this already complex algorithm.
 
401
        if (previousSiblingProtector)
 
402
            currentRenderer = previousSiblingOrParent(currentRenderer);
 
403
        else
 
404
            currentRenderer = previousInPreOrder(currentRenderer);
 
405
    }
 
406
    return false;
 
407
}
 
408
 
 
409
static CounterNode* makeCounterNode(RenderObject* object, const AtomicString& identifier, bool alwaysCreateCounter)
 
410
{
 
411
    ASSERT(object);
 
412
 
 
413
    if (object->hasCounterNodeMap()) {
 
414
        if (CounterMap* nodeMap = counterMaps().get(object)) {
 
415
            if (CounterNode* node = nodeMap->get(identifier).get())
 
416
                return node;
 
417
        }
 
418
    }
 
419
 
 
420
    bool isReset = false;
 
421
    int value = 0;
 
422
    if (!planCounter(object, identifier, isReset, value) && !alwaysCreateCounter)
 
423
        return 0;
 
424
 
 
425
    RefPtr<CounterNode> newParent = 0;
 
426
    RefPtr<CounterNode> newPreviousSibling = 0;
 
427
    RefPtr<CounterNode> newNode = CounterNode::create(object, isReset, value);
 
428
    if (findPlaceForCounter(object, identifier, isReset, newParent, newPreviousSibling))
 
429
        newParent->insertAfter(newNode.get(), newPreviousSibling.get(), identifier);
 
430
    CounterMap* nodeMap;
 
431
    if (object->hasCounterNodeMap())
 
432
        nodeMap = counterMaps().get(object);
 
433
    else {
 
434
        nodeMap = new CounterMap;
 
435
        counterMaps().set(object, adoptPtr(nodeMap));
 
436
        object->setHasCounterNodeMap(true);
 
437
    }
 
438
    nodeMap->set(identifier, newNode);
 
439
    if (newNode->parent())
 
440
        return newNode.get();
 
441
    // Checking if some nodes that were previously counter tree root nodes
 
442
    // should become children of this node now.
 
443
    CounterMaps& maps = counterMaps();
 
444
    Element* stayWithin = parentElement(object);
 
445
    bool skipDescendants;
 
446
    for (RenderObject* currentRenderer = nextInPreOrder(object, stayWithin); currentRenderer; currentRenderer = nextInPreOrder(currentRenderer, stayWithin, skipDescendants)) {
 
447
        skipDescendants = false;
 
448
        if (!currentRenderer->hasCounterNodeMap())
 
449
            continue;
 
450
        CounterNode* currentCounter = maps.get(currentRenderer)->get(identifier).get();
 
451
        if (!currentCounter)
 
452
            continue;
 
453
        skipDescendants = true;
 
454
        if (currentCounter->parent())
 
455
            continue;
 
456
        if (stayWithin == parentElement(currentRenderer) && currentCounter->hasResetType())
 
457
            break;
 
458
        newNode->insertAfter(currentCounter, newNode->lastChild(), identifier);
 
459
    }
 
460
    return newNode.get();
 
461
}
 
462
 
 
463
RenderCounter::RenderCounter(Document* node, const CounterContent& counter)
 
464
    : RenderText(node, StringImpl::empty())
 
465
    , m_counter(counter)
 
466
    , m_counterNode(0)
 
467
    , m_nextForSameCounter(0)
 
468
{
 
469
    view()->addRenderCounter();
 
470
}
 
471
 
 
472
RenderCounter::~RenderCounter()
 
473
{
 
474
    if (m_counterNode) {
 
475
        m_counterNode->removeRenderer(this);
 
476
        ASSERT(!m_counterNode);
 
477
    }
 
478
}
 
479
 
 
480
void RenderCounter::willBeDestroyed()
 
481
{
 
482
    if (view())
 
483
        view()->removeRenderCounter();
 
484
    RenderText::willBeDestroyed();
 
485
}
 
486
 
 
487
const char* RenderCounter::renderName() const
 
488
{
 
489
    return "RenderCounter";
 
490
}
 
491
 
 
492
bool RenderCounter::isCounter() const
 
493
{
 
494
    return true;
 
495
}
 
496
 
 
497
PassRefPtr<StringImpl> RenderCounter::originalText() const
 
498
{
 
499
    if (!m_counterNode) {
 
500
        RenderObject* beforeAfterContainer = parent();
 
501
        while (true) {
 
502
            if (!beforeAfterContainer)
 
503
                return 0;
 
504
            if (!beforeAfterContainer->isAnonymous())
 
505
                return 0; // RenderCounters are restricted to before and after pseudo elements
 
506
            PseudoId containerStyle = beforeAfterContainer->style()->styleType();
 
507
            if ((containerStyle == BEFORE) || (containerStyle == AFTER))
 
508
                break;
 
509
            beforeAfterContainer = beforeAfterContainer->parent();
 
510
        }
 
511
        makeCounterNode(beforeAfterContainer, m_counter.identifier(), true)->addRenderer(const_cast<RenderCounter*>(this));
 
512
        ASSERT(m_counterNode);
 
513
    }
 
514
    CounterNode* child = m_counterNode;
 
515
    int value = child->actsAsReset() ? child->value() : child->countInParent();
 
516
 
 
517
    String text = listMarkerText(m_counter.listStyle(), value);
 
518
 
 
519
    if (!m_counter.separator().isNull()) {
 
520
        if (!child->actsAsReset())
 
521
            child = child->parent();
 
522
        while (CounterNode* parent = child->parent()) {
 
523
            text = listMarkerText(m_counter.listStyle(), child->countInParent())
 
524
                + m_counter.separator() + text;
 
525
            child = parent;
 
526
        }
 
527
    }
 
528
 
 
529
    return text.impl();
 
530
}
 
531
 
 
532
void RenderCounter::updateText()
 
533
{
 
534
    computePreferredLogicalWidths(0);
 
535
}
 
536
 
 
537
void RenderCounter::computePreferredLogicalWidths(float lead)
 
538
{
 
539
    setTextInternal(originalText());
 
540
    RenderText::computePreferredLogicalWidths(lead);
 
541
}
 
542
 
 
543
void RenderCounter::invalidate()
 
544
{
 
545
    m_counterNode->removeRenderer(this);
 
546
    ASSERT(!m_counterNode);
 
547
    if (documentBeingDestroyed())
 
548
        return;
 
549
    setNeedsLayoutAndPrefWidthsRecalc();
 
550
}
 
551
 
 
552
static void destroyCounterNodeWithoutMapRemoval(const AtomicString& identifier, CounterNode* node)
 
553
{
 
554
    CounterNode* previous;
 
555
    for (RefPtr<CounterNode> child = node->lastDescendant(); child && child != node; child = previous) {
 
556
        previous = child->previousInPreOrder();
 
557
        child->parent()->removeChild(child.get());
 
558
        ASSERT(counterMaps().get(child->owner())->get(identifier) == child);
 
559
        counterMaps().get(child->owner())->remove(identifier);
 
560
    }
 
561
    if (CounterNode* parent = node->parent())
 
562
        parent->removeChild(node);
 
563
}
 
564
 
 
565
void RenderCounter::destroyCounterNodes(RenderObject* owner)
 
566
{
 
567
    CounterMaps& maps = counterMaps();
 
568
    CounterMaps::iterator mapsIterator = maps.find(owner);
 
569
    if (mapsIterator == maps.end())
 
570
        return;
 
571
    CounterMap* map = mapsIterator->value.get();
 
572
    CounterMap::const_iterator end = map->end();
 
573
    for (CounterMap::const_iterator it = map->begin(); it != end; ++it) {
 
574
        destroyCounterNodeWithoutMapRemoval(it->key, it->value.get());
 
575
    }
 
576
    maps.remove(mapsIterator);
 
577
    owner->setHasCounterNodeMap(false);
 
578
}
 
579
 
 
580
void RenderCounter::destroyCounterNode(RenderObject* owner, const AtomicString& identifier)
 
581
{
 
582
    CounterMap* map = counterMaps().get(owner);
 
583
    if (!map)
 
584
        return;
 
585
    CounterMap::iterator mapIterator = map->find(identifier);
 
586
    if (mapIterator == map->end())
 
587
        return;
 
588
    destroyCounterNodeWithoutMapRemoval(identifier, mapIterator->value.get());
 
589
    map->remove(mapIterator);
 
590
    // We do not delete "map" here even if empty because we expect to reuse
 
591
    // it soon. In order for a renderer to lose all its counters permanently,
 
592
    // a style change for the renderer involving removal of all counter
 
593
    // directives must occur, in which case, RenderCounter::destroyCounterNodes()
 
594
    // must be called.
 
595
    // The destruction of the Renderer (possibly caused by the removal of its 
 
596
    // associated DOM node) is the other case that leads to the permanent
 
597
    // destruction of all counters attached to a Renderer. In this case
 
598
    // RenderCounter::destroyCounterNodes() must be and is now called, too.
 
599
    // RenderCounter::destroyCounterNodes() handles destruction of the counter
 
600
    // map associated with a renderer, so there is no risk in leaking the map.
 
601
}
 
602
 
 
603
void RenderCounter::rendererRemovedFromTree(RenderObject* renderer)
 
604
{
 
605
    ASSERT(renderer->view());
 
606
    if (!renderer->view()->hasRenderCounters())
 
607
        return;
 
608
    RenderObject* currentRenderer = renderer->lastLeafChild();
 
609
    if (!currentRenderer)
 
610
        currentRenderer = renderer;
 
611
    while (true) {
 
612
        destroyCounterNodes(currentRenderer);
 
613
        if (currentRenderer == renderer)
 
614
            break;
 
615
        currentRenderer = currentRenderer->previousInPreOrder();
 
616
    }
 
617
}
 
618
 
 
619
static void updateCounters(RenderObject* renderer)
 
620
{
 
621
    ASSERT(renderer->style());
 
622
    const CounterDirectiveMap* directiveMap = renderer->style()->counterDirectives();
 
623
    if (!directiveMap)
 
624
        return;
 
625
    CounterDirectiveMap::const_iterator end = directiveMap->end();
 
626
    if (!renderer->hasCounterNodeMap()) {
 
627
        for (CounterDirectiveMap::const_iterator it = directiveMap->begin(); it != end; ++it)
 
628
            makeCounterNode(renderer, it->key, false);
 
629
        return;
 
630
    }
 
631
    CounterMap* counterMap = counterMaps().get(renderer);
 
632
    ASSERT(counterMap);
 
633
    for (CounterDirectiveMap::const_iterator it = directiveMap->begin(); it != end; ++it) {
 
634
        RefPtr<CounterNode> node = counterMap->get(it->key);
 
635
        if (!node) {
 
636
            makeCounterNode(renderer, it->key, false);
 
637
            continue;
 
638
        }
 
639
        RefPtr<CounterNode> newParent = 0;
 
640
        RefPtr<CounterNode> newPreviousSibling = 0;
 
641
        
 
642
        findPlaceForCounter(renderer, it->key, node->hasResetType(), newParent, newPreviousSibling);
 
643
        if (node != counterMap->get(it->key))
 
644
            continue;
 
645
        CounterNode* parent = node->parent();
 
646
        if (newParent == parent && newPreviousSibling == node->previousSibling())
 
647
            continue;
 
648
        if (parent)
 
649
            parent->removeChild(node.get());
 
650
        if (newParent)
 
651
            newParent->insertAfter(node.get(), newPreviousSibling.get(), it->key);
 
652
    }
 
653
}
 
654
 
 
655
void RenderCounter::rendererSubtreeAttached(RenderObject* renderer)
 
656
{
 
657
    ASSERT(renderer->view());
 
658
    if (!renderer->view()->hasRenderCounters())
 
659
        return;
 
660
    Node* node = renderer->node();
 
661
    if (node)
 
662
        node = node->parentNode();
 
663
    else
 
664
        node = renderer->generatingNode();
 
665
    if (node && !node->attached())
 
666
        return; // No need to update if the parent is not attached yet
 
667
    for (RenderObject* descendant = renderer; descendant; descendant = descendant->nextInPreOrder(renderer))
 
668
        updateCounters(descendant);
 
669
}
 
670
 
 
671
void RenderCounter::rendererStyleChanged(RenderObject* renderer, const RenderStyle* oldStyle, const RenderStyle* newStyle)
 
672
{
 
673
    Node* node = renderer->generatingNode();
 
674
    if (!node || !node->attached())
 
675
        return; // cannot have generated content or if it can have, it will be handled during attaching
 
676
    const CounterDirectiveMap* newCounterDirectives;
 
677
    const CounterDirectiveMap* oldCounterDirectives;
 
678
    if (oldStyle && (oldCounterDirectives = oldStyle->counterDirectives())) {
 
679
        if (newStyle && (newCounterDirectives = newStyle->counterDirectives())) {
 
680
            CounterDirectiveMap::const_iterator newMapEnd = newCounterDirectives->end();
 
681
            CounterDirectiveMap::const_iterator oldMapEnd = oldCounterDirectives->end();
 
682
            for (CounterDirectiveMap::const_iterator it = newCounterDirectives->begin(); it != newMapEnd; ++it) {
 
683
                CounterDirectiveMap::const_iterator oldMapIt = oldCounterDirectives->find(it->key);
 
684
                if (oldMapIt != oldMapEnd) {
 
685
                    if (oldMapIt->value == it->value)
 
686
                        continue;
 
687
                    RenderCounter::destroyCounterNode(renderer, it->key);
 
688
                }
 
689
                // We must create this node here, because the changed node may be a node with no display such as
 
690
                // as those created by the increment or reset directives and the re-layout that will happen will
 
691
                // not catch the change if the node had no children.
 
692
                makeCounterNode(renderer, it->key, false);
 
693
            }
 
694
            // Destroying old counters that do not exist in the new counterDirective map.
 
695
            for (CounterDirectiveMap::const_iterator it = oldCounterDirectives->begin(); it !=oldMapEnd; ++it) {
 
696
                if (!newCounterDirectives->contains(it->key))
 
697
                    RenderCounter::destroyCounterNode(renderer, it->key);
 
698
            }
 
699
        } else {
 
700
            if (renderer->hasCounterNodeMap())
 
701
                RenderCounter::destroyCounterNodes(renderer);
 
702
        }
 
703
    } else if (newStyle && (newCounterDirectives = newStyle->counterDirectives())) {
 
704
        CounterDirectiveMap::const_iterator newMapEnd = newCounterDirectives->end();
 
705
        for (CounterDirectiveMap::const_iterator it = newCounterDirectives->begin(); it != newMapEnd; ++it) {
 
706
            // We must create this node here, because the added node may be a node with no display such as
 
707
            // as those created by the increment or reset directives and the re-layout that will happen will
 
708
            // not catch the change if the node had no children.
 
709
            makeCounterNode(renderer, it->key, false);
 
710
        }
 
711
    }
 
712
}
 
713
 
 
714
} // namespace WebCore
 
715
 
 
716
#ifndef NDEBUG
 
717
 
 
718
void showCounterRendererTree(const WebCore::RenderObject* renderer, const char* counterName)
 
719
{
 
720
    if (!renderer)
 
721
        return;
 
722
    const WebCore::RenderObject* root = renderer;
 
723
    while (root->parent())
 
724
        root = root->parent();
 
725
 
 
726
    AtomicString identifier(counterName);
 
727
    for (const WebCore::RenderObject* current = root; current; current = current->nextInPreOrder()) {
 
728
        fprintf(stderr, "%c", (current == renderer) ? '*' : ' ');
 
729
        for (const WebCore::RenderObject* parent = current; parent && parent != root; parent = parent->parent())
 
730
            fprintf(stderr, "    ");
 
731
        fprintf(stderr, "%p N:%p P:%p PS:%p NS:%p C:%p\n",
 
732
            current, current->node(), current->parent(), current->previousSibling(),
 
733
            current->nextSibling(), current->hasCounterNodeMap() ?
 
734
            counterName ? WebCore::counterMaps().get(current)->get(identifier).get() : (WebCore::CounterNode*)1 : (WebCore::CounterNode*)0);
 
735
    }
 
736
    fflush(stderr);
 
737
}
 
738
 
 
739
#endif // NDEBUG