• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4  *           (C) 2001 Dirk Mueller (mueller@kde.org)
5  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
6  * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
7  * Copyright (C) 2011 Google Inc. All rights reserved.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public License
20  * along with this library; see the file COPYING.LIB.  If not, write to
21  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  *
24  */
25 
26 #include "config.h"
27 #include "core/dom/NodeRenderingContext.h"
28 
29 #include "RuntimeEnabledFeatures.h"
30 #include "core/css/resolver/StyleResolver.h"
31 #include "core/dom/ContainerNode.h"
32 #include "core/dom/FullscreenElementStack.h"
33 #include "core/dom/Node.h"
34 #include "core/dom/Text.h"
35 #include "core/dom/shadow/InsertionPoint.h"
36 #include "core/rendering/FlowThreadController.h"
37 #include "core/rendering/RenderFullScreen.h"
38 #include "core/rendering/RenderNamedFlowThread.h"
39 #include "core/rendering/RenderObject.h"
40 #include "core/rendering/RenderText.h"
41 #include "core/rendering/RenderView.h"
42 
43 namespace WebCore {
44 
isRendererReparented(const RenderObject * renderer)45 static bool isRendererReparented(const RenderObject* renderer)
46 {
47     if (!renderer->node()->isElementNode())
48         return false;
49     if (renderer->style() && !renderer->style()->flowThread().isEmpty())
50         return true;
51     if (toElement(renderer->node())->shouldBeReparentedUnderRenderView(renderer->style()))
52         return true;
53     return false;
54 }
55 
nextRenderer() const56 RenderObject* NodeRenderingContext::nextRenderer() const
57 {
58     if (RenderObject* renderer = m_node->renderer())
59         return renderer->nextSibling();
60 
61     Element* element = m_node->isElementNode() ? toElement(m_node) : 0;
62     if (element && element->shouldBeReparentedUnderRenderView(m_style.get())) {
63         // FIXME: Reparented renderers not in the top layer should probably be
64         // ordered in DOM tree order. We don't have a good way to do that yet,
65         // since NodeRenderingTraversal isn't aware of reparenting. It's safe to
66         // just append for now; it doesn't disrupt the top layer rendering as
67         // the layer collection in RenderLayer only requires that top layer
68         // renderers are orderered correctly relative to each other.
69         if (!element->isInTopLayer())
70             return 0;
71 
72         const Vector<RefPtr<Element> >& topLayerElements = element->document().topLayerElements();
73         size_t position = topLayerElements.find(element);
74         ASSERT(position != kNotFound);
75         for (size_t i = position + 1; i < topLayerElements.size(); ++i) {
76             if (RenderObject* renderer = topLayerElements[i]->renderer())
77                 return renderer;
78         }
79         return 0;
80     }
81 
82     if (m_parentFlowRenderer)
83         return m_parentFlowRenderer->nextRendererForNode(m_node);
84 
85     // Avoid an O(N^2) walk over the children when reattaching all children of a node.
86     if (m_renderingParent && m_renderingParent->needsAttach())
87         return 0;
88 
89     for (Node* sibling = NodeRenderingTraversal::nextSibling(m_node); sibling; sibling = NodeRenderingTraversal::nextSibling(sibling)) {
90         RenderObject* renderer = sibling->renderer();
91         if (renderer && !isRendererReparented(renderer))
92             return renderer;
93     }
94 
95     return 0;
96 }
97 
previousRenderer() const98 RenderObject* NodeRenderingContext::previousRenderer() const
99 {
100     if (RenderObject* renderer = m_node->renderer())
101         return renderer->previousSibling();
102 
103     // FIXME: This doesn't work correctly for reparented elements that are
104     // display: none. We'd need to duplicate the logic in nextRenderer, but since
105     // nothing needs that yet just assert.
106     ASSERT(!m_node->isElementNode() || !toElement(m_node)->shouldBeReparentedUnderRenderView(m_style.get()));
107 
108     if (m_parentFlowRenderer)
109         return m_parentFlowRenderer->previousRendererForNode(m_node);
110 
111     // FIXME: We should have the same O(N^2) avoidance as nextRenderer does
112     // however, when I tried adding it, several tests failed.
113     for (Node* sibling = NodeRenderingTraversal::previousSibling(m_node); sibling; sibling = NodeRenderingTraversal::previousSibling(sibling)) {
114         RenderObject* renderer = sibling->renderer();
115         if (renderer && !isRendererReparented(renderer))
116             return renderer;
117     }
118 
119     return 0;
120 }
121 
parentRenderer() const122 RenderObject* NodeRenderingContext::parentRenderer() const
123 {
124     if (RenderObject* renderer = m_node->renderer())
125         return renderer->parent();
126 
127     if (m_node->isElementNode() && toElement(m_node)->shouldBeReparentedUnderRenderView(m_style.get())) {
128         // The parent renderer of reparented elements is the RenderView, but only
129         // if the normal parent would have had a renderer.
130         // FIXME: This behavior isn't quite right as the spec for top layer
131         // only talks about display: none ancestors so putting a <dialog> inside
132         // an <optgroup> seems like it should still work even though this check
133         // will prevent it.
134         if (!m_renderingParent || !m_renderingParent->renderer())
135             return 0;
136         return m_node->document().renderView();
137     }
138 
139     if (m_parentFlowRenderer)
140         return m_parentFlowRenderer;
141 
142     return m_renderingParent ? m_renderingParent->renderer() : 0;
143 }
144 
shouldCreateRenderer() const145 bool NodeRenderingContext::shouldCreateRenderer() const
146 {
147     if (!m_renderingParent)
148         return false;
149     RenderObject* parentRenderer = this->parentRenderer();
150     if (!parentRenderer)
151         return false;
152     if (!parentRenderer->canHaveChildren())
153         return false;
154     if (!m_renderingParent->childShouldCreateRenderer(*m_node))
155         return false;
156     return true;
157 }
158 
159 // Check the specific case of elements that are children of regions but are flowed into a flow thread themselves.
elementInsideRegionNeedsRenderer()160 bool NodeRenderingContext::elementInsideRegionNeedsRenderer()
161 {
162     Element* element = toElement(m_node);
163     bool elementInsideRegionNeedsRenderer = false;
164     RenderObject* parentRenderer = this->parentRenderer();
165     if ((parentRenderer && !parentRenderer->canHaveChildren() && parentRenderer->isRenderNamedFlowFragmentContainer())
166         || (!parentRenderer && element->parentElement() && element->parentElement()->isInsideRegion())) {
167 
168         if (!m_style)
169             m_style = element->styleForRenderer();
170 
171         elementInsideRegionNeedsRenderer = element->shouldMoveToFlowThread(m_style.get());
172 
173         // Children of this element will only be allowed to be flowed into other flow-threads if display is NOT none.
174         if (element->rendererIsNeeded(*m_style))
175             element->setIsInsideRegion(true);
176     }
177 
178     return elementInsideRegionNeedsRenderer;
179 }
180 
moveToFlowThreadIfNeeded()181 void NodeRenderingContext::moveToFlowThreadIfNeeded()
182 {
183     if (!RuntimeEnabledFeatures::cssRegionsEnabled())
184         return;
185 
186     Element* element = toElement(m_node);
187 
188     if (!element->shouldMoveToFlowThread(m_style.get()))
189         return;
190 
191     ASSERT(m_node->document().renderView());
192     FlowThreadController* flowThreadController = m_node->document().renderView()->flowThreadController();
193     m_parentFlowRenderer = flowThreadController->ensureRenderFlowThreadWithName(m_style->flowThread());
194     flowThreadController->registerNamedFlowContentNode(m_node, m_parentFlowRenderer);
195 }
196 
createRendererForElementIfNeeded()197 void NodeRenderingContext::createRendererForElementIfNeeded()
198 {
199     ASSERT(!m_node->renderer());
200 
201     Element* element = toElement(m_node);
202 
203     element->setIsInsideRegion(false);
204 
205     if (!shouldCreateRenderer() && !elementInsideRegionNeedsRenderer())
206         return;
207 
208     if (!m_style)
209         m_style = element->styleForRenderer();
210 
211     moveToFlowThreadIfNeeded();
212 
213     if (!element->rendererIsNeeded(*m_style))
214         return;
215 
216     RenderObject* newRenderer = element->createRenderer(m_style.get());
217     if (!newRenderer)
218         return;
219 
220     RenderObject* parentRenderer = this->parentRenderer();
221 
222     if (!parentRenderer->isChildAllowed(newRenderer, m_style.get())) {
223         newRenderer->destroy();
224         return;
225     }
226 
227     // Make sure the RenderObject already knows it is going to be added to a RenderFlowThread before we set the style
228     // for the first time. Otherwise code using inRenderFlowThread() in the styleWillChange and styleDidChange will fail.
229     newRenderer->setFlowThreadState(parentRenderer->flowThreadState());
230 
231     RenderObject* nextRenderer = this->nextRenderer();
232     element->setRenderer(newRenderer);
233     newRenderer->setAnimatableStyle(m_style.release()); // setAnimatableStyle() can depend on renderer() already being set.
234 
235     if (FullscreenElementStack::isActiveFullScreenElement(element)) {
236         newRenderer = RenderFullScreen::wrapRenderer(newRenderer, parentRenderer, &element->document());
237         if (!newRenderer)
238             return;
239     }
240 
241     // Note: Adding newRenderer instead of renderer(). renderer() may be a child of newRenderer.
242     parentRenderer->addChild(newRenderer, nextRenderer);
243 }
244 
createRendererForTextIfNeeded()245 void NodeRenderingContext::createRendererForTextIfNeeded()
246 {
247     ASSERT(!m_node->renderer());
248 
249     Text* textNode = toText(m_node);
250 
251     if (!shouldCreateRenderer())
252         return;
253 
254     RenderObject* parentRenderer = this->parentRenderer();
255 
256     if (m_parentDetails.resetStyleInheritance())
257         m_style = textNode->document().ensureStyleResolver().defaultStyleForElement();
258     else
259         m_style = parentRenderer->style();
260 
261     if (!textNode->textRendererIsNeeded(*this))
262         return;
263 
264     RenderText* newRenderer = textNode->createTextRenderer(m_style.get());
265     if (!parentRenderer->isChildAllowed(newRenderer, m_style.get())) {
266         newRenderer->destroy();
267         return;
268     }
269 
270     // Make sure the RenderObject already knows it is going to be added to a RenderFlowThread before we set the style
271     // for the first time. Otherwise code using inRenderFlowThread() in the styleWillChange and styleDidChange will fail.
272     newRenderer->setFlowThreadState(parentRenderer->flowThreadState());
273 
274     RenderObject* nextRenderer = this->nextRenderer();
275     textNode->setRenderer(newRenderer);
276     // Parent takes care of the animations, no need to call setAnimatableStyle.
277     newRenderer->setStyle(m_style.release());
278     parentRenderer->addChild(newRenderer, nextRenderer);
279 }
280 
281 }
282