tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / rendering / RenderObjectChildList.cpp
1 /*
2  * Copyright (C) 2009, 2010 Apple Inc. All rights reserved.
3  * Copyright (C) Research In Motion Limited 2010. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
25  */
26
27 #include "config.h"
28 #include "RenderObjectChildList.h"
29
30 #include "AXObjectCache.h"
31 #include "ContentData.h"
32 #include "RenderBlock.h"
33 #include "RenderCounter.h"
34 #include "RenderFlowThread.h"
35 #include "RenderImage.h"
36 #include "RenderImageResourceStyleImage.h"
37 #include "RenderInline.h"
38 #include "RenderLayer.h"
39 #include "RenderListItem.h"
40 #include "RenderQuote.h"
41 #include "RenderRegion.h"
42 #include "RenderStyle.h"
43 #include "RenderTextFragment.h"
44 #include "RenderView.h"
45
46 namespace WebCore {
47
48 void RenderObjectChildList::destroyLeftoverChildren()
49 {
50     while (firstChild()) {
51         if (firstChild()->isListMarker() || (firstChild()->style()->styleType() == FIRST_LETTER && !firstChild()->isText()))
52             firstChild()->remove();  // List markers are owned by their enclosing list and so don't get destroyed by this container. Similarly, first letters are destroyed by their remaining text fragment.
53         else if (firstChild()->isRunIn() && firstChild()->node()) {
54             firstChild()->node()->setRenderer(0);
55             firstChild()->node()->setNeedsStyleRecalc();
56             firstChild()->destroy();
57         } else {
58             // Destroy any anonymous children remaining in the render tree, as well as implicit (shadow) DOM elements like those used in the engine-based text fields.
59             if (firstChild()->node())
60                 firstChild()->node()->setRenderer(0);
61             firstChild()->destroy();
62         }
63     }
64 }
65
66 static RenderFlowThread* renderFlowThreadContainer(RenderObject* object)
67 {
68     while (object && object->isAnonymousBlock() && !object->isRenderFlowThread())
69         object = object->parent();
70
71     return object && object->isRenderFlowThread() ? toRenderFlowThread(object) : 0;
72 }
73
74 RenderObject* RenderObjectChildList::removeChildNode(RenderObject* owner, RenderObject* oldChild, bool fullRemove)
75 {
76     ASSERT(oldChild->parent() == owner);
77
78     // So that we'll get the appropriate dirty bit set (either that a normal flow child got yanked or
79     // that a positioned child got yanked).  We also repaint, so that the area exposed when the child
80     // disappears gets repainted properly.
81     if (!owner->documentBeingDestroyed() && fullRemove && oldChild->m_everHadLayout) {
82         oldChild->setNeedsLayoutAndPrefWidthsRecalc();
83         if (oldChild->isBody())
84             owner->view()->repaint();
85         else
86             oldChild->repaint();
87     }
88
89     // If we have a line box wrapper, delete it.
90     if (oldChild->isBox())
91         toRenderBox(oldChild)->deleteLineBoxWrapper();
92
93     if (!owner->documentBeingDestroyed() && fullRemove) {
94         // if we remove visible child from an invisible parent, we don't know the layer visibility any more
95         RenderLayer* layer = 0;
96         if (owner->style()->visibility() != VISIBLE && oldChild->style()->visibility() == VISIBLE && !oldChild->hasLayer()) {
97             if ((layer = owner->enclosingLayer()))
98                 layer->dirtyVisibleContentStatus();
99         }
100
101          // Keep our layer hierarchy updated.
102         if (oldChild->firstChild() || oldChild->hasLayer()) {
103             if (!layer)
104                 layer = owner->enclosingLayer();
105             oldChild->removeLayers(layer);
106         }
107
108         if (oldChild->isListItem())
109             toRenderListItem(oldChild)->updateListMarkerNumbers();
110
111         if (oldChild->isPositioned() && owner->childrenInline())
112             owner->dirtyLinesFromChangedChild(oldChild);
113
114         if (oldChild->isRenderRegion())
115             toRenderRegion(oldChild)->detachRegion();
116
117         if (oldChild->inRenderFlowThread() && oldChild->isBox())
118             oldChild->enclosingRenderFlowThread()->removeRenderBoxRegionInfo(toRenderBox(oldChild));
119
120         if (RenderFlowThread* containerFlowThread = renderFlowThreadContainer(owner))
121             containerFlowThread->removeFlowChild(oldChild);
122
123 #if ENABLE(SVG)
124         // Update cached boundaries in SVG renderers, if a child is removed.
125         owner->setNeedsBoundariesUpdate();
126 #endif
127     }
128     
129     // If oldChild is the start or end of the selection, then clear the selection to
130     // avoid problems of invalid pointers.
131     // FIXME: The FrameSelection should be responsible for this when it
132     // is notified of DOM mutations.
133     if (!owner->documentBeingDestroyed() && oldChild->isSelectionBorder())
134         owner->view()->clearSelection();
135
136     // remove the child
137     if (oldChild->previousSibling())
138         oldChild->previousSibling()->setNextSibling(oldChild->nextSibling());
139     if (oldChild->nextSibling())
140         oldChild->nextSibling()->setPreviousSibling(oldChild->previousSibling());
141
142     if (firstChild() == oldChild)
143         setFirstChild(oldChild->nextSibling());
144     if (lastChild() == oldChild)
145         setLastChild(oldChild->previousSibling());
146
147     oldChild->setPreviousSibling(0);
148     oldChild->setNextSibling(0);
149     oldChild->setParent(0);
150
151     RenderCounter::rendererRemovedFromTree(oldChild);
152     RenderQuote::rendererRemovedFromTree(oldChild);
153
154     if (AXObjectCache::accessibilityEnabled())
155         owner->document()->axObjectCache()->childrenChanged(owner);
156
157     return oldChild;
158 }
159
160 void RenderObjectChildList::appendChildNode(RenderObject* owner, RenderObject* newChild, bool fullAppend)
161 {
162     ASSERT(newChild->parent() == 0);
163     ASSERT(!owner->isBlockFlow() || (!newChild->isTableSection() && !newChild->isTableRow() && !newChild->isTableCell()));
164
165     newChild->setParent(owner);
166     RenderObject* lChild = lastChild();
167
168     if (lChild) {
169         newChild->setPreviousSibling(lChild);
170         lChild->setNextSibling(newChild);
171     } else
172         setFirstChild(newChild);
173
174     setLastChild(newChild);
175     
176     if (fullAppend) {
177         // Keep our layer hierarchy updated.  Optimize for the common case where we don't have any children
178         // and don't have a layer attached to ourselves.
179         RenderLayer* layer = 0;
180         if (newChild->firstChild() || newChild->hasLayer()) {
181             layer = owner->enclosingLayer();
182             newChild->addLayers(layer);
183         }
184
185         // if the new child is visible but this object was not, tell the layer it has some visible content
186         // that needs to be drawn and layer visibility optimization can't be used
187         if (owner->style()->visibility() != VISIBLE && newChild->style()->visibility() == VISIBLE && !newChild->hasLayer()) {
188             if (!layer)
189                 layer = owner->enclosingLayer();
190             if (layer)
191                 layer->setHasVisibleContent(true);
192         }
193
194         if (newChild->isListItem())
195             toRenderListItem(newChild)->updateListMarkerNumbers();
196
197         if (!newChild->isFloating() && owner->childrenInline())
198             owner->dirtyLinesFromChangedChild(newChild);
199
200         if (newChild->isRenderRegion())
201             toRenderRegion(newChild)->attachRegion();
202
203         if (RenderFlowThread* containerFlowThread = renderFlowThreadContainer(owner))
204             containerFlowThread->addFlowChild(newChild);
205     }
206     RenderCounter::rendererSubtreeAttached(newChild);
207     RenderQuote::rendererSubtreeAttached(newChild);
208     newChild->setNeedsLayoutAndPrefWidthsRecalc(); // Goes up the containing block hierarchy.
209     if (!owner->normalChildNeedsLayout())
210         owner->setChildNeedsLayout(true); // We may supply the static position for an absolute positioned child.
211     
212     if (AXObjectCache::accessibilityEnabled())
213         owner->document()->axObjectCache()->childrenChanged(owner);
214 }
215
216 void RenderObjectChildList::insertChildNode(RenderObject* owner, RenderObject* child, RenderObject* beforeChild, bool fullInsert)
217 {
218     if (!beforeChild) {
219         appendChildNode(owner, child, fullInsert);
220         return;
221     }
222
223     ASSERT(!child->parent());
224     while (beforeChild->parent() != owner && beforeChild->parent()->isAnonymousBlock())
225         beforeChild = beforeChild->parent();
226     ASSERT(beforeChild->parent() == owner);
227
228     ASSERT(!owner->isBlockFlow() || (!child->isTableSection() && !child->isTableRow() && !child->isTableCell()));
229
230     if (beforeChild == firstChild())
231         setFirstChild(child);
232
233     RenderObject* prev = beforeChild->previousSibling();
234     child->setNextSibling(beforeChild);
235     beforeChild->setPreviousSibling(child);
236     if (prev)
237         prev->setNextSibling(child);
238     child->setPreviousSibling(prev);
239
240     child->setParent(owner);
241     
242     if (fullInsert) {
243         // Keep our layer hierarchy updated.  Optimize for the common case where we don't have any children
244         // and don't have a layer attached to ourselves.
245         RenderLayer* layer = 0;
246         if (child->firstChild() || child->hasLayer()) {
247             layer = owner->enclosingLayer();
248             child->addLayers(layer);
249         }
250
251         // if the new child is visible but this object was not, tell the layer it has some visible content
252         // that needs to be drawn and layer visibility optimization can't be used
253         if (owner->style()->visibility() != VISIBLE && child->style()->visibility() == VISIBLE && !child->hasLayer()) {
254             if (!layer)
255                 layer = owner->enclosingLayer();
256             if (layer)
257                 layer->setHasVisibleContent(true);
258         }
259
260         if (child->isListItem())
261             toRenderListItem(child)->updateListMarkerNumbers();
262
263         if (!child->isFloating() && owner->childrenInline())
264             owner->dirtyLinesFromChangedChild(child);
265
266         if (child->isRenderRegion())
267             toRenderRegion(child)->attachRegion();
268
269         if (RenderFlowThread* containerFlowThread = renderFlowThreadContainer(owner))
270             containerFlowThread->addFlowChild(child, beforeChild);
271     }
272
273     RenderCounter::rendererSubtreeAttached(child);
274     RenderQuote::rendererSubtreeAttached(child);
275     child->setNeedsLayoutAndPrefWidthsRecalc();
276     if (!owner->normalChildNeedsLayout())
277         owner->setChildNeedsLayout(true); // We may supply the static position for an absolute positioned child.
278     
279     if (AXObjectCache::accessibilityEnabled())
280         owner->document()->axObjectCache()->childrenChanged(owner);
281 }
282
283 static RenderObject* findBeforeAfterParent(RenderObject* object)
284 {
285     // Only table parts need to search for the :before or :after parent
286     if (!(object->isTable() || object->isTableSection() || object->isTableRow()))
287         return object;
288
289     // If there is a :first-letter style applied on the :before or :after content,
290     // then we want the parent of the first-letter block
291     RenderObject* beforeAfterParent = object;
292     while (beforeAfterParent && !(beforeAfterParent->isText() || beforeAfterParent->isImage())
293         && (beforeAfterParent->style()->styleType() != FIRST_LETTER))
294         beforeAfterParent = beforeAfterParent->firstChild();
295
296     return beforeAfterParent ? beforeAfterParent->parent() : 0;
297 }
298
299 RenderObject* RenderObjectChildList::beforePseudoElementRenderer(const RenderObject* owner) const
300 {
301     // An anonymous (generated) inline run-in that has PseudoId BEFORE must come from a grandparent.
302     // Therefore we should skip these generated run-ins when checking our immediate children.
303     // If we don't find our :before child immediately, then we should check if we own a
304     // generated inline run-in in the next level of children.
305     RenderObject* first = const_cast<RenderObject*>(owner);
306     do {
307         first = first->firstChild();
308         // Skip list markers and generated run-ins.
309         while (first && (first->isListMarker() || (first->isRenderInline() && first->isRunIn())))
310             first = first->nextInPreOrderAfterChildren(owner);
311     } while (first && first->isAnonymous() && first->style()->styleType() == NOPSEUDO);
312
313     if (!first)
314         return 0;
315
316     if (first->isBeforeContent())
317         return first;
318
319     // Check for a possible generated run-in, using run-in positioning rules.
320     first = owner->firstChild();
321     if (!first->isRenderBlock())
322         return 0;
323     
324     first = first->firstChild();
325     // We still need to skip any list markers that could exist before the run-in.
326     while (first && first->isListMarker())
327         first = first->nextSibling();
328     if (first && first->isBeforeContent() && first->isRenderInline() && first->isRunIn())
329         return first;
330     
331     return 0;
332 }
333
334 RenderObject* RenderObjectChildList::afterPseudoElementRenderer(const RenderObject* owner) const
335 {
336     RenderObject* last = const_cast<RenderObject*>(owner);
337     do {
338         last = last->lastChild();
339     } while (last && last->isAnonymous() && last->style()->styleType() == NOPSEUDO && !last->isListMarker());
340     if (last && !last->isAfterContent())
341         return 0;
342     return last;
343 }
344
345 void RenderObjectChildList::updateBeforeAfterContent(RenderObject* owner, PseudoId type, const RenderObject* styledObject)
346 {
347     // Double check that the document did in fact use generated content rules.  Otherwise we should not have been called.
348     ASSERT(owner->document()->usesBeforeAfterRules());
349
350     // In CSS2, before/after pseudo-content cannot nest.  Check this first.
351     if (owner->style()->styleType() == BEFORE || owner->style()->styleType() == AFTER)
352         return;
353     
354     if (!styledObject)
355         styledObject = owner;
356
357     RenderStyle* pseudoElementStyle = styledObject->getCachedPseudoStyle(type);
358     RenderObject* child;
359     switch (type) {
360     case BEFORE:
361         child = beforePseudoElementRenderer(owner);
362         break;
363     case AFTER:
364         child = afterPseudoElementRenderer(owner);
365         break;
366     default:
367         ASSERT_NOT_REACHED();
368         return;
369     }
370
371     // Whether or not we currently have generated content attached.
372     bool oldContentPresent = child;
373
374     // Whether or not we now want generated content.
375     bool newContentWanted = pseudoElementStyle && pseudoElementStyle->display() != NONE;
376
377     // For <q><p/></q>, if this object is the inline continuation of the <q>, we only want to generate
378     // :after content and not :before content.
379     if (newContentWanted && type == BEFORE && owner->isElementContinuation())
380         newContentWanted = false;
381
382     // Similarly, if we're the beginning of a <q>, and there's an inline continuation for our object,
383     // then we don't generate the :after content.
384     if (newContentWanted && type == AFTER && owner->virtualContinuation())
385         newContentWanted = false;
386     
387     // If we don't want generated content any longer, or if we have generated content, but it's no longer
388     // identical to the new content data we want to build render objects for, then we nuke all
389     // of the old generated content.
390     if (oldContentPresent && (!newContentWanted || Node::diff(child->style(), pseudoElementStyle) == Node::Detach)) {
391         // Nuke the child. 
392         if (child->style()->styleType() == type) {
393             oldContentPresent = false;
394             child->destroy();
395             child = (type == BEFORE) ? owner->virtualChildren()->firstChild() : owner->virtualChildren()->lastChild();
396         }
397     }
398
399     // If we have no pseudo-element style or if the pseudo-element style's display type is NONE, then we
400     // have no generated content and can now return.
401     if (!newContentWanted)
402         return;
403
404     if (owner->isRenderInline() && !pseudoElementStyle->isDisplayInlineType() && !pseudoElementStyle->isFloating() &&
405         !(pseudoElementStyle->position() == AbsolutePosition || pseudoElementStyle->position() == FixedPosition))
406         // According to the CSS2 spec (the end of section 12.1), the only allowed
407         // display values for the pseudo style are NONE and INLINE for inline flows.
408         // FIXME: CSS2.1 lifted this restriction, but block display types will crash.
409         // For now we at least relax the restriction to allow all inline types like inline-block
410         // and inline-table.
411         pseudoElementStyle->setDisplay(INLINE);
412
413     if (oldContentPresent) {
414         if (child && child->style()->styleType() == type) {
415             // We have generated content present still.  We want to walk this content and update our
416             // style information with the new pseudo-element style.
417             child->setStyle(pseudoElementStyle);
418
419             RenderObject* beforeAfterParent = findBeforeAfterParent(child);
420             if (!beforeAfterParent)
421                 return;
422
423             // When beforeAfterParent is not equal to child (e.g. in tables),
424             // we need to create new styles inheriting from pseudoElementStyle 
425             // on all the intermediate parents (leaving their display same).
426             if (beforeAfterParent != child) {
427                 RenderObject* curr = beforeAfterParent;
428                 while (curr && curr != child) {
429                     ASSERT(curr->isAnonymous());
430                     RefPtr<RenderStyle> newStyle = RenderStyle::create();
431                     newStyle->inheritFrom(pseudoElementStyle);
432                     newStyle->setDisplay(curr->style()->display());
433                     newStyle->setStyleType(curr->style()->styleType());
434                     curr->setStyle(newStyle);
435                     curr = curr->parent();
436                 }
437             }
438
439             // Note that if we ever support additional types of generated content (which should be way off
440             // in the future), this code will need to be patched.
441             for (RenderObject* genChild = beforeAfterParent->firstChild(); genChild; genChild = genChild->nextSibling()) {
442                 if (genChild->isText())
443                     // Generated text content is a child whose style also needs to be set to the pseudo-element style.
444                     genChild->setStyle(pseudoElementStyle);
445                 else if (genChild->isImage()) {
446                     // Images get an empty style that inherits from the pseudo.
447                     RefPtr<RenderStyle> style = RenderStyle::create();
448                     style->inheritFrom(pseudoElementStyle);
449                     genChild->setStyle(style.release());
450                 } else {
451                     // RenderListItem may insert a list marker here. We do not need to care about this case.
452                     // Otherwise, genChild must be a first-letter container. updateFirstLetter() will take care of it.
453                     ASSERT(genChild->isListMarker() || genChild->style()->styleType() == FIRST_LETTER);
454                 }
455             }
456         }
457         return; // We've updated the generated content. That's all we needed to do.
458     }
459     
460     RenderObject* insertBefore = (type == BEFORE) ? owner->virtualChildren()->firstChild() : 0;
461     if (insertBefore && insertBefore->isAnonymousBlock() && insertBefore->childrenInline() && !insertBefore->isEmpty()) {
462         // We are going to add the "before" element. We have to check whether the "insertBefore" element
463         // is an anonymous block with inline children. If it is, then we should insert the "before" element
464         // before the first inline child of the anonymous block, otherwise we will end up with the "before"
465         // element in a different block. We do this only when the anonymous block has children, otherwise
466         // we end up with the before element in a wrong block.
467         insertBefore = insertBefore->firstChild();
468     }
469
470     // Generated content consists of a single container that houses multiple children (specified
471     // by the content property).  This generated content container gets the pseudo-element style set on it.
472     RenderObject* generatedContentContainer = 0;
473     
474     // Walk our list of generated content and create render objects for each.
475     for (const ContentData* content = pseudoElementStyle->contentData(); content; content = content->next()) {
476         RenderObject* renderer = 0;
477         switch (content->type()) {
478             case CONTENT_NONE:
479                 break;
480             case CONTENT_TEXT:
481                 renderer = new (owner->renderArena()) RenderTextFragment(owner->document() /* anonymous object */, static_cast<const TextContentData*>(content)->text().impl());
482                 renderer->setStyle(pseudoElementStyle);
483                 break;
484             case CONTENT_OBJECT: {
485                 RenderImage* image = new (owner->renderArena()) RenderImage(owner->document()); // anonymous object
486                 RefPtr<RenderStyle> style = RenderStyle::create();
487                 style->inheritFrom(pseudoElementStyle);
488                 image->setStyle(style.release());
489                 if (const StyleImage* styleImage = static_cast<const ImageContentData*>(content)->image())
490                     image->setImageResource(RenderImageResourceStyleImage::create(const_cast<StyleImage*>(styleImage)));
491                 else
492                     image->setImageResource(RenderImageResource::create());
493                 renderer = image;
494                 break;
495             }
496         case CONTENT_COUNTER:
497             renderer = new (owner->renderArena()) RenderCounter(owner->document(), *static_cast<const CounterContentData*>(content)->counter());
498             renderer->setStyle(pseudoElementStyle);
499             break;
500         case CONTENT_QUOTE:
501             renderer = new (owner->renderArena()) RenderQuote(owner->document(), static_cast<const QuoteContentData*>(content)->quote());
502             renderer->setStyle(pseudoElementStyle);
503             break;
504         }
505
506         if (renderer) {
507             if (!generatedContentContainer) {
508                 // Make a generated box that might be any display type now that we are able to drill down into children
509                 // to find the original content properly.
510                 generatedContentContainer = RenderObject::createObject(owner->document(), pseudoElementStyle);
511                 ASSERT(styledObject->node()); // The styled object cannot be anonymous or else it could not have ':before' or ':after' pseudo elements.
512                 generatedContentContainer->setNode(styledObject->node()); // This allows access to the generatingNode.
513                 generatedContentContainer->setStyle(pseudoElementStyle);
514                 if (!owner->isChildAllowed(generatedContentContainer, pseudoElementStyle)) {
515                     // The generated content container is not allowed here -> abort.
516                     generatedContentContainer->destroy();
517                     renderer->destroy();
518                     return;
519                 }
520                 owner->addChild(generatedContentContainer, insertBefore);
521             }
522             if (generatedContentContainer->isChildAllowed(renderer, pseudoElementStyle))
523                 generatedContentContainer->addChild(renderer);
524             else
525                 renderer->destroy();
526         }
527     }
528 }
529
530 } // namespace WebCore