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 "ExceptionCodePlaceholder.h"
34 typedef void (*NodeCallback)(Node*, unsigned);
37 template<class GenericNode, class GenericNodeContainer>
38 void addChildNodesToDeletionQueue(GenericNode*& head, GenericNode*& tail, GenericNodeContainer*);
41 class ContainerNode : public Node {
43 virtual ~ContainerNode();
45 Node* firstChild() const { return m_firstChild; }
46 Node* lastChild() const { return m_lastChild; }
47 bool hasChildNodes() const { return m_firstChild; }
49 unsigned childNodeCount() const;
50 Node* childNode(unsigned index) const;
52 bool insertBefore(PassRefPtr<Node> newChild, Node* refChild, ExceptionCode& = ASSERT_NO_EXCEPTION, bool shouldLazyAttach = false);
53 bool replaceChild(PassRefPtr<Node> newChild, Node* oldChild, ExceptionCode& = ASSERT_NO_EXCEPTION, bool shouldLazyAttach = false);
54 bool removeChild(Node* child, ExceptionCode& = ASSERT_NO_EXCEPTION);
55 bool appendChild(PassRefPtr<Node> newChild, ExceptionCode& = ASSERT_NO_EXCEPTION, bool shouldLazyAttach = false);
57 // These methods are only used during parsing.
58 // They don't send DOM mutation events or handle reparenting.
59 // However, arbitrary code may be run by beforeload handlers.
60 void parserAddChild(PassRefPtr<Node>);
61 void parserRemoveChild(Node*);
62 void parserInsertBefore(PassRefPtr<Node> newChild, Node* refChild);
64 // FIXME: It's not good to have two functions with such similar names, especially public functions.
65 // How do removeChildren and removeAllChildren differ?
66 void removeChildren();
67 void removeAllChildren();
69 void takeAllChildrenFrom(ContainerNode*);
71 void cloneChildNodes(ContainerNode* clone);
73 virtual void attach() OVERRIDE;
74 virtual void detach() OVERRIDE;
75 virtual LayoutRect getRect() const OVERRIDE;
76 virtual void setFocus(bool = true) OVERRIDE;
77 virtual void setActive(bool active = true, bool pause = false) OVERRIDE;
78 virtual void setHovered(bool = true) OVERRIDE;
79 virtual void scheduleSetNeedsStyleRecalc(StyleChangeType = FullStyleChange) OVERRIDE;
81 // -----------------------------------------------------------------------------
82 // Notification of document structure changes (see Node.h for more notification methods)
84 // Notifies the node that it's list of children have changed (either by adding or removing child nodes), or a child
85 // node that is of the type CDATA_SECTION_NODE, TEXT_NODE or COMMENT_NODE has changed its value.
86 virtual void childrenChanged(bool createdByParser = false, Node* beforeChange = 0, Node* afterChange = 0, int childCountDelta = 0);
89 void attachChildren();
90 void attachChildrenIfNeeded();
91 void attachChildrenLazily();
93 void detachChildren();
94 void detachChildrenIfNeeded();
96 void disconnectDescendantFrames();
98 // More efficient versions of these two functions for the case where we are starting with a ContainerNode.
99 Node* traverseNextNode() const;
100 Node* traverseNextNode(const Node* stayWithin) const;
102 virtual void reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
104 MemoryClassInfo<ContainerNode> info(memoryObjectInfo, this, MemoryInstrumentation::DOM);
105 info.visitBaseClass<Node>(this);
106 info.addInstrumentedMember(m_firstChild);
107 info.addInstrumentedMember(m_lastChild);
111 ContainerNode(Document*, ConstructionType = CreateContainer);
113 static void queuePostAttachCallback(NodeCallback, Node*, unsigned = 0);
114 static bool postAttachCallbacksAreSuspended();
115 void suspendPostAttachCallbacks();
116 void resumePostAttachCallbacks();
118 template<class GenericNode, class GenericNodeContainer>
119 friend void appendChildToContainer(GenericNode* child, GenericNodeContainer*);
121 template<class GenericNode, class GenericNodeContainer>
122 friend void Private::addChildNodesToDeletionQueue(GenericNode*& head, GenericNode*& tail, GenericNodeContainer*);
124 void setFirstChild(Node* child) { m_firstChild = child; }
125 void setLastChild(Node* child) { m_lastChild = child; }
128 void removeBetween(Node* previousChild, Node* nextChild, Node* oldChild);
129 void insertBeforeCommon(Node* nextChild, Node* oldChild);
131 static void dispatchPostAttachCallbacks();
133 bool getUpperLeftCorner(FloatPoint&) const;
134 bool getLowerRightCorner(FloatPoint&) const;
140 inline ContainerNode* toContainerNode(Node* node)
142 ASSERT(!node || node->isContainerNode());
143 return static_cast<ContainerNode*>(node);
146 inline const ContainerNode* toContainerNode(const Node* node)
148 ASSERT(!node || node->isContainerNode());
149 return static_cast<const ContainerNode*>(node);
152 // This will catch anyone doing an unnecessary cast.
153 void toContainerNode(const ContainerNode*);
155 inline ContainerNode::ContainerNode(Document* document, ConstructionType type)
156 : Node(document, type)
162 inline void ContainerNode::attachAsNode()
167 inline void ContainerNode::attachChildren()
169 for (Node* child = firstChild(); child; child = child->nextSibling())
173 inline void ContainerNode::attachChildrenIfNeeded()
175 for (Node* child = firstChild(); child; child = child->nextSibling()) {
176 if (!child->attached())
181 inline void ContainerNode::attachChildrenLazily()
183 for (Node* child = firstChild(); child; child = child->nextSibling())
184 if (!child->attached())
188 inline void ContainerNode::detachAsNode()
193 inline void ContainerNode::detachChildrenIfNeeded()
195 for (Node* child = firstChild(); child; child = child->nextSibling()) {
196 if (child->attached())
201 inline void ContainerNode::detachChildren()
203 for (Node* child = firstChild(); child; child = child->nextSibling())
207 inline unsigned Node::childNodeCount() const
209 if (!isContainerNode())
211 return toContainerNode(this)->childNodeCount();
214 inline Node* Node::childNode(unsigned index) const
216 if (!isContainerNode())
218 return toContainerNode(this)->childNode(index);
221 inline Node* Node::firstChild() const
223 if (!isContainerNode())
225 return toContainerNode(this)->firstChild();
228 inline Node* Node::lastChild() const
230 if (!isContainerNode())
232 return toContainerNode(this)->lastChild();
235 inline Node* Node::highestAncestor() const
237 Node* node = const_cast<Node*>(this);
238 Node* highest = node;
239 for (; node; node = node->parentNode())
244 inline Node* Node::traverseNextSibling() const
247 return nextSibling();
248 return traverseNextAncestorSibling();
251 inline Node* Node::traverseNextNode() const
255 return traverseNextSibling();
258 inline Node* ContainerNode::traverseNextNode() const
260 // More efficient than the Node::traverseNextNode above, because
261 // this does not need to do the isContainerNode check inside firstChild.
264 return traverseNextSibling();
267 inline Node* Node::traverseNextSibling(const Node* stayWithin) const
269 if (this == stayWithin)
272 return nextSibling();
273 return traverseNextAncestorSibling(stayWithin);
276 inline Node* Node::traverseNextNode(const Node* stayWithin) const
280 return traverseNextSibling(stayWithin);
283 inline Node* ContainerNode::traverseNextNode(const Node* stayWithin) const
285 // More efficient than the Node::traverseNextNode above, because
286 // this does not need to do the isContainerNode check inside firstChild.
289 return traverseNextSibling(stayWithin);
292 typedef Vector<RefPtr<Node>, 11> NodeVector;
294 inline void getChildNodes(Node* node, NodeVector& nodes)
296 ASSERT(!nodes.size());
297 for (Node* child = node->firstChild(); child; child = child->nextSibling())
301 } // namespace WebCore
303 #endif // ContainerNode_h