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 void willRemove() OVERRIDE;
76 virtual LayoutRect getRect() const OVERRIDE;
77 virtual void setFocus(bool = true) OVERRIDE;
78 virtual void setActive(bool active = true, bool pause = false) OVERRIDE;
79 virtual void setHovered(bool = true) OVERRIDE;
80 virtual void insertedIntoDocument() OVERRIDE;
81 virtual void removedFromDocument() OVERRIDE;
82 virtual void scheduleSetNeedsStyleRecalc(StyleChangeType = FullStyleChange) OVERRIDE;
84 // -----------------------------------------------------------------------------
85 // Notification of document structure changes (see Node.h for more notification methods)
87 // These functions are called whenever you are connected or disconnected from a tree. That tree may be the main
88 // document tree, or it could be another disconnected tree. Override these functions to do any work that depends
89 // on connectedness to some ancestor (e.g., an ancestor <form>).
90 virtual void insertedIntoTree(bool deep);
91 virtual void removedFromTree(bool deep);
93 // Notifies the node that it's list of children have changed (either by adding or removing child nodes), or a child
94 // node that is of the type CDATA_SECTION_NODE, TEXT_NODE or COMMENT_NODE has changed its value.
95 virtual void childrenChanged(bool createdByParser = false, Node* beforeChange = 0, Node* afterChange = 0, int childCountDelta = 0);
98 void attachChildren();
99 void attachChildrenIfNeeded();
100 void attachChildrenLazily();
102 void detachChildren();
103 void detachChildrenIfNeeded();
106 ContainerNode(Document*, ConstructionType = CreateContainer);
108 static void queuePostAttachCallback(NodeCallback, Node*, unsigned = 0);
109 static bool postAttachCallbacksAreSuspended();
110 void suspendPostAttachCallbacks();
111 void resumePostAttachCallbacks();
113 template<class GenericNode, class GenericNodeContainer>
114 friend void appendChildToContainer(GenericNode* child, GenericNodeContainer*);
116 template<class GenericNode, class GenericNodeContainer>
117 friend void Private::addChildNodesToDeletionQueue(GenericNode*& head, GenericNode*& tail, GenericNodeContainer*);
119 void setFirstChild(Node* child) { m_firstChild = child; }
120 void setLastChild(Node* child) { m_lastChild = child; }
123 void removeBetween(Node* previousChild, Node* nextChild, Node* oldChild);
124 void insertBeforeCommon(Node* nextChild, Node* oldChild);
126 static void dispatchPostAttachCallbacks();
128 bool getUpperLeftCorner(FloatPoint&) const;
129 bool getLowerRightCorner(FloatPoint&) const;
135 inline ContainerNode* toContainerNode(Node* node)
137 ASSERT(!node || node->isContainerNode());
138 return static_cast<ContainerNode*>(node);
141 inline const ContainerNode* toContainerNode(const Node* node)
143 ASSERT(!node || node->isContainerNode());
144 return static_cast<const ContainerNode*>(node);
147 // This will catch anyone doing an unnecessary cast.
148 void toContainerNode(const ContainerNode*);
150 inline ContainerNode::ContainerNode(Document* document, ConstructionType type)
151 : Node(document, type)
157 inline void ContainerNode::attachAsNode()
162 inline void ContainerNode::attachChildren()
164 for (Node* child = firstChild(); child; child = child->nextSibling())
168 inline void ContainerNode::attachChildrenIfNeeded()
170 for (Node* child = firstChild(); child; child = child->nextSibling()) {
171 if (!child->attached())
176 inline void ContainerNode::attachChildrenLazily()
178 for (Node* child = firstChild(); child; child = child->nextSibling())
179 if (!child->attached())
183 inline void ContainerNode::detachAsNode()
188 inline void ContainerNode::detachChildrenIfNeeded()
190 for (Node* child = firstChild(); child; child = child->nextSibling()) {
191 if (child->attached())
196 inline void ContainerNode::detachChildren()
198 for (Node* child = firstChild(); child; child = child->nextSibling())
202 inline unsigned Node::childNodeCount() const
204 if (!isContainerNode())
206 return toContainerNode(this)->childNodeCount();
209 inline Node* Node::childNode(unsigned index) const
211 if (!isContainerNode())
213 return toContainerNode(this)->childNode(index);
216 inline Node* Node::firstChild() const
218 if (!isContainerNode())
220 return toContainerNode(this)->firstChild();
223 inline Node* Node::lastChild() const
225 if (!isContainerNode())
227 return toContainerNode(this)->lastChild();
230 typedef Vector<RefPtr<Node>, 11> NodeVector;
232 inline void getChildNodes(Node* node, NodeVector& nodes)
234 ASSERT(!nodes.size());
235 for (Node* child = node->firstChild(); child; child = child->nextSibling())
239 } // namespace WebCore
241 #endif // ContainerNode_h