Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / core / dom / LayoutTreeBuilder.cpp
blobbbd8b190102dee95fe5b8dca49eb9a134da2b8bc
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.
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.
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.
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.
26 #include "config.h"
27 #include "core/dom/LayoutTreeBuilder.h"
29 #include "core/HTMLNames.h"
30 #include "core/SVGNames.h"
31 #include "core/css/resolver/StyleResolver.h"
32 #include "core/dom/FirstLetterPseudoElement.h"
33 #include "core/dom/Fullscreen.h"
34 #include "core/dom/Node.h"
35 #include "core/dom/PseudoElement.h"
36 #include "core/dom/Text.h"
37 #include "core/dom/shadow/InsertionPoint.h"
38 #include "core/layout/LayoutFullScreen.h"
39 #include "core/layout/LayoutObject.h"
40 #include "core/layout/LayoutText.h"
41 #include "core/layout/LayoutView.h"
42 #include "core/svg/SVGElement.h"
43 #include "platform/RuntimeEnabledFeatures.h"
45 namespace blink {
47 LayoutTreeBuilderForElement::LayoutTreeBuilderForElement(Element& element, ComputedStyle* style)
48 : LayoutTreeBuilder(element, nullptr)
49 , m_style(style)
51 ASSERT(!isActiveInsertionPoint(element));
52 if (element.isFirstLetterPseudoElement()) {
53 if (LayoutObject* nextLayoutObject = FirstLetterPseudoElement::firstLetterTextLayoutObject(element))
54 m_layoutObjectParent = nextLayoutObject->parent();
55 } else if (ContainerNode* containerNode = LayoutTreeBuilderTraversal::parent(element)) {
56 m_layoutObjectParent = containerNode->layoutObject();
60 LayoutObject* LayoutTreeBuilderForElement::nextLayoutObject() const
62 ASSERT(m_layoutObjectParent);
64 if (m_node->isInTopLayer())
65 return LayoutTreeBuilderTraversal::nextInTopLayer(*m_node);
67 if (m_node->isFirstLetterPseudoElement())
68 return FirstLetterPseudoElement::firstLetterTextLayoutObject(*m_node);
70 return LayoutTreeBuilder::nextLayoutObject();
73 LayoutObject* LayoutTreeBuilderForElement::parentLayoutObject() const
75 if (m_layoutObjectParent) {
76 // FIXME: Guarding this by parentLayoutObject isn't quite right as the spec for
77 // top layer only talks about display: none ancestors so putting a <dialog> inside an
78 // <optgroup> seems like it should still work even though this check will prevent it.
79 if (m_node->isInTopLayer())
80 return m_node->document().layoutView();
83 return m_layoutObjectParent;
86 bool LayoutTreeBuilderForElement::shouldCreateLayoutObject() const
88 if (!m_layoutObjectParent)
89 return false;
91 // FIXME: Should the following be in SVGElement::layoutObjectIsNeeded()?
92 if (m_node->isSVGElement()) {
93 // SVG elements only render when inside <svg>, or if the element is an <svg> itself.
94 if (!isSVGSVGElement(*m_node) && (!m_layoutObjectParent->node() || !m_layoutObjectParent->node()->isSVGElement()))
95 return false;
96 if (!toSVGElement(m_node)->isValid())
97 return false;
100 LayoutObject* parentLayoutObject = this->parentLayoutObject();
101 if (!parentLayoutObject)
102 return false;
103 if (!parentLayoutObject->canHaveChildren())
104 return false;
106 return m_node->layoutObjectIsNeeded(style());
109 ComputedStyle& LayoutTreeBuilderForElement::style() const
111 if (!m_style)
112 m_style = m_node->styleForLayoutObject();
113 return *m_style;
116 void LayoutTreeBuilderForElement::createLayoutObject()
118 ComputedStyle& style = this->style();
120 LayoutObject* newLayoutObject = m_node->createLayoutObject(style);
121 if (!newLayoutObject)
122 return;
124 LayoutObject* parentLayoutObject = this->parentLayoutObject();
126 if (!parentLayoutObject->isChildAllowed(newLayoutObject, style)) {
127 newLayoutObject->destroy();
128 return;
131 // Make sure the LayoutObject already knows it is going to be added to a LayoutFlowThread before we set the style
132 // for the first time. Otherwise code using inLayoutFlowThread() in the styleWillChange and styleDidChange will fail.
133 newLayoutObject->setIsInsideFlowThread(parentLayoutObject->isInsideFlowThread());
135 LayoutObject* nextLayoutObject = this->nextLayoutObject();
136 m_node->setLayoutObject(newLayoutObject);
137 newLayoutObject->setStyle(&style); // setStyle() can depend on layoutObject() already being set.
139 if (Fullscreen::isActiveFullScreenElement(*m_node)) {
140 newLayoutObject = LayoutFullScreen::wrapLayoutObject(newLayoutObject, parentLayoutObject, &m_node->document());
141 if (!newLayoutObject)
142 return;
145 // Note: Adding newLayoutObject instead of layoutObject(). layoutObject() may be a child of newLayoutObject.
146 parentLayoutObject->addChild(newLayoutObject, nextLayoutObject);
149 void LayoutTreeBuilderForText::createLayoutObject()
151 ComputedStyle& style = m_layoutObjectParent->mutableStyleRef();
153 ASSERT(m_node->textLayoutObjectIsNeeded(style, *m_layoutObjectParent));
155 LayoutText* newLayoutObject = m_node->createTextLayoutObject(style);
156 if (!m_layoutObjectParent->isChildAllowed(newLayoutObject, style)) {
157 newLayoutObject->destroy();
158 return;
161 // Make sure the LayoutObject already knows it is going to be added to a LayoutFlowThread before we set the style
162 // for the first time. Otherwise code using inLayoutFlowThread() in the styleWillChange and styleDidChange will fail.
163 newLayoutObject->setIsInsideFlowThread(m_layoutObjectParent->isInsideFlowThread());
165 LayoutObject* nextLayoutObject = this->nextLayoutObject();
166 m_node->setLayoutObject(newLayoutObject);
167 // Parent takes care of the animations, no need to call setAnimatableStyle.
168 newLayoutObject->setStyle(&style);
169 m_layoutObjectParent->addChild(newLayoutObject, nextLayoutObject);