137150a752a73ea0c14ab4b5c221235137aeba88
[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, 2013 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/core/v8/ExceptionStatePlaceholder.h"
28 #include "core/dom/Node.h"
29 #include "core/html/CollectionType.h"
30 #include "wtf/OwnPtr.h"
31 #include "wtf/Vector.h"
32
33 namespace blink {
34
35 class ClassCollection;
36 class ExceptionState;
37 class FloatPoint;
38 class HTMLCollection;
39 template <typename NodeType> class StaticNodeTypeList;
40 typedef StaticNodeTypeList<Element> StaticElementList;
41 class TagCollection;
42
43 enum DynamicRestyleFlags {
44     ChildrenOrSiblingsAffectedByFocus = 1 << 0,
45     ChildrenOrSiblingsAffectedByHover = 1 << 1,
46     ChildrenOrSiblingsAffectedByActive = 1 << 2,
47     ChildrenOrSiblingsAffectedByDrag = 1 << 3,
48     ChildrenAffectedByFirstChildRules = 1 << 4,
49     ChildrenAffectedByLastChildRules = 1 << 5,
50     ChildrenAffectedByDirectAdjacentRules = 1 << 6,
51     ChildrenAffectedByIndirectAdjacentRules = 1 << 7,
52     ChildrenAffectedByForwardPositionalRules = 1 << 8,
53     ChildrenAffectedByBackwardPositionalRules = 1 << 9,
54
55     NumberOfDynamicRestyleFlags = 10,
56 };
57
58 // This constant controls how much buffer is initially allocated
59 // for a Node Vector that is used to store child Nodes of a given Node.
60 // FIXME: Optimize the value.
61 const int initialNodeVectorSize = 11;
62 typedef WillBeHeapVector<RefPtrWillBeMember<Node>, initialNodeVectorSize> NodeVector;
63
64 class ContainerNode : public Node {
65 public:
66     virtual ~ContainerNode();
67
68     Node* firstChild() const { return m_firstChild; }
69     Node* lastChild() const { return m_lastChild; }
70     bool hasChildren() const { return m_firstChild; }
71
72     bool hasOneChild() const { return m_firstChild && !m_firstChild->nextSibling(); }
73     bool hasOneTextChild() const { return hasOneChild() && m_firstChild->isTextNode(); }
74     bool hasChildCount(unsigned) const;
75
76     PassRefPtrWillBeRawPtr<HTMLCollection> children();
77
78     unsigned countChildren() const;
79
80     PassRefPtrWillBeRawPtr<Element> querySelector(const AtomicString& selectors, ExceptionState&);
81     PassRefPtrWillBeRawPtr<StaticElementList> querySelectorAll(const AtomicString& selectors, ExceptionState&);
82
83     PassRefPtrWillBeRawPtr<Node> insertBefore(PassRefPtrWillBeRawPtr<Node> newChild, Node* refChild, ExceptionState& = ASSERT_NO_EXCEPTION);
84     PassRefPtrWillBeRawPtr<Node> replaceChild(PassRefPtrWillBeRawPtr<Node> newChild, PassRefPtrWillBeRawPtr<Node> oldChild, ExceptionState& = ASSERT_NO_EXCEPTION);
85     PassRefPtrWillBeRawPtr<Node> removeChild(PassRefPtrWillBeRawPtr<Node> child, ExceptionState& = ASSERT_NO_EXCEPTION);
86     PassRefPtrWillBeRawPtr<Node> appendChild(PassRefPtrWillBeRawPtr<Node> newChild, ExceptionState& = ASSERT_NO_EXCEPTION);
87
88     Element* getElementById(const AtomicString& id) const;
89     PassRefPtrWillBeRawPtr<TagCollection> getElementsByTagName(const AtomicString&);
90     PassRefPtrWillBeRawPtr<TagCollection> getElementsByTagNameNS(const AtomicString& namespaceURI, const AtomicString& localName);
91     PassRefPtrWillBeRawPtr<NameNodeList> getElementsByName(const AtomicString& elementName);
92     PassRefPtrWillBeRawPtr<ClassCollection> getElementsByClassName(const AtomicString& classNames);
93     PassRefPtrWillBeRawPtr<RadioNodeList> radioNodeList(const AtomicString&, bool onlyMatchImgElements = false);
94
95     // These methods are only used during parsing.
96     // They don't send DOM mutation events or handle reparenting.
97     void parserAppendChild(PassRefPtrWillBeRawPtr<Node>);
98     void parserRemoveChild(Node&);
99     void parserInsertBefore(PassRefPtrWillBeRawPtr<Node> newChild, Node& refChild);
100     void parserTakeAllChildrenFrom(ContainerNode&);
101
102     void removeChildren();
103
104     void cloneChildNodes(ContainerNode* clone);
105
106     virtual void attach(const AttachContext& = AttachContext()) OVERRIDE;
107     virtual void detach(const AttachContext& = AttachContext()) OVERRIDE;
108     virtual LayoutRect boundingBox() const OVERRIDE FINAL;
109     virtual void setFocus(bool) OVERRIDE;
110     void focusStateChanged();
111     virtual void setActive(bool = true) OVERRIDE;
112     virtual void setHovered(bool = true) OVERRIDE;
113
114     bool childrenOrSiblingsAffectedByFocus() const { return hasRestyleFlag(ChildrenOrSiblingsAffectedByFocus); }
115     void setChildrenOrSiblingsAffectedByFocus() { setRestyleFlag(ChildrenOrSiblingsAffectedByFocus); }
116
117     bool childrenOrSiblingsAffectedByHover() const { return hasRestyleFlag(ChildrenOrSiblingsAffectedByHover); }
118     void setChildrenOrSiblingsAffectedByHover() { setRestyleFlag(ChildrenOrSiblingsAffectedByHover); }
119
120     bool childrenOrSiblingsAffectedByActive() const { return hasRestyleFlag(ChildrenOrSiblingsAffectedByActive); }
121     void setChildrenOrSiblingsAffectedByActive() { setRestyleFlag(ChildrenOrSiblingsAffectedByActive); }
122
123     bool childrenOrSiblingsAffectedByDrag() const { return hasRestyleFlag(ChildrenOrSiblingsAffectedByDrag); }
124     void setChildrenOrSiblingsAffectedByDrag() { setRestyleFlag(ChildrenOrSiblingsAffectedByDrag); }
125
126     bool childrenAffectedByPositionalRules() const { return hasRestyleFlag(ChildrenAffectedByForwardPositionalRules) || hasRestyleFlag(ChildrenAffectedByBackwardPositionalRules); }
127
128     bool childrenAffectedByFirstChildRules() const { return hasRestyleFlag(ChildrenAffectedByFirstChildRules); }
129     void setChildrenAffectedByFirstChildRules() { setRestyleFlag(ChildrenAffectedByFirstChildRules); }
130
131     bool childrenAffectedByLastChildRules() const { return hasRestyleFlag(ChildrenAffectedByLastChildRules); }
132     void setChildrenAffectedByLastChildRules() { setRestyleFlag(ChildrenAffectedByLastChildRules); }
133
134     bool childrenAffectedByDirectAdjacentRules() const { return hasRestyleFlag(ChildrenAffectedByDirectAdjacentRules); }
135     void setChildrenAffectedByDirectAdjacentRules() { setRestyleFlag(ChildrenAffectedByDirectAdjacentRules); }
136
137     bool childrenAffectedByIndirectAdjacentRules() const { return hasRestyleFlag(ChildrenAffectedByIndirectAdjacentRules); }
138     void setChildrenAffectedByIndirectAdjacentRules() { setRestyleFlag(ChildrenAffectedByIndirectAdjacentRules); }
139
140     bool childrenAffectedByForwardPositionalRules() const { return hasRestyleFlag(ChildrenAffectedByForwardPositionalRules); }
141     void setChildrenAffectedByForwardPositionalRules() { setRestyleFlag(ChildrenAffectedByForwardPositionalRules); }
142
143     bool childrenAffectedByBackwardPositionalRules() const { return hasRestyleFlag(ChildrenAffectedByBackwardPositionalRules); }
144     void setChildrenAffectedByBackwardPositionalRules() { setRestyleFlag(ChildrenAffectedByBackwardPositionalRules); }
145
146     // FIXME: These methods should all be renamed to something better than "check",
147     // since it's not clear that they alter the style bits of siblings and children.
148     void checkForChildrenAdjacentRuleChanges();
149     enum SiblingCheckType { FinishedParsingChildren, SiblingElementInserted, SiblingElementRemoved };
150     void checkForSiblingStyleChanges(SiblingCheckType, Node* nodeBeforeChange, Node* nodeAfterChange);
151
152     bool childrenSupportStyleSharing() const { return !hasRestyleFlags(); }
153
154     // -----------------------------------------------------------------------------
155     // Notification of document structure changes (see core/dom/Node.h for more notification methods)
156
157     enum ChildrenChangeType { ElementInserted, NonElementInserted, ElementRemoved, NonElementRemoved, AllChildrenRemoved, TextChanged };
158     enum ChildrenChangeSource { ChildrenChangeSourceAPI, ChildrenChangeSourceParser };
159     struct ChildrenChange {
160         STACK_ALLOCATED();
161     public:
162         static ChildrenChange forInsertion(Node& node, ChildrenChangeSource byParser)
163         {
164             ChildrenChange change = {
165                 node.isElementNode() ? ElementInserted : NonElementInserted,
166                 node.previousSibling(),
167                 node.nextSibling(),
168                 byParser
169             };
170             return change;
171         }
172
173         static ChildrenChange forRemoval(Node& node, Node* previousSibling, Node* nextSibling, ChildrenChangeSource byParser)
174         {
175             ChildrenChange change = {
176                 node.isElementNode() ? ElementRemoved : NonElementRemoved,
177                 previousSibling,
178                 nextSibling,
179                 byParser
180             };
181             return change;
182         }
183
184         bool isChildInsertion() const { return type == ElementInserted || type == NonElementInserted; }
185         bool isChildRemoval() const { return type == ElementRemoved || type == NonElementRemoved; }
186         bool isChildElementChange() const { return type == ElementInserted || type == ElementRemoved; }
187
188         ChildrenChangeType type;
189         RawPtrWillBeMember<Node> siblingBeforeChange;
190         RawPtrWillBeMember<Node> siblingAfterChange;
191         ChildrenChangeSource byParser;
192     };
193
194     // Notifies the node that it's list of children have changed (either by adding or removing child nodes), or a child
195     // node that is of the type CDATA_SECTION_NODE, TEXT_NODE or COMMENT_NODE has changed its value.
196     virtual void childrenChanged(const ChildrenChange&);
197
198     void disconnectDescendantFrames();
199
200     virtual void trace(Visitor*) OVERRIDE;
201
202 protected:
203     ContainerNode(TreeScope*, ConstructionType = CreateContainer);
204
205     void invalidateNodeListCachesInAncestors(const QualifiedName* attrName = 0, Element* attributeOwnerElement = 0);
206
207 #if !ENABLE(OILPAN)
208     void removeDetachedChildren();
209 #endif
210
211     void setFirstChild(Node* child) { m_firstChild = child; }
212     void setLastChild(Node* child) { m_lastChild = child; }
213
214     // Utility functions for NodeListsNodeData API.
215     template <typename Collection> PassRefPtrWillBeRawPtr<Collection> ensureCachedCollection(CollectionType);
216     template <typename Collection> PassRefPtrWillBeRawPtr<Collection> ensureCachedCollection(CollectionType, const AtomicString& name);
217     template <typename Collection> PassRefPtrWillBeRawPtr<Collection> ensureCachedCollection(CollectionType, const AtomicString& namespaceURI, const AtomicString& localName);
218     template <typename Collection> Collection* cachedCollection(CollectionType);
219
220 private:
221     bool isContainerNode() const WTF_DELETED_FUNCTION; // This will catch anyone doing an unnecessary check.
222     bool isTextNode() const WTF_DELETED_FUNCTION; // This will catch anyone doing an unnecessary check.
223
224     NodeListsNodeData& ensureNodeLists();
225     void removeBetween(Node* previousChild, Node* nextChild, Node& oldChild);
226     void insertBeforeCommon(Node& nextChild, Node& oldChild);
227     void appendChildCommon(Node& child);
228     void updateTreeAfterInsertion(Node& child);
229     void willRemoveChildren();
230     void willRemoveChild(Node& child);
231     void removeDetachedChildrenInContainer(ContainerNode&);
232     void addChildNodesToDeletionQueue(Node*&, Node*&, ContainerNode&);
233
234     void notifyNodeInserted(Node&, ChildrenChangeSource = ChildrenChangeSourceAPI);
235     void notifyNodeInsertedInternal(Node&, NodeVector& postInsertionNotificationTargets);
236     void notifyNodeRemoved(Node&);
237
238     bool hasRestyleFlag(DynamicRestyleFlags mask) const { return hasRareData() && hasRestyleFlagInternal(mask); }
239     bool hasRestyleFlags() const { return hasRareData() && hasRestyleFlagsInternal(); }
240     void setRestyleFlag(DynamicRestyleFlags);
241     bool hasRestyleFlagInternal(DynamicRestyleFlags) const;
242     bool hasRestyleFlagsInternal() const;
243
244     inline bool checkAcceptChildGuaranteedNodeTypes(const Node& newChild, ExceptionState&) const;
245     inline bool checkAcceptChild(const Node* newChild, const Node* oldChild, ExceptionState&) const;
246     inline bool containsConsideringHostElements(const Node&) const;
247     inline bool isChildTypeAllowed(const Node& child) const;
248
249     void attachChildren(const AttachContext& = AttachContext());
250     void detachChildren(const AttachContext& = AttachContext());
251
252     bool getUpperLeftCorner(FloatPoint&) const;
253     bool getLowerRightCorner(FloatPoint&) const;
254
255     RawPtrWillBeMember<Node> m_firstChild;
256     RawPtrWillBeMember<Node> m_lastChild;
257 };
258
259 #if ENABLE(ASSERT)
260 bool childAttachedAllowedWhenAttachingChildren(ContainerNode*);
261 #endif
262
263 DEFINE_NODE_TYPE_CASTS(ContainerNode, isContainerNode());
264
265 inline bool ContainerNode::hasChildCount(unsigned count) const
266 {
267     Node* child = m_firstChild;
268     while (count && child) {
269         child = child->nextSibling();
270         --count;
271     }
272     return !count && !child;
273 }
274
275 inline ContainerNode::ContainerNode(TreeScope* treeScope, ConstructionType type)
276     : Node(treeScope, type)
277     , m_firstChild(nullptr)
278     , m_lastChild(nullptr)
279 {
280 }
281
282 inline void ContainerNode::attachChildren(const AttachContext& context)
283 {
284     AttachContext childrenContext(context);
285     childrenContext.resolvedStyle = 0;
286
287     for (Node* child = firstChild(); child; child = child->nextSibling()) {
288         ASSERT(child->needsAttach() || childAttachedAllowedWhenAttachingChildren(this));
289         if (child->needsAttach())
290             child->attach(childrenContext);
291     }
292 }
293
294 inline void ContainerNode::detachChildren(const AttachContext& context)
295 {
296     AttachContext childrenContext(context);
297     childrenContext.resolvedStyle = 0;
298
299     for (Node* child = firstChild(); child; child = child->nextSibling())
300         child->detach(childrenContext);
301 }
302
303 inline unsigned Node::countChildren() const
304 {
305     if (!isContainerNode())
306         return 0;
307     return toContainerNode(this)->countChildren();
308 }
309
310 inline Node* Node::firstChild() const
311 {
312     if (!isContainerNode())
313         return 0;
314     return toContainerNode(this)->firstChild();
315 }
316
317 inline Node* Node::lastChild() const
318 {
319     if (!isContainerNode())
320         return 0;
321     return toContainerNode(this)->lastChild();
322 }
323
324 inline ContainerNode* Node::parentElementOrShadowRoot() const
325 {
326     ContainerNode* parent = parentNode();
327     return parent && (parent->isElementNode() || parent->isShadowRoot()) ? parent : 0;
328 }
329
330 inline ContainerNode* Node::parentElementOrDocumentFragment() const
331 {
332     ContainerNode* parent = parentNode();
333     return parent && (parent->isElementNode() || parent->isDocumentFragment()) ? parent : 0;
334 }
335
336 inline bool Node::isTreeScope() const
337 {
338     return &treeScope().rootNode() == this;
339 }
340
341 inline void getChildNodes(ContainerNode& node, NodeVector& nodes)
342 {
343     ASSERT(!nodes.size());
344     for (Node* child = node.firstChild(); child; child = child->nextSibling())
345         nodes.append(child);
346 }
347
348 } // namespace blink
349
350 #endif // ContainerNode_h