4df98d42119d2b7b6a1c9feb3695d93af41c9aba
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / ContainerNode.h
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, 2009, 2010, 2011 Apple Inc. All rights reserved.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  *
22  */
23
24 #ifndef ContainerNode_h
25 #define ContainerNode_h
26
27 #include "bindings/v8/ExceptionStatePlaceholder.h"
28 #include "core/dom/Node.h"
29 #include "wtf/OwnPtr.h"
30 #include "wtf/Vector.h"
31
32 namespace WebCore {
33
34 class ClassCollection;
35 class ExceptionState;
36 class FloatPoint;
37 class HTMLCollection;
38 class StaticNodeList;
39 class TagCollection;
40
41 namespace Private {
42     template<class GenericNode, class GenericNodeContainer>
43     void addChildNodesToDeletionQueue(GenericNode*& head, GenericNode*& tail, GenericNodeContainer&);
44 }
45
46 enum DynamicRestyleFlags {
47     ChildrenOrSiblingsAffectedByFocus = 1 << 0,
48     ChildrenOrSiblingsAffectedByHover = 1 << 1,
49     ChildrenOrSiblingsAffectedByActive = 1 << 2,
50     ChildrenOrSiblingsAffectedByDrag = 1 << 3,
51     ChildrenAffectedByFirstChildRules = 1 << 4,
52     ChildrenAffectedByLastChildRules = 1 << 5,
53     ChildrenAffectedByDirectAdjacentRules = 1 << 6,
54     ChildrenAffectedByIndirectAdjacentRules = 1 << 7,
55     ChildrenAffectedByForwardPositionalRules = 1 << 8,
56     ChildrenAffectedByBackwardPositionalRules = 1 << 9,
57
58     NumberOfDynamicRestyleFlags = 10,
59 };
60
61 // This constant controls how much buffer is initially allocated
62 // for a Node Vector that is used to store child Nodes of a given Node.
63 // FIXME: Optimize the value.
64 const int initialNodeVectorSize = 11;
65 typedef WillBeHeapVector<RefPtrWillBeMember<Node>, initialNodeVectorSize> NodeVector;
66
67 class ContainerNode : public Node {
68 public:
69     virtual ~ContainerNode();
70
71     Node* firstChild() const { return m_firstChild; }
72     Node* lastChild() const { return m_lastChild; }
73     bool hasChildren() const { return m_firstChild; }
74
75     bool hasOneChild() const { return m_firstChild && !m_firstChild->nextSibling(); }
76     bool hasOneTextChild() const { return hasOneChild() && m_firstChild->isTextNode(); }
77     bool hasChildCount(unsigned) const;
78
79     PassRefPtrWillBeRawPtr<HTMLCollection> children();
80
81     unsigned countChildren() const;
82     Node* traverseToChildAt(unsigned index) const;
83
84     PassRefPtrWillBeRawPtr<Element> querySelector(const AtomicString& selectors, ExceptionState&);
85     PassRefPtrWillBeRawPtr<StaticNodeList> querySelectorAll(const AtomicString& selectors, ExceptionState&);
86
87     void insertBefore(PassRefPtrWillBeRawPtr<Node> newChild, Node* refChild, ExceptionState& = ASSERT_NO_EXCEPTION);
88     void replaceChild(PassRefPtrWillBeRawPtr<Node> newChild, Node* oldChild, ExceptionState& = ASSERT_NO_EXCEPTION);
89     void removeChild(Node* child, ExceptionState& = ASSERT_NO_EXCEPTION);
90     void appendChild(PassRefPtrWillBeRawPtr<Node> newChild, ExceptionState& = ASSERT_NO_EXCEPTION);
91
92     Element* getElementById(const AtomicString& id) const;
93     PassRefPtrWillBeRawPtr<TagCollection> getElementsByTagName(const AtomicString&);
94     PassRefPtrWillBeRawPtr<TagCollection> getElementsByTagNameNS(const AtomicString& namespaceURI, const AtomicString& localName);
95     PassRefPtrWillBeRawPtr<NameNodeList> getElementsByName(const AtomicString& elementName);
96     PassRefPtrWillBeRawPtr<ClassCollection> getElementsByClassName(const AtomicString& classNames);
97     PassRefPtrWillBeRawPtr<RadioNodeList> radioNodeList(const AtomicString&, bool onlyMatchImgElements = false);
98
99     // These methods are only used during parsing.
100     // They don't send DOM mutation events or handle reparenting.
101     void parserAppendChild(PassRefPtrWillBeRawPtr<Node>);
102     void parserRemoveChild(Node&);
103     void parserInsertBefore(PassRefPtrWillBeRawPtr<Node> newChild, Node& refChild);
104     void parserTakeAllChildrenFrom(ContainerNode&);
105
106     void removeChildren();
107
108     void cloneChildNodes(ContainerNode* clone);
109
110     virtual void attach(const AttachContext& = AttachContext()) OVERRIDE;
111     virtual void detach(const AttachContext& = AttachContext()) OVERRIDE;
112     virtual LayoutRect boundingBox() const OVERRIDE FINAL;
113     virtual void setFocus(bool) OVERRIDE;
114     void focusStateChanged();
115     virtual void setActive(bool = true) OVERRIDE;
116     virtual void setHovered(bool = true) OVERRIDE;
117
118     bool childrenOrSiblingsAffectedByFocus() const { return hasRestyleFlag(ChildrenOrSiblingsAffectedByFocus); }
119     void setChildrenOrSiblingsAffectedByFocus() { setRestyleFlag(ChildrenOrSiblingsAffectedByFocus); }
120
121     bool childrenOrSiblingsAffectedByHover() const { return hasRestyleFlag(ChildrenOrSiblingsAffectedByHover); }
122     void setChildrenOrSiblingsAffectedByHover() { setRestyleFlag(ChildrenOrSiblingsAffectedByHover); }
123
124     bool childrenOrSiblingsAffectedByActive() const { return hasRestyleFlag(ChildrenOrSiblingsAffectedByActive); }
125     void setChildrenOrSiblingsAffectedByActive() { setRestyleFlag(ChildrenOrSiblingsAffectedByActive); }
126
127     bool childrenOrSiblingsAffectedByDrag() const { return hasRestyleFlag(ChildrenOrSiblingsAffectedByDrag); }
128     void setChildrenOrSiblingsAffectedByDrag() { setRestyleFlag(ChildrenOrSiblingsAffectedByDrag); }
129
130     bool childrenAffectedByPositionalRules() const { return hasRestyleFlag(ChildrenAffectedByForwardPositionalRules) || hasRestyleFlag(ChildrenAffectedByBackwardPositionalRules); }
131
132     bool childrenAffectedByFirstChildRules() const { return hasRestyleFlag(ChildrenAffectedByFirstChildRules); }
133     void setChildrenAffectedByFirstChildRules() { setRestyleFlag(ChildrenAffectedByFirstChildRules); }
134
135     bool childrenAffectedByLastChildRules() const { return hasRestyleFlag(ChildrenAffectedByLastChildRules); }
136     void setChildrenAffectedByLastChildRules() { setRestyleFlag(ChildrenAffectedByLastChildRules); }
137
138     bool childrenAffectedByDirectAdjacentRules() const { return hasRestyleFlag(ChildrenAffectedByDirectAdjacentRules); }
139     void setChildrenAffectedByDirectAdjacentRules() { setRestyleFlag(ChildrenAffectedByDirectAdjacentRules); }
140
141     bool childrenAffectedByIndirectAdjacentRules() const { return hasRestyleFlag(ChildrenAffectedByIndirectAdjacentRules); }
142     void setChildrenAffectedByIndirectAdjacentRules() { setRestyleFlag(ChildrenAffectedByIndirectAdjacentRules); }
143
144     bool childrenAffectedByForwardPositionalRules() const { return hasRestyleFlag(ChildrenAffectedByForwardPositionalRules); }
145     void setChildrenAffectedByForwardPositionalRules() { setRestyleFlag(ChildrenAffectedByForwardPositionalRules); }
146
147     bool childrenAffectedByBackwardPositionalRules() const { return hasRestyleFlag(ChildrenAffectedByBackwardPositionalRules); }
148     void setChildrenAffectedByBackwardPositionalRules() { setRestyleFlag(ChildrenAffectedByBackwardPositionalRules); }
149
150     // FIXME: These methods should all be renamed to something better than "check",
151     // since it's not clear that they alter the style bits of siblings and children.
152     void checkForChildrenAdjacentRuleChanges();
153     void checkForSiblingStyleChanges(bool finishedParsingCallback, Node* beforeChange, Node* afterChange, int childCountDelta);
154
155     bool childrenSupportStyleSharing() const { return !hasRestyleFlags(); }
156
157     // -----------------------------------------------------------------------------
158     // Notification of document structure changes (see core/dom/Node.h for more notification methods)
159
160     // Notifies the node that it's list of children have changed (either by adding or removing child nodes), or a child
161     // node that is of the type CDATA_SECTION_NODE, TEXT_NODE or COMMENT_NODE has changed its value.
162     virtual void childrenChanged(bool createdByParser = false, Node* beforeChange = 0, Node* afterChange = 0, int childCountDelta = 0);
163
164     void disconnectDescendantFrames();
165
166     virtual void trace(Visitor*) OVERRIDE;
167
168     void notifyNodeInserted(Node&);
169     void notifyNodeRemoved(Node&);
170
171 protected:
172     ContainerNode(TreeScope*, ConstructionType = CreateContainer);
173
174     template<class GenericNode, class GenericNodeContainer>
175     friend void Private::addChildNodesToDeletionQueue(GenericNode*& head, GenericNode*& tail, GenericNodeContainer&);
176
177 #if !ENABLE(OILPAN)
178     void removeDetachedChildren();
179 #endif
180
181     void setFirstChild(Node* child) { m_firstChild = child; }
182     void setLastChild(Node* child) { m_lastChild = child; }
183
184 private:
185     void removeBetween(Node* previousChild, Node* nextChild, Node& oldChild);
186     void insertBeforeCommon(Node& nextChild, Node& oldChild);
187     void appendChildCommon(Node& child);
188     void updateTreeAfterInsertion(Node& child);
189     void willRemoveChildren();
190     void willRemoveChild(Node& child);
191
192     void notifyNodeInsertedInternal(Node&, NodeVector& postInsertionNotificationTargets);
193
194     bool hasRestyleFlag(DynamicRestyleFlags mask) const { return hasRareData() && hasRestyleFlagInternal(mask); }
195     bool hasRestyleFlags() const { return hasRareData() && hasRestyleFlagsInternal(); }
196     void setRestyleFlag(DynamicRestyleFlags);
197     bool hasRestyleFlagInternal(DynamicRestyleFlags) const;
198     bool hasRestyleFlagsInternal() const;
199
200     inline bool checkAcceptChildGuaranteedNodeTypes(const Node& newChild, ExceptionState&) const;
201     inline bool checkAcceptChild(const Node* newChild, const Node* oldChild, ExceptionState&) const;
202     inline bool containsConsideringHostElements(const Node&) const;
203     inline bool isChildTypeAllowed(const Node& child) const;
204
205     void attachChildren(const AttachContext& = AttachContext());
206     void detachChildren(const AttachContext& = AttachContext());
207
208     bool getUpperLeftCorner(FloatPoint&) const;
209     bool getLowerRightCorner(FloatPoint&) const;
210
211     RawPtrWillBeMember<Node> m_firstChild;
212     RawPtrWillBeMember<Node> m_lastChild;
213 };
214
215 #ifndef NDEBUG
216 bool childAttachedAllowedWhenAttachingChildren(ContainerNode*);
217 #endif
218
219 DEFINE_NODE_TYPE_CASTS(ContainerNode, isContainerNode());
220
221 inline bool ContainerNode::hasChildCount(unsigned count) const
222 {
223     Node* child = m_firstChild;
224     while (count && child) {
225         child = child->nextSibling();
226         --count;
227     }
228     return !count && !child;
229 }
230
231 inline ContainerNode::ContainerNode(TreeScope* treeScope, ConstructionType type)
232     : Node(treeScope, type)
233     , m_firstChild(nullptr)
234     , m_lastChild(nullptr)
235 {
236 }
237
238 inline void ContainerNode::attachChildren(const AttachContext& context)
239 {
240     AttachContext childrenContext(context);
241     childrenContext.resolvedStyle = 0;
242
243     for (Node* child = firstChild(); child; child = child->nextSibling()) {
244         ASSERT(child->needsAttach() || childAttachedAllowedWhenAttachingChildren(this));
245         if (child->needsAttach())
246             child->attach(childrenContext);
247     }
248 }
249
250 inline void ContainerNode::detachChildren(const AttachContext& context)
251 {
252     AttachContext childrenContext(context);
253     childrenContext.resolvedStyle = 0;
254
255     for (Node* child = firstChild(); child; child = child->nextSibling())
256         child->detach(childrenContext);
257 }
258
259 inline unsigned Node::countChildren() const
260 {
261     if (!isContainerNode())
262         return 0;
263     return toContainerNode(this)->countChildren();
264 }
265
266 inline Node* Node::traverseToChildAt(unsigned index) const
267 {
268     if (!isContainerNode())
269         return 0;
270     return toContainerNode(this)->traverseToChildAt(index);
271 }
272
273 inline Node* Node::firstChild() const
274 {
275     if (!isContainerNode())
276         return 0;
277     return toContainerNode(this)->firstChild();
278 }
279
280 inline Node* Node::lastChild() const
281 {
282     if (!isContainerNode())
283         return 0;
284     return toContainerNode(this)->lastChild();
285 }
286
287 inline Node& Node::highestAncestorOrSelf() const
288 {
289     Node* node = const_cast<Node*>(this);
290     Node* highest = node;
291     for (; node; node = node->parentNode())
292         highest = node;
293     return *highest;
294 }
295
296 inline ContainerNode* Node::parentElementOrShadowRoot() const
297 {
298     ContainerNode* parent = parentNode();
299     return parent && (parent->isElementNode() || parent->isShadowRoot()) ? parent : 0;
300 }
301
302 inline ContainerNode* Node::parentElementOrDocumentFragment() const
303 {
304     ContainerNode* parent = parentNode();
305     return parent && (parent->isElementNode() || parent->isDocumentFragment()) ? parent : 0;
306 }
307
308 inline void getChildNodes(Node& node, NodeVector& nodes)
309 {
310     ASSERT(!nodes.size());
311     for (Node* child = node.firstChild(); child; child = child->nextSibling())
312         nodes.append(child);
313 }
314
315 } // namespace WebCore
316
317 #endif // ContainerNode_h