2 * Copyright (C) 2009, 2010 Apple Inc. All rights reserved.
3 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include "RenderObjectChildList.h"
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"
48 void RenderObjectChildList::destroyLeftoverChildren()
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();
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();
66 static RenderFlowThread* renderFlowThreadContainer(RenderObject* object)
68 while (object && object->isAnonymousBlock() && !object->isRenderFlowThread())
69 object = object->parent();
71 return object && object->isRenderFlowThread() ? toRenderFlowThread(object) : 0;
74 RenderObject* RenderObjectChildList::removeChildNode(RenderObject* owner, RenderObject* oldChild, bool fullRemove)
76 ASSERT(oldChild->parent() == owner);
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();
89 // If we have a line box wrapper, delete it.
90 if (oldChild->isBox())
91 toRenderBox(oldChild)->deleteLineBoxWrapper();
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();
101 // Keep our layer hierarchy updated.
102 if (oldChild->firstChild() || oldChild->hasLayer()) {
104 layer = owner->enclosingLayer();
105 oldChild->removeLayers(layer);
108 if (oldChild->isListItem())
109 toRenderListItem(oldChild)->updateListMarkerNumbers();
111 if (oldChild->isPositioned() && owner->childrenInline())
112 owner->dirtyLinesFromChangedChild(oldChild);
114 if (oldChild->isRenderRegion())
115 toRenderRegion(oldChild)->detachRegion();
117 if (oldChild->inRenderFlowThread() && oldChild->isBox())
118 oldChild->enclosingRenderFlowThread()->removeRenderBoxRegionInfo(toRenderBox(oldChild));
120 if (RenderFlowThread* containerFlowThread = renderFlowThreadContainer(owner))
121 containerFlowThread->removeFlowChild(oldChild);
124 // Update cached boundaries in SVG renderers, if a child is removed.
125 owner->setNeedsBoundariesUpdate();
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();
137 if (oldChild->previousSibling())
138 oldChild->previousSibling()->setNextSibling(oldChild->nextSibling());
139 if (oldChild->nextSibling())
140 oldChild->nextSibling()->setPreviousSibling(oldChild->previousSibling());
142 if (firstChild() == oldChild)
143 setFirstChild(oldChild->nextSibling());
144 if (lastChild() == oldChild)
145 setLastChild(oldChild->previousSibling());
147 oldChild->setPreviousSibling(0);
148 oldChild->setNextSibling(0);
149 oldChild->setParent(0);
151 RenderCounter::rendererRemovedFromTree(oldChild);
152 RenderQuote::rendererRemovedFromTree(oldChild);
154 if (AXObjectCache::accessibilityEnabled())
155 owner->document()->axObjectCache()->childrenChanged(owner);
160 void RenderObjectChildList::appendChildNode(RenderObject* owner, RenderObject* newChild, bool fullAppend)
162 ASSERT(newChild->parent() == 0);
163 ASSERT(!owner->isBlockFlow() || (!newChild->isTableSection() && !newChild->isTableRow() && !newChild->isTableCell()));
165 newChild->setParent(owner);
166 RenderObject* lChild = lastChild();
169 newChild->setPreviousSibling(lChild);
170 lChild->setNextSibling(newChild);
172 setFirstChild(newChild);
174 setLastChild(newChild);
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);
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()) {
189 layer = owner->enclosingLayer();
191 layer->setHasVisibleContent(true);
194 if (newChild->isListItem())
195 toRenderListItem(newChild)->updateListMarkerNumbers();
197 if (!newChild->isFloating() && owner->childrenInline())
198 owner->dirtyLinesFromChangedChild(newChild);
200 if (newChild->isRenderRegion())
201 toRenderRegion(newChild)->attachRegion();
203 if (RenderFlowThread* containerFlowThread = renderFlowThreadContainer(owner))
204 containerFlowThread->addFlowChild(newChild);
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.
212 if (AXObjectCache::accessibilityEnabled())
213 owner->document()->axObjectCache()->childrenChanged(owner);
216 void RenderObjectChildList::insertChildNode(RenderObject* owner, RenderObject* child, RenderObject* beforeChild, bool fullInsert)
219 appendChildNode(owner, child, fullInsert);
223 ASSERT(!child->parent());
224 while (beforeChild->parent() != owner && beforeChild->parent()->isAnonymousBlock())
225 beforeChild = beforeChild->parent();
226 ASSERT(beforeChild->parent() == owner);
228 ASSERT(!owner->isBlockFlow() || (!child->isTableSection() && !child->isTableRow() && !child->isTableCell()));
230 if (beforeChild == firstChild())
231 setFirstChild(child);
233 RenderObject* prev = beforeChild->previousSibling();
234 child->setNextSibling(beforeChild);
235 beforeChild->setPreviousSibling(child);
237 prev->setNextSibling(child);
238 child->setPreviousSibling(prev);
240 child->setParent(owner);
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);
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()) {
255 layer = owner->enclosingLayer();
257 layer->setHasVisibleContent(true);
260 if (child->isListItem())
261 toRenderListItem(child)->updateListMarkerNumbers();
263 if (!child->isFloating() && owner->childrenInline())
264 owner->dirtyLinesFromChangedChild(child);
266 if (child->isRenderRegion())
267 toRenderRegion(child)->attachRegion();
269 if (RenderFlowThread* containerFlowThread = renderFlowThreadContainer(owner))
270 containerFlowThread->addFlowChild(child, beforeChild);
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.
279 if (AXObjectCache::accessibilityEnabled())
280 owner->document()->axObjectCache()->childrenChanged(owner);
283 static RenderObject* findBeforeAfterParent(RenderObject* object)
285 // Only table parts need to search for the :before or :after parent
286 if (!(object->isTable() || object->isTableSection() || object->isTableRow()))
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();
296 return beforeAfterParent ? beforeAfterParent->parent() : 0;
299 RenderObject* RenderObjectChildList::beforePseudoElementRenderer(const RenderObject* owner) const
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);
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);
316 if (first->isBeforeContent())
319 // Check for a possible generated run-in, using run-in positioning rules.
320 first = owner->firstChild();
321 if (!first->isRenderBlock())
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())
334 RenderObject* RenderObjectChildList::afterPseudoElementRenderer(const RenderObject* owner) const
336 RenderObject* last = const_cast<RenderObject*>(owner);
338 last = last->lastChild();
339 } while (last && last->isAnonymous() && last->style()->styleType() == NOPSEUDO && !last->isListMarker());
340 if (last && !last->isAfterContent())
345 void RenderObjectChildList::updateBeforeAfterContent(RenderObject* owner, PseudoId type, const RenderObject* styledObject)
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());
350 // In CSS2, before/after pseudo-content cannot nest. Check this first.
351 if (owner->style()->styleType() == BEFORE || owner->style()->styleType() == AFTER)
355 styledObject = owner;
357 RenderStyle* pseudoElementStyle = styledObject->getCachedPseudoStyle(type);
361 child = beforePseudoElementRenderer(owner);
364 child = afterPseudoElementRenderer(owner);
367 ASSERT_NOT_REACHED();
371 // Whether or not we currently have generated content attached.
372 bool oldContentPresent = child;
374 // Whether or not we now want generated content.
375 bool newContentWanted = pseudoElementStyle && pseudoElementStyle->display() != NONE;
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;
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;
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)) {
392 if (child->style()->styleType() == type) {
393 oldContentPresent = false;
395 child = (type == BEFORE) ? owner->virtualChildren()->firstChild() : owner->virtualChildren()->lastChild();
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)
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
411 pseudoElementStyle->setDisplay(INLINE);
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);
419 RenderObject* beforeAfterParent = findBeforeAfterParent(child);
420 if (!beforeAfterParent)
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();
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());
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);
457 return; // We've updated the generated content. That's all we needed to do.
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();
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;
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()) {
481 renderer = new (owner->renderArena()) RenderTextFragment(owner->document() /* anonymous object */, static_cast<const TextContentData*>(content)->text().impl());
482 renderer->setStyle(pseudoElementStyle);
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)));
492 image->setImageResource(RenderImageResource::create());
496 case CONTENT_COUNTER:
497 renderer = new (owner->renderArena()) RenderCounter(owner->document(), *static_cast<const CounterContentData*>(content)->counter());
498 renderer->setStyle(pseudoElementStyle);
501 renderer = new (owner->renderArena()) RenderQuote(owner->document(), static_cast<const QuoteContentData*>(content)->quote());
502 renderer->setStyle(pseudoElementStyle);
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();
520 owner->addChild(generatedContentContainer, insertBefore);
522 if (generatedContentContainer->isChildAllowed(renderer, pseudoElementStyle))
523 generatedContentContainer->addChild(renderer);
530 } // namespace WebCore