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.
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.
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.
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.
24 #ifndef ContainerNode_h
25 #define ContainerNode_h
27 #include "bindings/v8/ExceptionStatePlaceholder.h"
28 #include "core/dom/Node.h"
29 #include "wtf/OwnPtr.h"
30 #include "wtf/Vector.h"
34 class ClassCollection;
42 template<class GenericNode, class GenericNodeContainer>
43 void addChildNodesToDeletionQueue(GenericNode*& head, GenericNode*& tail, GenericNodeContainer&);
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,
58 NumberOfDynamicRestyleFlags = 10,
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;
67 class ContainerNode : public Node {
69 virtual ~ContainerNode();
71 Node* firstChild() const { return m_firstChild; }
72 Node* lastChild() const { return m_lastChild; }
73 bool hasChildren() const { return m_firstChild; }
75 bool hasOneChild() const { return m_firstChild && !m_firstChild->nextSibling(); }
76 bool hasOneTextChild() const { return hasOneChild() && m_firstChild->isTextNode(); }
77 bool hasChildCount(unsigned) const;
79 PassRefPtrWillBeRawPtr<HTMLCollection> children();
81 unsigned countChildren() const;
82 Node* traverseToChildAt(unsigned index) const;
84 PassRefPtrWillBeRawPtr<Element> querySelector(const AtomicString& selectors, ExceptionState&);
85 PassRefPtrWillBeRawPtr<StaticNodeList> querySelectorAll(const AtomicString& selectors, ExceptionState&);
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);
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);
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&);
106 void removeChildren();
108 void cloneChildNodes(ContainerNode* clone);
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;
118 bool childrenOrSiblingsAffectedByFocus() const { return hasRestyleFlag(ChildrenOrSiblingsAffectedByFocus); }
119 void setChildrenOrSiblingsAffectedByFocus() { setRestyleFlag(ChildrenOrSiblingsAffectedByFocus); }
121 bool childrenOrSiblingsAffectedByHover() const { return hasRestyleFlag(ChildrenOrSiblingsAffectedByHover); }
122 void setChildrenOrSiblingsAffectedByHover() { setRestyleFlag(ChildrenOrSiblingsAffectedByHover); }
124 bool childrenOrSiblingsAffectedByActive() const { return hasRestyleFlag(ChildrenOrSiblingsAffectedByActive); }
125 void setChildrenOrSiblingsAffectedByActive() { setRestyleFlag(ChildrenOrSiblingsAffectedByActive); }
127 bool childrenOrSiblingsAffectedByDrag() const { return hasRestyleFlag(ChildrenOrSiblingsAffectedByDrag); }
128 void setChildrenOrSiblingsAffectedByDrag() { setRestyleFlag(ChildrenOrSiblingsAffectedByDrag); }
130 bool childrenAffectedByPositionalRules() const { return hasRestyleFlag(ChildrenAffectedByForwardPositionalRules) || hasRestyleFlag(ChildrenAffectedByBackwardPositionalRules); }
132 bool childrenAffectedByFirstChildRules() const { return hasRestyleFlag(ChildrenAffectedByFirstChildRules); }
133 void setChildrenAffectedByFirstChildRules() { setRestyleFlag(ChildrenAffectedByFirstChildRules); }
135 bool childrenAffectedByLastChildRules() const { return hasRestyleFlag(ChildrenAffectedByLastChildRules); }
136 void setChildrenAffectedByLastChildRules() { setRestyleFlag(ChildrenAffectedByLastChildRules); }
138 bool childrenAffectedByDirectAdjacentRules() const { return hasRestyleFlag(ChildrenAffectedByDirectAdjacentRules); }
139 void setChildrenAffectedByDirectAdjacentRules() { setRestyleFlag(ChildrenAffectedByDirectAdjacentRules); }
141 bool childrenAffectedByIndirectAdjacentRules() const { return hasRestyleFlag(ChildrenAffectedByIndirectAdjacentRules); }
142 void setChildrenAffectedByIndirectAdjacentRules() { setRestyleFlag(ChildrenAffectedByIndirectAdjacentRules); }
144 bool childrenAffectedByForwardPositionalRules() const { return hasRestyleFlag(ChildrenAffectedByForwardPositionalRules); }
145 void setChildrenAffectedByForwardPositionalRules() { setRestyleFlag(ChildrenAffectedByForwardPositionalRules); }
147 bool childrenAffectedByBackwardPositionalRules() const { return hasRestyleFlag(ChildrenAffectedByBackwardPositionalRules); }
148 void setChildrenAffectedByBackwardPositionalRules() { setRestyleFlag(ChildrenAffectedByBackwardPositionalRules); }
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);
155 bool childrenSupportStyleSharing() const { return !hasRestyleFlags(); }
157 // -----------------------------------------------------------------------------
158 // Notification of document structure changes (see core/dom/Node.h for more notification methods)
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);
164 void disconnectDescendantFrames();
166 virtual void trace(Visitor*) OVERRIDE;
168 void notifyNodeInserted(Node&);
169 void notifyNodeRemoved(Node&);
172 ContainerNode(TreeScope*, ConstructionType = CreateContainer);
174 template<class GenericNode, class GenericNodeContainer>
175 friend void Private::addChildNodesToDeletionQueue(GenericNode*& head, GenericNode*& tail, GenericNodeContainer&);
178 void removeDetachedChildren();
181 void setFirstChild(Node* child) { m_firstChild = child; }
182 void setLastChild(Node* child) { m_lastChild = child; }
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);
192 void notifyNodeInsertedInternal(Node&, NodeVector& postInsertionNotificationTargets);
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;
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;
205 void attachChildren(const AttachContext& = AttachContext());
206 void detachChildren(const AttachContext& = AttachContext());
208 bool getUpperLeftCorner(FloatPoint&) const;
209 bool getLowerRightCorner(FloatPoint&) const;
211 RawPtrWillBeMember<Node> m_firstChild;
212 RawPtrWillBeMember<Node> m_lastChild;
216 bool childAttachedAllowedWhenAttachingChildren(ContainerNode*);
219 DEFINE_NODE_TYPE_CASTS(ContainerNode, isContainerNode());
221 inline bool ContainerNode::hasChildCount(unsigned count) const
223 Node* child = m_firstChild;
224 while (count && child) {
225 child = child->nextSibling();
228 return !count && !child;
231 inline ContainerNode::ContainerNode(TreeScope* treeScope, ConstructionType type)
232 : Node(treeScope, type)
233 , m_firstChild(nullptr)
234 , m_lastChild(nullptr)
238 inline void ContainerNode::attachChildren(const AttachContext& context)
240 AttachContext childrenContext(context);
241 childrenContext.resolvedStyle = 0;
243 for (Node* child = firstChild(); child; child = child->nextSibling()) {
244 ASSERT(child->needsAttach() || childAttachedAllowedWhenAttachingChildren(this));
245 if (child->needsAttach())
246 child->attach(childrenContext);
250 inline void ContainerNode::detachChildren(const AttachContext& context)
252 AttachContext childrenContext(context);
253 childrenContext.resolvedStyle = 0;
255 for (Node* child = firstChild(); child; child = child->nextSibling())
256 child->detach(childrenContext);
259 inline unsigned Node::countChildren() const
261 if (!isContainerNode())
263 return toContainerNode(this)->countChildren();
266 inline Node* Node::traverseToChildAt(unsigned index) const
268 if (!isContainerNode())
270 return toContainerNode(this)->traverseToChildAt(index);
273 inline Node* Node::firstChild() const
275 if (!isContainerNode())
277 return toContainerNode(this)->firstChild();
280 inline Node* Node::lastChild() const
282 if (!isContainerNode())
284 return toContainerNode(this)->lastChild();
287 inline Node& Node::highestAncestorOrSelf() const
289 Node* node = const_cast<Node*>(this);
290 Node* highest = node;
291 for (; node; node = node->parentNode())
296 inline ContainerNode* Node::parentElementOrShadowRoot() const
298 ContainerNode* parent = parentNode();
299 return parent && (parent->isElementNode() || parent->isShadowRoot()) ? parent : 0;
302 inline ContainerNode* Node::parentElementOrDocumentFragment() const
304 ContainerNode* parent = parentNode();
305 return parent && (parent->isElementNode() || parent->isDocumentFragment()) ? parent : 0;
308 inline void getChildNodes(Node& node, NodeVector& nodes)
310 ASSERT(!nodes.size());
311 for (Node* child = node.firstChild(); child; child = child->nextSibling())
315 } // namespace WebCore
317 #endif // ContainerNode_h